]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/rtc8564/rtc8564.py
srd: PDs: Use strings for states, too.
[libsigrokdecode.git] / decoders / rtc8564 / rtc8564.py
index 0e7a532f5d8de55289704feb8c56f0fb63e5bb91..2e2f10fc0a44a97f736677c02de5fc8b6954fb4c 100644 (file)
 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 ##
 
-#
-# Epson RTC-8564 JE/NB decoder
-#
+# Epson RTC-8564 JE/NB protocol decoder
 
 import sigrokdecode as srd
 
-# States
-IDLE = 0
-GET_SLAVE_ADDR = 1
-GET_REG_ADDR = 2
-READ_RTC_REGS = 3
-READ_RTC_REGS2 = 4
-WRITE_RTC_REGS = 5
-
 # Return the specified BCD number (max. 8 bits) as integer.
 def bcd2int(b):
     return (b & 0x0f) + ((b >> 4) * 10)
@@ -47,18 +37,18 @@ class Decoder(srd.Decoder):
     inputs = ['i2c']
     outputs = ['rtc8564']
     probes = []
-    extra_probes = [
+    optional_probes = [
         {'id': 'clkout', 'name': 'CLKOUT', 'desc': 'TODO.'},
         {'id': 'clkoe', 'name': 'CLKOE', 'desc': 'TODO.'},
         {'id': 'int', 'name': 'INT#', 'desc': 'TODO.'},
     ]
     options = {}
     annotations = [
-        ['TODO', 'TODO'], 
+        ['TODO', 'TODO'],
     ]
 
     def __init__(self, **kwargs):
-        self.state = IDLE
+        self.state = 'IDLE'
         self.hours = -1
         self.minutes = -1
         self.seconds = -1
@@ -156,34 +146,34 @@ class Decoder(srd.Decoder):
         pass
 
     def decode(self, ss, es, data):
-        cmd, databyte, ack = data
+        cmd, databyte = data
 
         # Store the start/end samples of this I2C packet.
         self.ss, self.es = ss, es
 
         # State machine.
-        if self.state == IDLE:
+        if self.state == 'IDLE':
             # Wait for an I2C START condition.
             if cmd != 'START':
                 return
-            self.state = GET_SLAVE_ADDR
+            self.state = 'GET SLAVE ADDR'
             self.block_start_sample = ss
-        elif self.state == GET_SLAVE_ADDR:
+        elif self.state == 'GET SLAVE ADDR':
             # Wait for an address write operation.
             # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
             if cmd != 'ADDRESS WRITE':
                 return
-            self.state = GET_REG_ADDR
-        elif self.state == GET_REG_ADDR:
+            self.state = 'GET REG ADDR'
+        elif self.state == 'GET REG ADDR':
             # Wait for a data write (master selects the slave register).
             if cmd != 'DATA WRITE':
                 return
             self.reg = databyte
-            self.state = WRITE_RTC_REGS
-        elif self.state == WRITE_RTC_REGS:
+            self.state = 'WRITE RTC REGS'
+        elif self.state == 'WRITE RTC REGS':
             # If we see a Repeated Start here, it's probably an RTC read.
             if cmd == 'START REPEAT':
-                self.state = READ_RTC_REGS
+                self.state = 'READ RTC REGS'
                 return
             # Otherwise: Get data bytes until a STOP condition occurs.
             if cmd == 'DATA WRITE':
@@ -197,18 +187,18 @@ class Decoder(srd.Decoder):
                     self.years, self.hours, self.minutes, self.seconds)
                 self.put(self.block_start_sample, es, self.out_ann,
                          [0, ['Written date/time: %s' % d]])
-                self.state = IDLE
+                self.state = 'IDLE'
             else:
                 pass # TODO
-        elif self.state == READ_RTC_REGS:
+        elif self.state == 'READ RTC REGS':
             # Wait for an address read operation.
             # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
             if cmd == 'ADDRESS READ':
-                self.state = READ_RTC_REGS2
+                self.state = 'READ RTC REGS2'
                 return
             else:
-               pass # TODO
-        elif self.state == READ_RTC_REGS2:
+                pass # TODO
+        elif self.state == 'READ RTC REGS2':
             if cmd == 'DATA READ':
                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
                 handle_reg(databyte)
@@ -219,10 +209,9 @@ class Decoder(srd.Decoder):
                     self.years, self.hours, self.minutes, self.seconds)
                 self.put(self.block_start_sample, es, self.out_ann,
                          [0, ['Read date/time: %s' % d]])
-                self.state = IDLE
+                self.state = 'IDLE'
             else:
                 pass # TODO?
         else:
-            # Shouldn't happen.
-            raise Exception('Unknown state: %d', self.state)
+            raise Exception('Invalid state: %d' % self.state)