Commit | Line | Data |
---|---|---|
63efb482 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 | ||
24 | import sigrokdecode as srd | |
25 | ||
26 | class Decoder(srd.Decoder): | |
27 | api_version = 3 | |
28 | id = 'caliper' | |
29 | name = 'Caliper' | |
30 | longname = 'Digital calipers' | |
31 | desc = 'Protocol of cheap generic digital calipers' | |
32 | license = 'mit' | |
33 | inputs = ['logic'] | |
34 | outputs = [] | |
35 | channels = ( | |
36 | {'id': 'clk', 'name': 'CLK', 'desc': 'Serial clock line'}, | |
37 | {'id': 'data', 'name': 'DATA', 'desc': 'Serial data line'}, | |
38 | ) | |
39 | options = ( | |
40 | {'id': 'timeout_ms', 'desc': 'Timeout packet after X ms, 0 to disable', 'default': 10}, | |
41 | {'id': 'unit', 'desc': 'Convert units', 'default': 'keep', 'values': ('keep', 'mm', 'inch')}, | |
42 | ) | |
43 | tags = ['Analog/digital', 'IC', 'Sensor'] | |
44 | annotations = ( | |
45 | ('measurements', 'Measurements'), | |
46 | ('warning', 'Warnings'), | |
47 | ) | |
48 | annotation_rows = ( | |
49 | ('measurements', 'Measurements', (0,)), | |
50 | ('warnings', 'Warnings', (1,)), | |
51 | ) | |
52 | ||
53 | def reset_data(self): | |
54 | self.bits = 0 | |
55 | self.number = 0 | |
56 | self.flags = 0 | |
57 | ||
58 | def metadata(self, key, value): | |
59 | if key == srd.SRD_CONF_SAMPLERATE: | |
60 | self.samplerate = value | |
61 | ||
62 | def __init__(self): | |
63 | self.reset() | |
64 | ||
65 | def reset(self): | |
66 | self.ss_cmd, self.es_cmd = 0, 0 | |
67 | self.reset_data() | |
68 | ||
69 | def start(self): | |
70 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
71 | ||
72 | #Switch bit order of variable x, which is l bit long | |
73 | def bitr(self,x,l): | |
74 | return int(bin(x)[2:].zfill(l)[::-1], 2) | |
75 | ||
76 | def decode(self): | |
77 | while True: | |
78 | clk, data = self.wait([{0: 'r'},{'skip': round(self.samplerate/1000)}]) | |
79 | #print([clk,data]) | |
80 | ||
81 | #Timeout after inactivity | |
82 | if(self.options['timeout_ms'] > 0): | |
83 | if self.samplenum > self.es_cmd + (self.samplerate/(1000/self.options['timeout_ms'])): | |
84 | if self.bits > 0: | |
85 | self.put(self.ss_cmd, self.samplenum, self.out_ann, [1, ['timeout with %s bits in buffer'%(self.bits),'timeout']]) | |
86 | self.reset() | |
87 | ||
88 | #Do nothing if there was timeout without rising clock edge | |
89 | if self.matched == (False, True): | |
90 | continue | |
91 | ||
92 | #Store position of last activity | |
93 | self.es_cmd = self.samplenum | |
94 | ||
95 | #Store position of first bit | |
96 | if self.ss_cmd == 0: | |
97 | self.ss_cmd = self.samplenum | |
98 | ||
99 | #Shift in measured number | |
100 | if self.bits < 16: | |
101 | self.number = (self.number << 1) | (data & 0b1) | |
102 | self.bits+=1 | |
103 | continue | |
104 | ||
105 | #Shift in flag bits | |
106 | if self.bits < 24: | |
107 | self.flags = (self.flags << 1) | (data & 0b1) | |
108 | self.bits+=1 | |
109 | if self.bits < 24: | |
110 | continue | |
111 | #Hooray! We got last bit of data | |
112 | self.es_cmd = self.samplenum | |
113 | ||
114 | #Do actual decoding | |
115 | ||
116 | #print(format(self.flags, '08b')); | |
117 | ||
118 | negative = ((self.flags & 0b00001000) >> 3) | |
119 | inch = (self.flags & 0b00000001) | |
120 | ||
121 | number = self.bitr(self.number, 16) | |
122 | ||
123 | #print(format(number, '016b')) | |
124 | ||
125 | if negative > 0: | |
126 | number = -number | |
127 | ||
128 | inchmm = 25.4 #how many mms in inch | |
129 | ||
130 | if inch: | |
131 | number = number/2000 | |
132 | if self.options['unit'] == 'mm': | |
133 | number *= inchmm | |
134 | inch = 0 | |
135 | else: | |
136 | number = number/100 | |
137 | if self.options['unit'] == 'inch': | |
138 | number = round(number/inchmm,4) | |
139 | inch = 1 | |
140 | ||
141 | units = "in" if inch else "mm" | |
142 | ||
143 | measurement = (str(number)+units) | |
144 | #print(measurement) | |
145 | ||
146 | self.put(self.ss_cmd, self.es_cmd, self.out_ann, [0, [measurement, str(number)]]) | |
147 | ||
148 | #Prepare for next packet | |
149 | self.reset() |