]> sigrok.org Git - libsigrokdecode.git/blob - decoders/stepper_motor/pd.py
stepper_motor: Convert to PD API version 3.
[libsigrokdecode.git] / decoders / stepper_motor / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2015 Petteri Aimonen <jpa@sigrok.mail.kapsi.fi>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import sigrokdecode as srd
22
23 class SamplerateError(Exception):
24     pass
25
26 class Decoder(srd.Decoder):
27     api_version = 3
28     id = 'stepper_motor'
29     name = 'Stepper motor'
30     longname = 'Stepper motor position / speed'
31     desc = 'Absolute position and movement speed from step/dir.'
32     license = 'gplv2+'
33     inputs = ['logic']
34     outputs = ['stepper_motor']
35     channels = (
36         {'id': 'step', 'name': 'Step', 'desc': 'Step pulse'},
37         {'id': 'dir', 'name': 'Direction', 'desc': 'Direction select'},
38     )
39     options = (
40         {'id': 'unit', 'desc': 'Unit', 'default': 'steps',
41          'values': ('steps', 'mm')},
42         {'id': 'steps_per_mm', 'desc': 'Steps per mm', 'default': 100.0},
43     )
44     annotations = (
45         ('speed', 'Speed'),
46         ('position', 'Position')
47     )
48     annotation_rows = (
49         ('speed', 'Speed', (0,)),
50         ('position', 'Position', (1,)),
51     )
52
53     def __init__(self):
54         self.oldstep = None
55         self.ss_prev_step = None
56         self.pos = 0
57         self.prev_speed = None
58         self.prev_pos = None
59
60     def start(self):
61         self.out_ann = self.register(srd.OUTPUT_ANN)
62
63         if self.options['unit'] == 'steps':
64             self.scale = 1
65             self.format = '%0.0f'
66             self.unit = 'steps'
67         else:
68             self.scale = self.options['steps_per_mm']
69             self.format = '%0.2f'
70             self.unit = 'mm'
71
72     def step(self, ss, direction):
73         if self.ss_prev_step is not None:
74             delta = ss - self.ss_prev_step
75             speed = self.samplerate / delta / self.scale
76             speed_txt = self.format % speed
77             pos_txt = self.format % (self.pos / self.scale)
78             self.put(self.ss_prev_step, ss, self.out_ann,
79                 [0, [speed_txt + ' ' + self.unit + '/s', speed_txt]])
80             self.put(self.ss_prev_step, ss, self.out_ann,
81                 [1, [pos_txt + ' ' + self.unit, pos_txt]])
82
83         self.pos += (1 if direction else -1)
84         self.ss_prev_step = ss
85
86     def metadata(self, key, value):
87         if key == srd.SRD_CONF_SAMPLERATE:
88             self.samplerate = value
89
90     def decode(self):
91         if not self.samplerate:
92             raise SamplerateError('Cannot decode without samplerate.')
93         while True:
94             step, direction = self.wait({0: 'r'})
95             self.step(self.samplenum, direction)