https://github.com/LOBOT-ROBOT/Qbit
不好用 https://github.com/waveshare/JoyStick
2020年6月2日 星期二
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)
})
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()
Joystick of MicroBit and Raspberry PI
將 MicroBit 的搖桿透過藍芽連上樹莓派
參考 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()
參考 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")
Waveshare 之 Joystick
Name | PIN | Function |
---|---|---|
A | Pin5 | Button A |
B | Pin11 | Button B |
C | Pin15 | Button C |
D | Pin14 | Button D |
E | Pin13 | Button E |
F | Pin12 | Button F |
X | Pin1 | Joystick X |
Y | Pin2 | Joystick Y |
P | Pin8 | Joystick Press |
Buzzer | Pin0 | Buzzer |
2020年5月4日 星期一
MicroBit 之開發
Micro:bit 使用 c 開發,可使用 線上開發 或 離線開發
但是 線上開發的版本沒有更新,導致新的板子無法使用
加速度和磁力計有兩種不同版本, MMA8653 和 LSM303
離線開發是使用 yotta
yotta 安裝方式
參考 yotta Documentation 和 Yotta
$ sudo apt-get update
$ sudo apt-get install python-setuptools cmake build-essential ninja-build python-dev libffi-dev libssl-dev
$ sudo easy_install pip
$ pip install yotta
$ sudo mkdir -p /usr/local/lib/yotta_modules
$ sudo chown $USER /usr/local/lib/yotta_modules
$ chmod 755 /usr/local/lib/yotta_modules
$ sudo apt-get remove binutils-arm-none-eabi gcc-arm-none-eabi
$ sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
$ sudo apt-get update
$ sudo apt-get install gcc-arm-embedded
$ sudo apt-get install clang-3.9
$ pip install --upgrade cryptography
$ sudo apt-get install srecord
$ git clone https://github.com/lancaster-university/microbit-samples
$ yt target bcc-microbit-classic-gcc
$ cd microbit-samples
$ cp source/examples/bluetooth-services/* source/
$ yt clean
$ yt build
資料產生於 build/bcc-microbit-classic-gcc/source/microbit-samples-combined.hex
但是 線上開發的版本沒有更新,導致新的板子無法使用
加速度和磁力計有兩種不同版本, MMA8653 和 LSM303
離線開發是使用 yotta
yotta 安裝方式
參考 yotta Documentation 和 Yotta
$ sudo apt-get update
$ sudo apt-get install python-setuptools cmake build-essential ninja-build python-dev libffi-dev libssl-dev
$ sudo easy_install pip
$ pip install yotta
$ sudo mkdir -p /usr/local/lib/yotta_modules
$ sudo chown $USER /usr/local/lib/yotta_modules
$ chmod 755 /usr/local/lib/yotta_modules
$ sudo apt-get remove binutils-arm-none-eabi gcc-arm-none-eabi
$ sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
$ sudo apt-get update
$ sudo apt-get install gcc-arm-embedded
$ sudo apt-get install clang-3.9
$ pip install --upgrade cryptography
$ sudo apt-get install srecord
$ git clone https://github.com/lancaster-university/microbit-samples
$ yt target bcc-microbit-classic-gcc
$ cd microbit-samples
$ cp source/examples/bluetooth-services/* source/
$ yt clean
$ yt build
資料產生於 build/bcc-microbit-classic-gcc/source/microbit-samples-combined.hex
訂閱:
文章 (Atom)