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