This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrs232.cpp
82 lines (69 loc) · 1.75 KB
/
rs232.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "rs232.h"
#include "logging.h"
namespace fskube {
LOG_HANDLE("rs232")
Rs232Synthesizer::Rs232Synthesizer() {}
// It looks like the stackmat flips its data bits. I don't know
// if this is part of the 8N1 protocol, or just something weird
// the stackmat does.
#define FLIP_DATA_BITS true
void Rs232Synthesizer::receive(int data) {
if(data < 0) {
// Send our "idle" signal (low) for a good little while.
for(int j = 0; j < 8*3; j++) {
send(0);
}
return;
} else {
// start signal (high)
send(1);
// send bits of char, least significant first (little endian)
for(int i = 0; i < 8; i++) {
bool bit = (data >> i) & 1;
#ifdef FLIP_DATA_BITS
bit = !bit;
#endif
send(bit);
}
// stop signal (low)
send(0);
}
}
Rs232Interpreter::Rs232Interpreter() {
reset();
}
void Rs232Interpreter::reset() {
waitingForStart = true;
idleCount = 0;
inProgressChar = 0;
}
void Rs232Interpreter::receive(bool b) {
if(waitingForStart) {
if(b == 1) {
waitingForStart = false;
nthBit = 0;
} else {
idleCount++;
send(-1); // -1 means idle
}
return;
}
if(nthBit == 8) {
// We've filled up inProgressChar. We expect this next bit to be a
// stop signal (low). If it isn't, we drop it.
if(b == 0) {
send(inProgressChar);
} else {
LOG1("Throwing away incomplete character %d", inProgressChar);
}
waitingForStart = true;
idleCount = 0;
inProgressChar = 0;
}
#ifdef FLIP_DATA_BITS
b = !b;
#endif
inProgressChar |= (b << nthBit);
nthBit++;
}
}