stepper_motor: make samplerate meta data optional
authorGerhard Sittig <gerhard.sittig@gmx.net>
Sun, 17 Dec 2017 22:59:16 +0000 (23:59 +0100)
committerUwe Hermann <uwe@hermann-uwe.de>
Tue, 13 Feb 2018 17:50:54 +0000 (18:50 +0100)
Only emit the speed annotations when a sample rate was specified. Cope
with the absence of a sample rate for the input stream. Decoding is
still possible, it's just that no timing information is available.

decoders/stepper_motor/pd.py

index 9ddc6453200366a2c55b000dd866097c0b4a5727..05463958a28797b196405843cda420f4fd7d543d 100644 (file)
@@ -19,9 +19,6 @@
 
 import sigrokdecode as srd
 
-class SamplerateError(Exception):
-    pass
-
 class Decoder(srd.Decoder):
     api_version = 3
     id = 'stepper_motor'
@@ -53,6 +50,7 @@ class Decoder(srd.Decoder):
         self.reset()
 
     def reset(self):
+        self.samplerate = None
         self.oldstep = None
         self.ss_prev_step = None
         self.pos = 0
@@ -73,12 +71,13 @@ class Decoder(srd.Decoder):
 
     def step(self, ss, direction):
         if self.ss_prev_step is not None:
-            delta = ss - self.ss_prev_step
-            speed = self.samplerate / delta / self.scale
-            speed_txt = self.format % speed
+            if self.samplerate:
+                delta = ss - self.ss_prev_step
+                speed = self.samplerate / delta / self.scale
+                speed_txt = self.format % speed
+                self.put(self.ss_prev_step, ss, self.out_ann,
+                    [0, [speed_txt + ' ' + self.unit + '/s', speed_txt]])
             pos_txt = self.format % (self.pos / self.scale)
-            self.put(self.ss_prev_step, ss, self.out_ann,
-                [0, [speed_txt + ' ' + self.unit + '/s', speed_txt]])
             self.put(self.ss_prev_step, ss, self.out_ann,
                 [1, [pos_txt + ' ' + self.unit, pos_txt]])
 
@@ -90,8 +89,6 @@ class Decoder(srd.Decoder):
             self.samplerate = value
 
     def decode(self):
-        if not self.samplerate:
-            raise SamplerateError('Cannot decode without samplerate.')
         while True:
             step, direction = self.wait({0: 'r'})
             self.step(self.samplenum, direction)