|
15 | 15 | "Generic ESP32S3 module" : I2CInterface("hw", 0, None, None),
|
16 | 16 | }
|
17 | 17 |
|
| 18 | +PINSTRAP_ADDRESS_MAP = { |
| 19 | + 0x3C: "Buzzer", |
| 20 | + 0x7C: "Buttons", |
| 21 | + 0x76: "Knob", |
| 22 | + 0x74: "Knob", |
| 23 | + 0x6C: "Pixels" |
| 24 | +} |
| 25 | + |
18 | 26 | class I2CHelper:
|
19 | 27 | """
|
20 | 28 | A helper class for interacting with I2C devices on supported boards.
|
@@ -174,6 +182,31 @@ def pin_strap_address(self):
|
174 | 182 | # The first byte is always the pinstrap address
|
175 | 183 | return data[0]
|
176 | 184 |
|
| 185 | + @property |
| 186 | + def device_type(self): |
| 187 | + """ |
| 188 | + Returns the type of the modulino based on the pinstrap address. |
| 189 | + """ |
| 190 | + return PINSTRAP_ADDRESS_MAP.get(self.pin_strap_address, None) |
| 191 | + |
| 192 | + def change_address(self, new_address): |
| 193 | + """ |
| 194 | + Sets the address of the i2c device to the given value. |
| 195 | + """ |
| 196 | + # TODO: Check if device supports this feature by looking at the type |
| 197 | + |
| 198 | + data = bytearray(40) |
| 199 | + # Set the first two bytes to 'C' and 'F' followed by the new address |
| 200 | + data[0:2] = b'CF' |
| 201 | + data[2] = new_address * 2 |
| 202 | + |
| 203 | + try: |
| 204 | + self.write(data) |
| 205 | + except OSError: |
| 206 | + pass # Device resets immediately and causes ENODEV to be thrown which is expected |
| 207 | + |
| 208 | + self.address = new_address |
| 209 | + |
177 | 210 | def read(self, amount_of_bytes):
|
178 | 211 | """
|
179 | 212 | Reads the given amount of bytes from the i2c device and returns the data.
|
@@ -207,6 +240,19 @@ def has_default_address(self):
|
207 | 240 | """
|
208 | 241 | return self.address in self.default_addresses
|
209 | 242 |
|
| 243 | + @staticmethod |
| 244 | + def available_devices(): |
| 245 | + """ |
| 246 | + Finds all devices on the i2c bus and returns a list of Modulino objects. |
| 247 | + """ |
| 248 | + bus = I2CHelper.get_interface() |
| 249 | + device_addresses = bus.scan() |
| 250 | + devices = [] |
| 251 | + for address in device_addresses: |
| 252 | + device = Modulino(i2c_bus=bus, address=address) |
| 253 | + devices.append(device) |
| 254 | + return devices |
| 255 | + |
210 | 256 | @staticmethod
|
211 | 257 | def reset_bus(i2c_bus):
|
212 | 258 | """
|
|
0 commit comments