问题描述
我在 RaspBerry Pi Pico 上使用 Circuit Python 来为我提供键盘快捷键的硬件按钮。我使用的是 Circuit Python 而不是 MicroPython,因为它具有 USB_HID 库。
我不希望 Pico 在插入时自动挂载为 USB 存储设备。我只希望它充当 HID 设备。我知道除了 code.py 之外,我还可以编写 boot.py 脚本,但我无法在网上找到任何可以防止它安装为 USB 设备的内容。有时我仍然希望它作为 USB 安装(当按下按钮/连接 GPIO 引脚时),所以我仍然有办法更改设备上的代码。
这可能吗?并且,如果是这样,那么只有在连接了某个 GPIO 引脚时,boot.py 应该是什么样子的?
解决方法
我最近遇到了一个需要做你正在寻找的同样的事情,在一个不错的兔子洞之后,我决定现在不能做。
https://github.com/adafruit/circuitpython/issues/1015
看起来请求是在几年前打开的,现在仍然列为打开状态。
我不确定在“小工具”模式下运行 pi 零是否可以实现这一点,但可能值得一看
,CircuitPython 学习指南的这一部分涵盖了这一点:https://learn.adafruit.com/circuitpython-essentials/circuitpython-storage
,这是一个很好的 HID 指南:
https://learn.adafruit.com/circuitpython-essentials/circuitpython-hid-keyboard-and-mouse
这是 HID 示例:
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
# A simple neat keyboard demo in CircuitPython
# The pins we'll use,each will have an internal pullup
keypress_pins = [board.A1,board.A2]
# Our array of key objects
key_pin_array = []
# The Keycode sent for each button,will be paired with a control key
keys_pressed = [Keycode.A,"Hello World!\n"]
control_key = Keycode.SHIFT
# The keyboard object!
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
# Make all pin objects inputs with pullups
for pin in keypress_pins:
key_pin = digitalio.DigitalInOut(pin)
key_pin.direction = digitalio.Direction.INPUT
key_pin.pull = digitalio.Pull.UP
key_pin_array.append(key_pin)
# For most CircuitPython boards:
led = digitalio.DigitalInOut(board.D13)
# For QT Py M0:
# led = digitalio.DigitalInOut(board.SCK)
led.direction = digitalio.Direction.OUTPUT
print("Waiting for key pin...")
while True:
# Check each pin
for key_pin in key_pin_array:
if not key_pin.value: # Is it grounded?
i = key_pin_array.index(key_pin)
print("Pin #%d is grounded." % i)
# Turn on the red LED
led.value = True
while not key_pin.value:
pass # Wait for it to be ungrounded!
# "Type" the Keycode or string
key = keys_pressed[i] # Get the corresponding Keycode or string
if isinstance(key,str): # If it's a string...
keyboard_layout.write(key) # ...Print the string
else: # If it's not a string...
keyboard.press(control_key,key) # "Press"...
keyboard.release_all() # ..."Release"!
# Turn off the red LED
led.value = False
time.sleep(0.01) ```
The example only prints Hello World,so it's not very useful,but it's a good base to mod.