Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 file under a new decoder directory, depending on your OS:

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

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.


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 = (
('bit', 'Values', (0,)),
def __init__(self):
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
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.