]> sigrok.org Git - libsigrokdecode.git/blame - decoders/xy2-100/pd.py
xy2-100: Initial implementation
[libsigrokdecode.git] / decoders / xy2-100 / pd.py
CommitLineData
6e02446f
UH
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2019 Uli Huber
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, see <http://www.gnu.org/licenses/>.
18##
19
20import re
21import sigrokdecode as srd
22
23ann_hdrbit, ann_databit, ann_paritybit, ann_bitlegende, ann_pos, ann_warning = range(6)
24
25class Decoder(srd.Decoder):
26 api_version = 3
27 id = 'xy2-100'
28 name = 'XY2-100'
29 longname = 'XY2-100 Galvo Protocol'
30 desc = 'Serial protocol for Galvo positioning'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = []
34 tags = ['Embedded/industrial']
35 channels = (
36 {'id': 'clk', 'name': 'D0', 'desc': 'Clock','default': 0},
37 {'id': 'sync', 'name': 'D1', 'desc': 'Sync','default': 1},
38 {'id': 'PosX', 'name': 'D2', 'desc': 'X/Y/Z','default': 2},
39 )
40
41 annotations = (
42 ('hdrbit', 'Header bit'),
43 ('databit', 'Data bit'),
44 ('paritybit', 'Parity bit'),
45 ('bitlegende', 'Bit Legende'),
46 ('position', 'Position Data'),
47 ('warning', 'Human-readable warnings'),
48 )
49 annotation_rows = (
50 ('bits', 'Bits', (ann_hdrbit, ann_databit, ann_paritybit)),
51 ('legende', 'Legende', (ann_bitlegende,)),
52 ('positions', 'Position', (ann_pos,)),
53 ('warnings', 'Warnings', (ann_warning,)),
54 )
55
56 def __init__(self):
57 self.samplerate = None
58 self.reset()
59
60 def reset(self):
61 self.hdrbits = []
62 self.databits = []
63 self.paritybits = []
64
65 def metadata(self, key, value):
66 if key == srd.SRD_CONF_SAMPLERATE:
67 self.samplerate = value
68
69 def start(self):
70 self.out_ann = self.register(srd.OUTPUT_ANN)
71
72 def putbit(self, ss, es, typ, value):
73 self.put(ss, es, self.out_ann, [typ, ['%s' % (value)]])
74
75 def decode(self):
76 headerstart = 0
77 datastart = 0
78 dataend = 0
79 lastsample = 0
80 while True:
81 # Wait for any edge CLK or SYNC
82 clk, sync, PosX = self.wait({0: 'r'})
83 bitstart = self.samplenum
84 bitend = bitstart+(bitstart-lastsample)
85
86 # start data collection
87 if sync == 1:
88 # wait for falling edge clk
89 clk, sync, PosX = self.wait({0: 'f'})
90 if len(self.hdrbits) < 3:
91 if len(self.hdrbits) == 0:
92 headerstart = bitstart
93 self.hdrbits = [(PosX, bitstart, self.samplenum)] + self.hdrbits
94 self.putbit(bitstart, bitend, ann_hdrbit, PosX)
95 else:
96 if len(self.databits) == 0:
97 datastart = bitstart
98 self.databits = [(PosX, bitstart, self.samplenum)] + self.databits
99 #self.putbit(bitstart, self.samplenum+1, ann_databit, PosX)
100 self.putbit(bitstart,bitend, ann_databit, PosX)
101 dataend = bitend
102
103 # get parity bit, calculate position
104 elif sync == 0:
105 clk, sync, PosX = self.wait({0: 'f'})
106 self.paritybits = [PosX]
107 self.putbit(dataend, bitend, ann_paritybit, PosX)
108 self.put(dataend,bitend, self.out_ann, [ann_bitlegende, ['Parity' ]])
109 self.put(headerstart,datastart, self.out_ann, [ann_bitlegende, ['Header' ]])
110 self.put(datastart, dataend, self.out_ann, [ann_bitlegende, ['Position' ]])
111
112 par=0
113 for x in self.hdrbits:
114 par ^= x[0]&1
115 positionX = 0
116 stelle = 0
117 for x in self.databits:
118 par ^= x[0]&1
119 if x[0] == 1:
120 positionX = positionX + (1 << stelle)
121 stelle += 1
122
123 self.put(datastart, dataend, self.out_ann, [ann_pos, ['%02d' % (positionX)]])
124 check = 'NOK'
125 if PosX == par:
126 check = 'OK'
127 self.put(dataend, bitend, self.out_ann, [ann_pos, ['%02s' % (check)]])
128
129 #self.put(datastart, self.samplenum, self.out_ann,
130 #[ann_warning, ['%s: %02X' % ('WARNUNG: ', 4711)]])
131
132 self.databits = []
133 self.hdrbits = []
134
135 lastsample = bitstart