Skip to content

Commit 2a3f208

Browse files
Alexebrahimebrahim
authored andcommitted
Add parameters in TX device functions to send/request info from specific module. (#75)
1 parent 98abc7e commit 2a3f208

1 file changed

Lines changed: 30 additions & 26 deletions

File tree

src/openlifu/io/LIFUTXDevice.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
ProfileOpts = Literal['active', 'configured', 'all']
102102
TriggerModeOpts = Literal['sequence', 'continuous','single']
103103
DEFAULT_PULSE_WIDTH_US = 20
104+
HW_ID_DATA_LENGTH = 12
105+
TEMPERATURE_DATA_LENGTH = 4
104106

105107
from openlifu.io.LIFUConfig import (
106108
OW_CMD_ASYNC,
@@ -198,7 +200,7 @@ def close(self):
198200
if self.uart and self.uart.is_connected():
199201
self.uart.disconnect()
200202

201-
def ping(self) -> bool:
203+
def ping(self, module:int=1) -> bool:
202204
"""
203205
Send a ping command to the TX device to verify connectivity.
204206
@@ -216,7 +218,7 @@ def ping(self) -> bool:
216218

217219
logger.info("Send Ping to Device.")
218220

219-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_PING)
221+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_PING, addr=module)
220222
self.uart.clear_buffer()
221223

222224
if r.packet_type == OW_ERROR:
@@ -232,7 +234,7 @@ def ping(self) -> bool:
232234
logger.error("Unexpected error during process: %s", e)
233235
raise # Re-raise the exception for the caller to handle
234236

235-
def get_version(self) -> str:
237+
def get_version(self, module:int=1) -> str:
236238
"""
237239
Retrieve the firmware version of the TX device.
238240
@@ -251,9 +253,9 @@ def get_version(self) -> str:
251253
logger.error("TX Device not connected")
252254
return 'v0.0.0'
253255

254-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_VERSION)
256+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_VERSION, addr=module)
255257
self.uart.clear_buffer()
256-
# r.print_packet()
258+
r.print_packet()
257259
if r.data_len == 3:
258260
ver = f'v{r.data[0]}.{r.data[1]}.{r.data[2]}'
259261
else:
@@ -316,7 +318,7 @@ def echo(self, echo_data = None) -> tuple[bytes, int]:
316318
logger.error("Unexpected error during echo process: %s", e)
317319
raise # Re-raise the exception for the caller to handle
318320

319-
def toggle_led(self) -> bool:
321+
def toggle_led(self, module:int=1) -> bool:
320322
"""
321323
Toggle the LED on the TX device.
322324
@@ -332,7 +334,7 @@ def toggle_led(self) -> bool:
332334
logger.error("TX Device not connected")
333335
return False
334336

335-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_TOGGLE_LED)
337+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_TOGGLE_LED, addr=module)
336338
self.uart.clear_buffer()
337339
# r.print_packet()
338340
return True
@@ -345,7 +347,7 @@ def toggle_led(self) -> bool:
345347
logger.error("Unexpected error during process: %s", e)
346348
raise # Re-raise the exception for the caller to handle
347349

348-
def get_hardware_id(self) -> str:
350+
def get_hardware_id(self, module:int=1) -> str:
349351
"""
350352
Retrieve the hardware ID of the TX device.
351353
@@ -364,10 +366,10 @@ def get_hardware_id(self) -> str:
364366
logger.error("TX Device not connected")
365367
return None
366368

367-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_HWID)
369+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_HWID, addr=module)
368370
self.uart.clear_buffer()
369371
# r.print_packet()
370-
if r.data_len == 16:
372+
if r.data_len == HW_ID_DATA_LENGTH:
371373
return r.data.hex()
372374
else:
373375
return None
@@ -379,7 +381,7 @@ def get_hardware_id(self) -> str:
379381
logger.error("Unexpected error during process: %s", e)
380382
raise # Re-raise the exception for the caller to handle
381383

382-
def get_temperature(self) -> float:
384+
def get_temperature(self, module:int=1) -> float:
383385
"""
384386
Retrieve the temperature reading from the TX device.
385387
@@ -399,12 +401,12 @@ def get_temperature(self) -> float:
399401
return 0
400402

401403
# Send the GET_TEMP command
402-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_GET_TEMP)
404+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_GET_TEMP, addr=module)
403405
self.uart.clear_buffer()
404406
# r.print_packet()
405407

406408
# Check if the data length matches a float (4 bytes)
407-
if r.data_len == 4:
409+
if r.data_len == TEMPERATURE_DATA_LENGTH:
408410
# Unpack the float value from the received data (assuming little-endian)
409411
temperature = struct.unpack('<f', r.data)[0]
410412
# Truncate the temperature to 2 decimal places
@@ -420,7 +422,7 @@ def get_temperature(self) -> float:
420422
logger.error("Unexpected error during process: %s", e)
421423
raise # Re-raise the exception for the caller to handle
422424

423-
def get_ambient_temperature(self) -> float:
425+
def get_ambient_temperature(self, module:int=1) -> float:
424426
"""
425427
Retrieve the ambient temperature reading from the TX device.
426428
@@ -440,12 +442,12 @@ def get_ambient_temperature(self) -> float:
440442
return 0
441443

442444
# Send the GET_TEMP command
443-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_GET_AMBIENT)
445+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_GET_AMBIENT, addr=module)
444446
self.uart.clear_buffer()
445447
# r.print_packet()
446448

447449
# Check if the data length matches a float (4 bytes)
448-
if r.data_len == 4:
450+
if r.data_len == TEMPERATURE_DATA_LENGTH:
449451
# Unpack the float value from the received data (assuming little-endian)
450452
temperature = struct.unpack('<f', r.data)[0]
451453
# Truncate the temperature to 2 decimal places
@@ -722,7 +724,7 @@ def stop_trigger(self) -> bool:
722724
logger.error("Unexpected error during process: %s", e)
723725
raise # Re-raise the exception for the caller to handle
724726

725-
def soft_reset(self) -> bool:
727+
def soft_reset(self, module:int=1) -> bool:
726728
"""
727729
Perform a soft reset on the TX device.
728730
@@ -740,7 +742,7 @@ def soft_reset(self) -> bool:
740742
if not self.uart.is_connected():
741743
raise ValueError("TX Device not connected")
742744

743-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_RESET)
745+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_RESET, addr=module)
744746
self.uart.clear_buffer()
745747
# r.print_packet()
746748
if r.packet_type == OW_ERROR:
@@ -756,7 +758,7 @@ def soft_reset(self) -> bool:
756758
logger.error("Unexpected error during process: %s", e)
757759
raise # Re-raise the exception for the caller to handle
758760

759-
def enter_dfu(self) -> bool:
761+
def enter_dfu(self, module:int=1) -> bool:
760762
"""
761763
Perform a soft reset to enter DFU mode on TX device.
762764
@@ -774,7 +776,7 @@ def enter_dfu(self) -> bool:
774776
if not self.uart.is_connected():
775777
raise ValueError("TX Device not connected")
776778

777-
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_DFU)
779+
r = self.uart.send_packet(id=None, packetType=OW_CONTROLLER, command=OW_CMD_DFU, addr=module)
778780
self.uart.clear_buffer()
779781
# r.print_packet()
780782
if r.packet_type == OW_ERROR:
@@ -970,7 +972,7 @@ def write_register(self, identifier:int, address: int, value: int) -> bool:
970972
logger.error("Unexpected error during process: %s", e)
971973
raise # Re-raise the exception for the caller to handle
972974

973-
def read_register(self, address: int) -> int:
975+
def read_register(self, identifier:int, address: int) -> int:
974976
"""
975977
Read a register value from the TX device.
976978
@@ -992,7 +994,7 @@ def read_register(self, address: int) -> int:
992994
raise ValueError("TX Device not connected")
993995

994996
# Validate the identifier
995-
if self.identifier < 0:
997+
if identifier < 0:
996998
raise ValueError("TX Chip address NOT SET")
997999

9981000
# Pack the address into the required format
@@ -1007,13 +1009,13 @@ def read_register(self, address: int) -> int:
10071009
id=None,
10081010
packetType=OW_TX7332,
10091011
command=OW_TX7332_RREG,
1010-
addr=self.identifier,
1012+
addr=identifier,
10111013
data=data
10121014
)
10131015

10141016
# Clear UART buffer after sending the packet
10151017
self.uart.clear_buffer()
1016-
1018+
# r.print_packet()
10171019
# Check for errors in the response
10181020
if r.packet_type == OW_ERROR:
10191021
logger.error("Error reading TX register value")
@@ -1022,14 +1024,16 @@ def read_register(self, address: int) -> int:
10221024
# Verify data length and unpack the register value
10231025
if r.data_len == 4:
10241026
try:
1025-
return struct.unpack('<I', r.data)[0]
1027+
value = struct.unpack('<I', r.data)[0]
10261028
except struct.error as e:
10271029
logger.error(f"Error unpacking register value: {e}")
10281030
return 0
10291031
else:
10301032
logger.error(f"Unexpected data length: {r.data_len}")
10311033
return 0
10321034

1035+
logger.info(f"Successfully read value 0x{value:08X} from register 0x{address:04X}")
1036+
return value
10331037
except ValueError as v:
10341038
logger.error("ValueError: %s", v)
10351039
raise # Re-raise the exception for the caller to handle
@@ -1100,7 +1104,7 @@ def write_block(self, identifier: int, start_address: int, reg_values: List[int]
11001104

11011105
# Clear the UART buffer after sending
11021106
self.uart.clear_buffer()
1103-
1107+
# r.print_packet()
11041108
# Check for errors in the response
11051109
if r.packet_type == OW_ERROR:
11061110
logger.error(f"Error writing TX block at chunk {i}")

0 commit comments

Comments
 (0)