Skip to content

Commit 79308f0

Browse files
committedOct 24, 2024
Add logic to change device address
1 parent d17b8c1 commit 79308f0

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
 

‎src/modulino/modulino.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
"Generic ESP32S3 module" : I2CInterface("hw", 0, None, None),
1616
}
1717

18+
PINSTRAP_ADDRESS_MAP = {
19+
0x3C: "Buzzer",
20+
0x7C: "Buttons",
21+
0x76: "Knob",
22+
0x74: "Knob",
23+
0x6C: "Pixels"
24+
}
25+
1826
class I2CHelper:
1927
"""
2028
A helper class for interacting with I2C devices on supported boards.
@@ -174,6 +182,31 @@ def pin_strap_address(self):
174182
# The first byte is always the pinstrap address
175183
return data[0]
176184

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+
177210
def read(self, amount_of_bytes):
178211
"""
179212
Reads the given amount of bytes from the i2c device and returns the data.
@@ -207,6 +240,19 @@ def has_default_address(self):
207240
"""
208241
return self.address in self.default_addresses
209242

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+
210256
@staticmethod
211257
def reset_bus(i2c_bus):
212258
"""

0 commit comments

Comments
 (0)
Please sign in to comment.