Skip to content

An event-driven class that imitates two SR latches - with 2 set (high,low) values and 1 reset (middle) value.

License

Notifications You must be signed in to change notification settings

surdouski/micropython-SRBand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SRBand

The SRBand class is used to monitor a value within a specified range, triggering events when the value crosses high and low thresholds.

Installation

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

Usage

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())

SRBand Class

SRBand(target: float, high: float, low: float)

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 satisfy low < target < high.

run(new_value: float, trigger_events: True) -> None

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.

update_high(new_high: float) -> None

Updates the _high property, updating the object status; does not trigger events.

update_low(new_low: float) -> None

Updates the _low property, updating the object status; does not trigger events.

update_target(new_target: float) -> None

Updates the _target property, updating the object status; does not trigger events.

SRBandException Class

Custom exception for the SRBand class.

Tests

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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

An event-driven class that imitates two SR latches - with 2 set (high,low) values and 1 reset (middle) value.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages