-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHbButton.cpp
More file actions
104 lines (89 loc) · 1.89 KB
/
Copy pathHbButton.cpp
File metadata and controls
104 lines (89 loc) · 1.89 KB
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
103
104
#include "Arduino.h"
#include "HbButton.h"
HbButton::HbButton() {
}
HbButton::HbButton(int pin, boolean isDigital, boolean isPullup)
{
this->pin = pin;
this->isDigital = isDigital;
this->isPullup = isPullup;
if (isDigital) {
if (isPullup) {
pinMode(pin, INPUT_PULLUP);
} else {
pinMode(pin, INPUT);
}
} else {
}
}
HbButton::HbButton(int pin)
{
this->pin = pin;
this->isDigital = true;
this->isPullup = true;
if (isDigital) {
if (isPullup) {
pinMode(pin, INPUT_PULLUP);
} else {
pinMode(pin, INPUT);
}
} else {
}
}
void HbButton::setPin(int pin)
{
this->pin = pin;
}
void HbButton::setMode(boolean isDigital, boolean isPullup)
{
this->isDigital = isDigital;
this->isPullup = isPullup;
}
void HbButton::setMode(boolean isDigital)
{
this->isDigital = isDigital;
}
void HbButton::init()
{
if (this->isDigital) {
if (this->isPullup) {
pinMode(this->pin, INPUT_PULLUP);
} else {
pinMode(this->pin, INPUT);
}
} else {
}
}
void HbButton::attach(int pin, boolean isDigital, boolean isPullup)
{
(this->setPin)(pin);
(this->setMode)(isDigital, isPullup);
(this->init)();
}
void HbButton::attach(int pin)
{
(this->attach)(pin, true, true);
}
boolean HbButton::read()
{
int count = 0;
if (isDigital) {
for(int i; i<HBBUTTON_MAX_COUNT; i++) {
if (digitalRead(this->pin) == HIGH) count++;
}
} else {
for(int i; i<HBBUTTON_MAX_COUNT; i++) {
if (analogRead(this->pin) > 512) count++;
}
}
if (2*count > HBBUTTON_MAX_COUNT) return HIGH;
else return LOW;
}
boolean HbButton::readraw()
{
if (isDigital) {
return digitalRead(this->pin);
} else {
return analogRead(this->pin) > 512 ? HIGH : LOW;
}
}