From dbb1c97673a83e9aa57d1ca4fdaeb6b2c01555e1 Mon Sep 17 00:00:00 2001 From: schneider <schneider@blinkenlichts.net> Date: Mon, 27 Dec 2021 21:01:08 +0100 Subject: [PATCH] demos: Add ws2812 BLE demo --- demos/ble-ws2812-card10.py | 35 +++++++++++++++++++++++++++++++++++ demos/ble-ws2812-host.py | 17 +++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 demos/ble-ws2812-card10.py create mode 100644 demos/ble-ws2812-host.py diff --git a/demos/ble-ws2812-card10.py b/demos/ble-ws2812-card10.py new file mode 100644 index 00000000..6216ea3b --- /dev/null +++ b/demos/ble-ws2812-card10.py @@ -0,0 +1,35 @@ +import ws2812, gpio, bluetooth, time, display +from micropython import const + +_IRQ_GATTS_WRITE = const(3) + +WS2812_SERVICE_UUID = \ + bluetooth.UUID("23230300-2342-2342-2342-234223422342") +SET_ALL = ( + bluetooth.UUID("23230301-2342-2342-2342-234223422342"), + bluetooth.FLAG_WRITE +) +WS2812_SERVICE = ( + WS2812_SERVICE_UUID, + (SET_ALL,) +) + +def irq(event, data): + if event == _IRQ_GATTS_WRITE: + conn_handle, value_handle = data + value = ble.gatts_read(value_handle) + ws2812.set_all(gpio.WRISTBAND_3, [value] * 3) + +if __name__ == "__main__": + display.open().backlight(0) + gpio.set_mode(gpio.WRISTBAND_3, gpio.mode.OUTPUT) + + ble = bluetooth.BLE() + ble.active(True) + ble.irq(irq) + ble.gatts_register_services((WS2812_SERVICE,)) + + print("Waiting for connection!") + + while True: + time.sleep(1) diff --git a/demos/ble-ws2812-host.py b/demos/ble-ws2812-host.py new file mode 100644 index 00000000..b00ee7c8 --- /dev/null +++ b/demos/ble-ws2812-host.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +import bluepy +import time +import colorsys + +p = bluepy.btle.Peripheral("CA:4D:10:01:ff:64") +c = p.getCharacteristics( + uuid='23230301-2342-2342-2342-234223422342')[0] + +hue = 0 +while 1: + r,g,b = colorsys.hsv_to_rgb(hue, 1, 0.1) + c.write(b"%c%c%c" % + (int(r*255), int(g*255), int(b*255)), True) + time.sleep(.1) + hue += 0.1 + -- GitLab