Skip to content

Instantly share code, notes, and snippets.

@matwey
Last active December 10, 2020 16:30
Show Gist options
  • Save matwey/e149fc2bf864ae2fa8bf55fb37e42972 to your computer and use it in GitHub Desktop.
Save matwey/e149fc2bf864ae2fa8bf55fb37e42972 to your computer and use it in GitHub Desktop.
BLE LED
#!/usr/bin/env python
import time
import gatt
manager = gatt.DeviceManager(adapter_name='hci0')
def array2raw(arr):
raw = b"".join(map(lambda x: x.to_bytes(1, byteorder='big'), arr))
return raw
def rgb2value(r, g, b):
val = [126, 255, 5, 3, r, g, b, 255, 239]
return array2raw(val)
class AnyDevice(gatt.Device):
def services_resolved(self):
super().services_resolved()
service = next(s for s in self.services if s.uuid == '0000ffe0-0000-1000-8000-00805f9b34fb')
characteristic = next(c for c in service.characteristics if c.uuid == '0000ffe1-0000-1000-8000-00805f9b34fb')
# raw = rgb2value(255, 128, 0)
# raw = array2raw([126, 255, 4, 0, 255, 255, 255, 255, 239])
raw = array2raw([126, 255, 4, 1, 255, 255, 255, 255, 239])
characteristic.write_value(raw)
device = AnyDevice(mac_address='78:9c:e7:0d:3b:f5', manager=manager)
device.connect()
manager.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment