-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaclLib.cpp
More file actions
149 lines (137 loc) · 4.07 KB
/
yaclLib.cpp
File metadata and controls
149 lines (137 loc) · 4.07 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
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
/*
yaclLib.cpp
Created on: 11 03 2019
Author: Cricri042
*/
#include "yaclLib.h"
namespace yaclLibSpace {
/************************************************/
yaclLib::yaclLib(Stream& dev, cmdLineCommand* cmds, size_t s)
: stream(dev), commands(cmds), userFuncSize(s) {
ok = true;
stream.setTimeout(10);
}
/************************************************/
yaclLib::~yaclLib() {
}
/************************************************/
int yaclLib::read() {
return stream.read();
}
/************************************************/
int yaclLib::available() {
return stream.available();
}
/************************************************/
int yaclLib::peek() {
return stream.peek();
}
/************************************************/
size_t yaclLib::write(uint8_t b) {
return stream.write(b);
}
/*************************************************************************************************************
getCommandLineFromSerialPort()
Return the string of the next command. Commands are delimited by return"
Handle BackSpace character
Make all chars lowercase
*************************************************************************************************************/
bool yaclLib::getCommandLineFromSerialPort() {
if (stream.available()) {
int cr = readBytesUntil('\n', commandLine, COMMAND_BUFFER_LENGTH);
int l = strcspn(commandLine, "\r\n");
commandLine[l] = NULLCHAR;
return (1 > 0);
}
return false;
}
/* ****************************
readNumber: return a 16bit (for Arduino Uno) signed integer from the command line
readWord: get a text word from the command line
*/
int yaclLib::readNumber() {
ok = true;
char *numTextPtr = strtok(NULL, delimiters); //K&R string.h pg. 250
if (numTextPtr == NULL) {
ok = false;
}
return atoi(numTextPtr); //K&R string.h pg. 251
}
/************************************************/
long yaclLib::readLong() {
ok = true;
char *numTextPtr = strtok(NULL, delimiters); //K&R string.h pg. 250
if (numTextPtr == NULL) {
ok = false;
}
return atol(numTextPtr); //K&R string.h pg. 251
}
/************************************************/
float yaclLib::readFloat() {
ok = true;
char *numTextPtr = strtok(NULL, delimiters); //K&R string.h pg. 250
if (numTextPtr == NULL) {
ok = false;
}
return atof(numTextPtr); //K&R string.h pg. 251
}
/************************************************/
char *yaclLib::readWord() {
char *word = strtok(NULL, delimiters); //K&R string.h pg. 250
return word;
}
/************************************************/
void yaclLib::nullCommand(char *ptrToCommandName) {
print2(F("Command not found: "), ptrToCommandName); //see above for macro print2
}
/************************************************/
int yaclLib::isNumericString(char *s) {
int i = 0, isNumeric = 1, ctDecimalPointsSeen = 0;
if (s[i] == '+' || s[i] == '-') {
i += 1;
} //Start from the next character
while (s[i] != '\0') {
if (!isDigit(s[i])) {
if (s[i] == '.' && ctDecimalPointsSeen < 1) {
ctDecimalPointsSeen += 1;
} else { /*if the number of decimal points is more than 1 or if the character encountered isn't a digit or .*/
isNumeric = 0;
break;
}
}
i += 1;
}
return isNumeric;
}
/****************************************************
DoMyCommand
*/
bool yaclLib::DoMyCommand() {
if (strlen(commandLine) == 0) return false;
char* ptrToCommandName = strtok(commandLine, delimiters);
for (int i = 0; i < userFuncSize; ++i) {
if (strcmp(ptrToCommandName, commands[i].tokens) == 0) {
cmdToken = commands[i].tokens;
commands[i].userFunc();
commandLine[0] = NULLCHAR;
return true;
}
}
cmdToken = "";
nullCommand(ptrToCommandName);
commandLine[0] = NULLCHAR;
return false;
}
/************************************************/
bool yaclLib::checkCommands() {
// disable all interrupts
//noInterrupts();
bool received = getCommandLineFromSerialPort();
if (received) {
return DoMyCommand();
}
return false;
// enable all interrupts
//interrupts();
}
} /* namespace yaclLibSpace */