Seeduino XIAO CircuitPythonメモ
Suns & Moon Laboratory
https://www.seeedstudio.com/xiao-series-page
https://www.seeedstudio.com/Seeeduino-XIAO-Arduino-Microcontroller-SAMD21-Cortex-M0+-p-4426.html
はじめに
XIAOの開発環境っていろいろあるけど、CircuitPythonにしてみる。
ちなみにMicroPythonもあるが、BLEやUSB使うならこっちの方がよさそう。
https://docs.circuitpython.org/en/latest/README.html#differences-from-micropython
Differences from MicroPython
CircuitPython:
・Supports native USB on most boards and BLE otherwise, allowing file editing without special tools.
・Floats (aka decimals) are enabled for all builds.
・Error messages are translated into 10+ languages.
・Concurrency within Python is not well supported. Interrupts and threading are disabled. async/await keywords are available on some boards for cooperative multitasking. Some concurrency is achieved with native modules for tasks that require it such as audio file playback.
https://docs.circuitpython.org/en/latest/docs/supported_ports.html
https://sourcegraph.com/github.com/adafruit/circuitpython/-/tree/ports/atmel-samd/boards/seeeduino_xiao
やってみる
https://wiki.seeedstudio.com/Seeeduino-XIAO-CircuitPython
LED
昔のサンプルコードはLEDだったんだけど、LED_INVERTEDになってた。
led = DigitalInOut(board.LED_INVERTED)
一覧
定義名 | ピン |
LED_INVERTED | PA17 |
YELLOW_LED_INVERTED | PA17 |
RX_LED_INVERTED | PA18 |
TX_LED_INVERTED | PA19 |
ソースみたらこう。
MP_QSTR_除いた名前になるらしい
https://micropython-usermod.readthedocs.io/en/latest/usermods_05.html#referring-to-user-functions
circuitpython\ports\atmel-samd\boards\seeeduino_xiao\pins.c(46): { MP_ROM_QSTR(MP_QSTR_LED_INVERTED), MP_ROM_PTR(&pin_PA17) },
circuitpython\ports\atmel-samd\boards\seeeduino_xiao\pins.c(47): { MP_ROM_QSTR(MP_QSTR_YELLOW_LED_INVERTED), MP_ROM_PTR(&pin_PA17) },
circuitpython\ports\atmel-samd\boards\seeeduino_xiao\pins.c(49): { MP_ROM_QSTR(MP_QSTR_RX_LED_INVERTED), MP_ROM_PTR(&pin_PA18) },
circuitpython\ports\atmel-samd\boards\seeeduino_xiao\pins.c(52): { MP_ROM_QSTR(MP_QSTR_TX_LED_INVERTED), MP_ROM_PTR(&pin_PA19) },
circuitpython\ports\atmel-samd\boards\seeeduino_xiao_kb\pins.c(46): { MP_ROM_QSTR(MP_QSTR_LED_INVERTED), MP_ROM_PTR(&pin_PA17) },
circuitpython\ports\atmel-samd\boards\seeeduino_xiao_kb\pins.c(47): { MP_ROM_QSTR(MP_QSTR_YELLOW_LED_INVERTED), MP_ROM_PTR(&pin_PA17) },
circuitpython\ports\atmel-samd\boards\seeeduino_xiao_kb\pins.c(49): { MP_ROM_QSTR(MP_QSTR_RX_LED_INVERTED), MP_ROM_PTR(&pin_PA18) },
circuitpython\ports\atmel-samd\boards\seeeduino_xiao_kb\pins.c(52): { MP_ROM_QSTR(MP_QSTR_TX_LED_INVERTED), MP_ROM_PTR(&pin_PA19) },
circuitpython\ports\espressif\boards\gravitech_cucumber_m\pins.c(9): { MP_ROM_QSTR(MP_QSTR_LED_INVERTED), MP_ROM_PTR(&pin_GPIO2) },
circuitpython\ports\espressif\boards\gravitech_cucumber_ms\pins.c(9): { MP_ROM_QSTR(MP_QSTR_LED_INVERTED), MP_ROM_PTR(&pin_GPIO2) },
circuitpython\ports\espressif\boards\gravitech_cucumber_r\pins.c(9): { MP_ROM_QSTR(MP_QSTR_LED_INVERTED), MP_ROM_PTR(&pin_GPIO2) },
circuitpython\ports\espressif\boards\gravitech_cucumber_rs\pins.c(9): { MP_ROM_QSTR(MP_QSTR_LED_INVERTED), MP_ROM_PTR(&pin_GPIO2) },
USB HIDキーボード
CircuitPython 8.0.5
ライブラリをダウンロード
https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases
今回はこれ
adafruit-circuitpython-bundle-8.x-mpy-20230416.zip
展開したら、lib/adafruit_hid を、CIRCUITPY/libにコピー
code.py
# Import the libraries
import time
import board
import usb_hid
from digitalio import DigitalInOut, Direction, Pull
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
# configure device as keyboard
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
d2 = DigitalInOut(board.D2)
d2.direction = Direction.INPUT
d2.pull = Pull.UP
# loop forever
while True:
if not d2.value:
kbd.send(Keycode.GUI ,Keycode.L)
time.sleep(0.5) # debounce delay. tenuki
end.
2024-08-14 11:00:27 32400