You've already forked TEF6686_Driver
minor changes in functionality
This commit is contained in:
43
base_tef.py
43
base_tef.py
@@ -1,6 +1,5 @@
|
||||
from protocol import I2CPCClient, time
|
||||
from tef_102_patch import *
|
||||
from tef_205_patch import *
|
||||
from patches import *
|
||||
|
||||
from typing import Callable, TypeVar, ParamSpec, Concatenate
|
||||
from collections.abc import Callable as ABCCallable
|
||||
@@ -8,36 +7,36 @@ from collections.abc import Callable as ABCCallable
|
||||
P = ParamSpec("P")
|
||||
T = TypeVar("T")
|
||||
|
||||
ADDRESS = 0x64
|
||||
class BaseTEF668X:
|
||||
def __init__(self, p: I2CPCClient) -> None:
|
||||
def __init__(self, p: I2CPCClient, address: int = 0x64) -> None:
|
||||
self.p = p
|
||||
p.set_baudrate(921600)
|
||||
self.p.set_clock(100_000)
|
||||
self.address = address
|
||||
def close(self):
|
||||
self.p.set_baudrate(115200)
|
||||
self.p.close()
|
||||
def _reset(self):
|
||||
self.p.write_i2c(ADDRESS, b"\x1e\x5a\x01\x5a\x5a")
|
||||
self.p.write_i2c(self.address, b"\x1e\x5a\x01\x5a\x5a")
|
||||
def _write_firmware(self, patch: bytes | list[int], patch_lut: bytes | list[int]):
|
||||
self.p.set_clock(400_000)
|
||||
self.p.write_i2c(ADDRESS, b"\x1c\x00\x00")
|
||||
self.p.write_i2c(ADDRESS, b"\x1c\x00\x74")
|
||||
self.p.write_i2c(self.address, b"\x1c\x00\x00")
|
||||
self.p.write_i2c(self.address, b"\x1c\x00\x74")
|
||||
def send_patch(_patch: bytes):
|
||||
for i in range(0, len(_patch), 64):
|
||||
for i in range(0, len(_patch), 64): # More data in a single step is less data sent over serial
|
||||
data = _patch[i:i+64]
|
||||
if self.p.write_i2c(ADDRESS, b"\x1b" + data)[0] != 0: raise Exception
|
||||
if self.p.write_i2c(self.address, b"\x1b" + data)[0] != 0: raise Exception
|
||||
send_patch(bytes(patch))
|
||||
self.p.write_i2c(ADDRESS, b"\x1c\x00\x00")
|
||||
self.p.write_i2c(ADDRESS, b"\x1c\x00\x75")
|
||||
self.p.write_i2c(self.address, b"\x1c\x00\x00")
|
||||
self.p.write_i2c(self.address, b"\x1c\x00\x75")
|
||||
send_patch(bytes(patch_lut))
|
||||
self.p.write_i2c(ADDRESS, b"\x1c\x00\x00")
|
||||
self.p.write_i2c(self.address, b"\x1c\x00\x00")
|
||||
self.p.set_clock(100_000)
|
||||
|
||||
def APPL_Get_Operation_Status(self):
|
||||
data = self.p.write_read_i2c(ADDRESS, b"\x40\x80\x01", 2)
|
||||
data = self.p.write_read_i2c(self.address, b"\x40\x80\x01", 2)
|
||||
while data[0] != 0:
|
||||
data = self.p.write_read_i2c(ADDRESS, b"\x40\x80\x01", 2)
|
||||
data = self.p.write_read_i2c(self.address, b"\x40\x80\x01", 2)
|
||||
time.sleep(0.01)
|
||||
return data[-1]
|
||||
|
||||
@@ -45,8 +44,8 @@ class BaseTEF668X:
|
||||
def _base_command_wrapper(func: Callable[Concatenate["BaseTEF668X", P], tuple[bytes, int | None, ABCCallable[[bytes], T] | None]]) -> Callable[Concatenate["BaseTEF668X", P], bytes | T]:
|
||||
def inner(self: "BaseTEF668X", *args: P.args, **kwargs: P.kwargs ) -> bytes | T:
|
||||
data, read_bytes, out_parser = func(self, *args, **kwargs)
|
||||
if read_bytes: data = self.p.write_read_i2c(ADDRESS, data, read_bytes)
|
||||
else: data = self.p.write_i2c(ADDRESS, data)
|
||||
if read_bytes: data = self.p.write_read_i2c(self.address, data, read_bytes)
|
||||
else: data = self.p.write_i2c(self.address, data)
|
||||
if out_parser: return out_parser(data)
|
||||
return data
|
||||
return inner
|
||||
@@ -60,10 +59,14 @@ class BaseTEF668X:
|
||||
|
||||
def init(self, patch = tef_102_patch, patch_lut = tef_102_patch_lut, clock: int = 9216000):
|
||||
self._reset()
|
||||
while self.APPL_Get_Operation_Status() != 0: time.sleep(0.01)
|
||||
while self.APPL_Get_Operation_Status() != 0: time.sleep(0.025) # TODO: timeouts
|
||||
self._write_firmware(patch, patch_lut)
|
||||
self.p.write_i2c(ADDRESS, b"\x14\x00\x01")
|
||||
while self.APPL_Get_Operation_Status() != 1: time.sleep(0.01)
|
||||
self.p.write_i2c(self.address, b"\x14\x00\x01")
|
||||
while self.APPL_Get_Operation_Status() != 1: time.sleep(0.025)
|
||||
if clock != 9216000: self.APPL_Set_ReferenceClock(clock, False)
|
||||
self.APPL_Activate()
|
||||
while self.APPL_Get_Operation_Status() != 2: time.sleep(0.01)
|
||||
while self.APPL_Get_Operation_Status() != 2: time.sleep(0.025)
|
||||
|
||||
def __enter__(self): return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb): self.close()
|
||||
Reference in New Issue
Block a user