From 56335df11d799a3a79027beabecfa22a83eacf59 Mon Sep 17 00:00:00 2001 From: Brian Whitman Date: Wed, 18 Sep 2024 14:52:59 -0400 Subject: [PATCH] convering the m5_8encoder class to a simpler module to match how the others work --- tulip/shared/py/m5_8encoder.py | 127 ++++++++++++++++----------------- 1 file changed, 60 insertions(+), 67 deletions(-) diff --git a/tulip/shared/py/m5_8encoder.py b/tulip/shared/py/m5_8encoder.py index da604d31b..84606d098 100644 --- a/tulip/shared/py/m5_8encoder.py +++ b/tulip/shared/py/m5_8encoder.py @@ -5,90 +5,83 @@ from machine import I2C import struct +i2c = I2C(0, freq=400000) -class M5_8Encoder: - ENCODERS = const(8) - LEDS = const(9) +ENCODERS = const(8) +LEDS = const(9) +address = 0x41 - def __init__( - self, - i2c: I2C, - address: int = 0x41, - ): - self.address = address - self._i2c = i2c +def _read(mem_addr: int, num_bytes: int): + i2c.writeto(address, bytes([mem_addr])) + return i2c.readfrom(address, num_bytes) - def _read(self, mem_addr: int, num_bytes: int): - self._i2c.writeto(self.address, bytes([mem_addr])) - return self._i2c.readfrom(self.address, num_bytes) +def _write(mem_addr: int, data: bytes): + i2c.writeto_mem(address, mem_addr, data) - def _write(self, mem_addr: int, data: bytes): - self._i2c.writeto_mem(self.address, mem_addr, data) +def read_counter(counter: int): + if not (0 <= counter < ENCODERS): + raise ValueError( + f"Invalid counter number {counter} (valid values: 0-{ENCODERS - 1})" + ) - def read_counter(self, counter: int): - if not (0 <= counter < self.ENCODERS): - raise ValueError( - f"Invalid counter number {counter} (valid values: 0-{self.ENCODERS - 1})" - ) + val = _read(4 * counter, 4) + return struct.unpack("