參考 Waveshare 之 Joystick
到 https://makecode.microbit.org/ 編寫程式 切換 JavaScript 可以直接貼上
bluetooth.onBluetoothConnected(function () {
basic.showString("C")
})
bluetooth.onBluetoothDisconnected(function () {
basic.showString("D")
})
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)
basic.showLeds(`
# . . # #
# . . # #
# # # . .
# . # . .
# # # . .
`)
bluetooth.startAccelerometerService()
bluetooth.startLEDService()
bluetooth.startIOPinService()
bluetooth.startTemperatureService()
右上齒輪/Project Settings
選擇 JustWorks pairing(default): Pairing is automatic once the pairing is initiated.
下載至 MicroBit
選擇 JustWorks pairing(default): Pairing is automatic once the pairing is initiated.
下載至 MicroBit
同時按下 AB 兩按鈕,再按下背面的 Reset 並放開 Reset
等待 畫面顯示完成,放開 AB 兩按鈕
在樹莓派右下角藍芽圖示,按滑鼠右鍵
選擇 Add Device
選擇 MicroBit, 按 Pair
參考 https://github.com/jaglees/pibit
參考 python-bluezero
參考 https://github.com/ukBaz/python-bluezero
$ bluetoothctl
[bluetooth]# show
Controller 5C:87:9C:1D:A8:7F (public)
Name: nano-desktop
Alias: nano-desktop
Class: 0x00000000
Powered: yes
Discoverable: no
Pairable: yes
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0530
Discovering: no
取得 adapter 的 MAC Address
[bluetooth]# power off
[bluetooth]# agent off
[bluetooth]# scan off
[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# scan on
取得 MicroBit 的 MAC Address
[bluetooth]# connect EA:40:D8:89:24:66
[bluetooth]# pair EA:40:D8:89:24:66
若是一直不成功,試試 remove, 再去 connect
[bluetooth]# remove EA:40:D8:89:24:66
將 MicroBit 的 MAC Address 填入 microbits.cnf
修改 python 程式中的 adapter
參考 python-bluezero
參考 https://github.com/ukBaz/python-bluezero
$ bluetoothctl
[bluetooth]# show
Controller 5C:87:9C:1D:A8:7F (public)
Name: nano-desktop
Alias: nano-desktop
Class: 0x00000000
Powered: yes
Discoverable: no
Pairable: yes
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0530
Discovering: no
取得 adapter 的 MAC Address
[bluetooth]# power off
[bluetooth]# agent off
[bluetooth]# scan off
[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# scan on
取得 MicroBit 的 MAC Address
[bluetooth]# connect EA:40:D8:89:24:66
[bluetooth]# pair EA:40:D8:89:24:66
若是一直不成功,試試 remove, 再去 connect
[bluetooth]# remove EA:40:D8:89:24:66
修改 python 程式中的 adapter
import time
from bluezero import microbit
from keyboardRead import NonBlockingConsole
# Set the address of the Pi Bluetooth controler
adapter='DC:A6:32:9D:C9:74'
devices={}
filepath = './microbits.cnf'
print ("Initialising microbits found in ", filepath)
try:
# Read list of Microbit MAC addresses and attempt to connect to each
with open(filepath) as fp:
line = fp.readline()
while line:
key=line.strip()
print ("..Device: [",key.strip(),"]")
try:
# Define microbit object
print ("....Initialising")
devices[key] = microbit.Microbit(adapter_addr=adapter,
device_addr=key,
accelerometer_service=True,
button_service=False,
led_service=True,
magnetometer_service=False,
pin_service=True,
temperature_service=True)
print ("....Initialised")
# Connect to microbit
print ("....Connecting")
devices[key].connect()
print ("....Connected")
devices[key].set_pin(5, True, False)
devices[key].set_pin(11, True, False)
devices[key].set_pin(15, True, False)
devices[key].set_pin(14, True, False)
devices[key].set_pin(13, True, False)
devices[key].set_pin(12, True, False)
devices[key].set_pin(1, True, True)
devices[key].set_pin(2, True, True)
devices[key].set_pin(8, True, False)
except Exception as e:
# If initilisation or connection failed remove this device from device list
print(e)
print ("....Initialisation or Connection failed")
devices.pop(key, None)
line=fp.readline()
except:
print("Failed to open config file")
exit()
# Check that at least one device is connected to - if not exit
if (devices is None or len(devices)==0):
print ("No connected devices - exiting")
exit()
# Loop whilst listening for keyboard input (on the Pi)
with NonBlockingConsole() as nbc:
looping = True
while looping:
# Check each device in turn - getting the unique key of each device
for dKey in devices:
# If the escape key is pressed on the PI then break
keyboardVal = nbc.get_data()
if keyboardVal == '\x1b': # x1b is ESC
print ("Raspberry Pi Escape key pressed")
looping=False
elif keyboardVal == 'm':
msg = input("Enter message to output to all Pis:")
for d in devices:
devices[d].text=msg
# If the A button is pressed - exit the loop (a kill switch)
#if (devices[dKey].button_a > 0):
# print("Device [", dKey, "] Button A Pressed" )
# #looping=False
# Print the temperature from the device
print('Temperature [', dKey, '] = ', devices[dKey].temperature)
print('Accelerometer [', dKey, '] = ', devices[dKey].accelerometer)
#print("Device [", dKey, "] Button A = " , devices[dKey].button_a)
#print("Device [", dKey, "] Button B = " , devices[dKey].button_b)
print("Device [", dKey, "] Pin = " , devices[dKey].pin_values)
time.sleep(1)
# Tidy up by disconnecting from all devices
print ("Disconnecting from all devices")
for dKey in devices:
print ("..Device [",dKey,"]")
print ("....Disconnecting")
devices[key].disconnect()
print ("....Disconnected")
沒有留言:
張貼留言