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