I have a dust sensor, and I have to measure a sum of inactive time in 30 seconds cycles.
For DigitalInputDevice gpiozero provides cool functionality, I mean two properties, and two callbacks:
- properties - active_time (return float if pin is active or None) and inactive_time (return float if pin is inactive or None),
- callbacks - when_activated and when_deactivated
I thought that inside those callbacks I would have access to the digit input in the previous state, which would allow me to grab active time when pin change state to inactive, and inactive time when pin change state to active, but I was wrong.
So my question is: There is any possibility in this library to grab a whole active time when pin turns to inactive and vice versa?
Thank you!
Clarification: what I want is described in the image below
image of chart my sensor docs: https://botland.com.pl/index.php?controller=attachment&id_attachment=1565
I do it now in this way:
from gpiozero import DigitalInputDevice
from datetime import timedelta
import time
sensor_one_micrometer = DigitalInputDevice(20)
while True:
active_time = 0
inactive_time = 0
sensor_one_micrometer.wait_for_active()
while active_time + inactive_time < 30:
watch_start = time.time()
sensor_one_micrometer.wait_for_inactive()
active_time += time.time() - watch_start
watch_start = time.time()
sensor_one_micrometer.wait_for_active()
inactive_time += time.time() - watch_start
percent = inactive_time/(active_time+inactive_time)
print("Active Time: ", active_time)
print("Inactive Time: ", inactive_time)
print(
inactive_time/(active_time+inactive_time)
)