X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fdcf77%2Fpd.py;h=7365134efc0b82bfea5ba7ed84bd6e99f7eb8376;hp=9a273b21498dbdc828c8f07461a9bffde46debd2;hb=6cbba91f23b9f9ace75b4722c9c0776b9211008d;hpb=f2a5df42ea41e6c4370d4efa1a27ab942ba1ddff diff --git a/decoders/dcf77/pd.py b/decoders/dcf77/pd.py index 9a273b2..7365134 100644 --- a/decoders/dcf77/pd.py +++ b/decoders/dcf77/pd.py @@ -1,7 +1,7 @@ ## ## This file is part of the libsigrokdecode project. ## -## Copyright (C) 2012-2013 Uwe Hermann +## Copyright (C) 2012-2016 Uwe Hermann ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -14,78 +14,75 @@ ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## along with this program; if not, see . ## -# DCF77 protocol decoder - import sigrokdecode as srd import calendar +from common.srdhelper import bcd2int -# Return the specified BCD number (max. 8 bits) as integer. -def bcd2int(b): - return (b & 0x0f) + ((b >> 4) * 10) +class SamplerateError(Exception): + pass class Decoder(srd.Decoder): - api_version = 1 + api_version = 3 id = 'dcf77' name = 'DCF77' longname = 'DCF77 time protocol' desc = 'European longwave time signal (77.5kHz carrier signal).' license = 'gplv2+' inputs = ['logic'] - outputs = ['dcf77'] - probes = [ + outputs = [] + tags = ['Clock/timing'] + channels = ( {'id': 'data', 'name': 'DATA', 'desc': 'DATA line'}, - ] - optional_probes = [] - options = {} - annotations = [ - ['start_of_minute', 'Start of minute'], - ['special_bits', 'Special bits (civil warnings, weather forecast)'], - ['call_bit', 'Call bit'], - ['summer_time', 'Summer time announcement'], - ['cest', 'CEST bit'], - ['cet', 'CET bit'], - ['leap_second', 'Leap second bit'], - ['start_of_time', 'Start of encoded time'], - ['minute', 'Minute'], - ['minute_parity', 'Minute parity bit'], - ['hour', 'Hour'], - ['hour_parity', 'Hour parity bit'], - ['day', 'Day of month'], - ['day_of_week', 'Day of week'], - ['month', 'Month'], - ['year', 'Year'], - ['date_parity', 'Date parity bit'], - ['raw_bits', 'Raw bits'], - ['unknown_bits', 'Unknown bits'], - ['warnings', 'Human-readable warnings'], - ] + ) + annotations = ( + ('start-of-minute', 'Start of minute'), + ('special-bits', 'Special bits (civil warnings, weather forecast)'), + ('call-bit', 'Call bit'), + ('summer-time', 'Summer time announcement'), + ('cest', 'CEST bit'), + ('cet', 'CET bit'), + ('leap-second', 'Leap second bit'), + ('start-of-time', 'Start of encoded time'), + ('minute', 'Minute'), + ('minute-parity', 'Minute parity bit'), + ('hour', 'Hour'), + ('hour-parity', 'Hour parity bit'), + ('day', 'Day of month'), + ('day-of-week', 'Day of week'), + ('month', 'Month'), + ('year', 'Year'), + ('date-parity', 'Date parity bit'), + ('raw-bits', 'Raw bits'), + ('unknown-bits', 'Unknown bits'), + ('warnings', 'Human-readable warnings'), + ) + annotation_rows = ( + ('bits', 'Bits', (17, 18)), + ('fields', 'Fields', tuple(range(0, 16 + 1))), + ('warnings', 'Warnings', (19,)), + ) + + def __init__(self): + self.reset() - def __init__(self, **kwargs): + def reset(self): self.samplerate = None self.state = 'WAIT FOR RISING EDGE' - self.oldpins = None - self.oldval = None - self.samplenum = 0 self.ss_bit = self.ss_bit_old = self.es_bit = self.ss_block = 0 self.datebits = [] self.bitcount = 0 # Counter for the DCF77 bits (0..58) self.dcf77_bitnumber_is_known = 0 def start(self): - # self.out_proto = self.add(srd.OUTPUT_PYTHON, 'dcf77') - self.out_ann = self.add(srd.OUTPUT_ANN, 'dcf77') + self.out_ann = self.register(srd.OUTPUT_ANN) def metadata(self, key, value): if key == srd.SRD_CONF_SAMPLERATE: self.samplerate = value - def report(self): - pass - def putx(self, data): # Annotation for a single DCF77 bit. self.put(self.ss_bit, self.es_bit, self.out_ann, data) @@ -131,7 +128,7 @@ class Decoder(srd.Decoder): else: self.tmp |= (bit << (c - 1)) if c == 14: - s = bin(self.tmp)[2:].zfill(14) + s = '{:014b}'.format(self.tmp) self.putb([1, ['Special bits: %s' % s, 'SB: %s' % s]]) elif c == 15: s = '' if (bit == 1) else 'not ' @@ -214,9 +211,13 @@ class Decoder(srd.Decoder): self.tmp |= (bit << (c - 42)) if c == 44: d = bcd2int(self.tmp) - dn = calendar.day_name[d - 1] # day_name[0] == Monday - self.putb([13, ['Day of week: %d (%s)' % (d, dn), - 'DoW: %d (%s)' % (d, dn)]]) + try: + dn = calendar.day_name[d - 1] # day_name[0] == Monday + self.putb([13, ['Day of week: %d (%s)' % (d, dn), + 'DoW: %d (%s)' % (d, dn)]]) + except IndexError: + self.putb([19, ['Day of week: %d (%s)' % (d, 'invalid'), + 'DoW: %d (%s)' % (d, 'inv')]]) elif c in range(45, 49 + 1): # Month (1-12): DCF77 bits 45-49 (BCD format). if c == 45: @@ -226,9 +227,13 @@ class Decoder(srd.Decoder): self.tmp |= (bit << (c - 45)) if c == 49: m = bcd2int(self.tmp) - mn = calendar.month_name[m] # month_name[1] == January - self.putx([14, ['Month: %d (%s)' % (m, mn), - 'Mon: %d (%s)' % (m, mn)]]) + try: + mn = calendar.month_name[m] # month_name[1] == January + self.putb([14, ['Month: %d (%s)' % (m, mn), + 'Mon: %d (%s)' % (m, mn)]]) + except IndexError: + self.putb([19, ['Month: %d (%s)' % (m, 'invalid'), + 'Mon: %d (%s)' % (m, 'inv')]]) elif c in range(50, 57 + 1): # Year (0-99): DCF77 bits 50-57 (BCD format). if c == 50: @@ -242,26 +247,19 @@ class Decoder(srd.Decoder): # Even parity over date bits (36-58): DCF77 bit 58. parity = self.datebits.count(1) s = 'OK' if ((parity % 2) == 0) else 'INVALID!' - self.putx([16, ['Date parity: %s' % s, 'DP: %s' %s]]) + self.putx([16, ['Date parity: %s' % s, 'DP: %s' % s]]) self.datebits = [] else: - raise Exception('Invalid DCF77 bit: %d' % c) - - def decode(self, ss, es, data): - if self.samplerate is None: - raise Exception("Cannot decode without samplerate.") - for (self.samplenum, pins) in data: - - # Ignore identical samples early on (for performance reasons). - if self.oldpins == pins: - continue - self.oldpins, (val,) = pins, pins + self.putx([19, ['Invalid DCF77 bit: %d' % c, + 'Invalid bit: %d' % c, 'Inv: %d' % c]]) + def decode(self): + if not self.samplerate: + raise SamplerateError('Cannot decode without samplerate.') + while True: if self.state == 'WAIT FOR RISING EDGE': # Wait until the next rising edge occurs. - if not (self.oldval == 0 and val == 1): - self.oldval = val - continue + self.wait({0: 'r'}) # Save the sample number where the DCF77 bit begins. self.ss_bit = self.samplenum @@ -286,9 +284,7 @@ class Decoder(srd.Decoder): elif self.state == 'GET BIT': # Wait until the next falling edge occurs. - if not (self.oldval == 1 and val == 0): - self.oldval = val - continue + self.wait({0: 'f'}) # Save the sample number where the DCF77 bit ends. self.es_bit = self.samplenum @@ -304,17 +300,12 @@ class Decoder(srd.Decoder): elif len_high_ms in range(161, 260 + 1): bit = 1 else: - bit = -1 # TODO: Error? + bit = -1 - # There's no bit 59, make sure none is decoded. - if bit in (0, 1) and self.bitcount in range(0, 58 + 1): + if bit in (0, 1): self.handle_dcf77_bit(bit) self.bitcount += 1 + else: + self.putx([19, ['Invalid bit timing', 'Inv timing', 'Inv']]) self.state = 'WAIT FOR RISING EDGE' - - else: - raise Exception('Invalid state: %s' % self.state) - - self.oldval = val -