From: Tomas Mudrunka Date: Fri, 13 Mar 2020 22:48:26 +0000 (+0100) Subject: sigrok decoder for caliper X-Git-Url: http://git.harvie.cz/?p=mirrors%2FPrograms.git;a=commitdiff_plain;h=63efb48203536b99be48591aad6aa3b5ecaaa9c5 sigrok decoder for caliper --- diff --git a/plugins/sigrok-decoders/caliper/__init__.py b/plugins/sigrok-decoders/caliper/__init__.py new file mode 100644 index 0000000..65620f8 --- /dev/null +++ b/plugins/sigrok-decoders/caliper/__init__.py @@ -0,0 +1,25 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2020 Tomas Mudrunka +## +## 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 . +## + +''' +This decodes digital output of cheap generic calipers (usualy made in china) +Decoder will show measured value in milimeters or inches. +''' + +from .pd import Decoder diff --git a/plugins/sigrok-decoders/caliper/pd.py b/plugins/sigrok-decoders/caliper/pd.py new file mode 100644 index 0000000..9a9ad73 --- /dev/null +++ b/plugins/sigrok-decoders/caliper/pd.py @@ -0,0 +1,149 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2020 Tomas Mudrunka +## +## Permission is hereby granted, free of charge, to any person obtaining a copy +## of this software and associated documentation files (the "Software"), to deal +## in the Software without restriction, including without limitation the rights +## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +## copies of the Software, and to permit persons to whom the Software is +## furnished to do so, subject to the following conditions: +## +## The above copyright notice and this permission notice shall be included in all +## copies or substantial portions of the Software. +## +## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +## SOFTWARE. + +import sigrokdecode as srd + +class Decoder(srd.Decoder): + api_version = 3 + id = 'caliper' + name = 'Caliper' + longname = 'Digital calipers' + desc = 'Protocol of cheap generic digital calipers' + license = 'mit' + inputs = ['logic'] + outputs = [] + channels = ( + {'id': 'clk', 'name': 'CLK', 'desc': 'Serial clock line'}, + {'id': 'data', 'name': 'DATA', 'desc': 'Serial data line'}, + ) + options = ( + {'id': 'timeout_ms', 'desc': 'Timeout packet after X ms, 0 to disable', 'default': 10}, + {'id': 'unit', 'desc': 'Convert units', 'default': 'keep', 'values': ('keep', 'mm', 'inch')}, + ) + tags = ['Analog/digital', 'IC', 'Sensor'] + annotations = ( + ('measurements', 'Measurements'), + ('warning', 'Warnings'), + ) + annotation_rows = ( + ('measurements', 'Measurements', (0,)), + ('warnings', 'Warnings', (1,)), + ) + + def reset_data(self): + self.bits = 0 + self.number = 0 + self.flags = 0 + + def metadata(self, key, value): + if key == srd.SRD_CONF_SAMPLERATE: + self.samplerate = value + + def __init__(self): + self.reset() + + def reset(self): + self.ss_cmd, self.es_cmd = 0, 0 + self.reset_data() + + def start(self): + self.out_ann = self.register(srd.OUTPUT_ANN) + + #Switch bit order of variable x, which is l bit long + def bitr(self,x,l): + return int(bin(x)[2:].zfill(l)[::-1], 2) + + def decode(self): + while True: + clk, data = self.wait([{0: 'r'},{'skip': round(self.samplerate/1000)}]) + #print([clk,data]) + + #Timeout after inactivity + if(self.options['timeout_ms'] > 0): + if self.samplenum > self.es_cmd + (self.samplerate/(1000/self.options['timeout_ms'])): + if self.bits > 0: + self.put(self.ss_cmd, self.samplenum, self.out_ann, [1, ['timeout with %s bits in buffer'%(self.bits),'timeout']]) + self.reset() + + #Do nothing if there was timeout without rising clock edge + if self.matched == (False, True): + continue + + #Store position of last activity + self.es_cmd = self.samplenum + + #Store position of first bit + if self.ss_cmd == 0: + self.ss_cmd = self.samplenum + + #Shift in measured number + if self.bits < 16: + self.number = (self.number << 1) | (data & 0b1) + self.bits+=1 + continue + + #Shift in flag bits + if self.bits < 24: + self.flags = (self.flags << 1) | (data & 0b1) + self.bits+=1 + if self.bits < 24: + continue + #Hooray! We got last bit of data + self.es_cmd = self.samplenum + + #Do actual decoding + + #print(format(self.flags, '08b')); + + negative = ((self.flags & 0b00001000) >> 3) + inch = (self.flags & 0b00000001) + + number = self.bitr(self.number, 16) + + #print(format(number, '016b')) + + if negative > 0: + number = -number + + inchmm = 25.4 #how many mms in inch + + if inch: + number = number/2000 + if self.options['unit'] == 'mm': + number *= inchmm + inch = 0 + else: + number = number/100 + if self.options['unit'] == 'inch': + number = round(number/inchmm,4) + inch = 1 + + units = "in" if inch else "mm" + + measurement = (str(number)+units) + #print(measurement) + + self.put(self.ss_cmd, self.es_cmd, self.out_ann, [0, [measurement, str(number)]]) + + #Prepare for next packet + self.reset() diff --git a/plugins/sigrok-decoders/caliper/tests/-123.45mm.sr b/plugins/sigrok-decoders/caliper/tests/-123.45mm.sr new file mode 100644 index 0000000..0773429 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/-123.45mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/-1mm.sr b/plugins/sigrok-decoders/caliper/tests/-1mm.sr new file mode 100644 index 0000000..4484052 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/-1mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/0.0005in.sr b/plugins/sigrok-decoders/caliper/tests/0.0005in.sr new file mode 100644 index 0000000..cabde92 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/0.0005in.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/0.5555in.sr b/plugins/sigrok-decoders/caliper/tests/0.5555in.sr new file mode 100644 index 0000000..067bfd4 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/0.5555in.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/0.55mm.sr b/plugins/sigrok-decoders/caliper/tests/0.55mm.sr new file mode 100644 index 0000000..988c64d Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/0.55mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/0.5in.sr b/plugins/sigrok-decoders/caliper/tests/0.5in.sr new file mode 100644 index 0000000..35b17cd Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/0.5in.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/0.5mm.sr b/plugins/sigrok-decoders/caliper/tests/0.5mm.sr new file mode 100644 index 0000000..16af496 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/0.5mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/0in.sr b/plugins/sigrok-decoders/caliper/tests/0in.sr new file mode 100644 index 0000000..1b73895 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/0in.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/0mm.sr b/plugins/sigrok-decoders/caliper/tests/0mm.sr new file mode 100644 index 0000000..8f90f87 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/0mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/100mm.sr b/plugins/sigrok-decoders/caliper/tests/100mm.sr new file mode 100644 index 0000000..3608de3 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/100mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/10mm.sr b/plugins/sigrok-decoders/caliper/tests/10mm.sr new file mode 100644 index 0000000..0962845 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/10mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/123.45mm.sr b/plugins/sigrok-decoders/caliper/tests/123.45mm.sr new file mode 100644 index 0000000..ea7bbb3 Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/123.45mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/55.55mm.sr b/plugins/sigrok-decoders/caliper/tests/55.55mm.sr new file mode 100644 index 0000000..362ecbc Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/55.55mm.sr differ diff --git a/plugins/sigrok-decoders/caliper/tests/5in.sr b/plugins/sigrok-decoders/caliper/tests/5in.sr new file mode 100644 index 0000000..20ffa0e Binary files /dev/null and b/plugins/sigrok-decoders/caliper/tests/5in.sr differ