Created
December 22, 2021 10:02
-
-
Save willwade/ff4ad61fcef409c182db303e6133e5d4 to your computer and use it in GitHub Desktop.
DemoSendSwitchPress circuitPy code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |
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) | |
uart = busio.UART(board.TX, board.RX, baudrate=9600) | |
while True: | |
while not ble.connected: | |
pass | |
while ble.connected: | |
data = uart.read(32) # read up to 32 bytes | |
if data is not None: | |
# convert bytearray to string | |
data_string = ''.join([chr(b) for b in data]) | |
#print(data_string, end="") | |
if data_string.startswith("AT+SendSwitchPress") | |
k.send(Keycode.SPACE) | |
time.sleep(0.1) | |
ble.start_advertising(advertisement) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import serial | |
from time import sleep | |
from sys import exit | |
import sys | |
import serial.tools.list_ports | |
import logging | |
import argparse | |
# Use AT+BAUDRATE=115200 but make sure hardware flow control CTS/RTS works | |
DEFAULT_BAUD = 115200 | |
# BAUD = 9600 | |
nrfVID = '239A' | |
nrfPID = '8029' | |
devName = 'NONE' | |
RETRY_TIMEOUT = 10 | |
QUEUE_TIMEOUT = 3 | |
def find_device_path(seldev): | |
dev = None | |
# Default names | |
if (os.name == 'posix'): | |
dev = '/dev/ttyUSB0' if seldev is None else seldev | |
else: | |
dev = 'COM6' if seldev is None else seldev | |
# Look for Adafruit CP2104 break out board or Feather nRF52. Use the first | |
# one found. Default is /dev/ttyUSB0 Or COM6 (Windows) | |
# tty for Bluetooth device with baud | |
# NB: Could be p.device with a suitable name we are looking for. Noticed some variation around this | |
if seldev is None: | |
for p in serial.tools.list_ports.comports(): | |
if "CP2104" in p.description: | |
logging.debug('serial desc:' + str(p)) | |
dev = p.device | |
break | |
elif "nRF52" in p.description: | |
logging.debug('serial desc:' + str(p)) | |
dev = p.device | |
break | |
elif nrfVID and nrfPID in p.hwid: | |
logging.debug('serial desc:' + str(p)) | |
dev = p.device | |
break | |
return dev | |
def main(): | |
## If you know what COM port its on - pop it here as a string | |
seldev=None | |
devicepath = find_device_path(seldev) | |
SerialCls = serial.Serial | |
with SerialCls(devicepath, baud, rtscts=1) as ser: | |
#So now we are connected to the Arduino.. | |
# This is where we do some logic and when ready we send the command to the board | |
# E.g.. detect earSwitch.. now send the command to the board | |
ser.write("AT+SendSwitchPress"+b"\r\n") | |
ser.flushInput() | |
if __name__ == '__main__': | |
ret = main() | |
exit(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment