Skip to content

Commit da663a8

Browse files
committed
almost there but not excited?
1 parent 8f769b0 commit da663a8

File tree

2 files changed

+33
-41
lines changed

2 files changed

+33
-41
lines changed

i2c_transactions.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,12 @@ def _load_register_map(self):
157157
self.register_map = map_loader.map
158158

159159
def process_transaction(self):
160-
txn = self.current_transaction
161-
# address_byte = txn.data.pop(0)
162-
# if self.mode == MODE_AUTO_INCREMENT_ADDR_MSB_HIGH:
163-
# address_byte &= 0x7F # clear any MSB used for auto increment
164-
165-
############ register naming #####################
166-
160+
print(self.current_transaction)
167161
transaction_string = self.decoder.decode_transaction(self.current_transaction)
168162

169163
###################################################
170164
# transaction_string = str(txn)
171-
print(transaction_string)
165+
print("DECODED:", transaction_string)
172166
new_frame = {
173167
'type': 'transaction',
174168
'start_time': self.current_transaction.start_time,

register_decoder.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,8 @@ def __init__(self, register_map=None):
8686
AttributeError("you must provide a register map")
8787
self.prev_single_byte_write = None
8888
self.current_bank = 0
89-
# if len(self.register_map) is 1 and self.current_bank is -1:
90-
# self.current_bank = 0
91-
# print("reg map length:", len(self.register_map))
92-
# print("********* REG MAP?!*************")
93-
# pretty(self.register_map)
94-
# print("*************************************")
95-
def decode(self, row_num, row):
96-
9789

90+
def decode(self, row_num, row):
9891
adjusted_row_num = row_num + ROW_NUMBER_OFFSET
9992
b0 = None
10093
b1 = None
@@ -138,44 +131,45 @@ def decode(self, row_num, row):
138131
print(self.decode_bytes(rw, b0, b1))
139132

140133
def decode_transaction(self, reg_txn):
134+
# decoder should be able to use these but I think it's currently
135+
# fguring out what register_address is
141136
# reg_txn.is_read
142137
# reg_txn.i2c_node_addr #sensor/outgoing address
143138
# reg_txn.register_address #destination of write, source of read
144139
# reg_txn.data # ints/non-string list
140+
out_str = "CAN'T DECODE: %s"%reg_txn
141+
if reg_txn.is_read:
142+
rw = "READ"
143+
else:
144+
rw= "WRITE"
145+
if len(reg_txn.data)>2:
146+
return "[UNDER CONSTRUCTION: len(dat)>2]"
147+
if reg_txn.is_read and len(reg_txn.data) >2:
148+
return "READ %s"%reg_txn.data
149+
150+
try:
151+
out_str = self.decode_bytes(rw, *reg_txn.data)
152+
except RuntimeError as e:
153+
print("Error Decoding:")
154+
print(type(e))
155+
print(e.args)
156+
print(e)
145157

146-
147-
148-
149-
# if reg_txn.is_read:
150-
# rw = "READ"
151-
# else:
152-
# rw= "WRITE"
153-
# if len(reg_txn.data)>2:
154-
# return ""
155-
# out_str = "+>%s"%reg_txn
156-
# out_str = self.decode_bytes(rw, *reg_txn.data)
157-
out_str = str(reg_txn)
158158
return out_str
159159

160160
# TODO: Take bool, for rw
161161
# TODO: Return string, print from caller
162+
# TODO: Take a bytearray
162163
def decode_bytes(self, rw, b0=None, b1=None):
163-
if DEBUG >=2:
164-
if not b0:
165-
b0s = " "
166-
else:
167-
b0s = hex(b0)
168-
if not b1:
169-
b1s = " "
170-
else:
171-
b1s = hex(b1)
172-
print("[%s : %s : %s]"%(rw, b0s, b1s))
173-
if b1 is None:
164+
165+
if b1 is None: # single byte
174166
return self.single_byte_decode(rw, b0)
175167
elif rw == "WRITE":
168+
if b0 not in self.register_map[self.current_bank]:
169+
return "UNKNOWN REG: %s"%hex(b0)
176170
return self.decode_set_value(rw, b0, b1)
177171
else:
178-
#raise RuntimeError("Multi-byte reads not supported")
172+
raise RuntimeError("Multi-byte reads not supported")
179173
return
180174

181175
def single_byte_decode(self, rw, b0):
@@ -209,14 +203,18 @@ def single_byte_decode(self, rw, b0):
209203
# self._h(b0),
210204
# )
211205
# )
212-
206+
213207
# isn't this going to do nothing because it's a local?
214208
current_register['last_read_value'] = b0
215209
self.prev_single_byte_write = None # shouldn't be needed
216210
# else:
217211
# raise ("UNEXPECTED READ WITHOUT PRECEDING WRITE")
218212

219213
def decode_set_value(self, rw, reg_addr, value_byte):
214+
215+
print("\tbank:", self.current_bank)
216+
print('reg_addr', reg_addr)
217+
220218
current_register = self.register_map[self.current_bank][reg_addr]
221219
bitfields = self.load_bitfields(current_register)
222220

0 commit comments

Comments
 (0)