-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.cpp
executable file
·118 lines (94 loc) · 2.74 KB
/
utils.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
#include "utils.h"
bool getStrArgument(uint8_t index, char* arg, uint8_t argBuffLen){
//try to get the index_th arguments from userInputBuffer as string
//the argument's index must be >0
//if(index==0){
//return false;
//}
uint8_t tokenLen = 0;
char* l_ptrTokenBegin;
if (!getCmdToken(index, l_ptrTokenBegin, tokenLen) || tokenLen > MAX_ARG_LENGTH+1){
return false;
}
strlcpy(arg, l_ptrTokenBegin, tokenLen+1);
return true;
}
bool getIntArgument(uint8_t index, int &arg){
//try to get the index_th arguments from userInputBuffer as integer
//the argument's index must be >0
//if(index==0){
//return false;
//}
char* l_ptrArgBegin;
uint8_t len;
if (!getCmdToken(index, l_ptrArgBegin, len)){
return false;
}
char l_sToInt[len+1];
strlcpy(l_sToInt, l_ptrArgBegin, len+1);
//check if it's an integer
for (uint8_t i = 0; i < len; i++){
if(!isdigit(l_sToInt[i])) return false;
}
arg = atoi(l_sToInt);
return true;
}
bool getFloatArgument(uint8_t index, float &arg){
//try to get the index_th arguments from userInputBuffer as float
//the argument's index must be >0
//if(index==0){
//return false;
//}
char* l_ptrArgBegin;
uint8_t len;
if (!getCmdToken(index, l_ptrArgBegin, len)){
return false;
}
char l_sToFloat[len+1];
strlcpy(l_sToFloat, l_ptrArgBegin, len+1);
//check it's a float
for (uint8_t i = 0; i < len; i++){
if((!isdigit(l_sToFloat[i]) && l_sToFloat[i]!='.')) return false;
}
arg = atof(l_sToFloat);
return true;
}
void inputBufferTokenEnd(char* &cmdPtrCurrent){
//skip the current token in the userInputBuffer and return the pointer to its end
//find the token's end
while(*cmdPtrCurrent != ' ' && *cmdPtrCurrent != '\0') {
++cmdPtrCurrent;
}
}
void inputBufferNextToken(char* &cmdPtrCurrent){
//skip to the next token's begin in the userInputBuffer
while(*cmdPtrCurrent == ' ' || *cmdPtrCurrent == '\t') {
++cmdPtrCurrent;
}
}
bool getCmdToken(uint8_t index, char* &ptrTokenBegin, uint8_t &len){
//return the pointer to the begin of the index_nth token of the command present in the
//userInputBuffer of the START and its length
ptrTokenBegin = START::userInputBuffer;
for (uint8_t i = 0; i < index; i++){
inputBufferTokenEnd(ptrTokenBegin);
inputBufferNextToken(ptrTokenBegin);
}
char* l_ArgumentEnd = ptrTokenBegin;
inputBufferTokenEnd(l_ArgumentEnd);
len = l_ArgumentEnd - ptrTokenBegin;
return len>0 && len<=MAX_ARG_LENGTH ? true : false;
}
bool isInput(uint8_t pinNumber){
uint8_t bit = digitalPinToBitMask(pinNumber);
uint8_t port = digitalPinToPort(pinNumber);
volatile uint8_t *reg = portModeRegister(port);
if (*reg & bit) {
// It's an output
return false;
}
else {
// It's an input
return true;
}
}