-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpd.py
508 lines (433 loc) · 18.2 KB
/
pd.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# -*- coding: utf-8 -*-
"""This file is part of the libsigrokdecode project.
Copyright (C) 2019 Libor Gabaj <libor.gabaj@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
"""
import sigrokdecode as srd
import common.srdhelper as hlp
###############################################################################
# Enumeration classes for device parameters
###############################################################################
class Address:
"""Enumeration of possible slave addresses.
- Device address is determined by the sensor's ADD0 pin physical connection
to particular power rail.
"""
(GND, VCC) = (0x23, 0x5c)
class Register:
"""Enumeration of possible slave register addresses."""
(
PWRDOWN, PWRUP, RESET, MTHIGH, MTLOW, # Operation commands
MCHIGH, MCHIGH2, MCLOW, # Continuous measurement modes
MOHIGH, MOHIGH2, MOLOW, # One-time measurement modes
) = (
0x00, 0x01, 0x07, 0x40, 0x60,
0x10, 0x11, 0x13,
0x20, 0x21, 0x23,
)
class MTregHighBits:
"""Range of high data bits of the measurement time register."""
(MIN, MAX) = (0, 2)
class MTregLowBits:
"""Range of low data bits of the measurement time register."""
(MIN, MAX) = (0, 4)
class Params:
"""Specific parameters."""
(
MTREG_TYP, # Typical value
ACCURACY_TYP, ACCURACY_MAX, ACCURACY_MIN, # Count per lux
UNIT_LIGHT
) = (69, 1.20, 1.44, 0.96, "lux")
###############################################################################
# Enumeration classes for annotations
###############################################################################
class AnnAddrs:
"""Enumeration of annotations for addresses."""
(GND, VCC) = range(2)
class AnnRegs:
"""Enumeration of annotations for registers."""
(
PWRDOWN, PWRUP, RESET, MTHIGH, MTLOW,
MCHIGH, MCHIGH2, MCLOW,
MOHIGH, MOHIGH2, MOLOW,
DATA,
) = range(AnnAddrs.VCC + 1, (AnnAddrs.VCC + 1) + 12)
class AnnBits:
"""Enumeration of annotations for bits."""
(RESERVED, DATA) = range(AnnRegs.DATA + 1, (AnnRegs.DATA + 1) + 2)
class AnnInfo:
"""Enumeration of annotations for various strings."""
(
WARN, BADADD, CHECK, WRITE, READ,
SENSE, LIGHT, MTREG, MTIME,
) = range(AnnBits.DATA + 1, (AnnBits.DATA + 1) + 9)
###############################################################################
# Parameters mapping
###############################################################################
addr_annots = { # Convert address value to annotation index
Address.GND: AnnAddrs.GND,
Address.VCC: AnnAddrs.VCC,
}
reg_annots = { # Convert register value to annotation index
Register.PWRDOWN: AnnRegs.PWRDOWN,
Register.PWRUP: AnnRegs.PWRUP,
Register.RESET: AnnRegs.RESET,
Register.MTHIGH: AnnRegs.MTHIGH,
Register.MTLOW: AnnRegs.MTLOW,
Register.MCHIGH: AnnRegs.MCHIGH,
Register.MCHIGH2: AnnRegs.MCHIGH2,
Register.MCLOW: AnnRegs.MCLOW,
Register.MOHIGH: AnnRegs.MOHIGH,
Register.MOHIGH2: AnnRegs.MOHIGH2,
Register.MOLOW: AnnRegs.MOLOW,
}
prm_map_accuracy = { # Convert parameter option to accuracy parameter
"Typical": Params.ACCURACY_TYP,
"Minimal": Params.ACCURACY_MIN,
"Maximal": Params.ACCURACY_MAX,
}
###############################################################################
# Parameters anotations definitions
###############################################################################
addresses = {
AnnAddrs.GND: ["ADDR grounded", "ADDR_GND", "AG"],
AnnAddrs.VCC: ["ADDR powered", "ADDR_VCC", "AV"],
}
registers = {
AnnRegs.PWRDOWN: ["Power down", "Pwr Dwn", "Off", "D"],
AnnRegs.PWRUP: ["Power up", "Pwr Up", "On", "U"],
AnnRegs.RESET: ["Reset light register", "Reset light", "Reset",
"Rst", "R"],
AnnRegs.MTHIGH: ["Measurement time high bits", "Mtime Hbits", "MTH", "H"],
AnnRegs.MTLOW: ["Measurement time low bits", "Mtime Lbits", "MTL", "L"],
AnnRegs.MCHIGH: ["Continuous measurement high resolution",
"Continuous high res", "Cont high", "CH"],
AnnRegs.MCHIGH2: ["Continuous measurement double high resolution",
"Continuous double high res", "Cont double", "CH2"],
AnnRegs.MCLOW: ["Continuous measurement low resolution",
"Continuous low res", "Cont low", "CL"],
AnnRegs.MOHIGH: ["One time measurement high resolution",
"One time high res", "One high", "OH"],
AnnRegs.MOHIGH2: ["One time measurement double high resolution",
"One time double high res", "One double", "OH2"],
AnnRegs.MOLOW: ["One time measurement low resolution",
"One time low res", "One low", "OL"],
AnnRegs.DATA: ["Illuminance data register", "Illuminance register",
"Illuminance", "Light", "L"],
}
bits = {
AnnBits.RESERVED: ["Reserved", "Rsvd", "R"],
AnnBits.DATA: ["Measurement time", "MT", "M"],
}
info = {
AnnInfo.WARN: ["Warnings", "Warn", "W"],
AnnInfo.BADADD: ["Uknown slave address", "Unknown address", "Uknown",
"Unk", "U"],
AnnInfo.CHECK: ["Slave presence check", "Slave check", "Check",
"Chk", "C"],
AnnInfo.WRITE: ["Write", "Wr", "W"],
AnnInfo.READ: ["Read", "Rd", "R"],
AnnInfo.SENSE: ["Sensitivity", "Sense", "S"],
AnnInfo.LIGHT: ["Ambient light", "Light", "L"],
AnnInfo.MTREG: ["Measurement time register", "MTreg", "MTR", "R"],
AnnInfo.MTIME: ["Measurement time", "MTime", "MT", "T"],
}
###############################################################################
# Decoder
###############################################################################
class Decoder(srd.Decoder):
"""Protocol decoder for digital ambient light sensor ``BH1750``."""
api_version = 3
id = "bh1750"
name = "BH1750"
longname = "Digital ambient light sensor BH1750"
desc = "Digital 16bit Serial Output Type Ambient Light Sensor IC."
license = "gplv2+"
inputs = ["i2c"]
outputs = ["bh1750"]
options = (
{"id": "radix", "desc": "Number format", "default": "Hex",
"values": ("Hex", "Dec", "Oct", "Bin")},
{"id": "params", "desc": "Datasheet parameter used",
"default": "Typical",
"values": ("Typical", "Maximal", "Minimal")},
)
annotations = hlp.create_annots(
{
"addr": addresses,
"reg": registers,
"bit": bits,
"info": info,
}
)
annotation_rows = (
("bits", "Bits", (AnnBits.RESERVED, AnnBits.DATA)),
("regs", "Registers",
tuple(range(AnnAddrs.GND, AnnRegs.DATA + 1))),
("info", "Info",
tuple(range(AnnInfo.CHECK, AnnInfo.MTIME + 1))),
("warnings", "Warnings", (AnnInfo.WARN, AnnInfo.BADADD)),
)
def __init__(self):
"""Initialize decoder."""
self.reset()
def reset(self):
"""Reset decoder and initialize instance variables."""
# Common parameters for I2C sampling
self.ss = 0 # Start sample
self.es = 0 # End sample
self.ssb = 0 # Start sample of an annotation transmission block
self.write = None # Flag about recent write action
self.state = "IDLE"
# Specific parameters for a device
self.addr = Address.GND # Slave address
self.reg = Register.PWRDOWN # Processed register
self.mode = Register.MCHIGH # Measurement mode
self.mtreg = Params.MTREG_TYP # MTreg default value
self.clear_data()
def clear_data(self):
"""Clear data cache."""
self.ssd = 0 # Start sample of an annotation data block
self.bytes = [] # List of recent processed bytes
self.bits = [] # List of recent processed byte bits
def start(self):
"""Actions before the beginning of the decoding."""
self.out_ann = self.register(srd.OUTPUT_ANN)
def putd(self, sb, eb, data):
"""Span data output across bit range.
- Because bits are order with MSB first, the output is an annotation
block from the last sample of the start bit (sb) to the first sample
of the end bit (eb).
- The higher bit the lower sample number.
"""
self.put(self.bits[eb][1], self.bits[sb][2], self.out_ann, data)
def putb(self, sb, eb=None, ann=AnnBits.RESERVED):
"""Span special bit annotation across bit range bit by bit.
Arguments
---------
sb : integer
Number of the annotated start bit counting from 0.
eb : integer
Number of the end bit right after the last annotated bit
counting from 0. If none value is provided, the method uses
start value increased by 1, so that just the first bit will be
annotated.
ann : integer
Index of the special bit's annotation in the annotations list
`bits`. Default value is for reserved bit.
"""
annots = hlp.compose_annot(bits[ann])
for bit in range(sb, eb or (sb + 1)):
self.put(self.bits[bit][1], self.bits[bit][2],
self.out_ann, [ann, annots])
def check_addr(self, addr_slave):
"""Check correct slave address."""
if addr_slave in (Address.GND, Address.VCC):
return True
ann = AnnInfo.BADADD
val = hlp.format_data(self.addr, self.options["radix"])
annots = hlp.compose_annot(info[ann], ann_value=val)
self.put(self.ss, self.es, self.out_ann, [ann, annots])
return False
def calculate_sensitivity(self):
"""Calculate measurement light sensitivity in lux per count."""
accuracy = prm_map_accuracy[self.options["params"]]
sensitivity = 1 / accuracy * Params.MTREG_TYP / self.mtreg # lux/count
if self.mode in [Register.MCHIGH2, Register.MOHIGH2]:
sensitivity /= 2
return sensitivity
def calculate_light(self, rawdata):
"""Calculate ambient light.
Arguments
---------
rawdata : int
Content of the illuminance data register.
Returns
-------
float
Ambient light in lux.
"""
light = rawdata * self.calculate_sensitivity()
return light
def collect_data(self, databyte):
"""Collect data byte to a data cache."""
if self.bytes:
self.bytes.insert(0, databyte)
else:
self.ssd = self.ss
self.bytes.append(databyte)
def handle_addr(self):
"""Process slave address."""
if not self.bytes:
return
# Registers row
self.addr = self.bytes[0]
ann = addr_annots[self.addr]
annots = hlp.compose_annot(addresses[ann])
self.put(self.ss, self.es, self.out_ann, [ann, annots])
self.clear_data()
def handle_reg(self):
"""Process slave register and call its handler."""
if not (self.bytes and self.write):
return
self.reg = self.bytes[0]
# Handle measurement time registers
mask_mthigh = ~((1 << (MTregHighBits.MAX + 1)) - 1)
if (self.reg & mask_mthigh) == Register.MTHIGH:
self.handle_mtreg_high()
return
mask_mtlow = ~((1 << (MTregLowBits.MAX + 1)) - 1)
if (self.reg & mask_mtlow) == Register.MTLOW:
self.handle_mtreg_low()
return
# Detect measurement mode registers
if self.reg in range(Register.MCHIGH, Register.MOLOW + 1):
self.mode = self.reg
# Registers row
ann = reg_annots[self.reg]
annots = hlp.compose_annot(registers[ann])
self.put(self.ssd, self.es, self.out_ann, [ann, annots])
self.clear_data()
def handle_mtreg_high(self):
"""Process measurement time register with high bits."""
mask = (1 << (MTregLowBits.MAX + 1)) - 1
self.mtreg &= mask # Clear high bits
mtreg = (self.reg << (MTregLowBits.MAX + 1)) & 0xff
self.mtreg |= mtreg
self.reg = Register.MTHIGH
# Bits row - high bits
bit_min = MTregHighBits.MIN
bit_max = MTregHighBits.MAX + 1
self.putb(bit_min, bit_max, AnnBits.DATA)
# Registers row
ann = AnnRegs.MTHIGH
annots = hlp.compose_annot(registers[ann])
self.put(self.ssd, self.es, self.out_ann, [ann, annots])
self.clear_data()
def handle_mtreg_low(self):
"""Process measurement time register with low bits."""
mask = (1 << (MTregLowBits.MAX + 1)) - 1
self.mtreg &= ~mask # Clear low bits
mtreg = self.reg & mask
self.mtreg |= mtreg
self.reg = Register.MTLOW
# Bits row - low bits
bit_min = MTregLowBits.MIN
bit_max = MTregLowBits.MAX + 1
self.putb(bit_min, bit_max, AnnBits.DATA)
# Registers row
ann = AnnRegs.MTLOW
annots = hlp.compose_annot(registers[ann])
self.put(self.ssd, self.es, self.out_ann, [ann, annots])
self.clear_data()
def handle_nodata(self):
"""Process transmission without any data."""
# Info row
ann = AnnInfo.CHECK
annots = hlp.compose_annot(info[ann])
self.put(self.ssb, self.es, self.out_ann, [ann, annots])
def handle_data(self):
"""Process read data."""
if self.write:
# Info row
if self.reg in [Register.MTHIGH, Register.MTLOW]:
ann = AnnInfo.MTREG
val = hlp.format_data(self.mtreg, self.options["radix"])
annots = hlp.compose_annot(info[ann], ann_value=val)
self.put(self.ssb, self.es, self.out_ann, [ann, annots])
if self.reg in range(Register.MCHIGH, Register.MOLOW + 1):
ann = AnnInfo.SENSE
val = "{:.2f}".format(self.calculate_sensitivity())
unit = " {}/cnt".format(Params.UNIT_LIGHT)
annots = hlp.compose_annot(info[ann], ann_value=val,
ann_unit=unit)
self.put(self.ssb, self.es, self.out_ann, [ann, annots])
else:
regword = (self.bytes[1] << 8) + self.bytes[0]
# Registers row
ann = AnnRegs.DATA
annots = hlp.compose_annot(registers[ann])
self.put(self.ssd, self.es, self.out_ann, [ann, annots])
# # Info row
ann = AnnInfo.LIGHT
val = "{:.2f}".format(self.calculate_light(regword))
unit = " {}".format(Params.UNIT_LIGHT)
annots = hlp.compose_annot(info[ann], ann_value=val, ann_unit=unit)
self.put(self.ssb, self.es, self.out_ann, [ann, annots])
self.clear_data()
def decode(self, ss, es, data):
"""Decode samples provided by parent decoder."""
cmd, databyte = data
self.ss, self.es = ss, es
if cmd == "BITS":
"""Collect packet of bits that belongs to the following command.
- Packet is in the form of list of bit lists:
["BITS", bitlist]
- Bit list is a list of 3 items list
[[bitvalue, startsample, endsample], ...]
- Samples are counted for aquisition sampling frequency.
- Parent decoder ``i2c``stores individual bits in the list from
the least significant bit (LSB) to the most significant bit
(MSB) as it is at representing numbers in computers, although I2C
bus transmits data in oposite order with MSB first.
"""
self.bits = databyte + self.bits
return
# State machine
if self.state == "IDLE":
"""Wait for an I2C transmission."""
if cmd != "START":
return
self.ssb = self.ss
self.state = "ADDRESS SLAVE"
elif self.state == "ADDRESS SLAVE":
"""Wait for a slave address."""
if cmd in ["ADDRESS WRITE", "ADDRESS READ"]:
if self.check_addr(databyte):
self.collect_data(databyte)
self.handle_addr()
if cmd == "ADDRESS READ":
self.write = False
elif cmd == "ADDRESS WRITE":
self.write = True
self.state = "REGISTER ADDRESS"
else:
self.state = "IDLE"
elif self.state == "REGISTER ADDRESS":
"""Process slave register."""
if cmd in ["DATA WRITE", "DATA READ"]:
self.collect_data(databyte)
self.handle_reg()
self.state = "REGISTER DATA"
elif cmd in ["STOP", "START REPEAT"]:
"""End of transmission without any register and data."""
self.handle_nodata()
self.state = "IDLE"
elif self.state == "REGISTER DATA":
"""Process data of a slave register.
- Individual command or data can end either with repeated start
condition or with stop condition.
"""
if cmd in ["DATA WRITE", "DATA READ"]:
self.collect_data(databyte)
elif cmd == "START REPEAT":
"""Output read data and continue in transmission."""
self.handle_data()
self.ssb = self.ss
self.state = "ADDRESS SLAVE"
elif cmd == "STOP":
"""Output formatted string with register data.
- This is end of an I2C transmission. Start waiting for another
one.
"""
self.handle_data()
self.state = "IDLE"