-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserial_handler.cpp
More file actions
81 lines (73 loc) · 1.62 KB
/
serial_handler.cpp
File metadata and controls
81 lines (73 loc) · 1.62 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
#include "serial_handler.h"
#include <Arduino.h>
void SerialHandler::init() {
#ifdef SOFTWARE_SERIAL
#ifdef DEBUG_MODE
Serial.begin(BAUD_RATE_SERIAL);
#endif
module = &ss;
ss.begin(BAUD_RATE_SERIAL);
#else
module = &Serial;
Serial.begin(BAUD_RATE_SERIAL);
#endif
}
void SerialHandler::send(char *text) {
module->println(text);
#ifdef DEBUG_MODE
#ifdef SOFTWARE_SERIAL
Serial.println(text);
#endif
#endif
}
void SerialHandler::receive() {
bufferIndex = 0;
buffer[0] = 0;
while (module->available()) {
char c = module->read();
if (c == '\r') continue;
if (c == '\n' || bufferIndex >= MAX_BUFFER_SIZE - 1) {
buffer[bufferIndex] = 0;
bufferIndex = 0;
if (strstr(buffer, "+CGNSINF") == buffer) {
strcpy(gnsinf, buffer);
}
#ifdef DEBUG_MODE
Serial.print("> ");
Serial.println(buffer);
#endif
continue;
}
buffer[bufferIndex++] = c;
}
}
void SerialHandler::command(char *text) {
while (module->available())
module->read();
send(text);
unsigned long current_time = millis();
while (module->available() == 0 && millis() - current_time < 3000);
delay(1000);
receive();
}
void SerialHandler::command(const char *format, ...) {
va_list args;
char buf[100];
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
command(buf);
}
static void SerialHandler::debug(char *text) {
#ifdef DEBUG_MODE
Serial.println(text);
#endif
}
static void SerialHandler::debug(const char *format, ...) {
va_list args;
char buf[100];
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
debug(buf);
}