X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fem4100%2Fpd.py;h=778cfd1621e6a918977a9567b0f3898dd877c4f6;hb=5b2c45db1db190ba7f921b0808c123c6e48a47f3;hp=d6a54ea2acb9e909e2a0a8c4829baa3960c52b60;hpb=1a782fad8a1e8addeb1f27e221b286ffe2b2e30a;p=libsigrokdecode.git diff --git a/decoders/em4100/pd.py b/decoders/em4100/pd.py index d6a54ea..778cfd1 100644 --- a/decoders/em4100/pd.py +++ b/decoders/em4100/pd.py @@ -5,8 +5,8 @@ ## ## 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 -## the Free Software Foundation; either data 2 of the License, or -## (at your option) any later data. +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,8 +14,7 @@ ## 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 . ## import sigrokdecode as srd @@ -24,7 +23,7 @@ class SamplerateError(Exception): pass class Decoder(srd.Decoder): - api_version = 2 + api_version = 3 id = 'em4100' name = 'EM4100' longname = 'RFID EM4100' @@ -62,7 +61,10 @@ class Decoder(srd.Decoder): ('tags', 'Tags', (9,)), ) - def __init__(self, **kwargs): + def __init__(self): + self.reset() + + def reset(self): self.samplerate = None self.oldpin = None self.last_samplenum = None @@ -74,12 +76,12 @@ class Decoder(srd.Decoder): self.oldpl = 0 self.oldsamplenum = 0 self.last_bit_pos = 0 - self.first_start = 0 + self.ss_first = 0 self.first_one = 0 self.state = 'HEADER' self.data = 0 self.data_bits = 0 - self.data_start = 0 + self.ss_data = 0 self.data_parity = 0 self.payload_cnt = 0 self.data_col_parity = [0, 0, 0, 0, 0, 0] @@ -98,20 +100,21 @@ class Decoder(srd.Decoder): def start(self): self.out_ann = self.register(srd.OUTPUT_ANN) - def add_bit(self, bit, bit_start, bit_stop): + def putbit(self, bit, ss, es): + self.put(ss, es, self.out_ann, [0, [str(bit)]]) if self.state == 'HEADER': if bit == 1: if self.first_one > 0: self.first_one += 1 if self.first_one == 9: - self.put(self.first_start, bit_stop, self.out_ann, + self.put(self.ss_first, es, self.out_ann, [1, ['Header', 'Head', 'He', 'H']]) self.first_one = 0 self.state = 'PAYLOAD' return if self.first_one == 0: self.first_one = 1 - self.first_start = bit_start + self.ss_first = ss if bit == 0: self.first_one = 0 @@ -120,20 +123,20 @@ class Decoder(srd.Decoder): if self.state == 'PAYLOAD': self.payload_cnt += 1 if self.data_bits == 0: - self.data_start = bit_start + self.ss_data = ss self.data = 0 self.data_parity = 0 self.data_bits += 1 if self.data_bits == 5: s = 'Version/customer' if self.payload_cnt <= 10 else 'Data' c = 2 if self.payload_cnt <= 10 else 3 - self.put(self.data_start, bit_start, self.out_ann, + self.put(self.ss_data, ss, self.out_ann, [c, [s + ': %X' % self.data, '%X' % self.data]]) s = 'OK' if self.data_parity == bit else 'ERROR' c = 4 if s == 'OK' else 5 if s == 'ERROR': self.all_row_parity_ok = False - self.put(bit_start, bit_stop, self.out_ann, + self.put(ss, es, self.out_ann, [c, ['Row parity: ' + s, 'RP: ' + s, 'RP', 'R']]) self.tag = (self.tag << 4) | self.data self.data_bits = 0 @@ -149,16 +152,15 @@ class Decoder(srd.Decoder): if self.state == 'TRAILER': self.payload_cnt += 1 if self.data_bits == 0: - self.data_start = bit_start + self.ss_data = ss self.data = 0 self.data_parity = 0 self.data_bits += 1 self.col_parity[self.data_bits] = bit - self.col_parity_pos.append([bit_start, bit_stop]) + self.col_parity_pos.append([ss, es]) if self.data_bits == 5: - self.put(bit_start, bit_stop, self.out_ann, - [8, ['Stop bit', 'SB', 'S']]) + self.put(ss, es, self.out_ann, [8, ['Stop bit', 'SB', 'S']]) for i in range(1, 5): s = 'OK' if self.data_col_parity[i] == \ @@ -172,7 +174,7 @@ class Decoder(srd.Decoder): # Emit an annotation for valid-looking tags. all_col_parity_ok = (self.data_col_parity[1:5] == self.col_parity[1:5]) if all_col_parity_ok and self.all_row_parity_ok: - self.put(self.first_start, bit_stop, self.out_ann, + self.put(self.ss_first, es, self.out_ann, [9, ['Tag: %010X' % self.tag, 'Tag', 'T']]) self.tag = 0 @@ -186,70 +188,50 @@ class Decoder(srd.Decoder): self.col_parity_pos = [] self.all_row_parity_ok = True - def putbit(self, bit, bit_start, bit_stop): - self.put(bit_start, bit_stop, self.out_ann, [0, [str(bit)]]) - self.add_bit(bit, bit_start, bit_stop) - - def manchester_decode(self, samplenum, pl, pp, pin): - bit_start = 0 - bit_stop = 0 + def manchester_decode(self, pl, pp, pin): bit = self.oldpin ^ self.polarity if pl > self.halfbit_limit: - samples = samplenum - self.oldsamplenum - t = samples / self.samplerate - + es = int(self.samplenum - pl/2) if self.oldpl > self.halfbit_limit: - bit_start = int(self.oldsamplenum - self.oldpl/2) - bit_stop = int(samplenum - pl/2) - self.putbit(bit, bit_start, bit_stop) - if self.oldpl <= self.halfbit_limit: - bit_start = int(self.oldsamplenum - self.oldpl) - bit_stop = int(samplenum - pl/2) - self.putbit(bit, bit_start, bit_stop) - self.last_bit_pos = int(samplenum - pl/2) - - if pl < self.halfbit_limit: - samples = samplenum - self.oldsamplenum - t = samples / self.samplerate - + ss = int(self.oldsamplenum - self.oldpl/2) + else: + ss = int(self.oldsamplenum - self.oldpl) + self.putbit(bit, ss, es) + self.last_bit_pos = int(self.samplenum - pl/2) + else: + es = int(self.samplenum) if self.oldpl > self.halfbit_limit: - bit_start = int(self.oldsamplenum - self.oldpl/2) - bit_stop = int(samplenum) - self.putbit(bit, bit_start, bit_stop) - self.last_bit_pos = int(samplenum) - if self.oldpl <= self.halfbit_limit: + ss = int(self.oldsamplenum - self.oldpl/2) + self.putbit(bit, ss, es) + self.last_bit_pos = int(self.samplenum) + else: if self.last_bit_pos <= self.oldsamplenum - self.oldpl: - bit_start = int(self.oldsamplenum - self.oldpl) - bit_stop = int(samplenum) - self.putbit(bit, bit_start, bit_stop) - self.last_bit_pos = int(samplenum) + ss = int(self.oldsamplenum - self.oldpl) + self.putbit(bit, ss, es) + self.last_bit_pos = int(self.samplenum) - def decode(self, ss, es, data): + def decode(self): if not self.samplerate: raise SamplerateError('Cannot decode without samplerate.') - for (samplenum, (pin,)) in data: - # Ignore identical samples early on (for performance reasons). - if self.oldpin == pin: - continue - - if self.oldpin is None: - self.oldpin = pin - self.last_samplenum = samplenum - self.lastlast_samplenum = samplenum - self.last_edge = samplenum - self.oldpl = 0 - self.oldpp = 0 - self.oldsamplenum = 0 - self.last_bit_pos = 0 - continue - - if self.oldpin != pin: - pl = samplenum - self.oldsamplenum - pp = pin - - self.manchester_decode(samplenum, pl, pp, pin) - - self.oldpl = pl - self.oldpp = pp - self.oldsamplenum = samplenum - self.oldpin = pin + + # Initialize internal state from the very first sample. + (pin,) = self.wait() + self.oldpin = pin + self.last_samplenum = self.samplenum + self.lastlast_samplenum = self.samplenum + self.last_edge = self.samplenum + self.oldpl = 0 + self.oldpp = 0 + self.oldsamplenum = 0 + self.last_bit_pos = 0 + + while True: + # Ignore identical samples, only process edges. + (pin,) = self.wait({0: 'e'}) + pl = self.samplenum - self.oldsamplenum + pp = pin + self.manchester_decode(pl, pp, pin) + self.oldpl = pl + self.oldpp = pp + self.oldsamplenum = self.samplenum + self.oldpin = pin