X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Ftransitioncounter.py;h=6092258d44b6ea25706dc686fff691723c875402;hp=46b5ac43f278d34f31c6cfccffcae49fffec52be;hb=d0e93c76e381eff58ca23949301f781b24ba4a8b;hpb=1c8ac5bf07e0ded582234c9ef50ba10f042bae52 diff --git a/decoders/transitioncounter.py b/decoders/transitioncounter.py index 46b5ac4..6092258 100644 --- a/decoders/transitioncounter.py +++ b/decoders/transitioncounter.py @@ -18,20 +18,9 @@ ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## -import sigrok +import sigrokdecode as srd -class Sample(): - def __init__(self, data): - self.data = data - def probe(self, probe): - s = ord(self.data[probe / 8]) & (1 << (probe % 8)) - return True if s else False - -def sampleiter(data, unitsize): - for i in range(0, len(data), unitsize): - yield(Sample(data[i:i+unitsize])) - -class Decoder(sigrok.Decoder): +class Decoder(srd.Decoder): id = 'transitioncounter' name = 'Transition counter' longname = '...' @@ -42,36 +31,32 @@ class Decoder(sigrok.Decoder): license = 'gplv2+' inputs = ['logic'] outputs = ['transitioncounts'] - probes = {} + probes = [ + ] # TODO? options = {} def __init__(self, **kwargs): - self.probes = Decoder.probes.copy() - - # TODO: Don't hardcode the number of channels. - self.channels = 8 - + self.channels = -1 self.lastsample = None - self.oldbit = [0] * self.channels - self.transitions = [0] * self.channels - self.rising = [0] * self.channels - self.falling = [0] * self.channels def start(self, metadata): - self.unitsize = metadata['unitsize'] + # self.out_proto = self.add(srd.SRD_OUTPUT_PROTO, 'transitioncounter') + self.out_ann = self.add(srd.SRD_OUTPUT_ANN, 'transitioncounter') def report(self): pass - def decode(self, data): - """Counts the low->high and high->low transitions in the specified - channel(s) of the signal.""" + def decode(self, timeoffset, duration, data): - # We should accept a list of samples and iterate... - for sample in sampleiter(data['data'], self.unitsize): + for (samplenum, s) in data: - # TODO: Eliminate the need for ord(). - s = ord(sample.data) + # ... + if self.channels == -1: + self.channels = len(s) + self.oldbit = [0] * self.channels + self.transitions = [0] * self.channels + self.rising = [0] * self.channels + self.falling = [0] * self.channels # Optimization: Skip identical samples (no transitions). if self.lastsample == s: @@ -81,12 +66,12 @@ class Decoder(sigrok.Decoder): if self.lastsample == None: self.lastsample = s for i in range(self.channels): - self.oldbit[i] = (self.lastsample & (1 << i)) >> i + self.oldbit[i] = self.lastsample[i] # Iterate over all channels/probes in this sample. # Count rising and falling edges for each channel. for i in range(self.channels): - curbit = (s & (1 << i)) >> i + curbit = s[i] # Optimization: Skip identical bits (no transitions). if self.oldbit[i] == curbit: continue @@ -108,5 +93,8 @@ class Decoder(sigrok.Decoder): outdata = [] for i in range(self.channels): outdata += [[self.transitions[i], self.rising[i], self.falling[i]]] - self.put(outdata) + + if outdata != []: + # self.put(0, 0, self.out_proto, out_proto) + self.put(0, 0, self.out_ann, outdata)