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