-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton_debounce.cpp
87 lines (78 loc) · 2.33 KB
/
button_debounce.cpp
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
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "button_debounce.pio.h"
// class that debounces a gpio pin
class Debounce {
public:
// constructor to debounce the gpio
Debounce(uint gpio) {
// instantiate a pio, for now use pio0
// TODO: if no sm in pio0 are available, try pio1
PIO pio = pio0;
// claim a state machine
sm = pio_claim_unused_sm(pio, false);
// check if this is a valid sm
if (sm == -1) {
valid = 0;
} else {
valid = 1;
}
// load the pio program into the pio memory
offset = pio_add_program(pio, &button_debounce_program);
// make a sm config
pio_sm_config c = button_debounce_program_get_default_config(offset);
// set the 'wait' pins
sm_config_set_in_pins(&c, gpio); // for WAIT, IN
// set the 'jmp' pins
sm_config_set_jmp_pin(&c, gpio); // for JMP
// set the clock divisor to set a reasonable debounce time
// TODO: let the user set the debounce time in ms, calculate clock divisor for pio delay of 31.
sm_config_set_clkdiv(&c, 11);
// init the pio sm with the config
pio_sm_init(pio, sm, offset, &c);
// enable the sm
pio_sm_set_enabled(pio, sm, true);
};
// return the value of the debounced gpio
uint read(void){
// read the program counter
uint pc = pio_sm_get_pc(pio0, sm);
// if it is at or beyond the "wait 0 pin 0" it has value 1, else 0
if (pc >= offset+6) {
return 1;
}else {
return 0;
}
};
// if no sm was available the debounce does not work
uint is_valid(void) {
return valid;
}
private:
// the pio instance
PIO pio;
// the state machine
uint sm;
// the location of the pio program in the memory
uint offset;
// indicator if the sm is valid
uint valid = 0;
};
/*
* The main program: instantiate the debouncer and keep printing the value
*/
int main()
{
// necessary for printf
stdio_init_all();
// instantiate the debouncer for a gpio
Debounce my_pin(5);
// check if it is valid (i.e. there is an active state machine)
printf("my_pin validity = %d\n", my_pin.is_valid());
// loop that reads and prints the gpio value
while (true) {
printf("value=%d\n", my_pin.read());
}
}