forked from juanmb/nextdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_command.cpp
More file actions
57 lines (48 loc) · 1.38 KB
/
serial_command.cpp
File metadata and controls
57 lines (48 loc) · 1.38 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
/******************************************************************
Author: Juan Menendez Blanco <juanmb@gmail.com>
This code is part of the NexStarAdapter project:
https://github.com/juanmb/NexStarAdapter
*******************************************************************/
#include <Arduino.h>
#include "serial_command.h"
SerialCommand::SerialCommand()
{
nCommands = 0;
cmdSize = 0;
bufPos = 0;
}
int SerialCommand::addCommand(const char firstChar, uint8_t size, cbFunction function)
{
if (nCommands >= MAX_COMMANDS) {
return 1;
}
commandList[nCommands].firstChar = firstChar;
commandList[nCommands].size = size;
commandList[nCommands].function = function;
nCommands++;
return 0;
}
void SerialCommand::readSerial()
{
while (Serial.available()) {
char c = Serial.read();
if (bufPos == 0) {
cmdSize = 0;
for (int i = 0; i < nCommands; i++) {
if (commandList[i].firstChar == c) {
cmdSize = commandList[i].size;
cmdFunction = commandList[i].function;
break;
}
}
if (cmdSize == 0) {
continue;
}
}
buffer[bufPos++] = c;
if (bufPos >= cmdSize) {
cmdFunction(buffer);
bufPos = 0;
}
}
}