]> sigrok.org Git - libsigrokdecode.git/blob - decoders/dcf77/dcf77.py
srd: PDs: Use strings for states, too.
[libsigrokdecode.git] / decoders / dcf77 / dcf77.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 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
23 import sigrokdecode as srd
24 import calendar
25
26 # Annotation feed formats
27 ANN_ASCII = 0
28
29 # Return the specified BCD number (max. 8 bits) as integer.
30 def bcd2int(b):
31     return (b & 0x0f) + ((b >> 4) * 10)
32
33 class Decoder(srd.Decoder):
34     api_version = 1
35     id = 'dcf77'
36     name = 'DCF77'
37     longname = 'DCF77 time protocol'
38     desc = 'TODO.'
39     longdesc = 'TODO.'
40     license = 'gplv2+'
41     inputs = ['logic']
42     outputs = ['dcf77']
43     probes = [
44         {'id': 'data', 'name': 'DATA', 'desc': 'DATA line'},
45     ]
46     optional_probes = [
47         {'id': 'pon', 'name': 'PON', 'desc': 'TODO'},
48     ]
49     options = {}
50     annotations = [
51         # ANN_ASCII
52         ['ASCII', 'TODO: description'],
53     ]
54
55     def __init__(self, **kwargs):
56         self.state = 'WAIT FOR RISING EDGE'
57         self.oldval = None
58         self.samplenum = 0
59         self.bit_start = 0
60         self.bit_start_old = 0
61         self.bitcount = 0 # Counter for the DCF77 bits (0..58)
62         self.dcf77_bitnumber_is_known = 0
63
64     def start(self, metadata):
65         self.samplerate = metadata['samplerate']
66         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'dcf77')
67         self.out_ann = self.add(srd.OUTPUT_ANN, 'dcf77')
68
69     def report(self):
70         pass
71
72     # TODO: Which range to use? Only the 100ms/200ms or full second?
73     def handle_dcf77_bit(self, bit):
74         c = self.bitcount
75         a = self.out_ann
76         ss = es = 0 # FIXME
77
78         # Create one annotation for each DCF77 bit (containing the 0/1 value).
79         # Use 'Unknown DCF77 bit x: val' if we're not sure yet which of the
80         # 0..58 bits it is (because we haven't seen a 'new minute' marker yet).
81         # Otherwise, use 'DCF77 bit x: val'.
82         s = '' if self.dcf77_bitnumber_is_known else 'Unknown '
83         self.put(ss, es, a, [0, ['%sDCF77 bit %d: %d' % (s, c, bit)]])
84
85         # If we're not sure yet which of the 0..58 DCF77 bits we have, return.
86         # We don't want to decode bogus data.
87         if not self.dcf77_bitnumber_is_known:
88             return
89
90         # Output specific "decoded" annotations for the respective DCF77 bits.
91         if c == 0:
92             # Start of minute: DCF bit 0.
93             if bit == 0:
94                 self.put(ss, es, a, [0, ['Start of minute (always 0)']])
95             else:
96                 self.put(ss, es, a, [0, ['ERROR: Start of minute != 0']])
97         elif c in range(1, 14 + 1):
98             # Special bits (civil warnings, weather forecast): DCF77 bits 1-14.
99             if c == 1:
100                 self.tmp = bit
101             else:
102                 self.tmp |= (bit << (c - 1))
103             if c == 14:
104                 self.put(ss, es, a, [0, ['Special bits: %s' % bin(self.tmp)]])
105         elif c == 15:
106             s = '' if (bit == 1) else 'not '
107             self.put(ss, es, a, [0, ['Call bit is %sset' % s]])
108             # TODO: Previously this bit indicated use of the backup antenna.
109         elif c == 16:
110             s = '' if (bit == 1) else 'not '
111             self.put(ss, es, a, [0, ['Summer time announcement %sactive' % s]])
112         elif c == 17:
113             s = '' if (bit == 1) else 'not '
114             self.put(ss, es, a, [0, ['CEST is %sin effect' % s]])
115         elif c == 18:
116             s = '' if (bit == 1) else 'not '
117             self.put(ss, es, a, [0, ['CET is %sin effect' % s]])
118         elif c == 19:
119             s = '' if (bit == 1) else 'not '
120             self.put(ss, es, a, [0, ['Leap second announcement %sactive' % s]])
121         elif c == 20:
122             # Start of encoded time: DCF bit 20.
123             if bit == 1:
124                 self.put(ss, es, a, [0, ['Start of encoded time (always 1)']])
125             else:
126                 self.put(ss, es, a,
127                          [0, ['ERROR: Start of encoded time != 1']])
128         elif c in range(21, 27 + 1):
129             # Minutes (0-59): DCF77 bits 21-27 (BCD format).
130             if c == 21:
131                 self.tmp = bit
132             else:
133                 self.tmp |= (bit << (c - 21))
134             if c == 27:
135                 self.put(ss, es, a, [0, ['Minutes: %d' % bcd2int(self.tmp)]])
136         elif c == 28:
137             # Even parity over minute bits (21-28): DCF77 bit 28.
138             self.tmp |= (bit << (c - 21))
139             parity = bin(self.tmp).count('1')
140             s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
141             self.put(ss, es, a, [0, ['Minute parity: %s' % s]])
142         elif c in range(29, 34 + 1):
143             # Hours (0-23): DCF77 bits 29-34 (BCD format).
144             if c == 29:
145                 self.tmp = bit
146             else:
147                 self.tmp |= (bit << (c - 29))
148             if c == 34:
149                 self.put(ss, es, a, [0, ['Hours: %d' % bcd2int(self.tmp)]])
150         elif c == 35:
151             # Even parity over hour bits (29-35): DCF77 bit 35.
152             self.tmp |= (bit << (c - 29))
153             parity = bin(self.tmp).count('1')
154             s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
155             self.put(ss, es, a, [0, ['Hour parity: %s' % s]])
156         elif c in range(36, 41 + 1):
157             # Day of month (1-31): DCF77 bits 36-41 (BCD format).
158             if c == 36:
159                 self.tmp = bit
160             else:
161                 self.tmp |= (bit << (c - 36))
162             if c == 41:
163                 self.put(ss, es, a, [0, ['Day: %d' % bcd2int(self.tmp)]])
164         elif c in range(42, 44 + 1):
165             # Day of week (1-7): DCF77 bits 42-44 (BCD format).
166             # A value of 1 means Monday, 7 means Sunday.
167             if c == 42:
168                 self.tmp = bit
169             else:
170                 self.tmp |= (bit << (c - 42))
171             if c == 44:
172                 d = bcd2int(self.tmp)
173                 dn = calendar.day_name[d - 1] # day_name[0] == Monday
174                 self.put(ss, es, a, [0, ['Day of week: %d (%s)' % (d, dn)]])
175         elif c in range(45, 49 + 1):
176             # Month (1-12): DCF77 bits 45-49 (BCD format).
177             if c == 45:
178                 self.tmp = bit
179             else:
180                 self.tmp |= (bit << (c - 45))
181             if c == 49:
182                 m = bcd2int(self.tmp)
183                 mn = calendar.month_name[m] # month_name[1] == January
184                 self.put(ss, es, a, [0, ['Month: %d (%s)' % (m, mn)]])
185         elif c in range(50, 57 + 1):
186             # Year (0-99): DCF77 bits 50-57 (BCD format).
187             if c == 50:
188                 self.tmp = bit
189             else:
190                 self.tmp |= (bit << (c - 50))
191             if c == 57:
192                 self.put(ss, es, a, [0, ['Year: %d' % bcd2int(self.tmp)]])
193         elif c == 58:
194             # Even parity over date bits (36-58): DCF77 bit 58.
195             self.tmp |= (bit << (c - 50))
196             parity = bin(self.tmp).count('1')
197             s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
198             self.put(ss, es, a, [0, ['Date parity: %s' % s]])
199         else:
200             raise Exception('Invalid DCF77 bit: %d' % c)
201
202     def decode(self, ss, es, data):
203         for samplenum, (val) in data: # TODO: Handle optional PON.
204
205             self.samplenum += 1 # FIXME. Use samplenum. Off-by-one?
206
207             if self.state == 'WAIT FOR RISING EDGE':
208                 # Wait until the next rising edge occurs.
209                 if not (self.oldval == 0 and val == 1):
210                     self.oldval = val
211                     continue
212
213                 # Save the sample number where the DCF77 bit begins.
214                 self.bit_start = self.samplenum
215
216                 # Calculate the length (in ms) between two rising edges.
217                 len_edges = self.bit_start - self.bit_start_old
218                 len_edges_ms = int((len_edges / self.samplerate) * 1000)
219
220                 # The time between two rising edges is usually around 1000ms.
221                 # For DCF77 bit 59, there is no rising edge at all, i.e. the
222                 # time between DCF77 bit 59 and DCF77 bit 0 (of the next
223                 # minute) is around 2000ms. Thus, if we see an edge with a
224                 # 2000ms distance to the last one, this edge marks the
225                 # beginning of a new minute (and DCF77 bit 0 of that minute).
226                 if len_edges_ms in range(1600, 2400 + 1):
227                     self.put(ss, es, self.out_ann, [0, ['New minute starts']])
228                     self.bitcount = 0
229                     self.bit_start_old = self.bit_start
230                     self.dcf77_bitnumber_is_known = 1
231                     # Don't switch to GET_BIT state this time.
232                     continue
233
234                 self.bit_start_old = self.bit_start
235                 self.state = GET_BIT
236
237             elif self.state == GET_BIT:
238                 # Wait until the next falling edge occurs.
239                 if not (self.oldval == 1 and val == 0):
240                     self.oldval = val
241                     continue
242
243                 # Calculate the length (in ms) of the current high period.
244                 len_high = self.samplenum - self.bit_start
245                 len_high_ms = int((len_high / self.samplerate) * 1000)
246
247                 # If the high signal was 100ms long, that encodes a 0 bit.
248                 # If it was 200ms long, that encodes a 1 bit.
249                 if len_high_ms in range(40, 160 + 1):
250                     bit = 0
251                 elif len_high_ms in range(161, 260 + 1):
252                     bit = 1
253                 else:
254                     bit = -1 # TODO: Error?
255
256                 # TODO: There's no bit 59, make sure none is decoded.
257                 if bit in (0, 1) and self.bitcount in range(0, 58 + 1):
258                     self.handle_dcf77_bit(bit)
259                     self.bitcount += 1
260
261                 self.state = 'WAIT FOR RISING EDGE'
262
263             else:
264                 raise Exception('Invalid state: %d' % self.state)
265
266             self.oldval = val
267