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