Adafruit nrf52840 循环程序

问题描述

我正在使用 Adafruit“BLE HID 键盘按钮”教程:我想使用箭头在具有辅助功能的窗口中导航-> iPad 上的访问控制,但是一旦我连接到蓝牙,定义的字符在我打开的任何文本字段(浏览器、笔记应用程序...等)中以重复顺序显示的五个按钮。仅当我按下五个按钮中的任何一个时,如何才能停止这种重复序列以使用五个按钮?我在脚本中没有看到任何循环(我只是摆弄编程)

#This example acts as a BLE HID keyboard to peer devices.
#Attach five buttons with pullup resistors to Feather nRF52840
#each button will send a configurable keycode to mobile device or computer

  import time
  import board
  from digitalio import DigitalInOut,Direction

  import adafruit_ble
  from adafruit_ble.advertising import Advertisement
  from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
  from adafruit_ble.services.standard.hid import HIDService
  from adafruit_ble.services.standard.device_info import DeviceInfoService
  from adafruit_hid.keyboard import Keyboard
  from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
  from adafruit_hid.keycode import Keycode

  button_1 = DigitalInOut(board.D11)
  button_2 = DigitalInOut(board.D10)
  button_3 = DigitalInOut(board.D9)
  button_4 = DigitalInOut(board.D6)
  button_5 = DigitalInOut(board.D5)

  button_1.direction = Direction.INPUT
  button_2.direction = Direction.INPUT
  button_3.direction = Direction.INPUT
  button_4.direction = Direction.INPUT
  button_5.direction = Direction.INPUT

  hid = HIDService()

  device_info = DeviceInfoService(software_revision=adafruit_ble.__version__,manufacturer="Adafruit Industries")
  advertisement = ProvideServicesAdvertisement(hid)
  advertisement.appearance = 961
  scan_response = Advertisement()
  scan_response.complete_name = "CircuitPython HID"

  ble = adafruit_ble.BLERadio()
  if not ble.connected:
      print("advertising")
      ble.start_advertising(advertisement,scan_response)
  else:
      print("already connected")
      print(ble.connections)

  k = Keyboard(hid.devices)
  kl = KeyboardLayoutUS(k)
  while True:
      while not ble.connected:
          pass
      print("Start typing:")

      while ble.connected:
          if not button_1.value:  # pull up logic means button low when pressed
              k.send(Keycode.DOWN_ARROW)
              time.sleep(0.4)

          if not button_2.value:
              k.send(Keycode.LEFT_ARROW)
              time.sleep(0.4)

          if not button_3.value:
              k.send(Keycode.UP_ARROW)
              time.sleep(0.4)

          if not button_4.value:
              k.send(Keycode.RIGHT_ARROW)
              time.sleep(0.4)

          if not button_5.value:
              k.send(Keycode.ENTER)
              time.sleep(0.4)

      ble.start_advertising(advertisement)

解决方法

USB HID 键盘要求您释放您按下的任何键,否则主机将(通常)执行它看到的最后一个按下的键的“类型化”重复。

尝试在每次按键后调用 release_all()。更多信息在源代码中:

https://circuitpython.readthedocs.io/projects/hid/en/latest/_modules/adafruit_hid/keyboard.html#Keyboard.release_all

,

阿雅,

感谢您的回复。问题是序列开始时没有按任何键。最后(感谢 Dave)将每个按钮的 pull 属性设置为 digitalio.Pull.UP 就成功了。 但是,我会尝试使用您的解决方案,因为我认为它在我的项目后期肯定会很有用。

问候

菲利普

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...