X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fnunchuk.py;h=d33074740eb50b4fb1fdea8137e89cbb3cffd947;hp=128b10e56294c44ade696de01a61440a229eae1b;hb=71071bcfd36bed13b110931a6cc8189a1493dd2a;hpb=16ba97cf2252bc9543f70a0cf3e7d4b2ec6e7ee1 diff --git a/decoders/nunchuk.py b/decoders/nunchuk.py index 128b10e..d330747 100644 --- a/decoders/nunchuk.py +++ b/decoders/nunchuk.py @@ -30,6 +30,15 @@ # https://www.sparkfun.com/products/9281 # +import sigrok + +# States +IDLE = 0 +START = 1 +NUNCHUK_SLAVE = 2 +INIT = 3 +INITIALIZED = 4 + # FIXME: This is just some example input for testing purposes... example_packets = [ # START condition. @@ -57,14 +66,15 @@ class Sample(): def __init__(self, data): self.data = data def probe(self, probe): - s = ord(self.data[probe / 8]) & (1 << (probe % 8)) + s = self.data[int(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(): +class Decoder(sigrok.Decoder): + id = 'nunchuk' name = 'Nunchuk' longname = 'Nintendo Wii Nunchuk decoder' desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.' @@ -79,14 +89,13 @@ class Decoder(): 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 @@ -94,13 +103,13 @@ class Decoder(): def start(self, metadata): self.unitsize = metadata['unitsize'] + # self.output_protocol = self.output_new(2) + self.output_annotation = self.output_new(1) def report(self): pass - def decode(self, data): - """Nintendo Wii Nunchuk decoder""" - + def decode(self, timeoffset, duration, data): out = [] o = {} @@ -112,7 +121,7 @@ class Decoder(): # 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 @@ -129,7 +138,7 @@ class 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: @@ -160,26 +169,26 @@ class 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 - sigrok.put(out) - -import sigrok + if out != []: + # self.put(self.output_protocol, 0, 0, out_proto) + self.put(self.output_annotation, 0, 0, out)