The SRBand
class is used to monitor a value within a specified range, triggering events when the value crosses high and low thresholds.
You can install SRBand from the REPL with mip.
# micropython REPL
import mip
mip.install("github:surdouski/micropython-SRBand")
Alternatively, you can install it by using mpremote if you don't have network connectivity on device.
$ mpremote mip install github:surdouski/micropython-SRBand
Here is an example of how to use the SRBand
class:
import asyncio
from sr_band import SRBand
async def main():
# Initialize the SRBand with target, high, and low values
band = SRBand(target=50, high=70, low=30)
# Setup some tasks to trigger when the event is set
# Don't forget to clear your events inside the tasks
task1 = asyncio.create_task(do_something(band.fall_event))
task2 = asyncio.create_task(do_something(band.rise_event))
# This should trigger the fall_event
band.run(75) # internal status: SET_FALL
await asyncio.sleep(1)
band.run(25) # This should trigger the rise_event
await asyncio.sleep(1) # internal status: SET_RISE
# These will trigger 3 more rise_event:
band.run(29) # Successive updates after a triggered; internal status: SET_RISE
band.run(31) # event, but while still below the; internal status: SET_RISE
band.run(49) # target, will continue triggering that event. internal status: SET_RISE
# No event is triggered here, as it has passed (or is equal to) the reset point.
band.run(51) # internal status: IDLE
# cleanup tasks
task1.cancel()
task2.cancel()
async def do_something(event: asyncio.Event):
while True:
await event.wait()
event.clear()
# do stuff here
# Run the async main function
asyncio.run(main())
Initializes an SRBand
object with specified target, high, and low thresholds.
Parameters:
target
(float): The target value.high
(float): The high threshold.low
(float): The low threshold.
Raises:
SRBandException
: If the provided arguments do not satisfylow < target < high
.
Runs the current value and triggers events if thresholds are crossed.
Parameters:
new_value
(float): The new value to update.trigger_events
(bool): If events should be set.
Updates the _high
property, updating the object status; does not trigger events.
Updates the _low
property, updating the object status; does not trigger events.
Updates the _target
property, updating the object status; does not trigger events.
Custom exception for the SRBand
class.
To run tests, do the following.
# install unittest, mounting the volume locally
$ docker run --rm -v $(pwd)/lib:/root/.micropython/lib micropython/unix micropython -m mip install unittest
# run the test, using the mounted volume for the unittest deps
$ docker run --rm -v $(pwd):/code -v $(pwd)/lib:/root/.micropython/lib -w /code micropython/unix micropython test.py
If you want to edit tests, you only need to run the last command again to see results.
This project is licensed under the MIT License. See the LICENSE file for details.