-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialInputHandler.cpp
More file actions
54 lines (49 loc) · 1.26 KB
/
SerialInputHandler.cpp
File metadata and controls
54 lines (49 loc) · 1.26 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
#include "SerialInputHandler.hpp"
#include <Arduino.h>
SerialInputHandler::SerialInputHandler()
{
// TODO Auto-generated constructor stub
}
void SerialInputHandler::handleNewSerialData(Stream &stream)
{
while (Serial.available() > 0)
{
const auto inData = Serial.read();
if (inData < 0)
{
break;
}
else
{
const auto inChar = static_cast<char>(inData);
static std::string begunLine;
begunLine += inChar;
if (inChar == '\n' || inChar == '\r')
{
messageQueue.push(begunLine);
begunLine.clear();
}
}
}
}
std::string SerialInputHandler::getNextLine()
{
const std::string line = messageQueue.front();
messageQueue.pop();
return line;
}
SerialInputHandler &SerialInputHandler::getInstance()
{
static SerialInputHandler instance;
return instance;
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent()
{
auto handler = SerialInputHandler::getInstance();
handler.handleNewSerialData(Serial);
}