- helpers
- movement
- modulino
- distance
- buzzer
- pixels
- knob
- buttons
- ModulinoButtonsLED
- ModulinoButtons
- __init__
- led_a
- led_b
- led_c
- set_led_status
- long_press_duration
- long_press_duration
- on_button_a_press
- on_button_a_press
- on_button_a_release
- on_button_a_release
- on_button_a_long_press
- on_button_a_long_press
- on_button_b_press
- on_button_b_press
- on_button_b_release
- on_button_b_release
- on_button_b_long_press
- on_button_b_long_press
- on_button_c_press
- on_button_c_press
- on_button_c_release
- on_button_c_release
- on_button_c_long_press
- on_button_c_long_press
- update
- is_pressed
- button_a_pressed
- button_b_pressed
- button_c_pressed
- thermo
def map_value(x: float | int, in_min: float | int, in_max: float | int,
out_min: float | int, out_max: float | int) -> float | int
Maps a value from one range to another.
Arguments:
x
- The value to map.in_min
- The minimum value of the input range.in_max
- The maximum value of the input range.out_min
- The minimum value of the output range.out_max
- The maximum value of the output range.
Returns:
The mapped value as a float or int depending on the input.
def map_value_int(x: float | int, in_min: float | int, in_max: float | int,
out_min: float | int, out_max: float | int) -> int
Maps a value from one range to another and returns an integer.
Arguments:
x
- The value to map.in_min
- The minimum value of the input range.in_max
- The maximum value of the input range.out_min
- The minimum value of the output range.out_max
- The maximum value of the output range.
Returns:
The mapped value as an integer.
A named tuple to store the x, y, and z values of the movement sensors.
class ModulinoMovement(Modulino)
Class to interact with the movement sensor (IMU) of the Modulino Movement.
def __init__(i2c_bus=None, address: int | None = None) -> None
Initializes the Modulino Movement.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The I2C address of the module. If not provided, the default address will be used.
@property
def accelerometer() -> MovementValues
Returns:
MovementValues
- The acceleration values in the x, y, and z axes. These values can be accessed as .x, .y, and .z properties or by using the index operator for tuple unpacking.
@property
def gyro() -> MovementValues
Returns:
MovementValues
- The gyroscope values in the x, y, and z axes. These values can be accessed as .x, .y, and .z properties or by using the index operator for tuple unpacking.
class Modulino()
Base class for all Modulino devices.
A list of default addresses that the modulino can have. This list needs to be overridden derived classes.
Determines if the default addresses need to be converted from 8-bit to 7-bit. Addresses of modulinos without native I2C modules need to be converted. This class variable needs to be overridden in derived classes.
def __init__(i2c_bus: I2C = None, address: int = None, name: str = None)
Initializes the Modulino object with the given i2c bus and address. If the address is not provided, the device will try to auto discover it. If the address is provided, the device will check if it is connected to the bus. If the address is 8-bit, it will be converted to 7-bit. If no bus is provided, the default bus will be used if available.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The address of the device. If not provided, the device will try to auto discover it.name
str - The name of the device.
def discover(default_addresses: list[int]) -> int | None
Tries to find the given modulino device in the device chain based on the pre-defined default addresses. The first address found will be returned. If the address has been changed to a custom one it won't be found with this function.
Returns:
int | None: The address of the device if found, None otherwise.
def __bool__() -> bool
Boolean cast operator to determine if the given i2c device has a correct address and if the bus is defined. In case of auto discovery this also means that the device was found on the bus because otherwise the address would be None.
@property
def connected() -> bool
Determines if the given modulino is connected to the i2c bus.
@property
def pin_strap_address() -> int | None
Returns the pin strap i2c address of the modulino. This address is set via resistors on the modulino board. Since all modulinos generally use the same firmware, the pinstrap address is needed to determine the type of the modulino at boot time, so it know what to do. At boot it checks the internal flash in case its address has been overridden by the user which would take precedence.
Returns:
int | None: The pin strap address of the modulino.
@property
def device_type() -> str | None
Returns the type of the modulino based on the pinstrap address as a string.
def change_address(new_address: int)
Sets the address of the i2c device to the given value. This is only supported on Modulinos that have a microcontroller.
def enter_bootloader()
Enters the I2C bootloader of the device. This is only supported on Modulinos that have a microcontroller.
Returns:
bool
- True if the device entered bootloader mode, False otherwise.
def read(amount_of_bytes: int) -> bytes | None
Reads the given amount of bytes from the i2c device and returns the data. It skips the first byte which is the pinstrap address.
Returns:
bytes | None: The data read from the device.
def write(data_buffer: bytearray) -> bool
Writes the given buffer to the i2c device.
Arguments:
data_buffer
bytearray - The data to be written to the device.
Returns:
bool
- True if the data was written successfully, False otherwise.
@property
def has_default_address() -> bool
Determines if the given modulino has a default address or if a custom one was set.
@staticmethod
def available_devices(bus: I2C = None) -> list[Modulino]
Finds all devices on the i2c bus and returns them as a list of Modulino objects.
Arguments:
bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.
Returns:
list
- A list of Modulino objects.
@staticmethod
def reset_bus(i2c_bus: I2C) -> I2C
Resets the i2c bus. This is useful when the bus is in an unknown state. The modulinos that are equipped with a micro controller use DMA operations. If the host board does a reset during such operation it can make the bus get stuck.
Returns:
I2C
- A new i2c bus object after resetting the bus.
class ModulinoDistance(Modulino)
Class to interact with the distance sensor of the Modulino Distance.
def __init__(i2c_bus=None, address: int | None = None) -> None
Initializes the Modulino Distance.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The I2C address of the module. If not provided, the default address will be used.
@property
def distance() -> int
Returns:
int
- The distance in centimeters.
class ModulinoBuzzer(Modulino)
Class to play tones on the piezo element of the Modulino Buzzer. Predefined notes are available in the NOTES dictionary e.g. ModulinoBuzzer.NOTES["C4"]
Dictionary with the notes and their corresponding frequencies. The supported notes are defined as follows:
- FS3, G3, GS3, A3, AS3, B3
- C4, CS4, D4, DS4, E4, F4, FS4, G4, GS4, A4, AS4, B4
- C5, CS5, D5, DS5, E5, F5, FS5, G5, GS5, A5, AS5, B5
- C6, CS6, D6, DS6, E6, F6, FS6, G6, GS6, A6, AS6, B6
- C7, CS7, D7, DS7, E7, F7, FS7, G7, GS7, A7, AS7, B7
- C8, CS8, D8, DS8
- REST (Silence)
def __init__(i2c_bus=None, address=None)
Initializes the Modulino Buzzer.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The I2C address of the module. If not provided, the default address will be used.
def tone(frequency: int,
lenght_ms: int = 0xFFFF,
blocking: bool = False) -> None
Plays a tone with the given frequency and duration. If blocking is set to True, the function will wait until the tone is finished.
Arguments:
frequency
- The frequency of the tone in Hz (freuqencies below 180 Hz are not supported)lenght_ms
- The duration of the tone in milliseconds. If omitted, the tone will play indefinitelyblocking
- If set to True, the function will wait until the tone is finished
def no_tone() -> None
Stops the current tone from playing.
class ModulinoColor()
Class to represent an RGB color. It comes with predefined colors:
- RED
- GREEN
- BLUE
- YELLOW
- CYAN
- VIOLET
- WHITE
They can be accessed e.g. as ModulinoColor.RED
def __init__(r: int, g: int, b: int)
Initializes the color with the given RGB values.
Arguments:
r
int - The red value of the color.g
int - The green value of the color.b
int - The blue value of the color.
def __int__() -> int
Return the 32-bit integer representation of the color. Used bits: 8 to 15 for blue, 16 to 23 for green, 24 to 31 for red.
class ModulinoPixels(Modulino)
Class to interact with the LEDs of the Modulino Pixels.
def __init__(i2c_bus=None, address=None)
Initializes the Modulino Pixels.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The I2C address of the module. If not provided, the default address will be used.
def set_range_rgb(index_from: int,
index_to: int,
r: int,
g: int,
b: int,
brightness: int = 100) -> 'ModulinoPixels'
Sets the color of the LEDs in the given range to the given RGB values.
Arguments:
index_from
int - The starting index of the range.index_to
int - The ending index (inclusive) of the range.r
int - The red value of the color.g
int - The green value of the color.b
int - The blue value of the color.brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def set_range_color(index_from: int,
index_to: int,
color: ModulinoColor,
brightness: int = 100) -> 'ModulinoPixels'
Sets the color of the LEDs in the given range to the given color.
Arguments:
index_from
int - The starting index of the range.index_to
int - The ending index (inclusive) of the range.color
ModulinoColor - The color of the LEDs.brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def set_all_rgb(r: int,
g: int,
b: int,
brightness: int = 100) -> 'ModulinoPixels'
Sets the color of all the LEDs to the given RGB values.
Arguments:
r
int - The red value of the color.g
int - The green value of the color.b
int - The blue value of the color.brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def set_all_color(color: ModulinoColor,
brightness: int = 100) -> 'ModulinoPixels'
Sets the color of all the LEDs to the given color.
Arguments:
color
ModulinoColor - The color of the LEDs.brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def set_color(idx: int,
rgb: ModulinoColor,
brightness: int = 100) -> 'ModulinoPixels'
Sets the color of the given LED index to the given color.
Arguments:
idx
int - The index of the LED (0..7).rgb
ModulinoColor - The color of the LED.brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def set_rgb(idx: int,
r: int,
g: int,
b: int,
brightness: int = 100) -> 'ModulinoPixels'
Set the color of the given LED index to the given RGB values.
Arguments:
idx
int - The index of the LED (0..7).r
int - The red value of the color.g
int - The green value of the color.b
int - The blue value of the color.brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def set_brightness(idx: int, brightness: int) -> 'ModulinoPixels'
Sets the brightness of the given LED index.
Arguments:
idx
int - The index of the LED (0..7).brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def set_all_brightness(brightness: int) -> 'ModulinoPixels'
Sets the brightness of all the LEDs.
Arguments:
brightness
int - The brightness of the LED. It should be a value between 0 and 100.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def clear(idx: int) -> 'ModulinoPixels'
Turns off the LED at the given index.
Arguments:
idx
int - The index of the LED (0..7).
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def clear_range(start: int, end: int) -> 'ModulinoPixels'
Turns off the LEDs in the given range.
Arguments:
start
int - The starting index of the range.end
int - The ending index (inclusive) of the range.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def clear_all() -> 'ModulinoPixels'
Turns all the LEDs off.
Returns:
ModulinoPixels
- The object itself. Allows for daisy chaining of methods.
def show() -> None
Applies the changes to the LEDs. This function needs to be called after any changes to the LEDs. Otherwise, the changes will not be visible.
class ModulinoKnob(Modulino)
Class to interact with the rotary encoder of the Modulinio Knob.
def __init__(i2c_bus=None, address=None)
Initializes the Modulino Knob.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The I2C address of the module. If not provided, the default address will be used.
def reset() -> None
Resets the encoder value to 0.
def update() -> bool
Reads new data from the Modulino and calls the corresponding callbacks if the encoder value or pressed status has changed.
Returns:
bool
- True if the encoder value or pressed status has changed.
@property
def range() -> tuple[int, int]
Returns the range of the encoder value.
@range.setter
def range(value: tuple[int, int]) -> None
Sets the range of the encoder value.
Arguments:
value
tuple - A tuple with two integers representing the minimum and maximum values of the range.
@property
def on_rotate_clockwise()
Returns the callback for the rotate clockwise event.
@on_rotate_clockwise.setter
def on_rotate_clockwise(value) -> None
Sets the callback for the rotate clockwise event.
Arguments:
value
function - The function to be called when the encoder is rotated clockwise.
@property
def on_rotate_counter_clockwise()
Returns the callback for the rotate counter clockwise event.
@on_rotate_counter_clockwise.setter
def on_rotate_counter_clockwise(value) -> None
Sets the callback for the rotate counter clockwise event.
Arguments:
value
function - The function to be called when the encoder is rotated counter clockwise.
@property
def on_press()
Returns the callback for the press event.
@on_press.setter
def on_press(value) -> None
Sets the callback for the press event.
Arguments:
value
function - The function to be called when the encoder is pressed.
@property
def on_release()
Returns the callback for the release event.
@on_release.setter
def on_release(value) -> None
Sets the callback for the release event.
Arguments:
value
function - The function to be called when the encoder is released.
@property
def value() -> int
Returns the current value of the encoder.
@value.setter
def value(new_value: int) -> None
Sets the value of the encoder. This overrides the previous value.
Arguments:
new_value
int - The new value of the encoder.
@property
def pressed() -> bool
Returns the pressed status of the encoder.
class ModulinoButtonsLED()
Class to interact with the LEDs of the Modulino Buttons.
def on()
Turns the LED on.
def off()
Turns the LED off.
@property
def value()
Returns the value of the LED (1 for on, 0 for off).
@value.setter
def value(value)
Sets the value of the LED (1 for on, 0 for off). Calling this method will update the physical status of the LED immediately.
class ModulinoButtons(Modulino)
Class to interact with the buttons of the Modulino Buttons.
def __init__(i2c_bus=None, address=None)
Initializes the Modulino Buttons.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The I2C address of the module. If not provided, the default address will be used.
@property
def led_a() -> ModulinoButtonsLED
Returns the LED A object of the module.
@property
def led_b() -> ModulinoButtonsLED
Returns the LED B object of the module.
@property
def led_c() -> ModulinoButtonsLED
Returns the LED C object of the module.
def set_led_status(a: bool, b: bool, c: bool) -> None
Turn on or off the button LEDs according to the given status.
Arguments:
a
bool - The status of the LED A.b
bool - The status of the LED B.c
bool - The status of the LED C.
@property
def long_press_duration() -> int
Returns the duration in milliseconds that the button must be pressed to trigger the long press event
@long_press_duration.setter
def long_press_duration(value: int) -> None
Sets the duration in milliseconds that the button must be pressed to trigger the long press event
@property
def on_button_a_press()
Returns the callback for the press event of button A.
@on_button_a_press.setter
def on_button_a_press(value) -> None
Sets the callback for the press event of button A.
@property
def on_button_a_release()
Returns the callback for the release event of button A.
@on_button_a_release.setter
def on_button_a_release(value) -> None
Sets the callback for the release event of button A.
@property
def on_button_a_long_press()
Returns the callback for the long press event of button A.
@on_button_a_long_press.setter
def on_button_a_long_press(value) -> None
Sets the callback for the long press event of button A.
@property
def on_button_b_press()
Returns the callback for the press event of button B.
@on_button_b_press.setter
def on_button_b_press(value) -> None
Sets the callback for the press event of button B.
@property
def on_button_b_release()
Returns the callback for the release event of button B.
@on_button_b_release.setter
def on_button_b_release(value) -> None
Sets the callback for the release event of button B.
@property
def on_button_b_long_press()
Returns the callback for the long press event of button B.
@on_button_b_long_press.setter
def on_button_b_long_press(value) -> None
Sets the callback for the long press event of button B.
@property
def on_button_c_press()
Returns the callback for the press event of button C.
@on_button_c_press.setter
def on_button_c_press(value) -> None
Sets the callback for the press event of button C.
@property
def on_button_c_release()
Returns the callback for the release event of button C.
@on_button_c_release.setter
def on_button_c_release(value) -> None
Sets the callback for the release event of button C.
@property
def on_button_c_long_press()
Returns the callback for the long press event of button C.
@on_button_c_long_press.setter
def on_button_c_long_press(value) -> None
Sets the callback for the long press event of button C.
def update() -> bool
Update the button status and call the corresponding callbacks. Returns True if any of the buttons has changed its state.
Returns:
bool
- True if any of the buttons has changed its state.
def is_pressed(index: int) -> bool
Returns True if the button at the given index is currently pressed.
Arguments:
index
int - The index of the button. A = 0, B = 1, C = 2.
@property
def button_a_pressed() -> bool
Returns True if button A is currently pressed.
@property
def button_b_pressed() -> bool
Returns True if button B is currently pressed.
@property
def button_c_pressed() -> bool
Returns True if button C is currently pressed.
A named tuple to store the temperature and relative humidity measurements.
class ModulinoThermo(Modulino)
Class to interact with the temperature and humidity sensor of the Modulino Thermo.
def __init__(i2c_bus: I2C = None, address: int = DEFAULT_ADDRESS) -> None
Initializes the Modulino Thermo.
Arguments:
i2c_bus
I2C - The I2C bus to use. If not provided, the default I2C bus will be used.address
int - The I2C address of the module. If not provided, the default address will be used.
@property
def measurements() -> Measurement
Return Temperature and Relative Humidity or None if the data is stalled
@property
def relative_humidity() -> float
The current relative humidity in % rH
@property
def temperature() -> float
The current temperature in Celsius