-
Notifications
You must be signed in to change notification settings - Fork 87
EventIO
EventIO provides basic multi-process event-driven programming for PyBBIO. It includes a simple API for creating background event loops and assigning callback functions to software or hardware events.
The basic structure of an EventIO event is an object that possesses two functions: a 'trigger' and an 'event'. When the event handler signals it to, the event object calls the 'trigger' function. If the return value is False it tells the event handler to continue on and check back later. If the 'trigger' returns True then the event object calls the 'event' function. If the 'event' function returns the EVENT_CONTINUE variable, or True, the event will remain in the loop, otherwise it will be removed.
Example program at PyBBIO/examples/EventIO_test.py
.
##API
Event loops are created using the EventLoop class:
####EventLoop()
Creates an instance of the EventLoop class, which is controlled with the following methods:
####EventLoop.add_event(event)
Adds the given event instance to the event loop (see event classes below).
####EventLoop.start()
Starts the event handler. Once this is called, no new events can be added.
These are the different event classes which may be passed to EventLoop.add_event(). They may be inherited from to create more complex events as long as they have a run() method, as this is what is called by the event handler.
####Event(trigger, event)
Creates an instance of the Event class, which is the most basic of the event classes. It takes two functions; when trigger returns True event is called. If event returns EVENT_CONTINUE the event is put back in the event loop. Otherwise it will only be triggered once.
####DebouncedEvent(trigger, event, debounce_ms)
Creates an instance of the DebouncedEvent class. This is the same as the basic Event class with the addition of debouncing: if an event is triggered and re-added to an event loop by returning EVENT_CONTINUE, the trigger will be ignored for the given number of milliseconds.
####TimedEvent(event, event_time_ms)
Creates an instance of the TimedEvent class, which will call event after approximately the given number of milliseconds has passed. Note that the more events in an event loop, and the longer the event functions take to return, the less accurate this will be.
####DigitalTrigger(digital_pin, trigger_state, event, debounce_ms, pull=0)
This inherits from DebouncedEvent and is triggered when the state on the given digital pin is equal to trigger_state ( HIGH or LOW). It takes care of setting the pin as an input with the given pullup/pulldown resistor setting when it is created.
####AnalogLevel(analog_pin, threshold, event, direction=1, n_points=4)
If direction = 1 this event is triggered when the value on the given analog pin is above the given threshold. If direction = -1 it is triggered when below threshold. It performs a running average using n_points samples to smooth hysteresis, or uses a single sample if n_points = 1.