Created
January 19, 2021 16:19
-
-
Save yswallow/c806d73385d466c7aa8dbeed1afeafed to your computer and use it in GitHub Desktop.
CircuitPythonでBLE UARTの通信を別のPeripheralから受信してBLE HIDでCentralに送る
This file contains hidden or 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
| """ | |
| Demonstration of a Bluefruit BLE Central for Circuit Playground Bluefruit. Connects to the first BLE | |
| UART peripheral it finds. Sends Bluefruit ColorPackets, read from three accelerometer axis, to the | |
| peripheral. | |
| """ | |
| import time | |
| import board | |
| import digitalio | |
| from adafruit_hid.keyboard import Keyboard | |
| import usb_hid | |
| from adafruit_ble import BLERadio | |
| from adafruit_ble.advertising.standard import ProvideServicesAdvertisement | |
| from adafruit_ble.services.nordic import UARTService | |
| from adafruit_ble.advertising import Advertisement | |
| from adafruit_ble.services.standard import BatteryService | |
| from adafruit_ble.services.standard.hid import HIDService as bleHIDService | |
| from adafruit_ble.services.standard.device_info import DeviceInfoService | |
| ble = BLERadio() | |
| # settings for peripheral | |
| bleHID = bleHIDService() | |
| device_info = DeviceInfoService( | |
| software_revision="1.0.0", | |
| manufacturer="Adafruit Industries", | |
| hardware_revision="0.0.1", | |
| ) | |
| advertisement = ProvideServicesAdvertisement(device_info, bleHID) | |
| advertisement.appearance = 961 | |
| scan_response = Advertisement() | |
| scan_response.complete_name = "CP Tester" | |
| ble.name = "CircuitPython dualrole" | |
| ble.start_advertising(advertisement, scan_response=scan_response) | |
| while not ble.connected: | |
| print("Waiting HID host connection...") | |
| time.sleep(2) | |
| keyboard = Keyboard(bleHID.devices) | |
| uart_connection = None | |
| # See if any existing connections are providing UARTService. | |
| if ble.connected: | |
| for connection in ble.connections: | |
| if UARTService in connection: | |
| uart_connection = connection | |
| elif bleHIDService in connection: | |
| hid_connection = connection | |
| while True: | |
| if not uart_connection: | |
| print("Scanning...") | |
| for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): | |
| if UARTService in adv.services: | |
| print("found a UARTService advertisement") | |
| uart_connection = ble.connect(adv) | |
| break | |
| # Stop scanning whether or not we are connected. | |
| ble.stop_scan() | |
| while uart_connection and uart_connection.connected: | |
| try: | |
| uart_service = uart_connection[UARTService] | |
| s = uart_service.readline().decode().replace("\n","") | |
| print("received: "+s) | |
| if len(s): | |
| i = int(s) | |
| keyboard.send(i+0x04); | |
| except Exception as e: | |
| try: | |
| print(repr(e)) | |
| except: # pylint: disable=bare-except | |
| pass | |
| uart_connection = None | |
| #time.sleep(0.3) |
This file contains hidden or 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
| """ | |
| Demonstration of a Bluefruit BLE Central for Circuit Playground Bluefruit. Connects to the first BLE | |
| UART peripheral it finds. Sends Bluefruit ColorPackets, read from three accelerometer axis, to the | |
| peripheral. | |
| """ | |
| import time | |
| import board | |
| import digitalio | |
| from adafruit_ble import BLERadio | |
| from adafruit_ble.advertising.standard import ProvideServicesAdvertisement | |
| from adafruit_ble.services.nordic import UARTService | |
| ble = BLERadio() | |
| uart = UARTService() | |
| advertisement = ProvideServicesAdvertisement(uart) | |
| ports = [ board.P0_31, board.P0_26, board.P0_04, board.P0_08 ] | |
| buttons = [] | |
| for p in ports: | |
| b = digitalio.DigitalInOut(p) | |
| b.pull = digitalio.Pull.UP | |
| buttons.append(b) | |
| while True: | |
| ble.start_advertising(advertisement) | |
| print("advertising....") | |
| while not ble.connected: | |
| pass | |
| print("Connected") | |
| while ble.connected: | |
| for button in buttons: | |
| if not button.value: | |
| i = buttons.index(button) | |
| uart.write(str(i).encode()) | |
| uart.write(b"\n") | |
| print("button #"+str(i)+" tap!") | |
| time.sleep(0.3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment