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