【问题标题】:How can I make the Raspberry Pico not automount as USB storage while using Circuit Python使用 Circuit Python 时,如何使 Raspberry Pico 不自动挂载为 USB 存储设备
【发布时间】:2021-05-24 02:06:34
【问题描述】:

我在 Raspberry Pi Pico 上使用 Circuit Python 为我提供用于键盘快捷键的硬件按钮。我使用的是 Circuit Python 而不是 MicroPython,因为它有 USB_HID 库。

我不希望 Pico 在插入时自动挂载为 USB 存储设备。我只是希望它充当 HID 设备。我知道除了 code.py 之外,我还可以编写 boot.py 脚本,但我无法在网上任何地方找到要放入的内容,这会阻止它作为 USB 设备安装。有时我仍然希望它安装为 USB(当按下按钮/连接 GPIO 引脚时),所以我仍然有一种方法可以更改设备上的代码。

这可能吗?如果是这样,只有在连接了某个 GPIO 引脚时才会挂载 boot.py 文件应该是什么样子?

【问题讨论】:

  • 我认为您应该确定您的操作系统,因为答案会有所不同。
  • 我将 Pico 插入更改的计算机,因此我希望它适用于所有操作系统。我对 boot.py 脚本的理解是它在安装 USB 卷之前运行,所以我希望 Pico 阻止它被安装,而不是我插入它的计算机的操作系统,以防止安装卷。我不知道 Raspberry Pi Pico 运行的狭义上是什么“操作系统”,它超出了必须向其闪烁的 Circuit Python。
  • 你可以试试raspberrypi.stackexchange.com,因为它是树莓派特有的。

标签: python raspberry-pi adafruit-circuitpython raspberry-pi-pico


【解决方案1】:

我最近遇到了一个需要做你正在寻找的同样的事情,并且在一个像样的兔子洞之后,我确定目前不能这样做。

https://github.com/adafruit/circuitpython/issues/1015

看起来该请求是几年前打开的,并且仍然列为打开状态。

我不确定在“小工具”模式下运行 pi 零是否可以完成此操作,但可能值得一看

【讨论】:

    【解决方案2】:

    CircuitPython 学习指南的这一部分对此进行了介绍:https://learn.adafruit.com/circuitpython-essentials/circuitpython-storage

    【讨论】:

      【解决方案3】:

      这是一个很好的 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. 
      

      【讨论】:

        【解决方案4】:

        下面的代码是我使用的。粗略地说,它会检查是否有按钮连接到 Pico 的 IO 引脚之一。如果在插入电缆时按下按钮,则它会安装为 USB 驱动器。如果未按下按钮,则未安装:

        import storage
        import board, digitalio
        
        import random
        
        # If not pressed, the key will be at +V (due to the pull-up).
        # https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/circuitpy-midi-serial#circuitpy-mass-storage-device-3096583-4
        button = digitalio.DigitalInOut(board.GP2)
        button.direction = digitalio.Direction.INPUT
        button.pull = digitalio.Pull.UP
        
        # Disable devices only if button is not pressed.
        if button.value:
            print(f"boot: button not pressed, disabling drive")
            storage.disable_usb_drive()
        

        本文改编自examples on Adafruit's site

        【讨论】:

          猜你喜欢
          • 2020-09-18
          • 1970-01-01
          • 1970-01-01
          • 2019-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多