-
Notifications
You must be signed in to change notification settings - Fork 54
add fri3d badge 2026 expander driver #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ThomasFarstrike
merged 1 commit into
MicroPythonOS:main
from
bertouttier:feat/add_2026_expander_driver
Mar 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import struct | ||
|
|
||
| from micropython import const | ||
| from machine import I2C | ||
|
|
||
| # common registers | ||
| _REG_VERSION = const(0x00) | ||
|
|
||
|
|
||
| class Device: | ||
| """Fri3d I2C device.""" | ||
|
|
||
| def __init__(self, i2c_bus: I2C, address: int): | ||
| """Read from a sensor on the given I2C bus, at the given address.""" | ||
| self.i2c = i2c_bus | ||
| self.address = address | ||
|
|
||
| def _read(self, format, reg, amount): | ||
| return struct.unpack(format, self.i2c.readfrom_mem(self.address, reg, amount)) | ||
|
|
||
| def _write(self, reg, value): | ||
| self.i2c.writeto_mem(self.address, reg, value, addrsize=8) | ||
|
|
||
| @property | ||
| def version(self) -> tuple[int, int, int]: | ||
| return self._read("BBB", _REG_VERSION, 3) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import struct | ||
|
|
||
| from micropython import const | ||
| from machine import I2C, Pin | ||
|
|
||
| from .device import Device | ||
|
|
||
| # registers | ||
| _EXPANDER_REG_INPUTS = const(0x04) | ||
| _EXPANDER_REG_ANALOG = const(0x06) | ||
| _EXPANDER_REG_LCD_BRIGHTNESS = const(0x12) | ||
| _EXPANDER_REG_DEBUG_LED = const(0x14) | ||
| _EXPANDER_REG_CONFIG = const(0x16) | ||
|
|
||
| _EXPANDER_I2CADDR_DEFAULT = const(0x50) | ||
|
|
||
|
|
||
| class Expander(Device): | ||
| """Fri3d Badge 2026 expander MCU.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| i2c_bus: I2C, | ||
| address: int = _EXPANDER_I2CADDR_DEFAULT, | ||
| int_pin: Pin = None, | ||
| ): | ||
| """Read from a sensor on the given I2C bus, at the given address.""" | ||
| Device.__init__(self, i2c_bus, address) | ||
| self.use_interrupt = False | ||
| if int_pin: | ||
| self.use_interrupt = True | ||
| self._rx_buf = bytearray(2) | ||
| self._rx_mv = memoryview(self._rx_buf) | ||
| self.int_pin = int_pin | ||
| self.i2c.readfrom_mem_into(self.address, _EXPANDER_REG_INPUTS, self._rx_mv) | ||
| self.int_pin.irq(trigger=Pin.IRQ_RISING, handler=self.int_callback) | ||
|
|
||
| def int_callback(self, p): | ||
| self.i2c.readfrom_mem_into(self.address, _EXPANDER_REG_INPUTS, self._rx_mv) | ||
|
|
||
| @property | ||
| def analog(self) -> tuple[int, int, int, int, int, int]: | ||
| """Read the analog inputs: ain1, ain0, battery_monitor, usb_monitor, joystick_y, joystick_x""" | ||
| return self._read("<HHHHHH", _EXPANDER_REG_ANALOG, 12) | ||
|
|
||
| @property | ||
| def digital( | ||
| self, | ||
| ) -> tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool]: | ||
| """Read the digital inputs: usb_plugged, joy_right, joy_left, joy_down, joy_up, button_menu, button_b, button_a, button_y, button_x, charger_standby, charger_charging""" | ||
| if self.use_interrupt: | ||
| inputs = struct.unpack("<H", self._rx_buf)[0] | ||
| else: | ||
| inputs = self._read("<H", _EXPANDER_REG_INPUTS, 2)[0] | ||
| return tuple([bool(int(digit)) for digit in "{:016b}".format(inputs)[4:]]) | ||
|
|
||
| @property | ||
| def lcd_brightness(self) -> int: | ||
| """Read the LCD brightness state (0-100)""" | ||
| return self._read("<H", _EXPANDER_REG_LCD_BRIGHTNESS, 2)[0] | ||
|
|
||
| @lcd_brightness.setter | ||
| def lcd_brightness(self, value: int): | ||
| """Set the LCD brightness (0-100)""" | ||
| if value >= 0 and value <= 100: | ||
| self._write(_EXPANDER_REG_LCD_BRIGHTNESS, struct.pack("<H", value)) | ||
|
|
||
| @property | ||
| def debug_led(self) -> int: | ||
| """Read the Debug LED state (0-100)""" | ||
| return self._read("<H", _EXPANDER_REG_DEBUG_LED, 2)[0] | ||
|
|
||
| @debug_led.setter | ||
| def debug_led(self, value: int): | ||
| """Set the Debug LED (0-100)""" | ||
| if value >= 0 and value <= 100: | ||
| self._write(_EXPANDER_REG_DEBUG_LED, struct.pack("<H", value)) | ||
|
|
||
| @property | ||
| def config(self) -> tuple[bool, bool, bool]: | ||
| """Read the configuration bits: reboot, lcd_reset, aux_power""" | ||
| config = self._read("B", _EXPANDER_REG_CONFIG, 1)[0] | ||
| return tuple([bool(int(digit)) for digit in "{:08b}".format(config)[5:]]) | ||
|
|
||
| @config.setter | ||
| def config(self, value: int): | ||
| """set the configuration byte""" | ||
| if value >= 0 and value <= 0xFF: | ||
| self._write(_EXPANDER_REG_CONFIG, struct.pack("B", value)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ThomasFarstrike should we use a
i2c.I2C.Device()here for thread-safety?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm I'm not sure...