-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualButton.cpp
122 lines (102 loc) · 3.97 KB
/
VirtualButton.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* VirtualButton.cpp - Library for reading from a virtual button aka the keyboard
* Created by Jarrett on July 21, 2022
*/
#include "VirtualButton.h"
VirtualButton::VirtualButton(bool toggleSwitchesON[]) {
_userButtonVal = 0;
_helperRanOnce = false;
_toggleSwitchesON = toggleSwitchesON;
}
VirtualButton::VirtualButton() {
_userButtonVal = 0;
_helperRanOnce = false;
_toggleSwitchesON = {};
}
bool VirtualButton::toggleSwitch(int switchNum) {
_toggleSwitchesON[switchNum] = !_toggleSwitchesON[switchNum]; // The ! switches the bool value
return _toggleSwitchesON[switchNum];
}
int VirtualButton::virtualDigitalRead(byte buttonPin) {
/* If _helperRanOnce is false then display an error message to inform the
user that they must run virtualDigitalReadHelper() first to monitor thier input*/
if (_helperRanOnce == false) {
Serial.println(F("ERROR: Call virtualDigitalReadHelper() first to monitor inputs. Stopping..."));
while (true) {
// Do nothing
}
}
// Check if the user entered a value and if it is a configured button
if (_userButtonVal != 0 && buttonPin == _userButtonVal) {
// Reset the _userButtonVal to prevent getting stuck running virtualDigitalRead
_userButtonVal = 0;
// It is a button so return HIGH
return HIGH;
}
return LOW; // It isn't so return LOW
}
void VirtualButton::virtualDigitalReadHelper() {
// The method was called once
_helperRanOnce = true;
// Call serialReadByte read to what the user typed
// It also sets the _userButtonVal
serialReadByte();
}
void VirtualButton::serialReadByte() {
/* NOTE: Local static variables allow the data to update without being reset
while prevent them from being called outside this function.
Also, variables cannot be declared within a case statement*/
// Set how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;
// Used to store the full input from the user (constructed byte by byte)
static char fullInput[MAX_INPUT];
// Used to store the position of the current byte
static unsigned int inputPosition = 0;
byte inputByte;
// While the serial data is available, process it
while (Serial.available() > 0) {
inputByte = Serial.read();
switch (inputByte) {
case '\r': // Carriage return
case '\n': // End of new line
fullInput[inputPosition] = '\0'; // Add terminating null byte
// Call this method to convert fullInput to a byte
convertToByte(fullInput, inputPosition);
// Reset buffer's inputPosition for next time
inputPosition = 0;
break;
default:
// Keep adding to the fullInput if it isn't full (MAX_INPUT - 1 to allow for terminating null byte)
if (inputPosition < (MAX_INPUT - 1)) {
// Add the inputByte to the array (converts from byte to char automatically)
fullInput[inputPosition] = inputByte;
inputPosition++; // Increase the inputPosition
}
break;
}
}
}
void VirtualButton::convertToByte(const char fullInput[], const unsigned int inputPosition) {
// Used to store if the fullInput is a positive integer
bool positiveInt = true;
// Used to store the integer entered by the user (fullInput converted)
int serialInt = 0;
// Loop through all the characters
for (int i = 0; i < inputPosition; i++) {
// If the character isn't a digit set postInt to false
if (!isDigit(fullInput[i])) {
positiveInt = false;
break; // Leave loop early
}
}
// Convert the fullInput to an int and store it
serialInt = atoi(fullInput);
// Check if the value is invalid
if (positiveInt == false || serialInt <= 0 || serialInt > 255) {
// Display the error
Serial.println(F("Invalid value entered (the input must be an int, > 0 and <= 255). Try again."));
} else { // Otherwise, the value is valid
// Convert the int to a byte and store it
_userButtonVal = byte(serialInt);
}
}