openmv/scripts/examples/50-Arduino-Boards/Nano-RP2040/04-Bluetooth/ble_blinky.py

67 lines
1.9 KiB
Python

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Bluetooth Blinky Example
#
# Use nRFConnect app from the App store, connect to the Nano and write 1/0 to control the LED.
import bluetooth
import time
from ble_advertising import advertising_payload
from machine import Pin
from micropython import const
LED_PIN = 6
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_FLAG_READ = const(0x0002)
_FLAG_WRITE = const(0x0008)
_FLAG_NOTIFY = const(0x0010)
_FLAG_INDICATE = const(0x0020)
_SERVICE_UUID = bluetooth.UUID(0x1523)
_LED_CHAR_UUID = (bluetooth.UUID(0x1525), _FLAG_WRITE)
_LED_SERVICE = (
_SERVICE_UUID,
(_LED_CHAR_UUID,),
)
class BLETemperature:
def __init__(self, ble, name="NANO RP2040"):
self._ble = ble
self._ble.active(True)
self._ble.irq(self._irq)
((self._handle,),) = self._ble.gatts_register_services((_LED_SERVICE,))
self._connections = set()
self._payload = advertising_payload(name=name, services=[_SERVICE_UUID])
self._advertise()
def _irq(self, event, data):
# Track connections so we can send notifications.
if event == _IRQ_CENTRAL_CONNECT:
conn_handle, _, _ = data
self._connections.add(conn_handle)
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle, _, _ = data
self._connections.remove(conn_handle)
# Start advertising again to allow a new connection.
self._advertise()
elif event == _IRQ_GATTS_WRITE:
Pin(LED_PIN, Pin.OUT).value(int(self._ble.gatts_read(data[-1])[0]))
def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)
if __name__ == "__main__":
ble = bluetooth.BLE()
temp = BLETemperature(ble)
while True:
time.sleep_ms(1000)