-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.h
More file actions
68 lines (40 loc) · 874 Bytes
/
button.h
File metadata and controls
68 lines (40 loc) · 874 Bytes
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
#ifndef INCLUDE_BUTTON777
#define INCLUDE_BUTTON
#include <avr/io.h>
class button
{
public:
button(PORT_t* port, uint8_t pin_nbr)
{
this->port = port;
this->pin_mask = ( 1 << pin_nbr);
port->DIRCLR = pin_mask;
volatile register8_t *ctrl_reg = &(port->PIN0CTRL) + pin_nbr;
*ctrl_reg = PORT_OPC_PULLUP_gc;
prev_state = port->IN & pin_mask;
}
bool isUp()
{
if (port->IN & pin_mask) return true;
else return false;
}
bool isDown()
{
if (port->IN & pin_mask) return false;
else return true;
}
bool isToggled()
{
uint8_t current_state = port->IN & pin_mask;
bool return_value;
if(current_state == prev_state) return_value = false;
else return_value = true;
prev_state = current_state;
return return_value;
}
private:
PORT_t* port;
uint8_t pin_mask;
uint8_t prev_state;
};
#endif //INCLUDE_BUTTON