mirror of
https://github.com/brain-hackers/lab
synced 2024-12-21 19:50:04 +09:00
Update read_keys impl
This commit is contained in:
parent
f7c9a96021
commit
e501a574a9
58
read_keys.py
58
read_keys.py
@ -10,19 +10,16 @@ addr = 0x28
|
||||
|
||||
def read(reg):
|
||||
v = i2c.read_byte_data(addr, reg)
|
||||
print(f"Read b {reg:#04x} = {v:#04x}")
|
||||
return v
|
||||
|
||||
|
||||
def readw(reg):
|
||||
v = i2c.read_word_data(addr, reg)
|
||||
#print(f"Read w {reg:#04x} = {v:#06x}")
|
||||
return v & 0x00ff, (v & 0xff00) >> 8
|
||||
|
||||
|
||||
def write(reg, v):
|
||||
i2c.write_byte_data(addr, reg, v)
|
||||
print(f"Write b {reg:#04x} = {v:#06x}")
|
||||
|
||||
|
||||
def setup():
|
||||
@ -51,6 +48,51 @@ def setup():
|
||||
write(0x82, 0x03)
|
||||
|
||||
|
||||
def setup_simple():
|
||||
"""Simplified sequence without redundant calls"""
|
||||
|
||||
print('Waiting the MCU to get ready')
|
||||
|
||||
st = read(0x00)
|
||||
while st & 0x40 != 0:
|
||||
print('.', end='')
|
||||
st = read(0x00)
|
||||
time.sleep(0.1)
|
||||
|
||||
working = read(0x0a) & 0x01 > 0
|
||||
if working:
|
||||
print('MCU is working')
|
||||
else:
|
||||
print('MCU is suspended')
|
||||
|
||||
event = read(0x01) & 0x20 > 0
|
||||
if event:
|
||||
print('Event is enabled')
|
||||
else:
|
||||
print('Event is disabled')
|
||||
|
||||
v = read(0x40)
|
||||
print(f'Reg (R) 0x40 -> {v:#04x}')
|
||||
|
||||
read(0x41)
|
||||
print(f'Reg (R) 0x41 -> {v:#04x}')
|
||||
|
||||
read(0x48)
|
||||
print(f'Reg (R) 0x48 -> {v:#04x}')
|
||||
|
||||
write(0x88, 0x08)
|
||||
print('Reg (W) 0x88 = 0x08')
|
||||
|
||||
write(0x82, 0x05)
|
||||
print('Reg (W) 0x82 = 0x05')
|
||||
|
||||
write(0x82, 0x02)
|
||||
print('Reg (W) 0x82 = 0x02')
|
||||
|
||||
write(0x82, 0x03)
|
||||
print('Reg (W) 0x82 = 0x03')
|
||||
|
||||
|
||||
def read_keys():
|
||||
keys = []
|
||||
n, key = readw(0x04)
|
||||
@ -59,12 +101,10 @@ def read_keys():
|
||||
|
||||
keys.append(key)
|
||||
|
||||
additional = n // 2
|
||||
for i in range(additional):
|
||||
if n >= 1:
|
||||
k1, k2 = readw(0x04)
|
||||
keys.append(k1)
|
||||
if i == additional-1 and n % 2 == 1:
|
||||
keys.append(k2)
|
||||
keys = keys + [k1, k2]
|
||||
keys = keys[:n]
|
||||
|
||||
print(f"N = {n:#04x}")
|
||||
|
||||
@ -72,7 +112,7 @@ def read_keys():
|
||||
print(f"Keys = {', '.join(keys)}")
|
||||
|
||||
|
||||
setup()
|
||||
setup_simple()
|
||||
while True:
|
||||
time.sleep(0.1)
|
||||
read_keys()
|
||||
|
Loading…
Reference in New Issue
Block a user