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:
|
A source which sets up a pin and polls its value. |
|
A source which sets up a Singal on a pin and polls its value. |
|
A source which sets up an ADC and polls its value. |
|
A source triggered by an IRQ on a pin. |
and the following sinks:
|
A sink that sets the value on a pin. |
|
A sink that sets the value of a signal. |
|
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:
|
Poll the value of a real-time clock periodically. |
|
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.