X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fnunchuk.py;h=d3ae8714d17c42e82481f70d9d871fc8aa6f30f3;hp=95ba3b63f9bf739368f558c3604a32d5fd67092d;hb=2fd89a85c4a1131ca259d794a43b26b8bd84b6a0;hpb=1c8ac5bf07e0ded582234c9ef50ba10f042bae52 diff --git a/decoders/nunchuk.py b/decoders/nunchuk.py index 95ba3b6..d3ae871 100644 --- a/decoders/nunchuk.py +++ b/decoders/nunchuk.py @@ -30,7 +30,14 @@ # https://www.sparkfun.com/products/9281 # -import sigrok +import sigrokdecode + +# States +IDLE = 0 +START = 1 +NUNCHUK_SLAVE = 2 +INIT = 3 +INITIALIZED = 4 # FIXME: This is just some example input for testing purposes... example_packets = [ @@ -55,18 +62,7 @@ example_packets = [ {'type': 'P', 'range': (32, 33), 'data': None, 'ann': ''}, ] -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(sigrokdecode.Decoder): id = 'nunchuk' name = 'Nunchuk' longname = 'Nintendo Wii Nunchuk decoder' @@ -77,45 +73,38 @@ class Decoder(sigrok.Decoder): license = 'gplv2+' inputs = ['i2c'] outputs = ['nunchuck'] - probes = {} + probes = [] # TODO options = {} def __init__(self, **kwargs): - self.probes = Decoder.probes.copy() + self.output_protocol = None + self.output_annotation = None - # TODO: Don't hardcode the number of channels. - self.channels = 8 - - self.IDLE, self.START, self.NUNCHUK_SLAVE, self.INIT, \ - self.INITIALIZED = range(5) - - self.state = self.IDLE # TODO: Can we assume a certain initial state? + self.state = IDLE # TODO: Can we assume a certain initial state? self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0 self.databytecount = 0 def start(self, metadata): - self.unitsize = metadata['unitsize'] + # self.output_protocol = self.add(2) + self.output_annotation = self.add(1) def report(self): pass - def decode(self, data): - """Nintendo Wii Nunchuk decoder""" - + def decode(self, timeoffset, duration, data): out = [] o = {} # We should accept a list of samples and iterate... - # for sample in sampleiter(data['data'], self.unitsize): - for p in example_packets: + for p in example_packets: # TODO # TODO: Eliminate the need for ord(). # s = ord(sample.data) if p['type'] == 'S': # TODO: Handle 'Sr' here, too? - self.state = self.START + self.state = START elif p['type'] == 'Sr': pass # FIXME @@ -132,7 +121,7 @@ class Decoder(sigrok.Decoder): else: pass # TODO: What to do here? Ignore? Error? - elif p['type'] == 'DR' and self.state == self.INITIALIZED: + elif p['type'] == 'DR' and self.state == INITIALIZED: if self.databytecount == 0: self.sx = p['data'] elif self.databytecount == 1: @@ -163,24 +152,26 @@ class Decoder(sigrok.Decoder): # TODO: If 6 bytes read -> save and reset # TODO - elif p['type'] == 'DR' and self.state != self.INITIALIZED: + elif p['type'] == 'DR' and self.state != INITIALIZED: pass elif p['type'] == 'DW': - if p['data'] == 0x40 and self.state == self.START: - self.state = self.INIT - elif p['data'] == 0x00 and self.state == self.INIT: + if p['data'] == 0x40 and self.state == START: + self.state = INIT + elif p['data'] == 0x00 and self.state == INIT: o = {'type': 'I', 'range': (0, 0), 'data': []} o['data'] = [0x40, 0x00] out.append(o) - self.state = self.INITIALIZED + self.state = INITIALIZED else: pass # TODO elif p['type'] == 'P': out.append(o) - self.state = self.INITIALIZED + self.state = INITIALIZED self.databytecount = 0 - self.put(out) + if out != []: + # self.put(0, 0, self.output_protocol, out_proto) + self.put(0, 0, self.output_annotation, out)