]> sigrok.org Git - libsigrokdecode.git/blame - decoders/caliper/pd.py
caliper: fixup boilerplate (and some coding style and whitespace)
[libsigrokdecode.git] / decoders / caliper / pd.py
CommitLineData
5ed5cea5
TM
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2020 Tomas Mudrunka <harvie@github>
5##
6## Permission is hereby granted, free of charge, to any person obtaining a copy
7## of this software and associated documentation files (the "Software"), to deal
8## in the Software without restriction, including without limitation the rights
9## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10## copies of the Software, and to permit persons to whom the Software is
11## furnished to do so, subject to the following conditions:
12##
13## The above copyright notice and this permission notice shall be included in all
14## copies or substantial portions of the Software.
15##
16## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22## SOFTWARE.
23
24import sigrokdecode as srd
25
26class Decoder(srd.Decoder):
27 api_version = 3
28 id = 'caliper'
29 name = 'Caliper'
30 longname = 'Digital calipers'
4afcb4d0 31 desc = 'Protocol of cheap generic digital calipers.'
5ed5cea5
TM
32 license = 'mit'
33 inputs = ['logic']
34 outputs = []
35 channels = (
4afcb4d0
GS
36 {'id': 'clk', 'name': 'CLK', 'desc': 'Serial clock line'},
37 {'id': 'data', 'name': 'DATA', 'desc': 'Serial data line'},
5ed5cea5
TM
38 )
39 options = (
4afcb4d0
GS
40 {'id': 'timeout_ms', 'desc': 'Packet timeout in ms, 0 to disable',
41 'default': 10},
42 {'id': 'unit', 'desc': 'Convert units', 'default': 'keep',
43 'values': ('keep', 'mm', 'inch')},
44 {'id': 'changes', 'desc': 'Changes only', 'default': 'no',
45 'values': ('no', 'yes')},
5ed5cea5 46 )
4afcb4d0 47 tags = ['Analog/digital', 'Sensor']
5ed5cea5 48 annotations = (
4afcb4d0
GS
49 ('measurement', 'Measurement'),
50 ('warning', 'Warning'),
5ed5cea5
TM
51 )
52 annotation_rows = (
53 ('measurements', 'Measurements', (0,)),
54 ('warnings', 'Warnings', (1,)),
55 )
56
57 def reset_data(self):
58 self.bits = 0
59 self.number = 0
60 self.flags = 0
61
62 def metadata(self, key, value):
63 if key == srd.SRD_CONF_SAMPLERATE:
64 self.samplerate = value
65
66 def __init__(self):
67 self.reset()
68
69 def reset(self):
70 self.ss_cmd, self.es_cmd = 0, 0
71 self.reset_data()
72
73 def start(self):
74 self.out_ann = self.register(srd.OUTPUT_ANN)
75
4afcb4d0
GS
76 # Switch bit order of variable x, which is l bit long.
77 def bitr(self, x, l):
5ed5cea5
TM
78 return int(bin(x)[2:].zfill(l)[::-1], 2)
79
80 def decode(self):
81 self.last_measurement = None
82 while True:
4afcb4d0 83 clk, data = self.wait([{0: 'r'}, {'skip': round(self.samplerate / 1000)}])
5ed5cea5 84
4afcb4d0
GS
85 # Timeout after inactivity.
86 if self.options['timeout_ms'] > 0:
87 if self.samplenum > self.es_cmd + (self.samplerate / (1000 / self.options['timeout_ms'])):
5ed5cea5 88 if self.bits > 0:
4afcb4d0 89 self.put(self.ss_cmd, self.samplenum, self.out_ann, [1, ['timeout with %s bits in buffer' % (self.bits), 'timeout']])
5ed5cea5
TM
90 self.reset()
91
4afcb4d0 92 # Do nothing if there was timeout without rising clock edge.
5ed5cea5
TM
93 if self.matched == (False, True):
94 continue
95
4afcb4d0 96 # Store position of last activity.
5ed5cea5
TM
97 self.es_cmd = self.samplenum
98
4afcb4d0 99 # Store position of first bit.
5ed5cea5
TM
100 if self.ss_cmd == 0:
101 self.ss_cmd = self.samplenum
102
4afcb4d0 103 # Shift in measured number.
5ed5cea5
TM
104 if self.bits < 16:
105 self.number = (self.number << 1) | (data & 0b1)
4afcb4d0 106 self.bits += 1
5ed5cea5
TM
107 continue
108
4afcb4d0 109 # Shift in flag bits.
5ed5cea5
TM
110 if self.bits < 24:
111 self.flags = (self.flags << 1) | (data & 0b1)
4afcb4d0 112 self.bits += 1
5ed5cea5
TM
113 if self.bits < 24:
114 continue
4afcb4d0 115 # We got last bit of data.
5ed5cea5
TM
116 self.es_cmd = self.samplenum
117
4afcb4d0 118 # Do actual decoding.
5ed5cea5
TM
119
120 negative = ((self.flags & 0b00001000) >> 3)
121 inch = (self.flags & 0b00000001)
122
123 number = self.bitr(self.number, 16)
124
5ed5cea5
TM
125 if negative > 0:
126 number = -number
127
128 inchmm = 25.4 #how many mms in inch
129
130 if inch:
4afcb4d0 131 number = number / 2000
5ed5cea5
TM
132 if self.options['unit'] == 'mm':
133 number *= inchmm
134 inch = 0
135 else:
4afcb4d0 136 number = number / 100
5ed5cea5 137 if self.options['unit'] == 'inch':
4afcb4d0 138 number = round(number / inchmm, 4)
5ed5cea5
TM
139 inch = 1
140
141 units = "in" if inch else "mm"
142
4afcb4d0 143 measurement = (str(number) + units)
5ed5cea5
TM
144
145 if ((self.options['changes'] == 'no') or (self.last_measurement != measurement)):
146 self.put(self.ss_cmd, self.es_cmd, self.out_ann, [0, [measurement, str(number)]])
147 self.last_measurement = measurement
148
4afcb4d0 149 # Prepare for next packet.
5ed5cea5 150 self.reset()