-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslight_ButtonInput__advanced.ino
178 lines (148 loc) · 5.81 KB
/
slight_ButtonInput__advanced.ino
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/******************************************
slight_ButtonInput__advanced
minimal example for slight_ButtonInput lib usage.
debugport: serial interface 115200baud
hardware:
Arduino board of any typ.
A3 --> Pushbutton closing to GND
A4 --> Pushbutton closing to GND
A5 --> Pushbutton closing to GND
libraries used:
~ slight_ButtonInput
written by stefan krueger (s-light),
[email protected], http://s-light.eu, https://github.com/s-light/
******************************************/
/******************************************
CC BY SA
This work is licensed under the
Creative Commons Attribution-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
The MIT License (MIT)
Copyright (c) 2019 Stefan Krüger
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
https://opensource.org/licenses/mit-license.php
******************************************/
#include <slight_ButtonInput_CallbackHelper.h>
#include <slight_ButtonInput.h>
// ------------------------------------------
// slight_ButtonInput things
boolean mybutton_get_input(slight_ButtonInput *instance) {
// read input + invert: button closes to GND.
return !digitalRead((*instance).pin);
}
void mybutton_event(slight_ButtonInput *instance) {
Serial.print(F("instance:"));
Serial.print((*instance).id);
Serial.print(F(" - event: "));
(*instance).printEventLast(Serial);
Serial.println();
// react on event
switch ((*instance).getEventLast()) {
case slight_ButtonInput::event_down : {
// Serial.println(F("down"));
} break;
case slight_ButtonInput::event_holddown : {
Serial.print(F("duration active: "));
Serial.println((*instance).getDurationActive());
} break;
case slight_ButtonInput::event_up : {
// Serial.println(F("up"));
} break;
case slight_ButtonInput::event_click : {
// Serial.println(F("click"));
} break;
case slight_ButtonInput::event_click_long : {
Serial.print(F("click long "));
Serial.println((*instance).getDurationActive());
} break;
case slight_ButtonInput::event_click_double : {
// Serial.println(F("click double"));
} break;
case slight_ButtonInput::event_click_triple : {
// Serial.println(F("click triple"));
} break;
case slight_ButtonInput::event_click_multi : {
Serial.print(F("click multi - count: "));
Serial.println((*instance).getClickCount());
} break;
} //end switch
}
const uint8_t mybuttons_count = 4;
slight_ButtonInput mybuttons[mybuttons_count] = {
slight_ButtonInput(0, A0, mybutton_get_input, mybutton_event),
slight_ButtonInput(
// uint8_t id_new
1,
// uint8_t pin_new,
A3,
// tCallbackFunctionGetInput callbackGetInput_new,
mybutton_get_input,
// tCallbackFunction callbackOnEvent_new,
mybutton_event,
// const uint16_t duration_debounce_new = 20,
10,
// const uint16_t duration_holddown_new = 1000,
1000,
// const uint16_t duration_click_long_new = 3000,
500,
// const uint16_t duration_click_double_new = 250
250
),
// using default values:
slight_ButtonInput(2, A4, mybutton_get_input, mybutton_event, 1),
slight_ButtonInput(3, A5, mybutton_get_input, mybutton_event, 1),
};
// ------------------------------------------
// setup
// ------------------------------------------
void setup() {
// ------------------------------------------
// init serial
// wait for arduino IDE to release all serial ports after upload.
delay(1000);
Serial.begin(115200);
delay(500);
Serial.println();
// ------------------------------------------
// print short welcome text
Serial.println(F("slight_ButtonInput__advanced.ino sketch."));
Serial.println(F("minimal example for library usage."));
// ------------------------------------------
// start slight_ButtonInput
Serial.println(F("setup slight_ButtonInput:")); {
for (size_t i = 0; i < mybuttons_count; i++) {
pinMode(mybuttons[i].pin, INPUT_PULLUP);
mybuttons[i].begin();
mybuttons[i].flag_filter_multi_click_events = true;
}
}
Serial.println(F(" finished."));
// ------------------------------------------
Serial.println(F("Loop:"));
}
// ------------------------------------------
// main loop
// ------------------------------------------
void loop() {
for (size_t i = 0; i < mybuttons_count; i++) {
mybuttons[i].update();
}
// nothing else to do here...
}
// ------------------------------------------
// THE END :-)
// ------------------------------------------