-
Notifications
You must be signed in to change notification settings - Fork 10
/
serialStr.cpp
61 lines (46 loc) · 2.43 KB
/
serialStr.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
#include "serialStr.h"
#include <resizeBuff.h>
// Constructor, set up all the fun bits and allocate a buffer for the text.
serialStr::serialStr(Stream* inPort,char endChar,int numBytes) {
port = inPort;
index = 0;
EOL = endChar;
bytes = numBytes;
overrun = false;
resizeBuff(numBytes,&buff);
}
// Destructor, recycle the buffer.
serialStr::~serialStr(void) { resizeBuff(0,&buff); }
// Take in the callback function pointer. While we're at it, do the hookup() call for
// doing idle function.
//void serialStr::setCallback(void(*funct)(const char*)) {
void serialStr::setCallback(void(*funct)(char*)) {
callback = funct;
hookup();
}
// All the work happens in idle(). Basically check for a char to come in, save it if we
// can. Or tell the user if its on EOT char and start over.
void serialStr::idle(void) {
char aChar;
if (buff) { // If we have a buffer to put things in..
if (port->available()) { // If there is a char in the waiting to be read..
aChar = port->read(); // Grab and save the char.
if (aChar==EOL) { // If its a newline char..
callback(buff); // Call our callback with the buffer.
overrun = false; // If set, clear the overrun flag.
buff[0] = '\0'; // Clear the inBuff string.
index = 0; // Reset the index to start a new number string.
} else { // Else, it wasn't a newline char..
if (index<(bytes-1)) { // If we have room in the buffer..
buff[index++] = aChar; // So we save the char we got into the c string.
buff[index] = '\0'; // And pop an end of string after it.
} else { // Else, we ran out of room in the buffer!
overrun = true; // Just blew past the buffer.
}
}
}
}
}
// We can ask if there was an overrun. This will return true if there was. Does not clear
// the overrun. Although it probably should.
bool serialStr::hadOverrun(void) { return overrun; }