| 12345678910111213141516171819202122232425262728 |
- import smbus2 as smbus
- from ..vendor.mcp3428 import MCP3428, MCP3428_DEFAULT_ADDRESS
- from time import sleep
- class I2C:
- def __init__(self, channels, gain = 1, resolution = 12):
- self.channels = channels if isinstance(channels, list) else [channels]
- self.gain = gain
- self.resolution = resolution
- self.mcp = None
- def initialize(self):
- bus = smbus.SMBus(1)
- self.mcp = MCP3428(bus, MCP3428_DEFAULT_ADDRESS)
-
- def read_channel(self, channel):
- if self.mcp is None:
- raise RuntimeError("I2C not initialized")
- self.mcp.config_command(channel, self.gain, self.resolution)
- sleep(0.01)
- return self.mcp.read_adc()
-
- def read(self):
- return {channel: self.read_channel(channel) for channel in self.channels}
|