i2c_playground.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import smbus2
  2. def main():
  3. # Initialize configuration
  4. config = {
  5. 'i2c': {
  6. 'bus': 1,
  7. 'debug': False
  8. }
  9. }
  10. # Create I2C service instance
  11. i2c_service = I2CService(config)
  12. # Test parameters
  13. i2c_address = 0x48
  14. num_slots = 1
  15. try:
  16. # Request status list from the device
  17. # status_list = i2c_service.request_status_list(i2c_address, num_slots)
  18. status_list = i2c_service.request_measure_values(i2c_address, 0)
  19. # status_list = i2c_service.send_cell_limits(i2c_address, 0, [2500, 4200, 500, 2800, 200])
  20. print(f"Status list received: {status_list}")
  21. except Exception as e:
  22. print(f"Error occurred: {str(e)}")
  23. class I2CService:
  24. status_register = 0x01
  25. cell_data_register = 0x02
  26. battery_limit_register = 0x03
  27. def __init__(self, config: dict):
  28. self.config = config
  29. bus_number = config.get('i2c', {}).get('bus', 1)
  30. self.bus = smbus2.SMBus(bus_number)
  31. def request_status_list(self, i2c_adress: int, num_slots: int):
  32. """Request the status of a all slots."""
  33. print(f"Requesting status list (i2c_adress: {i2c_adress}, register: {self.status_register})")
  34. status_list = self.bus.read_i2c_block_data(i2c_adress, self.status_register, num_slots)
  35. print(f"Received status list: {status_list} (i2c_adress: {i2c_adress})")
  36. return status_list
  37. def request_measure_values(self, i2c_adress: int, slot_id: int) -> list[int]:
  38. """Request the cell values of a specific slot.
  39. """
  40. print(f"Requesting measure values (i2c_adress: {i2c_adress}, slot_id: {slot_id})")
  41. slot_request = self.cell_data_register << 4 | slot_id
  42. response = self.bus.read_i2c_block_data(i2c_adress, slot_request, 8)
  43. print(f"Received response: {response} (i2c_adress: {i2c_adress}, slot_id: {slot_id})")
  44. # slot = int.from_bytes(response[0], byteorder='little')
  45. slot = response[0]
  46. voltage = int.from_bytes(response[2:4], byteorder='little')
  47. current = int.from_bytes(response[4:6], byteorder='little')
  48. temperature = int.from_bytes(response[6:8], byteorder='little')
  49. print(f"Decoded values: slot={slot}, voltage={voltage}, current={current}, temperature={temperature}")
  50. return response
  51. def send_cell_limits(self, i2c_adress: int, slot_id: int, limits: list[int]) -> bool:
  52. """Send the battery limits to the device.
  53. Args:
  54. i2c_adress: Device address
  55. slot_id: Target slot (0-15)
  56. limits: List of 16-bit values [max_voltage, min_voltage, max_current, min_current, max_temp]
  57. """
  58. # Convert each 16-bit value to 2 bytes in little-endian order
  59. msg_bytes = bytearray([slot_id]) # Start with slot_id
  60. for value in limits:
  61. # Convert each value to 2 bytes (16-bit) in little-endian order
  62. value_bytes = value.to_bytes(2, byteorder='little')
  63. msg_bytes.extend(value_bytes)
  64. self.bus.write_i2c_block_data(i2c_adress, self.battery_limit_register, msg_bytes)
  65. print(f"Sent msg (bytes): {list(msg_bytes)} (i2c_adress: {i2c_adress}, slot_id: {slot_id})")
  66. return True
  67. if __name__ == "__main__":
  68. main()