-
Notifications
You must be signed in to change notification settings - Fork 37
/
pwm.py
92 lines (78 loc) · 2.94 KB
/
pwm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
#
# © Takeyuki UEDA 2024 -
import time
import RPi.GPIO as GPIO
from gpiozero import Button
# exception
class GPIO_Edge_Timeout(Exception):
pass
def read_from_pwm_with_gpio(gpio=12, range=5000):
CYCLE_START_HIGHT_TIME = 2
TIMEOUT = 2000 # must be larger than PWM cycle time.
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio,GPIO.IN)
# wait falling ¯¯|_ to see end of last cycle
channel = GPIO.wait_for_edge(gpio, GPIO.FALLING, timeout=TIMEOUT)
if channel is None:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
# wait rising __|¯ to catch the start of this cycle
channel = GPIO.wait_for_edge(gpio,GPIO.RISING, timeout=TIMEOUT)
if channel is None:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
else:
rising = time.time() * 1000
# wait falling ¯¯|_ again to catch the end of TH duration
channel = GPIO.wait_for_edge(gpio, GPIO.FALLING, timeout=TIMEOUT)
if channel is None:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
else:
falling = time.time() * 1000
return {'co2': int(falling -rising - CYCLE_START_HIGHT_TIME) / 2 *(range/500)}
# refer https://gpiozero.readthedocs.io/en/latest/migrating_from_rpigpio.html#input-devices
def read_from_pwm_with_gpiozero(gpio=12, range=5000):
CYCLE_START_HIGHT_TIME = 2
TIMEOUT = 2000 # must be larger than PWM cycle time.
'''
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio,GPIO.IN)
'''
btn = Button(gpio)
# wait falling ¯¯|_ to see end of last cycle
'''
channel = GPIO.wait_for_edge(gpio, GPIO.FALLING, timeout=TIMEOUT)
if channel is None:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
'''
starting = time.time() * 1000
btn.wait_for_press(2) # 2 sec for timeout because 1sec cycle of pwm
last_falling = time.time() * 1000
if last_falling - starting > 1000:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
# wait rising __|¯ to catch the start of this cycle
'''
channel = GPIO.wait_for_edge(gpio,GPIO.RISING, timeout=TIMEOUT)
if channel is None:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
else:
rising = time.time() * 1000
'''
starting = time.time() * 1000
btn.wait_for_release(2) # 2 sec for timeout because 1sec cycle of pwm
rising = time.time() * 1000
if rising - starting > 1000:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
# wait falling ¯¯|_ again to catch the end of TH duration
'''
channel = GPIO.wait_for_edge(gpio, GPIO.FALLING, timeout=TIMEOUT)
if channel is None:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
else:
falling = time.time() * 1000
'''
starting = time.time() * 1000
btn.wait_for_press(2) # 2 sec for timeout because 1sec cycle of pwm
falling = time.time() * 1000
if falling - starting > 1000:
raise GPIO_Edge_Timeout("gpio {} edge timeout".format(gpio))
return {'co2': int(falling -rising - CYCLE_START_HIGHT_TIME) / 2 *(range/500)}