55
55
56
56
_ble = adafruit_ble .BLERadio ()
57
57
_sequence_number = 0
58
+
59
+
58
60
def broadcast (measurement , * , broadcast_time = 0.1 , extended = False ):
59
61
"""Broadcasts the given measurement for the given broadcast time. If extended is False and the
60
62
measurement would be too long, it will be split into multiple measurements for transmission."""
@@ -66,71 +68,75 @@ def broadcast(measurement, *, broadcast_time=0.1, extended=False):
66
68
_ble .stop_advertising ()
67
69
_sequence_number = (_sequence_number + 1 ) % 256
68
70
69
- device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}" .format (* _ble ._adapter .address .address_bytes )
70
71
71
- _MANUFACTURING_DATA_ADT = const (0xff )
72
+ device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}" .format (
73
+ * _ble ._adapter .address .address_bytes
74
+ )
75
+
76
+ _MANUFACTURING_DATA_ADT = const (0xFF )
72
77
_ADAFRUIT_COMPANY_ID = const (0x0822 )
73
78
79
+
74
80
class AdafruitSensorMeasurement (Advertisement ):
75
81
"""Broadcast a single RGB color."""
82
+
76
83
# This prefix matches all
77
- prefix = struct .pack ("<BBH" ,
78
- 3 ,
79
- _MANUFACTURING_DATA_ADT ,
80
- _ADAFRUIT_COMPANY_ID )
84
+ prefix = struct .pack ("<BBH" , 3 , _MANUFACTURING_DATA_ADT , _ADAFRUIT_COMPANY_ID )
81
85
82
- manufacturer_data = LazyObjectField (ManufacturerData ,
83
- "manufacturer_data" ,
84
- advertising_data_type = _MANUFACTURING_DATA_ADT ,
85
- company_id = _ADAFRUIT_COMPANY_ID ,
86
- key_encoding = "<H" )
86
+ manufacturer_data = LazyObjectField (
87
+ ManufacturerData ,
88
+ "manufacturer_data" ,
89
+ advertising_data_type = _MANUFACTURING_DATA_ADT ,
90
+ company_id = _ADAFRUIT_COMPANY_ID ,
91
+ key_encoding = "<H" ,
92
+ )
87
93
88
94
sequence_number = ManufacturerDataField (0x0003 , "<B" )
89
95
"""Sequence number of the measurement. Used to detect missed packets."""
90
96
91
- acceleration = ManufacturerDataField (0x0a00 , "<fff" , ("x" , "y" , "z" ))
97
+ acceleration = ManufacturerDataField (0x0A00 , "<fff" , ("x" , "y" , "z" ))
92
98
"""Acceleration as (x, y, z) tuple of floats in meters per second per second."""
93
99
94
- magnetic = ManufacturerDataField (0x0a01 , "<fff" , ("x" , "y" , "z" ))
100
+ magnetic = ManufacturerDataField (0x0A01 , "<fff" , ("x" , "y" , "z" ))
95
101
"""Magnetism as (x, y, z) tuple of floats in micro-Tesla."""
96
102
97
- orientation = ManufacturerDataField (0x0a02 , "<fff" , ("x" , "y" , "z" ))
103
+ orientation = ManufacturerDataField (0x0A02 , "<fff" , ("x" , "y" , "z" ))
98
104
"""Absolution orientation as (x, y, z) tuple of floats in degrees."""
99
105
100
- gyro = ManufacturerDataField (0x0a03 , "<fff" , ("x" , "y" , "z" ))
106
+ gyro = ManufacturerDataField (0x0A03 , "<fff" , ("x" , "y" , "z" ))
101
107
"""Gyro motion as (x, y, z) tuple of floats in radians per second."""
102
108
103
- temperature = ManufacturerDataField (0x0a04 , "<f" )
109
+ temperature = ManufacturerDataField (0x0A04 , "<f" )
104
110
"""Temperature as a float in degrees centigrade."""
105
111
106
- eCO2 = ManufacturerDataField (0x0a05 , "<f" )
112
+ eCO2 = ManufacturerDataField (0x0A05 , "<f" )
107
113
"""Equivalent CO2 as a float in parts per million."""
108
114
109
- TVOC = ManufacturerDataField (0x0a06 , "<f" )
115
+ TVOC = ManufacturerDataField (0x0A06 , "<f" )
110
116
"""Total Volatile Organic Compounds as a float in parts per billion."""
111
117
112
- distance = ManufacturerDataField (0x0a07 , "<f" )
118
+ distance = ManufacturerDataField (0x0A07 , "<f" )
113
119
"""Distance as a float in centimeters."""
114
120
115
- light = ManufacturerDataField (0x0a08 , "<f" )
121
+ light = ManufacturerDataField (0x0A08 , "<f" )
116
122
"""Brightness as a float without units."""
117
123
118
- lux = ManufacturerDataField (0x0a09 , "<f" )
124
+ lux = ManufacturerDataField (0x0A09 , "<f" )
119
125
"""Brightness as a float in SI lux."""
120
126
121
- pressure = ManufacturerDataField (0x0a0a , "<f" )
127
+ pressure = ManufacturerDataField (0x0A0A , "<f" )
122
128
"""Pressure as a float in hectopascals."""
123
129
124
- relative_humidity = ManufacturerDataField (0x0a0b , "<f" )
130
+ relative_humidity = ManufacturerDataField (0x0A0B , "<f" )
125
131
"""Relative humidity as a float percentage."""
126
132
127
- current = ManufacturerDataField (0x0a0c , "<f" )
133
+ current = ManufacturerDataField (0x0A0C , "<f" )
128
134
"""Current as a float in milliamps."""
129
135
130
- voltage = ManufacturerDataField (0x0a0d , "<f" )
136
+ voltage = ManufacturerDataField (0x0A0D , "<f" )
131
137
"""Voltage as a float in Volts."""
132
138
133
- color = ManufacturerDataField (0x0a0e , "<f" )
139
+ color = ManufacturerDataField (0x0A0E , "<f" )
134
140
"""Color as RGB integer."""
135
141
136
142
# alarm = ManufacturerDataField(0x0a0f, "<f")
@@ -139,19 +145,19 @@ class AdafruitSensorMeasurement(Advertisement):
139
145
# datetime = ManufacturerDataField(0x0a10, "<f")
140
146
"""Date and time as a struct. Not supported."""
141
147
142
- duty_cycle = ManufacturerDataField (0x0a11 , "<f" )
148
+ duty_cycle = ManufacturerDataField (0x0A11 , "<f" )
143
149
"""16-bit PWM duty cycle. Independent of frequency."""
144
150
145
- frequency = ManufacturerDataField (0x0a12 , "<f" )
151
+ frequency = ManufacturerDataField (0x0A12 , "<f" )
146
152
"""As integer Hertz"""
147
153
148
- value = ManufacturerDataField (0x0a13 , "<f" )
154
+ value = ManufacturerDataField (0x0A13 , "<f" )
149
155
"""16-bit unit-less value. Used for analog values and for booleans."""
150
156
151
- weight = ManufacturerDataField (0x0a14 , "<f" )
157
+ weight = ManufacturerDataField (0x0A14 , "<f" )
152
158
"""Weight as a float in grams."""
153
159
154
- battery_voltage = ManufacturerDataField (0x0a15 , "<H" )
160
+ battery_voltage = ManufacturerDataField (0x0A15 , "<H" )
155
161
"""Battery voltage in millivolts. Saves two bytes over voltage and is more readable in bare
156
162
packets."""
157
163
@@ -171,7 +177,7 @@ def __str__(self):
171
177
return "<{} {} >" .format (self .__class__ .__name__ , " " .join (parts ))
172
178
173
179
def split (self , max_packet_size = 31 ):
174
- current_size = 8 # baseline for mfg data and sequence number
180
+ current_size = 8 # baseline for mfg data and sequence number
175
181
if current_size + len (self .manufacturer_data ) < max_packet_size :
176
182
yield self
177
183
return
0 commit comments