Skip to content

Instantly share code, notes, and snippets.

@geekman
Last active June 28, 2023 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekman/c3a6d22741eb8cecd28e4191f67d39ad to your computer and use it in GitHub Desktop.
Save geekman/c3a6d22741eb8cecd28e4191f67d39ad to your computer and use it in GitHub Desktop.
libsigrok toy decoder

libsigrok Toy Decoder

This is a very minimal example for writing your own quick-and-dirty decoder that you can use in PulseView.

This decoder displays annotations for a signal that's made up of 1 ms units. It annotates from one signal edge to another, without regard for time. If you want to handle the actual units of time, more logic is required and that's left as an exercise for the reader.

Decoders are written as Python modules, and you will need to place this __init__.py file under a new decoder directory, depending on your OS:

  • Windows: %LOCALAPPDATA%\libsigrokdecode\decoders\toy\__init__.py
  • Linux: ~/.local/share/libsigrokdecode/decoders/toy/__init__.py

Tested on PulseView 0.4.1, libsigrok 0.5.1.

A sample file containing a test signal has been included to try this decoder on.

Licensed under GPLv2+, not because I want to, but because the sigrok project requires it. If it were up to me, I'd probably use something more liberal like MIT or BSD, which I usually do.

Documentation

You should refer to the following documentation to flesh it out:

#
# Toy sigrok decoder
#
# 2019.07.09 darell tan
# GPLv2+
#
"""A very simple decoder, which you can use to create your own from."""
import sigrokdecode as srd
class Decoder(srd.Decoder):
api_version = 3
id = 'toy'
name = 'Toy'
longname = 'Toy Decoder'
desc = 'A very simple decoder'
license = 'gplv2+'
inputs = ['logic']
outputs = []
tags = ['Clock/timing', 'Util']
channels = (
{'id': 'data', 'name': 'Data', 'desc': 'Data channel'},
)
options = (
)
annotations = (
('bit', 'Value'),
)
annotation_rows = (
('bits', 'Values', (0,)),
)
def __init__(self):
self.reset()
def reset(self):
self.samplerate = None
self.val = 0
self.prevN = 0
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 decode(self):
if self.samplerate is None:
raise Exception('Cannot decode without samplerate.')
while True:
# look for an edge, and capture both value and sample number
v, = self.wait({0: 'e'})
n = self.samplenum
# if this is the first edge, store it and go onto the next one
if not self.prevN:
self.prevN = n
self.val = v
continue
# space between signals is at least 10ms
# anything less than that is probably part of this data packet
t = (n - self.prevN) / self.samplerate
if t < 10e-3:
self.put(self.prevN, n, self.out_ann, [0, [str(self.val)]])
self.prevN = n
self.val = v
@davidrib
Copy link

Hi!! Thanks For sharing, I'm trying to install-it but it don't seams appear on PulseView Decoders. Can you help-me? I'm appreciate thanks

@geekman
Copy link
Author

geekman commented Jun 28, 2023

Thanks for reporting. Looks like the IDs on annotation and annotation_rows need to be unique. It's fixed now.

Hint: You can find debug/error messages under Settings > Logging.

@davidrib
Copy link

tank you very much, i I could never see that!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment