From 1385fd79322388d3f1613914c19c38398c3567de Mon Sep 17 00:00:00 2001 From: chenyue Date: Thu, 23 Apr 2026 21:25:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=88=9D=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xy2-100-cy/__init__.py | 28 ++ .../__pycache__/__init__.cpython-36.pyc | Bin 0 -> 310 bytes xy2-100-cy/__pycache__/pd.cpython-36.pyc | Bin 0 -> 5453 bytes xy2-100-cy/pd.py | 242 ++++++++++++++++++ 4 files changed, 270 insertions(+) create mode 100644 xy2-100-cy/__init__.py create mode 100644 xy2-100-cy/__pycache__/__init__.cpython-36.pyc create mode 100644 xy2-100-cy/__pycache__/pd.cpython-36.pyc create mode 100644 xy2-100-cy/pd.py diff --git a/xy2-100-cy/__init__.py b/xy2-100-cy/__init__.py new file mode 100644 index 0000000..676e1af --- /dev/null +++ b/xy2-100-cy/__init__.py @@ -0,0 +1,28 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2019 Uli Huber +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see . +## + +''' +XY2-100 is a serial bus for connecting galvo systems to controllers + +Details: + +http://www.newson.be/doc.php?id=XY2-100 +''' + +from .pd import Decoder diff --git a/xy2-100-cy/__pycache__/__init__.cpython-36.pyc b/xy2-100-cy/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..506c049ad24c1ca0a48202cea483a8262bdd7e75 GIT binary patch literal 310 zcmYk1&q~BF5XRGVWhJmTPo8q^VcS(NQv3%Nk0LCHE{maUVjD=4lF8bw7oW!m@MU`S z?)}jNW<3c3dlyO_Pmy)0Vog-g?Hx&iLG!v!a!rn&VFlEedGB)6r+tK|y zvU|Sl?m%D*}%%I$V#ltDu2{jmDSjo#OiFE zO%S%KyA?Lcrr5M>scx0sbZhK&cFG@HRabS^U~l+!iPhPg{8Sef9zB zqwEs<5cM&3nO#Bcu&c1lr=oDpZ}*wccXX_NSl-%RIDKw@{`7;eH!R=1x7#?AHBMb` zI9|Y_M^zW%^x%x7o20ow>85-s$0Z%ycnH$M^P zVz1v`Yv@AjGGPSXx-Sgohizem+X3M6z2+jI#F-a)!nk&2<;rmK?pw}12YR`Vx77_D zNwUJoi#9^AuUuIfPVZyMi28$W^xVD%QO?zFBy8$#QD13Uj+VVfv_0^jmu_(AG14e#%X+WP}`W&n$|THT-n6BY5UQS>XF^1(1_ItHe1tpJ=R8Az;e3KL(atH@@Lmti?^CL zTFX~HUAlSwvnwmt`5BCZmZXZ%*Zgf^KJj`R{yw#Yjle^&MBYs>hN+QCdyJHm4>l4x z74B3b^%0I|sHW#mHH6LmP8U0w^LI#x(yiMoOSe~ASDH;x=x;4+9}xVwH^aNR9Q_=x9%ky@93=HX{B$7#n7487&NjhA=Kd zn^qg=PslHVyx&R%j3b4YLCbX-%bl0eMp_*LlZ_&^IsubPt+ZH=D2R+1rK6Ho=+z$S z&=q>F;tE5L%Ct4@nZcdNAnH?<>1zsqD95ACv_lFNNWz6Ql49kXiD`+sb%I9X+I;Qj z;Jl`MF>zl{tfY|Gu^HPw)IzzcBx6w_sWY1uJ6bY68V${o3E7h4(3*^E3^PAd zvD#{mMRrmjjlnG7BLC(Z<(4R=mI(eFk7XnO5*0DBlC{2O<&K(+2PasC>H}rq;!a~F z_!3gaq$b~uBzzZ@q$b}#_ZtUFMG}xo1$E@5xE>WDU*q#~%tSmHm60mQ6rp3t$f!rQ zHo;2&te5* z>_j{l&#h{h;p7NA9?#{oK>JZtG+u8GwUdm~ow-A2{&1?p|jbI9y*)6k7O%NE@T#$ z3 z*ck`t((MXhC_7~|hn0NL{MO04Lwj+ZITAA@oW%Ol>`eALXPuL5SR|4}cm9W8qt35F zk|@wEg4M{}Kv>>j;3Lt~dww9wM9Oj{nG1+9>6I2Gh^?R>_+n~L$GM9T(}DwE?7fip zPVC{$Oe<`fg2aguVX2u@ok)<>Am^=S9O=p$htia#N{g^)4qS_}rmK2|L`@4)cN!t%Ya?aS~J)-2W0sPCwLlt$|%bq@VhX0$qS zOHN2s>ySZp4U`$#OEXZ94enR9yj4@*(1w$vtYTN=QD#wr(zLr|X7M*5bXsyea4D^( z%nNPGe)I^r!V8&({>a#uY3Ov%M*JX zkXIbdbBDNqtihIdLm{@Ag}cFiJe234ik!oWELMmMI0J2h>3t}(#A5Tt=*Z*Ov6>x9 zC;2si(VJvAD@cLi5{1vT;!3l!G9OqC<-E{5F6yl5x4R9)SdMCW;W%G zB@b7j`qFs~QiCvjn`>_Av<4udI^ikN8&%h5WWSVVEgU;moyC<6Pp;D+EDA* z+@J!tfe&lB*HR(PZ!}KVGoJo8okIamxPDPp=Vq{{W}R;4i2B@(4Ya;tut{ zlHxM+y!y%P3`U&}}{Od58}$oW6WaLt;QIoQ@e$=yltE5c`|z$5Te0xbfY1nvTeF~m$Z zvc>zGAwN$fdOY)Y34B0+&Vb!0;!ml$>vM@MP@e%nj*y=c;om)fcv`b?Xyf?TYX-hX zyzr;vyaey3blb2^6dnI{ds0emjEhRx?eP9u8W!mr*W_N%@qf^Q`8`tfK7pSQa0!s! X2Q5h0@25V!MA0X|B-q+vwXXdi_pM*n literal 0 HcmV?d00001 diff --git a/xy2-100-cy/pd.py b/xy2-100-cy/pd.py new file mode 100644 index 0000000..45faac5 --- /dev/null +++ b/xy2-100-cy/pd.py @@ -0,0 +1,242 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2019 Uli Huber +## Copyright (C) 2020 Soeren Apel +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see . +## + +import sigrokdecode as srd + +ann_bit, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_pos, ann_status, ann_warning = range(9) +frame_type_none, frame_type_command, frame_type_16bit_pos, frame_type_18bit_pos = range(4) + +class Decoder(srd.Decoder): + api_version = 3 + id = 'xy2-100-bsl' + name = 'XY2-100-bsl' + longname = 'XY2-100(E) and XY-200(E) galvanometer protocol' + desc = 'Serial protocol for galvanometer positioning in laser systems' + license = 'gplv2+' + inputs = ['logic'] + outputs = [] + + tags = ['Embedded/industrial'] + + channels = ( + {'id': 'clk', 'name': 'CLK', 'desc': 'Clock'}, + {'id': 'sync', 'name': 'SYNC', 'desc': 'Sync'}, + {'id': 'data', 'name': 'DATA', 'desc': 'X, Y or Z axis data'}, + ) + optional_channels = ( + {'id': 'status', 'name': 'STAT', 'desc': 'X, Y or Z axis status'}, + ) + + annotations = ( + ('bit', 'Data Bit'), + ('stat_bit', 'Status Bit'), + ('type', 'Frame Type'), + ('command', 'Command'), + ('parameter', 'Parameter'), + ('parity', 'Parity'), + ('position', 'Position'), + ('status', 'Status'), + ('warning', 'Human-readable warnings'), + ) + annotation_rows = ( + ('bits', 'Data Bits', (ann_bit,)), + ('stat_bits', 'Status Bits', (ann_stat_bit,)), + ('data', 'Data', (ann_type, ann_command, ann_parameter, ann_parity)), + ('positions', 'Positions', (ann_pos,)), + ('statuses', 'Statuses', (ann_status,)), + ('warnings', 'Warnings', (ann_warning,)), + ) + + def __init__(self): + self.samplerate = None + self.reset() + + def reset(self): + self.bits = [] + self.stat_bits = [] + self.stat_skip_bit = True + + def metadata(self, key, value): + if key == srd.SRD_CONF_SAMPLERATE: + self.samplerate = value + + def start(self): + self.out_ann = self.register(srd.OUTPUT_ANN) + + def put_ann(self, ss, es, ann_class, value): + self.put(ss, es, self.out_ann, [ann_class, value]) + + def process_bit(self, sync, bit_ss, bit_es, bit_value): + self.put_ann(bit_ss, bit_es, ann_bit, ['%d' % bit_value]) + self.bits.append((bit_ss, bit_es, bit_value)) + + if sync == 0: + if len(self.bits) < 20: + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Not enough data bits']) + self.reset() + return + + # Bit structure: + # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + # T --------------- 18-bit pos ----------------- PARITY or + # -TYPE-- ------------ 16-bit pos -------------- PARITY or + # -TYPE-- -8-bit command -8-bit parameter value- PARITY + + # Calculate parity, excluding the parity bit itself + parity = 0 + for ss, es, value in self.bits[:-1]: + parity ^= value + + par_ss, par_es, par_value = self.bits[19] + parity_even = 0 + parity_odd = 0 + if (par_value == parity): + parity_even = 1 + else: + parity_odd = 1 + + type_1_value = self.bits[0][2] + type_3_value = (self.bits[0][2] << 2) | (self.bits[1][2] << 1) | self.bits[2][2] + + # Determine frame type + type = frame_type_none + parity_status = ['X', 'Unknown'] + type_ss = self.bits[0][0] + type_es = self.bits[2][1] + + ### 18-bit position + if (type_1_value == 1) and (parity_odd == 1): + type = frame_type_18bit_pos + type_es = self.bits[0][1] + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) + ### 16-bit position + elif (type_3_value == 1): + type = frame_type_16bit_pos + if (parity_even == 1): + parity_status = ['OK'] + else: + parity_status = ['NOK'] + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Parity error', 'PE']) + ### Command + elif (type_3_value == 7) and (parity_even == 1): + type = frame_type_command + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) + ### Other + else: + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Error', 'Unknown command or parity error']) + self.reset() + return + + # Output command and parity annotations + if (type == frame_type_16bit_pos): + self.put_ann(type_ss, type_es, ann_type, ['16 bit Position Frame', '16 bit Pos', 'Pos', 'P']) + if (type == frame_type_18bit_pos): + self.put_ann(type_ss, type_es, ann_type, ['18 bit Position Frame', '18 bit Pos', 'Pos', 'P']) + if (type == frame_type_command): + self.put_ann(type_ss, type_es, ann_type, ['Command Frame', 'Command', 'C']) + + self.put_ann(par_ss, par_es, ann_parity, parity_status) + + # Output value + if (type == frame_type_16bit_pos) or (type == frame_type_18bit_pos): + pos = 0 + + if (type == frame_type_16bit_pos): + count = 15 + for ss, es, value in self.bits[3:19]: + pos |= value << count + count -= 1 + # pos = pos if pos < 32768 else pos - 65536 + else: + count = 17 + for ss, es, value in self.bits[3:19]: + pos |= value << count + count -= 1 + pos = pos if pos < 131072 else pos - 262144 + + self.put_ann(type_es, par_ss, ann_pos, ['%d' % pos]) + + if (type == frame_type_command): + count = 7 + cmd = 0 + cmd_es = 0 + for ss, es, value in self.bits[3:11]: + cmd |= value << count + count -= 1 + cmd_es = es + self.put_ann(type_es, cmd_es, ann_command, ['Command 0x%X' % cmd, 'Cmd 0x%X' % cmd, '0x%X' % cmd]) + + count = 7 + param = 0 + for ss, es, value in self.bits[11:19]: + param |= value << count + count -= 1 + self.put_ann(cmd_es, par_ss, ann_parameter, ['Parameter 0x%X / %d' % (param, param), '0x%X / %d' % (param, param),'0x%X' % param]) + + self.reset() + + def process_stat_bit(self, sync, bit_ss, bit_es, bit_value): + if self.stat_skip_bit: + self.stat_skip_bit = False + return + + self.put_ann(bit_ss, bit_es, ann_stat_bit, ['%d' % bit_value]) + self.stat_bits.append((bit_ss, bit_es, bit_value)) + + if (sync == 0) and (len(self.stat_bits) == 19): + stat_ss = self.stat_bits[0][0] + stat_es = self.stat_bits[18][1] + + status = 0 + count = 18 + for ss, es, value in self.stat_bits: + status |= value << count + count -= 1 + self.put_ann(stat_ss, stat_es, ann_status, ['Status 0x%X' % status, '0x%X' % status]) + + def decode(self): + bit_ss = None + bit_es = None + bit_value = 0 + stat_ss = None + stat_es = None + stat_value = 0 + sync_value = 0 + has_stat = self.has_channel(3) + + while True: + # Wait for any edge on clk + clk, sync, data, stat = self.wait({0: 'e'}) + + if clk == 1: + stat_value = stat + + bit_es = self.samplenum + if bit_ss: + self.process_bit(sync_value, bit_ss, bit_es, bit_value) + bit_ss = self.samplenum + else: + bit_value = data + sync_value = sync + + stat_es = self.samplenum + if stat_ss and has_stat: + self.process_stat_bit(sync_value, stat_ss, stat_es, stat_value) + stat_ss = self.samplenum From 1f140e9b0bea9e92a6808db3042677f21793b3ab Mon Sep 17 00:00:00 2001 From: chenyue Date: Thu, 23 Apr 2026 22:08:20 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E8=B7=AF?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xy2-100-cy/pd.py | 132 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 125 insertions(+), 7 deletions(-) diff --git a/xy2-100-cy/pd.py b/xy2-100-cy/pd.py index 45faac5..ef4f658 100644 --- a/xy2-100-cy/pd.py +++ b/xy2-100-cy/pd.py @@ -20,13 +20,13 @@ import sigrokdecode as srd -ann_bit, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_pos, ann_status, ann_warning = range(9) +ann_bit,ann_bit2, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_type2, ann_command2, ann_parameter2, ann_parity2, ann_pos,ann_pos2, ann_status, ann_warning = range(15) frame_type_none, frame_type_command, frame_type_16bit_pos, frame_type_18bit_pos = range(4) class Decoder(srd.Decoder): api_version = 3 - id = 'xy2-100-bsl' - name = 'XY2-100-bsl' + id = 'xy2-100-cy' + name = 'XY2-100-cy' longname = 'XY2-100(E) and XY-200(E) galvanometer protocol' desc = 'Serial protocol for galvanometer positioning in laser systems' license = 'gplv2+' @@ -40,7 +40,8 @@ class Decoder(srd.Decoder): {'id': 'sync', 'name': 'SYNC', 'desc': 'Sync'}, {'id': 'data', 'name': 'DATA', 'desc': 'X, Y or Z axis data'}, ) - optional_channels = ( + optional_channels = ( + {'id': 'data2', 'name': 'DATA2', 'desc': 'X, Y or Z axis data'}, {'id': 'status', 'name': 'STAT', 'desc': 'X, Y or Z axis status'}, ) @@ -57,9 +58,12 @@ class Decoder(srd.Decoder): ) annotation_rows = ( ('bits', 'Data Bits', (ann_bit,)), + ('bits2', 'Data Bits2', (ann_bit2,)), ('stat_bits', 'Status Bits', (ann_stat_bit,)), ('data', 'Data', (ann_type, ann_command, ann_parameter, ann_parity)), + ('data2', 'Data2', (ann_type2, ann_command2, ann_parameter2, ann_parity2)), ('positions', 'Positions', (ann_pos,)), + ('positions2', 'Positions2', (ann_pos2,)), ('statuses', 'Statuses', (ann_status,)), ('warnings', 'Warnings', (ann_warning,)), ) @@ -192,6 +196,116 @@ class Decoder(srd.Decoder): self.reset() + + def process_bit2(self, sync, bit_ss, bit_es, bit2_value): + self.put_ann(bit_ss, bit_es, ann_bit2, ['%d' % bit2_value]) + self.bits.append((bit_ss, bit_es, bit2_value)) + + if sync == 0: + if len(self.bits) < 20: + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Not enough data bits']) + self.reset() + return + + # Bit structure: + # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + # T --------------- 18-bit pos ----------------- PARITY or + # -TYPE-- ------------ 16-bit pos -------------- PARITY or + # -TYPE-- -8-bit command -8-bit parameter value- PARITY + + # Calculate parity, excluding the parity bit itself + parity = 0 + for ss, es, value in self.bits[:-1]: + parity ^= value + + par_ss, par_es, par_value = self.bits[19] + parity_even = 0 + parity_odd = 0 + if (par_value == parity): + parity_even = 1 + else: + parity_odd = 1 + + type_1_value = self.bits[0][2] + type_3_value = (self.bits[0][2] << 2) | (self.bits[1][2] << 1) | self.bits[2][2] + + # Determine frame type + type = frame_type_none + parity_status = ['X', 'Unknown'] + type_ss = self.bits[0][0] + type_es = self.bits[2][1] + + ### 18-bit position + if (type_1_value == 1) and (parity_odd == 1): + type = frame_type_18bit_pos + type_es = self.bits[0][1] + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) + ### 16-bit position + elif (type_3_value == 1): + type = frame_type_16bit_pos + if (parity_even == 1): + parity_status = ['OK'] + else: + parity_status = ['NOK'] + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Parity error', 'PE']) + ### Command + elif (type_3_value == 7) and (parity_even == 1): + type = frame_type_command + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) + ### Other + else: + self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Error', 'Unknown command or parity error']) + self.reset() + return + + # Output command and parity annotations + if (type == frame_type_16bit_pos): + self.put_ann(type_ss, type_es, ann_type2, ['16 bit Position Frame', '16 bit Pos', 'Pos', 'P']) + if (type == frame_type_18bit_pos): + self.put_ann(type_ss, type_es, ann_type2, ['18 bit Position Frame', '18 bit Pos', 'Pos', 'P']) + if (type == frame_type_command): + self.put_ann(type_ss, type_es, ann_type2, ['Command Frame', 'Command', 'C']) + + self.put_ann(par_ss, par_es, ann_parity2, parity_status) + + # Output value + if (type == frame_type_16bit_pos) or (type == frame_type_18bit_pos): + pos = 0 + + if (type == frame_type_16bit_pos): + count = 15 + for ss, es, value in self.bits[3:19]: + pos |= value << count + count -= 1 + # pos = pos if pos < 32768 else pos - 65536 + else: + count = 17 + for ss, es, value in self.bits[3:19]: + pos |= value << count + count -= 1 + pos = pos if pos < 131072 else pos - 262144 + + self.put_ann(type_es, par_ss, ann_pos2, ['%d' % pos]) + + if (type == frame_type_command): + count = 7 + cmd = 0 + cmd_es = 0 + for ss, es, value in self.bits[3:11]: + cmd |= value << count + count -= 1 + cmd_es = es + self.put_ann(type_es, cmd_es, ann_command2, ['Command 0x%X' % cmd, 'Cmd 0x%X' % cmd, '0x%X' % cmd]) + + count = 7 + param = 0 + for ss, es, value in self.bits[11:19]: + param |= value << count + count -= 1 + self.put_ann(cmd_es, par_ss, ann_parameter2, ['Parameter 0x%X / %d' % (param, param), '0x%X / %d' % (param, param),'0x%X' % param]) + + self.reset() + def process_stat_bit(self, sync, bit_ss, bit_es, bit_value): if self.stat_skip_bit: self.stat_skip_bit = False @@ -215,15 +329,16 @@ class Decoder(srd.Decoder): bit_ss = None bit_es = None bit_value = 0 + bit2_value = 0 stat_ss = None stat_es = None stat_value = 0 sync_value = 0 - has_stat = self.has_channel(3) - + has_data2 = self.has_channel(3) + has_stat = self.has_channel(4) while True: # Wait for any edge on clk - clk, sync, data, stat = self.wait({0: 'e'}) + clk, sync, data, data2, stat = self.wait({0: 'e'}) if clk == 1: stat_value = stat @@ -231,9 +346,12 @@ class Decoder(srd.Decoder): bit_es = self.samplenum if bit_ss: self.process_bit(sync_value, bit_ss, bit_es, bit_value) + if has_data2: + self.process_bit2(sync_value, bit_ss, bit_es, bit2_value) bit_ss = self.samplenum else: bit_value = data + bit2_value = data2 sync_value = sync stat_es = self.samplenum From 7ff53f177d2c3f6be02f11159bb89bdd75afa014 Mon Sep 17 00:00:00 2001 From: chenyue Date: Thu, 23 Apr 2026 22:52:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xy2-100-cy/pd.py | 55 +++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/xy2-100-cy/pd.py b/xy2-100-cy/pd.py index ef4f658..ce9edb3 100644 --- a/xy2-100-cy/pd.py +++ b/xy2-100-cy/pd.py @@ -20,7 +20,7 @@ import sigrokdecode as srd -ann_bit,ann_bit2, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_type2, ann_command2, ann_parameter2, ann_parity2, ann_pos,ann_pos2, ann_status, ann_warning = range(15) +ann_bit,ann_bit2, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_type2, ann_command2, ann_parameter2, ann_parity2, ann_pos,ann_pos2, ann_status, ann_warning, ann_warning2 = range(16) frame_type_none, frame_type_command, frame_type_16bit_pos, frame_type_18bit_pos = range(4) class Decoder(srd.Decoder): @@ -46,15 +46,22 @@ class Decoder(srd.Decoder): ) annotations = ( + ('bit', 'Data Bit'), ('bit', 'Data Bit'), ('stat_bit', 'Status Bit'), ('type', 'Frame Type'), ('command', 'Command'), ('parameter', 'Parameter'), ('parity', 'Parity'), + ('type', 'Frame Type'), + ('command', 'Command'), + ('parameter', 'Parameter'), + ('parity', 'Parity'), + ('position', 'Position'), ('position', 'Position'), ('status', 'Status'), ('warning', 'Human-readable warnings'), + ('warning', 'Human-readable warnings'), ) annotation_rows = ( ('bits', 'Data Bits', (ann_bit,)), @@ -66,16 +73,20 @@ class Decoder(srd.Decoder): ('positions2', 'Positions2', (ann_pos2,)), ('statuses', 'Statuses', (ann_status,)), ('warnings', 'Warnings', (ann_warning,)), + ('warnings', 'Warnings', (ann_warning2,)), ) def __init__(self): self.samplerate = None self.reset() + self.reset2() def reset(self): self.bits = [] self.stat_bits = [] self.stat_skip_bit = True + def reset2(self): + self.bits2 = [] def metadata(self, key, value): if key == srd.SRD_CONF_SAMPLERATE: @@ -199,12 +210,12 @@ class Decoder(srd.Decoder): def process_bit2(self, sync, bit_ss, bit_es, bit2_value): self.put_ann(bit_ss, bit_es, ann_bit2, ['%d' % bit2_value]) - self.bits.append((bit_ss, bit_es, bit2_value)) + self.bits2.append((bit_ss, bit_es, bit2_value)) if sync == 0: - if len(self.bits) < 20: - self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Not enough data bits']) - self.reset() + if len(self.bits2) < 20: + self.put_ann(self.bits2[0][0], bit_es, ann_warning2, ['Not enough data bits 0x%X' % len(self.bits2)]) + self.reset2() return # Bit structure: @@ -215,10 +226,10 @@ class Decoder(srd.Decoder): # Calculate parity, excluding the parity bit itself parity = 0 - for ss, es, value in self.bits[:-1]: + for ss, es, value in self.bits2[:-1]: parity ^= value - par_ss, par_es, par_value = self.bits[19] + par_ss, par_es, par_value = self.bits2[19] parity_even = 0 parity_odd = 0 if (par_value == parity): @@ -226,20 +237,20 @@ class Decoder(srd.Decoder): else: parity_odd = 1 - type_1_value = self.bits[0][2] - type_3_value = (self.bits[0][2] << 2) | (self.bits[1][2] << 1) | self.bits[2][2] + type_1_value = self.bits2[0][2] + type_3_value = (self.bits2[0][2] << 2) | (self.bits2[1][2] << 1) | self.bits2[2][2] # Determine frame type type = frame_type_none parity_status = ['X', 'Unknown'] - type_ss = self.bits[0][0] - type_es = self.bits[2][1] + type_ss = self.bits2[0][0] + type_es = self.bits2[2][1] ### 18-bit position if (type_1_value == 1) and (parity_odd == 1): type = frame_type_18bit_pos - type_es = self.bits[0][1] - self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) + type_es = self.bits2[0][1] + self.put_ann(self.bits2[0][0], bit_es, ann_warning2, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) ### 16-bit position elif (type_3_value == 1): type = frame_type_16bit_pos @@ -247,15 +258,15 @@ class Decoder(srd.Decoder): parity_status = ['OK'] else: parity_status = ['NOK'] - self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Parity error', 'PE']) + self.put_ann(self.bits2[0][0], bit_es, ann_warning2, ['Parity error', 'PE']) ### Command elif (type_3_value == 7) and (parity_even == 1): type = frame_type_command - self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) + self.put_ann(self.bits2[0][0], bit_es, ann_warning2, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified']) ### Other else: - self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Error', 'Unknown command or parity error']) - self.reset() + self.put_ann(self.bits2[0][0], bit_es, ann_warning2, ['Error', 'Unknown command or parity error']) + self.reset2() return # Output command and parity annotations @@ -274,13 +285,13 @@ class Decoder(srd.Decoder): if (type == frame_type_16bit_pos): count = 15 - for ss, es, value in self.bits[3:19]: + for ss, es, value in self.bits2[3:19]: pos |= value << count count -= 1 # pos = pos if pos < 32768 else pos - 65536 else: count = 17 - for ss, es, value in self.bits[3:19]: + for ss, es, value in self.bits2[3:19]: pos |= value << count count -= 1 pos = pos if pos < 131072 else pos - 262144 @@ -291,7 +302,7 @@ class Decoder(srd.Decoder): count = 7 cmd = 0 cmd_es = 0 - for ss, es, value in self.bits[3:11]: + for ss, es, value in self.bits2[3:11]: cmd |= value << count count -= 1 cmd_es = es @@ -299,12 +310,12 @@ class Decoder(srd.Decoder): count = 7 param = 0 - for ss, es, value in self.bits[11:19]: + for ss, es, value in self.bits2[11:19]: param |= value << count count -= 1 self.put_ann(cmd_es, par_ss, ann_parameter2, ['Parameter 0x%X / %d' % (param, param), '0x%X / %d' % (param, param),'0x%X' % param]) - self.reset() + self.reset2() def process_stat_bit(self, sync, bit_ss, bit_es, bit_value): if self.stat_skip_bit: