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