summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerhard Sittig <gerhard.sittig@gmx.net>2023-07-17 18:23:03 +0200
committerGerhard Sittig <gerhard.sittig@gmx.net>2023-07-18 21:09:40 +0200
commit01416b9810ceb759e3088bad174d89621704a210 (patch)
tree908f50c6765c7d6bd21f6b7f3ec3092bd7d13680
parent647aba6af7745c14961674c5feebf83fd18303a3 (diff)
downloadlibsigrokdecode-01416b9810ceb759e3088bad174d89621704a210.tar.gz
libsigrokdecode-01416b9810ceb759e3088bad174d89621704a210.zip
i2c: unify emission of annotations, always use proto{} table
Slightly unobfuscate how the I2C decoder invokes put methods. Present the annotation class and the list of texts for different zoom levels for readability. Also keep the data value presentation in that table so that it holds all texts which users will see during decoder use. Eliminate how the data bits used to bypass that table in the past. This commit does not address the unfortunate self.ss/es coupling of decoding code paths and annotation emitting helpers, which complicates review of the decoder's implemented logic.
-rw-r--r--decoders/i2c/pd.py46
1 files changed, 27 insertions, 19 deletions
diff --git a/decoders/i2c/pd.py b/decoders/i2c/pd.py
index d9061f5..70795c0 100644
--- a/decoders/i2c/pd.py
+++ b/decoders/i2c/pd.py
@@ -48,18 +48,19 @@ For example, a slave address field could be 0x51 (instead of 0xa2).
For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <pdata> is None.
'''
-# CMD: [annotation-type-index, long annotation, short annotation]
+# Meaning of table items:
+# command -> [annotation class, annotation text in order of decreasing length]
proto = {
- 'START': [0, 'Start', 'S'],
- 'START REPEAT': [1, 'Start repeat', 'Sr'],
- 'STOP': [2, 'Stop', 'P'],
- 'ACK': [3, 'ACK', 'A'],
- 'NACK': [4, 'NACK', 'N'],
- 'BIT': [5, 'Bit', 'B'],
- 'ADDRESS READ': [6, 'Address read', 'AR'],
- 'ADDRESS WRITE': [7, 'Address write', 'AW'],
- 'DATA READ': [8, 'Data read', 'DR'],
- 'DATA WRITE': [9, 'Data write', 'DW'],
+ 'START': [0, 'Start', 'S'],
+ 'START REPEAT': [1, 'Start repeat', 'Sr'],
+ 'STOP': [2, 'Stop', 'P'],
+ 'ACK': [3, 'ACK', 'A'],
+ 'NACK': [4, 'NACK', 'N'],
+ 'BIT': [5, '{b:1d}'],
+ 'ADDRESS READ': [6, 'Address read: {b:02X}', 'AR: {b:02X}', '{b:02X}'],
+ 'ADDRESS WRITE': [7, 'Address write: {b:02X}', 'AW: {b:02X}', '{b:02X}'],
+ 'DATA READ': [8, 'Data read: {b:02X}', 'DR: {b:02X}', '{b:02X}'],
+ 'DATA WRITE': [9, 'Data write: {b:02X}', 'DW: {b:02X}', '{b:02X}'],
}
class Decoder(srd.Decoder):
@@ -145,7 +146,8 @@ class Decoder(srd.Decoder):
self.pdu_bits = 0
cmd = 'START REPEAT' if self.is_repeat_start else 'START'
self.putp([cmd, None])
- self.putx([proto[cmd][0], proto[cmd][1:]])
+ cls, texts = proto[cmd][0], proto[cmd][1:]
+ self.putx([cls, texts])
self.state = 'FIND ADDRESS'
self.is_repeat_start = True
self.is_write = None
@@ -230,17 +232,21 @@ class Decoder(srd.Decoder):
self.putb([bin_class, bytes([d])])
- for bit in self.data_bits:
- self.put(bit[1], bit[2], self.out_ann, [5, ['%d' % bit[0]]])
+ for b, ss, es in self.data_bits:
+ cls, texts = proto['BIT'][0], proto['BIT'][1:]
+ texts = [t.format(b = b) for t in texts]
+ self.put(ss, es, self.out_ann, [cls, texts])
if cmd.startswith('ADDRESS') and is_seven:
self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
+ cls = proto[cmd][0]
w = ['Write', 'Wr', 'W'] if self.is_write else ['Read', 'Rd', 'R']
- self.putx([proto[cmd][0], w])
+ self.putx([cls, w])
self.ss, self.es = self.ss_byte, self.samplenum
- self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
- '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
+ cls, texts = proto[cmd][0], proto[cmd][1:]
+ texts = [t.format(b = d) for t in texts]
+ self.putx([cls, texts])
# Done with this packet.
self.data_bits.clear()
@@ -251,7 +257,8 @@ class Decoder(srd.Decoder):
self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
cmd = 'NACK' if (sda == 1) else 'ACK'
self.putp([cmd, None])
- self.putx([proto[cmd][0], proto[cmd][1:]])
+ cls, texts = proto[cmd][0], proto[cmd][1:]
+ self.putx([cls, texts])
# Slave addresses can span one or two bytes, before data bytes
# follow. There can be an arbitrary number of data bytes. Stick
# with getting more address bytes if applicable, or enter or
@@ -273,7 +280,8 @@ class Decoder(srd.Decoder):
cmd = 'STOP'
self.ss, self.es = self.samplenum, self.samplenum
self.putp([cmd, None])
- self.putx([proto[cmd][0], proto[cmd][1:]])
+ cls, texts = proto[cmd][0], proto[cmd][1:]
+ self.putx([cls, texts])
self.state = 'FIND START'
self.is_repeat_start = False
self.is_write = None