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