|
16 | 16 |
|
17 | 17 | **Hardware:** |
18 | 18 |
|
19 | | -.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST |
20 | | - inline format: "* `Link Text <url>`_" |
21 | | -
|
22 | 19 | **Software and Dependencies:** |
23 | 20 |
|
24 | 21 | * Adafruit CircuitPython firmware for the supported boards: |
25 | 22 | https://github.com/adafruit/circuitpython/releases |
26 | | -
|
27 | | -.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either. |
28 | | -
|
29 | | -# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
30 | | -# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
31 | 23 | """ |
32 | 24 |
|
33 | | -# imports |
| 25 | +import struct |
| 26 | + |
| 27 | +import _bleio |
| 28 | +from adafruit_ble.attributes import Attribute |
| 29 | +from adafruit_ble.services import Service |
| 30 | +from adafruit_ble.uuid import VendorUUID |
| 31 | +from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic |
34 | 32 |
|
35 | 33 | __version__ = "0.0.0-auto.0" |
36 | 34 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_LYWSD03MMC.git" |
| 35 | + |
| 36 | + |
| 37 | +class _Readings(ComplexCharacteristic): |
| 38 | + """Notify-only characteristic of temperature/humidity""" |
| 39 | + |
| 40 | + uuid = VendorUUID("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6") |
| 41 | + |
| 42 | + def __init__(self): |
| 43 | + super().__init__(properties=Characteristic.NOTIFY) |
| 44 | + |
| 45 | + def bind(self, service): |
| 46 | + """Bind to an LYWSD03MMCService.""" |
| 47 | + bound_characteristic = super().bind(service) |
| 48 | + bound_characteristic.set_cccd(notify=True) |
| 49 | + # Use a PacketBuffer that can store one packet to receive the data. |
| 50 | + return _bleio.PacketBuffer(bound_characteristic, buffer_size=1) |
| 51 | + |
| 52 | + |
| 53 | +class LYWSD03MMCService(Service): |
| 54 | + """Service for reading from an LYWSD03MMC sensor.""" |
| 55 | + |
| 56 | + def __init__(self, service=None): |
| 57 | + super().__init__(service=service) |
| 58 | + # Defer creating buffers until needed, since MTU is not known yet. |
| 59 | + self._settings_result_buf = None |
| 60 | + self._readings_buf = None |
| 61 | + |
| 62 | + uuid = VendorUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6") |
| 63 | + |
| 64 | + readings = _Readings() |
| 65 | + |
| 66 | + @property |
| 67 | + def temperature_humidity(self): |
| 68 | + """Return a tuple of (temperature, humidity).""" |
| 69 | + if self._readings_buf is None: |
| 70 | + self._readings_buf = bytearray( |
| 71 | + self.readings.packet_size # pylint: disable=no-member |
| 72 | + ) |
| 73 | + data = self._readings_buf |
| 74 | + length = self.readings.readinto(data) # pylint: disable=no-member |
| 75 | + if length > 0: |
| 76 | + low_temp, high_temp, hum = struct.unpack_from("<BBB", data) |
| 77 | + sign = high_temp & 0x80 |
| 78 | + temp = ((high_temp & 0x7F) << 8) | low_temp |
| 79 | + if sign: |
| 80 | + temp = temp - 32767 |
| 81 | + temp = temp / 100 |
| 82 | + return (temp, hum) |
| 83 | + # No data. |
| 84 | + return None |
0 commit comments