46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
|
from src.variables.service import get_variable, set_variable
|
||
|
from gpiozero import Button
|
||
|
import datetime, os, json
|
||
|
|
||
|
pulses_per_rotation = 8
|
||
|
reader = Button(4, pull_up=False, bounce_time=0.0005)
|
||
|
set_variable("rpm_pulses_counter", "[]", False)
|
||
|
set_variable("rpm_time_span_sec", 1, False)
|
||
|
|
||
|
def printer(text):
|
||
|
if bool(os.getenv("DEBUG")):
|
||
|
print(text)
|
||
|
|
||
|
def event():
|
||
|
actual = datetime.datetime.now()
|
||
|
|
||
|
printer("Impulse readed")
|
||
|
|
||
|
rpm_pulses_counter = [datetime.datetime.fromisoformat(t) for t in json.loads(get_variable("rpm_pulses_counter"))]
|
||
|
printer(rpm_pulses_counter)
|
||
|
|
||
|
rpm_pulses_counter.append(actual)
|
||
|
set_variable("rpm_pulses_counter", json.dumps([t.isoformat() for t in rpm_pulses_counter]), False)
|
||
|
|
||
|
rpm_time_span_sec = int(get_variable("rpm_time_span_sec"))
|
||
|
pulses = len([t for t in rpm_pulses_counter if (actual - t).total_seconds() <= rpm_time_span_sec])
|
||
|
|
||
|
actual_rpm = (pulses / pulses_per_rotation) * (60 / rpm_time_span_sec)
|
||
|
|
||
|
set_variable("actual_rpm", round(actual_rpm, 1), False)
|
||
|
printer("Measured RPM: {}".format(round(actual_rpm, 1)))
|
||
|
|
||
|
|
||
|
def remover():
|
||
|
actual = datetime.datetime.now()
|
||
|
rpm_pulses_counter = [datetime.datetime.fromisoformat(t) for t in json.loads(get_variable("rpm_pulses_counter"))]
|
||
|
rpm_time_span_sec = int(get_variable("rpm_time_span_sec")) + 1
|
||
|
|
||
|
printer("cleaning event")
|
||
|
|
||
|
rpm_pulses_counter = [t for t in rpm_pulses_counter if (actual - t).total_seconds() <= rpm_time_span_sec]
|
||
|
|
||
|
set_variable("rpm_pulses_counter", json.dumps([t.isoformat() for t in rpm_pulses_counter]), False)
|
||
|
|
||
|
reader.when_pressed = event
|