i2c_playground.py 862 B

12345678910111213141516171819202122232425262728293031323334
  1. from robot_control.src.api.i2c_handler import I2C
  2. from robot_control.src.vendor.mcp3428 import MCP3428
  3. from time import sleep
  4. import asyncio
  5. async def main():
  6. # Initialize with multiple channels (1 through 4)
  7. i2c = I2C(device_class=MCP3428, gain=1, resolution=12)
  8. i2c.initialize()
  9. counter = 0
  10. while True:
  11. # Read channel 4 every iteration
  12. selection = [4]
  13. # Read channel 3 every 4 seconds
  14. if counter % 4 == 0:
  15. selection.append(3)
  16. # Read channel 1 every 10 iterations
  17. if counter % 10 == 0:
  18. selection.append(1)
  19. readings = await i2c.read_channels(selection)
  20. for channel, value in readings.items():
  21. print(f"Channel {channel}: {value}")
  22. counter += 1
  23. await asyncio.sleep(1)
  24. if __name__ == "__main__":
  25. asyncio.run(main())