|
| 1 | +import enum |
| 2 | + |
| 3 | +import sigrokdecode as srd |
| 4 | + |
| 5 | +__all__ = ['Decoder'] |
| 6 | + |
| 7 | + |
| 8 | +class RegisterOperation(enum.Enum): |
| 9 | + READ = enum.auto() |
| 10 | + WRITE = enum.auto() |
| 11 | + |
| 12 | + |
| 13 | +class Decoder(srd.Decoder): |
| 14 | + api_version = 3 |
| 15 | + id = 'pixart-pmw' |
| 16 | + name = 'PixArt PMW' |
| 17 | + license = 'gplv2+' |
| 18 | + longname = 'PixArt PMW protocol over SPI' |
| 19 | + desc = ( |
| 20 | + 'Decoder for protocol used to communicate ' |
| 21 | + 'with PixArt PMW chips over SPI' |
| 22 | + ) |
| 23 | + inputs = ['spi'] |
| 24 | + outputs = [] |
| 25 | + annotations = ( |
| 26 | + ('command', 'Command'), |
| 27 | + ('reply', 'Reply'), |
| 28 | + ) |
| 29 | + annotation_rows = ( |
| 30 | + ('command', 'Command', (0, )), |
| 31 | + ('reply', 'Reply', (1, )), |
| 32 | + ) |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + self.reset() |
| 36 | + |
| 37 | + def start(self): |
| 38 | + self.out_ann = self.register(srd.OUTPUT_ANN) |
| 39 | + |
| 40 | + def reset(self): |
| 41 | + pass |
| 42 | + |
| 43 | + def decode(self, startsample, endsample, data): |
| 44 | + ptype, command_data, reply_data = data |
| 45 | + |
| 46 | + if ptype != 'TRANSFER': |
| 47 | + return |
| 48 | + |
| 49 | + if not command_data: |
| 50 | + return |
| 51 | + |
| 52 | + first_byte = command_data[0].val |
| 53 | + command_type = RegisterOperation.WRITE if first_byte & 0x80 else RegisterOperation.READ |
| 54 | + register_num = first_byte & ~0x80 |
| 55 | + |
| 56 | + command_end = endsample |
| 57 | + if command_type == RegisterOperation.READ: |
| 58 | + message = 'Read register {:02X}'.format(register_num) |
| 59 | + if len(command_data) > 1: |
| 60 | + command_end = command_data[1].ss |
| 61 | + elif command_type == RegisterOperation.WRITE: |
| 62 | + message = 'Write register {:02X}: {}'.format( |
| 63 | + register_num, format_transfer_data(command_data[1:])) |
| 64 | + self.put(startsample, command_end, self.out_ann, [0, [message]]) |
| 65 | + |
| 66 | + if command_type == RegisterOperation.READ: |
| 67 | + reply_start = startsample |
| 68 | + if len(reply_data) > 1: |
| 69 | + reply_start = reply_data[1].ss |
| 70 | + self.put(reply_start, endsample, self.out_ann, |
| 71 | + [1, [format_transfer_data(reply_data[1:])]]) |
| 72 | + |
| 73 | + |
| 74 | +def format_transfer_data(data): |
| 75 | + return ' '.join(['{:02X}'.format(val.val) for val in data]) |
0 commit comments