]> sigrok.org Git - libsigrokdecode.git/commitdiff
em4305: Convert to PD API version 3
authorGerhard Sittig <redacted>
Sun, 15 Jan 2017 09:48:44 +0000 (10:48 +0100)
committerUwe Hermann <redacted>
Wed, 18 Jan 2017 11:34:34 +0000 (12:34 +0100)
decoders/em4305/pd.py

index cda9b3043c5910984dbc1a78026d260e04d7f6ce..895ee15feb2e216b4d2af7f86f86e11e1188b00c 100644 (file)
@@ -23,7 +23,7 @@ class SamplerateError(Exception):
     pass
 
 class Decoder(srd.Decoder):
-    api_version = 2
+    api_version = 3
     id = 'em4305'
     name = 'EM4305'
     longname = 'RFID EM4205/EM4305'
@@ -66,7 +66,6 @@ class Decoder(srd.Decoder):
 
     def __init__(self):
         self.samplerate = None
-        self.oldpin = None
         self.last_samplenum = None
         self.state = 'FFS_SEARCH'
         self.bits_pos = [[0 for col in range(3)] for row in range(70)]
@@ -324,72 +323,68 @@ class Decoder(srd.Decoder):
             self.bits_pos[self.bit_nr][2] = es_bit
             self.bit_nr += 1
 
-    def decode(self, ss, es, data):
+    def decode(self):
         if not self.samplerate:
             raise SamplerateError('Cannot decode without samplerate.')
-        for (self.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 = self.samplenum
-                self.oldsamplenum = 0
-                self.old_gap_end = 0
-                self.gap_detected = 0
-                self.bit_nr = 0
-                continue
-
-            if self.oldpin != pin:
-                pl = self.samplenum - self.oldsamplenum
-                pp = pin
-                samples = self.samplenum - self.last_samplenum
-
-                if self.state == 'FFS_DETECTED':
-                    if pl > self.writegap:
-                        self.gap_detected = 1
-                    if (self.last_samplenum - self.old_gap_end) > self.nogap:
-                        self.gap_detected = 0
-                        self.state = 'FFS_SEARCH'
-                        self.put(self.old_gap_end, self.last_samplenum,
-                                 self.out_ann, [3, ['Write mode exit']])
-                        self.put_fields()
-
-                if self.state == 'FFS_SEARCH':
-                    if pl > self.ffs:
-                        self.gap_detected = 1
-                        self.put(self.last_samplenum, self.samplenum,
-                                 self.out_ann, [1, ['First field stop', 'Field stop', 'FFS']])
-                        self.state = 'FFS_DETECTED'
-
-                if self.gap_detected == 1:
+
+        # Initialize internal state.
+        self.last_samplenum = self.samplenum
+        self.oldsamplenum = 0
+        self.old_gap_end = 0
+        self.gap_detected = 0
+        self.bit_nr = 0
+
+        while True:
+            # Ignore identical samples, only process edges.
+            (pin,) = self.wait({0: 'e'})
+
+            pl = self.samplenum - self.oldsamplenum
+            pp = pin
+            samples = self.samplenum - self.last_samplenum
+
+            if self.state == 'FFS_DETECTED':
+                if pl > self.writegap:
+                    self.gap_detected = 1
+                if (self.last_samplenum - self.old_gap_end) > self.nogap:
                     self.gap_detected = 0
-                    if (self.last_samplenum - self.old_gap_end) > self.wzmin \
-                            and (self.last_samplenum - self.old_gap_end) < self.wzmax:
-                        self.put(self.old_gap_end, self.samplenum,
-                                 self.out_ann, [0, ['0']])
-                        self.add_bits_pos(0, self.old_gap_end, self.samplenum)
-                    if (self.last_samplenum - self.old_gap_end) > self.womax \
-                            and (self.last_samplenum-self.old_gap_end) < self.nogap:
-                        # One or more 1 bits
-                        one_bits = (int)((self.last_samplenum - self.old_gap_end) / self.womax)
-                        for ox in range(0, one_bits):
-                            bs = (int)(self.old_gap_end+ox*self.womax)
-                            be = (int)(self.old_gap_end+ox*self.womax + self.womax)
-                            self.put(bs, be, self.out_ann, [0, ['1']])
-                            self.add_bits_pos(1, bs, be)
-                        if (self.samplenum - self.last_samplenum) > self.wzmin \
-                                and (self.samplenum - self.last_samplenum) < self.wzmax:
-                            bs = (int)(self.old_gap_end+one_bits*self.womax)
-                            self.put(bs, self.samplenum, self.out_ann, [0, ['0']])
-                            self.add_bits_pos(0, bs, self.samplenum)
-
-                    self.old_gap_end = self.samplenum
-
-                if self.state == 'SKIP':
                     self.state = 'FFS_SEARCH'
-
-                self.oldsamplenum = self.samplenum
-                self.last_samplenum = self.samplenum
-                self.oldpin = pin
+                    self.put(self.old_gap_end, self.last_samplenum,
+                             self.out_ann, [3, ['Write mode exit']])
+                    self.put_fields()
+
+            if self.state == 'FFS_SEARCH':
+                if pl > self.ffs:
+                    self.gap_detected = 1
+                    self.put(self.last_samplenum, self.samplenum,
+                             self.out_ann, [1, ['First field stop', 'Field stop', 'FFS']])
+                    self.state = 'FFS_DETECTED'
+
+            if self.gap_detected == 1:
+                self.gap_detected = 0
+                if (self.last_samplenum - self.old_gap_end) > self.wzmin \
+                        and (self.last_samplenum - self.old_gap_end) < self.wzmax:
+                    self.put(self.old_gap_end, self.samplenum,
+                             self.out_ann, [0, ['0']])
+                    self.add_bits_pos(0, self.old_gap_end, self.samplenum)
+                if (self.last_samplenum - self.old_gap_end) > self.womax \
+                        and (self.last_samplenum-self.old_gap_end) < self.nogap:
+                    # One or more 1 bits
+                    one_bits = (int)((self.last_samplenum - self.old_gap_end) / self.womax)
+                    for ox in range(0, one_bits):
+                        bs = (int)(self.old_gap_end+ox*self.womax)
+                        be = (int)(self.old_gap_end+ox*self.womax + self.womax)
+                        self.put(bs, be, self.out_ann, [0, ['1']])
+                        self.add_bits_pos(1, bs, be)
+                    if (self.samplenum - self.last_samplenum) > self.wzmin \
+                            and (self.samplenum - self.last_samplenum) < self.wzmax:
+                        bs = (int)(self.old_gap_end+one_bits*self.womax)
+                        self.put(bs, self.samplenum, self.out_ann, [0, ['0']])
+                        self.add_bits_pos(0, bs, self.samplenum)
+
+                self.old_gap_end = self.samplenum
+
+            if self.state == 'SKIP':
+                self.state = 'FFS_SEARCH'
+
+            self.oldsamplenum = self.samplenum
+            self.last_samplenum = self.samplenum