]>
Commit | Line | Data |
---|---|---|
b1f2b85b JR |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2014 Johannes Roemer <jroemer@physik.uni-wuerzburg.de> | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
b1f2b85b JR |
18 | ## |
19 | ||
20 | import sigrokdecode as srd | |
21 | ||
22 | # Define valid timing values (in microseconds). | |
23 | timing = { | |
24 | 'START LOW' : {'min': 750, 'max': 25000}, | |
25 | 'START HIGH' : {'min': 10, 'max': 10000}, | |
ed758f09 UH |
26 | 'RESPONSE LOW' : {'min': 50, 'max': 90}, |
27 | 'RESPONSE HIGH' : {'min': 50, 'max': 90}, | |
b1f2b85b JR |
28 | 'BIT LOW' : {'min': 45, 'max': 90}, |
29 | 'BIT 0 HIGH' : {'min': 20, 'max': 35}, | |
30 | 'BIT 1 HIGH' : {'min': 65, 'max': 80}, | |
31 | } | |
32 | ||
33 | class SamplerateError(Exception): | |
34 | pass | |
35 | ||
36 | class Decoder(srd.Decoder): | |
b6bac00c | 37 | api_version = 3 |
b1f2b85b | 38 | id = 'am230x' |
2787cf2a | 39 | name = 'AM230x' |
e2ba53bb | 40 | longname = 'Aosong AM230x/DHTxx/RHTxx' |
2787cf2a | 41 | desc = 'Aosong AM230x/DHTxx/RHTxx humidity/temperature sensor.' |
b1f2b85b JR |
42 | license = 'gplv2+' |
43 | inputs = ['logic'] | |
6cbba91f | 44 | outputs = [] |
d6d8a8a4 | 45 | tags = ['IC', 'Sensor'] |
b1f2b85b JR |
46 | channels = ( |
47 | {'id': 'sda', 'name': 'SDA', 'desc': 'Single wire serial data line'}, | |
48 | ) | |
49 | options = ( | |
cda1352a | 50 | {'id': 'device', 'desc': 'Device type', |
e2ba53bb | 51 | 'default': 'am230x', 'values': ('am230x/rht', 'dht11')}, |
b1f2b85b JR |
52 | ) |
53 | annotations = ( | |
54 | ('start', 'Start'), | |
55 | ('response', 'Response'), | |
56 | ('bit', 'Bit'), | |
57 | ('end', 'End'), | |
58 | ('byte', 'Byte'), | |
59 | ('humidity', 'Relative humidity in percent'), | |
60 | ('temperature', 'Temperature in degrees Celsius'), | |
61 | ('checksum', 'Checksum'), | |
62 | ) | |
63 | annotation_rows = ( | |
64 | ('bits', 'Bits', (0, 1, 2, 3)), | |
65 | ('bytes', 'Bytes', (4,)), | |
66 | ('results', 'Results', (5, 6, 7)), | |
67 | ) | |
68 | ||
69 | def putfs(self, data): | |
70 | self.put(self.fall, self.samplenum, self.out_ann, data) | |
71 | ||
72 | def putb(self, data): | |
73 | self.put(self.bytepos[-1], self.samplenum, self.out_ann, data) | |
74 | ||
75 | def putv(self, data): | |
76 | self.put(self.bytepos[-2], self.samplenum, self.out_ann, data) | |
77 | ||
779c540d | 78 | def reset_variables(self): |
b1f2b85b | 79 | self.state = 'WAIT FOR START LOW' |
b1f2b85b JR |
80 | self.fall = 0 |
81 | self.rise = 0 | |
82 | self.bits = [] | |
83 | self.bytepos = [] | |
84 | ||
85 | def is_valid(self, name): | |
86 | dt = 0 | |
87 | if name.endswith('LOW'): | |
88 | dt = self.samplenum - self.fall | |
89 | elif name.endswith('HIGH'): | |
90 | dt = self.samplenum - self.rise | |
91 | if dt >= self.cnt[name]['min'] and dt <= self.cnt[name]['max']: | |
92 | return True | |
93 | return False | |
94 | ||
95 | def bits2num(self, bitlist): | |
96 | number = 0 | |
47405f47 UH |
97 | for i in range(len(bitlist)): |
98 | number += bitlist[-1 - i] * 2**i | |
b1f2b85b JR |
99 | return number |
100 | ||
101 | def calculate_humidity(self, bitlist): | |
102 | h = 0 | |
cda1352a | 103 | if self.options['device'] == 'dht11': |
b1f2b85b JR |
104 | h = self.bits2num(bitlist[0:8]) |
105 | else: | |
106 | h = self.bits2num(bitlist) / 10 | |
107 | return h | |
108 | ||
109 | def calculate_temperature(self, bitlist): | |
110 | t = 0 | |
cda1352a | 111 | if self.options['device'] == 'dht11': |
b1f2b85b JR |
112 | t = self.bits2num(bitlist[0:8]) |
113 | else: | |
114 | t = self.bits2num(bitlist[1:]) / 10 | |
115 | if bitlist[0] == 1: | |
116 | t = -t | |
117 | return t | |
118 | ||
119 | def calculate_checksum(self, bitlist): | |
120 | checksum = 0 | |
47405f47 | 121 | for i in range(8, len(bitlist) + 1, 8): |
b1f2b85b JR |
122 | checksum += self.bits2num(bitlist[i-8:i]) |
123 | return checksum % 256 | |
124 | ||
92b7b49f | 125 | def __init__(self): |
10aeb8ea GS |
126 | self.reset() |
127 | ||
128 | def reset(self): | |
b1f2b85b | 129 | self.samplerate = None |
779c540d | 130 | self.reset_variables() |
b1f2b85b JR |
131 | |
132 | def start(self): | |
133 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
134 | ||
135 | def metadata(self, key, value): | |
136 | if key != srd.SRD_CONF_SAMPLERATE: | |
137 | return | |
138 | self.samplerate = value | |
139 | # Convert microseconds to sample counts. | |
140 | self.cnt = {} | |
141 | for e in timing: | |
142 | self.cnt[e] = {} | |
143 | for t in timing[e]: | |
144 | self.cnt[e][t] = timing[e][t] * self.samplerate / 1000000 | |
145 | ||
0b6bc7f9 UH |
146 | def handle_byte(self, bit): |
147 | self.bits.append(bit) | |
148 | self.putfs([2, ['Bit: %d' % bit, '%d' % bit]]) | |
149 | self.fall = self.samplenum | |
150 | self.state = 'WAIT FOR BIT HIGH' | |
151 | if len(self.bits) % 8 == 0: | |
152 | byte = self.bits2num(self.bits[-8:]) | |
153 | self.putb([4, ['Byte: %#04x' % byte, '%#04x' % byte]]) | |
154 | if len(self.bits) == 16: | |
155 | h = self.calculate_humidity(self.bits[-16:]) | |
156 | self.putv([5, ['Humidity: %.1f %%' % h, 'RH = %.1f %%' % h]]) | |
157 | elif len(self.bits) == 32: | |
158 | t = self.calculate_temperature(self.bits[-16:]) | |
159 | self.putv([6, ['Temperature: %.1f °C' % t, 'T = %.1f °C' % t]]) | |
160 | elif len(self.bits) == 40: | |
161 | parity = self.bits2num(self.bits[-8:]) | |
162 | if parity == self.calculate_checksum(self.bits[0:32]): | |
163 | self.putb([7, ['Checksum: OK', 'OK']]) | |
164 | else: | |
165 | self.putb([7, ['Checksum: not OK', 'NOK']]) | |
166 | self.state = 'WAIT FOR END' | |
167 | self.bytepos.append(self.samplenum) | |
168 | ||
b6bac00c | 169 | def decode(self): |
b1f2b85b JR |
170 | if not self.samplerate: |
171 | raise SamplerateError('Cannot decode without samplerate.') | |
b6bac00c | 172 | while True: |
b1f2b85b JR |
173 | # State machine. |
174 | if self.state == 'WAIT FOR START LOW': | |
b6bac00c | 175 | self.wait({0: 'f'}) |
b1f2b85b JR |
176 | self.fall = self.samplenum |
177 | self.state = 'WAIT FOR START HIGH' | |
178 | elif self.state == 'WAIT FOR START HIGH': | |
b6bac00c | 179 | self.wait({0: 'r'}) |
b1f2b85b JR |
180 | if self.is_valid('START LOW'): |
181 | self.rise = self.samplenum | |
182 | self.state = 'WAIT FOR RESPONSE LOW' | |
183 | else: | |
779c540d | 184 | self.reset_variables() |
b1f2b85b | 185 | elif self.state == 'WAIT FOR RESPONSE LOW': |
b6bac00c | 186 | self.wait({0: 'f'}) |
b1f2b85b JR |
187 | if self.is_valid('START HIGH'): |
188 | self.putfs([0, ['Start', 'S']]) | |
189 | self.fall = self.samplenum | |
190 | self.state = 'WAIT FOR RESPONSE HIGH' | |
191 | else: | |
779c540d | 192 | self.reset_variables() |
b1f2b85b | 193 | elif self.state == 'WAIT FOR RESPONSE HIGH': |
b6bac00c | 194 | self.wait({0: 'r'}) |
b1f2b85b JR |
195 | if self.is_valid('RESPONSE LOW'): |
196 | self.rise = self.samplenum | |
197 | self.state = 'WAIT FOR FIRST BIT' | |
198 | else: | |
779c540d | 199 | self.reset_variables() |
b1f2b85b | 200 | elif self.state == 'WAIT FOR FIRST BIT': |
b6bac00c | 201 | self.wait({0: 'f'}) |
b1f2b85b JR |
202 | if self.is_valid('RESPONSE HIGH'): |
203 | self.putfs([1, ['Response', 'R']]) | |
204 | self.fall = self.samplenum | |
205 | self.bytepos.append(self.samplenum) | |
206 | self.state = 'WAIT FOR BIT HIGH' | |
207 | else: | |
779c540d | 208 | self.reset_variables() |
b1f2b85b | 209 | elif self.state == 'WAIT FOR BIT HIGH': |
b6bac00c | 210 | self.wait({0: 'r'}) |
b1f2b85b JR |
211 | if self.is_valid('BIT LOW'): |
212 | self.rise = self.samplenum | |
213 | self.state = 'WAIT FOR BIT LOW' | |
214 | else: | |
779c540d | 215 | self.reset_variables() |
b1f2b85b | 216 | elif self.state == 'WAIT FOR BIT LOW': |
b6bac00c | 217 | self.wait({0: 'f'}) |
b1f2b85b JR |
218 | if self.is_valid('BIT 0 HIGH'): |
219 | bit = 0 | |
220 | elif self.is_valid('BIT 1 HIGH'): | |
221 | bit = 1 | |
222 | else: | |
779c540d | 223 | self.reset_variables() |
b1f2b85b | 224 | continue |
0b6bc7f9 | 225 | self.handle_byte(bit) |
b1f2b85b | 226 | elif self.state == 'WAIT FOR END': |
b6bac00c | 227 | self.wait({0: 'r'}) |
b1f2b85b | 228 | self.putfs([3, ['End', 'E']]) |
779c540d | 229 | self.reset_variables() |