]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/dcf77/pd.py
dcf77: Provide short and long annotations.
[libsigrokdecode.git] / decoders / dcf77 / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.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
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
21# DCF77 protocol decoder
22
23import sigrokdecode as srd
24import calendar
25
26# Return the specified BCD number (max. 8 bits) as integer.
27def bcd2int(b):
28 return (b & 0x0f) + ((b >> 4) * 10)
29
30class Decoder(srd.Decoder):
31 api_version = 1
32 id = 'dcf77'
33 name = 'DCF77'
34 longname = 'DCF77 time protocol'
35 desc = 'European longwave time signal (77.5kHz carrier signal).'
36 license = 'gplv2+'
37 inputs = ['logic']
38 outputs = ['dcf77']
39 probes = [
40 {'id': 'data', 'name': 'DATA', 'desc': 'DATA line'},
41 ]
42 optional_probes = []
43 options = {}
44 annotations = [
45 ['start_of_minute', 'Start of minute'],
46 ['special_bits', 'Special bits (civil warnings, weather forecast)'],
47 ['call_bit', 'Call bit'],
48 ['summer_time', 'Summer time announcement'],
49 ['cest', 'CEST bit'],
50 ['cet', 'CET bit'],
51 ['leap_second', 'Leap second bit'],
52 ['start_of_time', 'Start of encoded time'],
53 ['minute', 'Minute'],
54 ['minute_parity', 'Minute parity bit'],
55 ['hour', 'Hour'],
56 ['hour_parity', 'Hour parity bit'],
57 ['day', 'Day of month'],
58 ['day_of_week', 'Day of week'],
59 ['month', 'Month'],
60 ['year', 'Year'],
61 ['date_parity', 'Date parity bit'],
62 ['raw_bits', 'Raw bits'],
63 ['unknown_bits', 'Unknown bits'],
64 ['warnings', 'Human-readable warnings'],
65 ]
66
67 def __init__(self, **kwargs):
68 self.state = 'WAIT FOR RISING EDGE'
69 self.oldpins = None
70 self.oldval = None
71 self.samplenum = 0
72 self.ss_bit = self.ss_bit_old = self.es_bit = self.ss_block = 0
73 self.bitcount = 0 # Counter for the DCF77 bits (0..58)
74 self.dcf77_bitnumber_is_known = 0
75
76 def start(self, metadata):
77 self.samplerate = metadata['samplerate']
78 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'dcf77')
79 self.out_ann = self.add(srd.OUTPUT_ANN, 'dcf77')
80
81 def report(self):
82 pass
83
84 def putx(self, data):
85 # Annotation for a single DCF77 bit.
86 self.put(self.ss_bit, self.es_bit, self.out_ann, data)
87
88 def putb(self, data):
89 # Annotation for a multi-bit DCF77 field.
90 self.put(self.ss_block, self.samplenum, self.out_ann, data)
91
92 # TODO: Which range to use? Only the 100ms/200ms or full second?
93 def handle_dcf77_bit(self, bit):
94 c = self.bitcount
95
96 # Create one annotation for each DCF77 bit (containing the 0/1 value).
97 # Use 'Unknown DCF77 bit x: val' if we're not sure yet which of the
98 # 0..58 bits it is (because we haven't seen a 'new minute' marker yet).
99 # Otherwise, use 'DCF77 bit x: val'.
100 s = 'B' if self.dcf77_bitnumber_is_known else 'Unknown b'
101 ann = 17 if self.dcf77_bitnumber_is_known else 18
102 self.putx([ann, ['%sit %d: %d' % (s, c, bit), '%d' % bit]])
103
104 # If we're not sure yet which of the 0..58 DCF77 bits we have, return.
105 # We don't want to decode bogus data.
106 if not self.dcf77_bitnumber_is_known:
107 return
108
109 # Output specific "decoded" annotations for the respective DCF77 bits.
110 if c == 0:
111 # Start of minute: DCF bit 0.
112 if bit == 0:
113 self.putx([0, ['Start of minute (always 0)',
114 'Start of minute', 'SoM']])
115 else:
116 self.putx([19, ['Start of minute != 0', 'SoM != 0']])
117 elif c in range(1, 14 + 1):
118 # Special bits (civil warnings, weather forecast): DCF77 bits 1-14.
119 if c == 1:
120 self.tmp = bit
121 self.ss_block = self.ss_bit
122 else:
123 self.tmp |= (bit << (c - 1))
124 if c == 14:
125 s = bin(self.tmp)[2:].zfill(14)
126 self.putb([1, ['Special bits: %s' % s, 'SB: %s' % s]])
127 elif c == 15:
128 s = '' if (bit == 1) else 'not '
129 self.putx([2, ['Call bit: %sset' % s, 'CB: %sset' % s]])
130 # TODO: Previously this bit indicated use of the backup antenna.
131 elif c == 16:
132 s = '' if (bit == 1) else 'not '
133 x = 'yes' if (bit == 1) else 'no'
134 self.putx([3, ['Summer time announcement: %sactive' % s,
135 'Summer time: %sactive' % s,
136 'Summer time: %s' % x, 'ST: %s' % x]])
137 elif c == 17:
138 s = '' if (bit == 1) else 'not '
139 x = 'yes' if (bit == 1) else 'no'
140 self.putx([4, ['CEST: %sin effect' % s, 'CEST: %s' % x]])
141 elif c == 18:
142 s = '' if (bit == 1) else 'not '
143 x = 'yes' if (bit == 1) else 'no'
144 self.putx([5, ['CET: %sin effect' % s, 'CET: %s' % x]])
145 elif c == 19:
146 s = '' if (bit == 1) else 'not '
147 x = 'yes' if (bit == 1) else 'no'
148 self.putx([6, ['Leap second announcement: %sactive' % s,
149 'Leap second: %sactive' % s,
150 'Leap second: %s' % x, 'LS: %s' % x]])
151 elif c == 20:
152 # Start of encoded time: DCF bit 20.
153 if bit == 1:
154 self.putx([7, ['Start of encoded time (always 1)',
155 'Start of encoded time', 'SoeT']])
156 else:
157 self.putx([19, ['Start of encoded time != 1', 'SoeT != 1']])
158 elif c in range(21, 27 + 1):
159 # Minutes (0-59): DCF77 bits 21-27 (BCD format).
160 if c == 21:
161 self.tmp = bit
162 self.ss_block = self.ss_bit
163 else:
164 self.tmp |= (bit << (c - 21))
165 if c == 27:
166 m = bcd2int(self.tmp)
167 self.putb([8, ['Minutes: %d' % m, 'Min: %d' % m]])
168 elif c == 28:
169 # Even parity over minute bits (21-28): DCF77 bit 28.
170 self.tmp |= (bit << (c - 21))
171 parity = bin(self.tmp).count('1')
172 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
173 self.putx([9, ['Minute parity: %s' % s, 'Min parity: %s' % s]])
174 elif c in range(29, 34 + 1):
175 # Hours (0-23): DCF77 bits 29-34 (BCD format).
176 if c == 29:
177 self.tmp = bit
178 self.ss_block = self.ss_bit
179 else:
180 self.tmp |= (bit << (c - 29))
181 if c == 34:
182 self.putb([10, ['Hours: %d' % bcd2int(self.tmp)]])
183 elif c == 35:
184 # Even parity over hour bits (29-35): DCF77 bit 35.
185 self.tmp |= (bit << (c - 29))
186 parity = bin(self.tmp).count('1')
187 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
188 self.putx([11, ['Hour parity: %s' % s]])
189 elif c in range(36, 41 + 1):
190 # Day of month (1-31): DCF77 bits 36-41 (BCD format).
191 if c == 36:
192 self.tmp = bit
193 self.ss_block = self.ss_bit
194 else:
195 self.tmp |= (bit << (c - 36))
196 if c == 41:
197 self.putb([12, ['Day: %d' % bcd2int(self.tmp)]])
198 elif c in range(42, 44 + 1):
199 # Day of week (1-7): DCF77 bits 42-44 (BCD format).
200 # A value of 1 means Monday, 7 means Sunday.
201 if c == 42:
202 self.tmp = bit
203 self.ss_block = self.ss_bit
204 else:
205 self.tmp |= (bit << (c - 42))
206 if c == 44:
207 d = bcd2int(self.tmp)
208 dn = calendar.day_name[d - 1] # day_name[0] == Monday
209 self.putb([13, ['Day of week: %d (%s)' % (d, dn),
210 'DoW: %d (%s)' % (d, dn)]])
211 elif c in range(45, 49 + 1):
212 # Month (1-12): DCF77 bits 45-49 (BCD format).
213 if c == 45:
214 self.tmp = bit
215 self.ss_block = self.ss_bit
216 else:
217 self.tmp |= (bit << (c - 45))
218 if c == 49:
219 m = bcd2int(self.tmp)
220 mn = calendar.month_name[m] # month_name[1] == January
221 self.putx([14, ['Month: %d (%s)' % (m, mn),
222 'Mon: %d (%s)' % (m, mn)]])
223 elif c in range(50, 57 + 1):
224 # Year (0-99): DCF77 bits 50-57 (BCD format).
225 if c == 50:
226 self.tmp = bit
227 self.ss_block = self.ss_bit
228 else:
229 self.tmp |= (bit << (c - 50))
230 if c == 57:
231 self.putb([15, ['Year: %d' % bcd2int(self.tmp)]])
232 elif c == 58:
233 # Even parity over date bits (36-58): DCF77 bit 58.
234 self.tmp |= (bit << (c - 50))
235 parity = bin(self.tmp).count('1')
236 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
237 self.putx([16, ['Date parity: %s' % s, 'DP: %s' %s]])
238 else:
239 raise Exception('Invalid DCF77 bit: %d' % c)
240
241 def decode(self, ss, es, data):
242 for (self.samplenum, pins) in data:
243
244 # Ignore identical samples early on (for performance reasons).
245 if self.oldpins == pins:
246 continue
247 self.oldpins, (val,) = pins, pins
248
249 if self.state == 'WAIT FOR RISING EDGE':
250 # Wait until the next rising edge occurs.
251 if not (self.oldval == 0 and val == 1):
252 self.oldval = val
253 continue
254
255 # Save the sample number where the DCF77 bit begins.
256 self.ss_bit = self.samplenum
257
258 # Calculate the length (in ms) between two rising edges.
259 len_edges = self.ss_bit - self.ss_bit_old
260 len_edges_ms = int((len_edges / self.samplerate) * 1000)
261
262 # The time between two rising edges is usually around 1000ms.
263 # For DCF77 bit 59, there is no rising edge at all, i.e. the
264 # time between DCF77 bit 59 and DCF77 bit 0 (of the next
265 # minute) is around 2000ms. Thus, if we see an edge with a
266 # 2000ms distance to the last one, this edge marks the
267 # beginning of a new minute (and DCF77 bit 0 of that minute).
268 if len_edges_ms in range(1600, 2400 + 1):
269 self.bitcount = 0
270 self.ss_bit_old = self.ss_bit
271 self.dcf77_bitnumber_is_known = 1
272
273 self.ss_bit_old = self.ss_bit
274 self.state = 'GET BIT'
275
276 elif self.state == 'GET BIT':
277 # Wait until the next falling edge occurs.
278 if not (self.oldval == 1 and val == 0):
279 self.oldval = val
280 continue
281
282 # Save the sample number where the DCF77 bit ends.
283 self.es_bit = self.samplenum
284
285 # Calculate the length (in ms) of the current high period.
286 len_high = self.samplenum - self.ss_bit
287 len_high_ms = int((len_high / self.samplerate) * 1000)
288
289 # If the high signal was 100ms long, that encodes a 0 bit.
290 # If it was 200ms long, that encodes a 1 bit.
291 if len_high_ms in range(40, 160 + 1):
292 bit = 0
293 elif len_high_ms in range(161, 260 + 1):
294 bit = 1
295 else:
296 bit = -1 # TODO: Error?
297
298 # There's no bit 59, make sure none is decoded.
299 if bit in (0, 1) and self.bitcount in range(0, 58 + 1):
300 self.handle_dcf77_bit(bit)
301 self.bitcount += 1
302
303 self.state = 'WAIT FOR RISING EDGE'
304
305 else:
306 raise Exception('Invalid state: %s' % self.state)
307
308 self.oldval = val
309