網頁

2020年5月28日 星期四

Joystick of MicroBit and Raspberry PI, Part 2

MicroBit 之 JavaScript
bluetooth.onUartDataReceived(serial.delimiters(Delimiters.Hash), function () {
    basic.showString(bluetooth.uartReadUntil(serial.delimiters(Delimiters.Hash)))
})
bluetooth.onBluetoothDisconnected(function () {
    bCnt = 0
    basic.showLeds(`
        # . . . #
        . # . # .
        . . # . .
        . # . # .
        # . . . #
        `)
})
bluetooth.onBluetoothConnected(function () {
    bCnt = 1
    basic.showLeds(`
        . . . . .
        . . . . #
        . . . # .
        # . # . .
        . # . . .
        `)
})
let bCnt = 0
bCnt = 0
pins.setPull(DigitalPin.P5, PinPullMode.PullUp)
pins.setPull(DigitalPin.P11, PinPullMode.PullUp)
pins.setPull(DigitalPin.P15, PinPullMode.PullUp)
pins.setPull(DigitalPin.P14, PinPullMode.PullUp)
pins.setPull(DigitalPin.P13, PinPullMode.PullUp)
pins.setPull(DigitalPin.P12, PinPullMode.PullUp)
bluetooth.startUartService()
basic.showString("b")
basic.forever(function () {
    if (bCnt == 1) {
        bluetooth.uartWriteString("A" + pins.digitalReadPin(DigitalPin.P5) + pins.digitalReadPin(DigitalPin.P11) + pins.digitalReadPin(DigitalPin.P15) + pins.digitalReadPin(DigitalPin.P14) + pins.digitalReadPin(DigitalPin.P13) + pins.digitalReadPin(DigitalPin.P12) + "B" + pins.digitalReadPin(DigitalPin.P8) + "C" + pins.analogReadPin(AnalogPin.P1) + "D" + pins.analogReadPin(AnalogPin.P2))
        bluetooth.uartWriteString("E" + input.acceleration(Dimension.X) + "F" + input.acceleration(Dimension.Y) + "G" + input.acceleration(Dimension.Z) + "H" + input.acceleration(Dimension.Strength) + "I" + input.temperature())
    }
    basic.pause(1)
})

樹莓派上的 python
from bluezero import microbit
from bluezero import async_tools

def uart_callback(rt):
    #print(rt)
    pass
    
def ping():
    ubit.uart = 'ping#'
    return True

def goodbye():
    ubit.quit_async()
    ubit.disconnect()
    return False

ubit = microbit.Microbit(adapter_addr='DC:A6:32:9D:C9:74',
    device_addr='EA:40:D8:89:24:66', # tipet
    #device_addr='FF:06:9C:77:46:E9', # gaviv
    accelerometer_service=False,
    button_service=False,
    led_service=False,
    magnetometer_service=False,
    pin_service=False,
    temperature_service=False,
    uart_service=True)
eloop = async_tools.EventLoop()

ubit.connect()

ubit.subscribe_uart(uart_callback)
eloop.add_timer(10000, ping)
eloop.add_timer(30000, goodbye)

ubit.run_async()


沒有留言:

張貼留言