-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadafruit_ble_magic_light.py
82 lines (67 loc) · 2.34 KB
/
adafruit_ble_magic_light.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# SPDX-FileCopyrightText: 2019 Scott Shawcroft for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
:py:mod:`~adafruit_ble.services.magic_light`
====================================================
This module provides Services available on a Magic Light, BLE RGB light bulb.
"""
try:
from typing import Optional
except ImportError:
pass
from adafruit_ble.services import Service
from adafruit_ble.uuid import VendorUUID
from adafruit_ble.characteristics import Characteristic
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_Magic_Light.git"
class MagicLightService(Service):
"""Service for controlling a Magic Light RGB bulb."""
# These UUIDs actually use the standard base UUID even though they aren't standard.
uuid = VendorUUID("0000ffe5-0000-1000-8000-00805f9b34fb")
_control = Characteristic(
uuid=VendorUUID("0000ffe9-0000-1000-8000-00805f9b34fb"), max_length=7
)
def __init__(self, service: Optional["MagicLightService"] = None) -> None:
super().__init__(service=service)
self._color = 0xFFFFFF
self._buf = bytearray(7)
self._buf[0] = 0x56
self._buf[6] = 0xAA
self._brightness = 1.0
def __getitem__(self, index: int) -> int:
if index > 0:
raise IndexError()
return self._color
def __setitem__(self, index: int, value: int) -> None:
if index > 0:
raise IndexError()
if isinstance(value, int):
r = (value >> 16) & 0xFF
g = (value >> 8) & 0xFF
b = value & 0xFF
else:
r, g, b = value
self._buf[1] = r
self._buf[2] = g
self._buf[3] = b
self._buf[4] = 0x00
self._buf[5] = 0xF0
self._control = self._buf
self._color = value
def __len__(self) -> int:
return 1
# Brightness doesn't preserve the color so comment it out for now. There are many other
# characteristics to try that may.
# @property
# def brightness(self):
# return self._brightness
#
# @brightness.setter
# def brightness(self, value):
# for i in range(3):
# self._buf[i + 1] = 0x00
# self._buf[4] = int(0xff * value)
# self._buf[5] = 0x0f
# self._control = self._buf
# self._brightness = value