-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.cpp
executable file
·102 lines (93 loc) · 2.39 KB
/
Button.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
||
|| @file Button.cpp
|| @version 1.6
|| @author Alexander Brevig
|| @contact [email protected]
||
|| @description
|| | Provide an easy way of making buttons
|| #
||
|| @license
|| | Copyright (c) 2009 Alexander Brevig. All rights reserved.
|| | This code is subject to AlphaLicence.txt
|| | alphabeta.alexanderbrevig.com/AlphaLicense.txt
|| #
||
*/
// include the class definition
#include "Button.h"
/*
|| <<constructor>>
|| @parameter buttonPin sets the pin that this switch is connected to
|| @parameter buttonMode indicates PULLUP or PULLDOWN resistor
*/
Button::Button(uint8_t buttonPin, uint8_t buttonMode) {
this->pin = buttonPin;
pinMode(pin, INPUT);
buttonMode == PULLDOWN ? pulldown() : pullup();
state = 0;
bitWrite(state, CURRENT, !mode);
}
/*
|| Set pin HIGH as default
*/
void Button::pullup(void) {
mode = PULLUP;
digitalWrite(pin, HIGH);
}
/*
|| Set pin LOW as default
*/
void Button::pulldown(void) {
mode = PULLDOWN;
// digitalWrite(pin,LOW);
}
/*
|| Return the bitWrite(state,CURRENT, of the switch
*/
bool Button::isPressed(void) {
bitWrite(state, PREVIOUS, bitRead(state, CURRENT));
if (digitalRead(pin) == mode) {
bitWrite(state, CURRENT, false);
} else {
bitWrite(state, CURRENT, true);
}
if (bitRead(state, CURRENT) != bitRead(state, PREVIOUS)) {
bitWrite(state, CHANGED, true);
} else {
bitWrite(state, CHANGED, false);
}
return bitRead(state, CURRENT);
}
/*
|| Return true if the button has been pressed
*/
bool Button::wasPressed(void) {
if (bitRead(state, CURRENT)) {
return true;
} else {
return false;
}
}
/*
|| Return true if state has been changed
*/
bool Button::stateChanged(void) { return bitRead(state, CHANGED); }
/*
|| Return true if the button is pressed, and was not pressed before
*/
bool Button::uniquePress(void) { return (isPressed() && stateChanged()); }
/*
|| @changelog
|| | 2009-05-05 - Alexander Brevig : Added uniquePress()
|| | 2009-04-24 - Alexander Brevig : Added wasPressed()
|| | 2009-04-12 - Alexander Brevig : Added constructor
|| | Shortened logic
|| | 2009-04-10 - Alexander Brevig : Namechange from Switch
|| | 2009-04-07 - Alexander Brevig : Altered API
|| | 2008-10-23 - Alexander Brevig : Initial Release
|| | 2008-10-22 - Alexander Brevig : Class implemented
|| #
*/