]>
Commit | Line | Data |
---|---|---|
d2bcb32b PA |
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 | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
d2bcb32b PA |
18 | ## |
19 | ||
20 | import sigrokdecode as srd | |
21 | ||
d2bcb32b | 22 | class Decoder(srd.Decoder): |
e500b376 | 23 | api_version = 3 |
d2bcb32b PA |
24 | id = 'stepper_motor' |
25 | name = 'Stepper motor' | |
26 | longname = 'Stepper motor position / speed' | |
a7c8dc5e | 27 | desc = 'Absolute position and movement speed from step/dir.' |
d2bcb32b PA |
28 | license = 'gplv2+' |
29 | inputs = ['logic'] | |
30 | outputs = ['stepper_motor'] | |
31 | channels = ( | |
32 | {'id': 'step', 'name': 'Step', 'desc': 'Step pulse'}, | |
33 | {'id': 'dir', 'name': 'Direction', 'desc': 'Direction select'}, | |
34 | ) | |
35 | options = ( | |
36 | {'id': 'unit', 'desc': 'Unit', 'default': 'steps', | |
37 | 'values': ('steps', 'mm')}, | |
38 | {'id': 'steps_per_mm', 'desc': 'Steps per mm', 'default': 100.0}, | |
39 | ) | |
40 | annotations = ( | |
41 | ('speed', 'Speed'), | |
42 | ('position', 'Position') | |
43 | ) | |
44 | annotation_rows = ( | |
45 | ('speed', 'Speed', (0,)), | |
46 | ('position', 'Position', (1,)), | |
47 | ) | |
48 | ||
92b7b49f | 49 | def __init__(self): |
10aeb8ea GS |
50 | self.reset() |
51 | ||
52 | def reset(self): | |
4d568406 | 53 | self.samplerate = None |
d2bcb32b | 54 | self.oldstep = None |
5b0b88ce | 55 | self.ss_prev_step = None |
d2bcb32b PA |
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): | |
5b0b88ce | 73 | if self.ss_prev_step is not None: |
4d568406 GS |
74 | if self.samplerate: |
75 | delta = ss - self.ss_prev_step | |
76 | speed = self.samplerate / delta / self.scale | |
77 | speed_txt = self.format % speed | |
78 | self.put(self.ss_prev_step, ss, self.out_ann, | |
79 | [0, [speed_txt + ' ' + self.unit + '/s', speed_txt]]) | |
d2bcb32b | 80 | pos_txt = self.format % (self.pos / self.scale) |
5b0b88ce | 81 | self.put(self.ss_prev_step, ss, self.out_ann, |
d2bcb32b PA |
82 | [1, [pos_txt + ' ' + self.unit, pos_txt]]) |
83 | ||
84 | self.pos += (1 if direction else -1) | |
5b0b88ce | 85 | self.ss_prev_step = ss |
d2bcb32b PA |
86 | |
87 | def metadata(self, key, value): | |
88 | if key == srd.SRD_CONF_SAMPLERATE: | |
89 | self.samplerate = value | |
90 | ||
e500b376 | 91 | def decode(self): |
e500b376 UH |
92 | while True: |
93 | step, direction = self.wait({0: 'r'}) | |
94 | self.step(self.samplenum, direction) |