Schmitt Trigger

The schmitt_trigger.py module provides a set of classes for implementing Schmitt Triggers. A Schmitt Trigger is a comparator circuit with hysteresis. It is used to detect signals in a noisy environment by providing separate thresholds for a rising and a falling edge.

SchmittState Enum

An enum representing the state of the Schmitt Trigger.

  • HIGH: The state is high.
  • LOW: The state is low.

AbstractSchmittTrigger

An abstract base class for Schmitt Triggers.

add(x)

Adds a new value to the trigger and returns the new state if it changed.

SchmittTrigger

A basic Schmitt Trigger implementation.

  • __init__(self, high_bound, low_bound, invert=False):
    • high_bound: The upper threshold.
    • low_bound: The lower threshold.
    • invert: Inverts the trigger’s behavior.

Usage Example

from flexlib.models.schmitt_trigger import SchmittTrigger

trigger = SchmittTrigger(high_bound=10, low_bound=5)
print(trigger.state)  # Output: SchmittState.LOW

print(trigger.add(8))   # Output: None
print(trigger.add(12))  # Output: SchmittState.HIGH
print(trigger.state)  # Output: SchmittState.HIGH

print(trigger.add(6))   # Output: None
print(trigger.add(4))   # Output: SchmittState.LOW
print(trigger.state)  # Output: SchmittState.LOW

CountingSchmittTrigger

A Schmitt Trigger that only changes state after a certain number of consecutive values have crossed the threshold.

  • __init__(self, high_bound, low_bound, invert=False, low_count=0, high_count=0):
    • low_count: The number of consecutive values required to transition to the LOW state.
    • high_count: The number of consecutive values required to transition to the HIGH state.

Usage Example

from flexlib.models.schmitt_trigger import CountingSchmittTrigger

trigger = CountingSchmittTrigger(high_bound=10, low_bound=5, high_count=3)
print(trigger.add(11))  # Output: None
print(trigger.add(12))  # Output: None
print(trigger.add(13))  # Output: SchmittState.HIGH

DelayedSchmittTrigger

A Schmitt Trigger that only changes state after a certain duration has passed since the first value crossed the threshold. This is useful for time-based events.

  • __init__(self, high_bound, low_bound, low_duration=datetime.timedelta(0), high_duration=datetime.timedelta(0), invert=False):
    • low_duration: The duration required to transition to the LOW state.
    • high_duration: The duration required to transition to the HIGH state.

Usage Example

import datetime
from flexlib.models.schmitt_trigger import DelayedSchmittTrigger, TimedComparable

trigger = DelayedSchmittTrigger(high_bound=10, low_bound=5, high_duration=datetime.timedelta(seconds=2))
now = datetime.datetime.now()

print(trigger.add(TimedComparable(11, now)))  # Output: None
print(trigger.add(TimedComparable(12, now + datetime.timedelta(seconds=1))))  # Output: None
print(trigger.add(TimedComparable(13, now + datetime.timedelta(seconds=3))))  # Output: SchmittState.HIGH

Predicate-based Triggers

The module also provides predicate-based Schmitt Triggers (PredicateSchmittTrigger, CountingPredicateSchmittTrigger, DelayedPredicateSchmittTrigger) that use functions to determine if a threshold is crossed, allowing for more complex conditions.