-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_gpio_on_change.py
executable file
·55 lines (37 loc) · 1.7 KB
/
example_gpio_on_change.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
#!/usr/bin/python3
#-*- coding: utf-8 -*-
wait_sec = 1.
import time
import rp2daq
rp = rp2daq.Rp2daq()
# Optional: Let's prepare PWM for some artificial signal on GPIO 0
# Note that with wrap_value=40, even 50k reports per second can be received, this is about the limit
print("Configuring PWM channel to make artificial 5kHz square wave (and waiting for it to settle)")
rp.pwm_configure_pair(gpio=0,
clkdiv=250, # clock at 1.000 MHz
wrap_value=50-1) # one rising or falling edge 20 kHz
time.sleep(.08) # unclear why, but 100 ms is safe
rp.pwm_set_value(0, 10)
## Define a report handler and start asynchronous reporting on each GPIO change
print("Registering rising/falling edge events...")
count = [0]
def handler(**kwargs):
#print(kwargs)
count[0] += 1
rp.gpio_on_change(0, on_rising_edge=1, on_falling_edge=1, _callback=handler)
time.sleep(wait_sec)
print(f'Number of received edge reports after {wait_sec} seconds:', count)
## Note one or few more reports may be on their way yet, so:
## 1. stop the PWM
print("Switching off PWM signal")
def dummy_cb(**kwargs): pass
rp.pwm_set_value(0, 0, _callback=dummy_cb) # stop PWM signal (immediately)
#rp.pwm_set_value(0, 0) # c.f. stop PWM signal (waits for all previous reports)
## 2. stop the device from issuing new reports, but wait for pending ones to arrive
#rp.pin_on_change(pin=0, on_rising_edge=1, on_falling_edge=0, _callback=handler)
## Receive pending reports (if any)
after_wait_sec = 0.1
time.sleep(after_wait_sec)
print(f'Total received edge reports after extra {after_wait_sec} seconds:', count)
time.sleep(after_wait_sec)
print(f'Total received edge reports after another extra {after_wait_sec} seconds:', count)