From 259ea914ff5399aba7e01bdc6853cc6a9fc4a8f1 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Tue, 24 Aug 2021 15:21:44 -0400 Subject: [PATCH] some bluetooth and odrive updates --- bluetooth/esp32/ble_advertising.py | 93 +++++++++++++ bluetooth/esp32/ble_arni_odrive_controller.py | 129 +++++++++++++++++ bluetooth/esp32/ble_uart_peripheral.py | 131 ++++++++++++++++++ bluetooth/zs_040/README.md | 3 +- motors/pwm/README.md | 3 +- uart/odrive_uart.py | 110 +++++++++++++++ 6 files changed, 467 insertions(+), 2 deletions(-) create mode 100644 bluetooth/esp32/ble_advertising.py create mode 100644 bluetooth/esp32/ble_arni_odrive_controller.py create mode 100644 bluetooth/esp32/ble_uart_peripheral.py create mode 100644 uart/odrive_uart.py diff --git a/bluetooth/esp32/ble_advertising.py b/bluetooth/esp32/ble_advertising.py new file mode 100644 index 0000000..24433e7 --- /dev/null +++ b/bluetooth/esp32/ble_advertising.py @@ -0,0 +1,93 @@ +# Helpers for generating BLE advertising payloads. + +from micropython import const +import struct +import bluetooth + +# Advertising payloads are repeated packets of the following form: +# 1 byte data length (N + 1) +# 1 byte type (see constants below) +# N bytes type-specific data + +_ADV_TYPE_FLAGS = const(0x01) +_ADV_TYPE_NAME = const(0x09) +_ADV_TYPE_UUID16_COMPLETE = const(0x3) +_ADV_TYPE_UUID32_COMPLETE = const(0x5) +_ADV_TYPE_UUID128_COMPLETE = const(0x7) +_ADV_TYPE_UUID16_MORE = const(0x2) +_ADV_TYPE_UUID32_MORE = const(0x4) +_ADV_TYPE_UUID128_MORE = const(0x6) +_ADV_TYPE_APPEARANCE = const(0x19) + + +# Generate a payload to be passed to gap_advertise(adv_data=...). +def advertising_payload(limited_disc=False, br_edr=False, name=None, services=None, appearance=0): + payload = bytearray() + + def _append(adv_type, value): + nonlocal payload + payload += struct.pack("BB", len(value) + 1, adv_type) + value + + _append( + _ADV_TYPE_FLAGS, + struct.pack("B", (0x01 if limited_disc else 0x02) + (0x18 if br_edr else 0x04)), + ) + + if name: + _append(_ADV_TYPE_NAME, name) + + if services: + for uuid in services: + b = bytes(uuid) + if len(b) == 2: + _append(_ADV_TYPE_UUID16_COMPLETE, b) + elif len(b) == 4: + _append(_ADV_TYPE_UUID32_COMPLETE, b) + elif len(b) == 16: + _append(_ADV_TYPE_UUID128_COMPLETE, b) + + # See org.bluetooth.characteristic.gap.appearance.xml + if appearance: + _append(_ADV_TYPE_APPEARANCE, struct.pack("