]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/ps2/pd.py
sdcard_sd: Use SrdStrEnum for the state machine.
[libsigrokdecode.git] / decoders / ps2 / pd.py
index 58c4d845bff3112254bef3b20175106ffb44fae8..a9d0a986daf0356b265c61f7e1fd6a24a09cfdd2 100644 (file)
@@ -26,14 +26,15 @@ class Ann:
 Bit = namedtuple('Bit', 'val ss es')
 
 class Decoder(srd.Decoder):
-    api_version = 2
+    api_version = 3
     id = 'ps2'
     name = 'PS/2'
     longname = 'PS/2'
     desc = 'PS/2 keyboard/mouse interface.'
     license = 'gplv2+'
     inputs = ['logic']
-    outputs = ['ps2']
+    outputs = []
+    tags = ['PC']
     channels = (
         {'id': 'clk', 'name': 'Clock', 'desc': 'Clock line'},
         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
@@ -53,11 +54,10 @@ class Decoder(srd.Decoder):
     )
 
     def __init__(self):
+        self.reset()
+
+    def reset(self):
         self.bits = []
-        self.prev_pins = None
-        self.prev_clock = None
-        self.samplenum = 0
-        self.clock_was_high = False
         self.bitcount = 0
 
     def start(self):
@@ -114,30 +114,13 @@ class Decoder(srd.Decoder):
 
         self.bits, self.bitcount = [], 0
 
-    def find_clk_edge(self, clock_pin, data_pin):
-        # Ignore sample if the clock pin hasn't changed.
-        if clock_pin == self.prev_clock:
-            return
-        self.prev_clock = clock_pin
-
-        # Sample on falling clock edge.
-        if clock_pin == 1:
-            return
-
-        # Found the correct clock edge, now get the bits.
-        self.handle_bits(data_pin)
-
-    def decode(self, ss, es, data):
-        for (self.samplenum, pins) in data:
-            clock_pin, data_pin = pins[0], pins[1]
-
-            # Ignore identical samples.
-            if self.prev_pins == pins:
-                continue
-            self.prev_pins = pins
-
-            if clock_pin == 0 and not self.clock_was_high:
-                continue
-            self.clock_was_high = True
-
-            self.find_clk_edge(clock_pin, data_pin)
+    def decode(self):
+        while True:
+            # Sample data bits on the falling clock edge (assume the device
+            # is the transmitter). Expect the data byte transmission to end
+            # at the rising clock edge. Cope with the absence of host activity.
+            _, data_pin = self.wait({0: 'f'})
+            self.handle_bits(data_pin)
+            if self.bitcount == 1 + 8 + 1 + 1:
+                _, data_pin = self.wait({0: 'r'})
+                self.handle_bits(data_pin)