Machine Classes#

The classes definied in ultimo_machine handle interaction with standard hardware interfaces provided by microcontrollers: GPIO pins, ADC, PWM, timers, etc. These are generally sources or sinks.

GPIO#

The ultimo_machine.gpio module contains sources and sinks based on GPIO pins and related functionality. The module provides the following sources:

PollPin(pin_id, pull[, interval])

A source which sets up a pin and polls its value.

PollSignal(pin_id, pull[, invert, interval])

A source which sets up a Singal on a pin and polls its value.

PollADC(pin_id[, interval])

A source which sets up an ADC and polls its value.

PinInterrupt(pin_id, pull[, trigger])

A source triggered by an IRQ on a pin.

and the following sinks:

PinSink(pin_id, pull[, source])

A sink that sets the value on a pin.

SignalSink(pin_id, pull[, invert, source])

A sink that sets the value of a signal.

PWMSink(pin_id, frequency[, duty_u16, source])

A sink that sets pulse-width modulation on a pin.

All of these expect a pin ID to know which pin they should use. The “Pin” and “Signal” classes need to know whether the pin is pulled up or down and emit or expect boolean values.

The PollADC produces unsigned 16-bit integer values (ie. 0-65535) and PWMSink expects to consume values in that range which are used to set the duty cycle. The PWMSink also needs to know the frequency with which to drive the pulses, and can be given an optional initial duty cycle.

The PinInterrupt class needs to know whether the pin is pulled up or down and what pin events should trigger it (it defaults to machine.Pin.IRQ_RISING). When triggered it emits the value of the pin at the time when the asyncio callback is run, which may or may not match the value of the pin at the time that the interrupt happened. The class also provides an async context manager that handles setting and removing the interrupt handler on the pin. Typical usage looks something like:

async with PinInterrupt(PIN_ID, Pin.PULL_UP, Pin.IRQ_FALLING) as interrupt:
    # do something with the interrupt
    ...

Time#

The ultimo_machine.gpio module provides the following time-related sources:

PollRTC([rtc_id, datetime, interval])

Poll the value of a real-time clock periodically.

TimerInterrupt(timer_id[, mode, freq, period])

Schedule an timer-based interrupt source.

The PollRTC class can be passed the ID of the RTC to use along with an initial datetime tuple (if supported by the hardware) and the polling interval. The values emitted are datetime tuples as returned by the machine.RTC.datetime() method.

The TimerInterrupt class must be passed a Timer ID, along with the timing mode (which defaults to machine.Timer.PERIODIC) and either the frequency or period. The timer’s iterator emits a True value whenever it is triggered. It also can be used as a context manager to set and remove the interrupt handler. Typical usage looks something like:

async with TimerInterrupt(TIMER_ID, freq=10) as interrupt:
    # do something with the interrupt
    ...

Note

Because the timer is calling back via asyncio, any behaviour depending on the interrupt will incur latency from the asyncio scheduling, and so it’s clear that this class provides much, if any, advantage over delaying using uasyncio.sleep(), particularly for one-shot timers.