-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcom.cpp
More file actions
155 lines (135 loc) · 4.42 KB
/
Copy pathcom.cpp
File metadata and controls
155 lines (135 loc) · 4.42 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "../includes/com.h"
#include "../includes/debug.h"
#include <Arduino.h>
#include <SoftwareSerial.h>
const byte codeSerialTxPin = 2;
const byte codeSerialRxPin = 3;
const byte debugSerialTxPin = 4;
const byte debugSerialRxPin = 5;
// Use for "up" communication to receive code from the lower block
// and sent it to the upper one
SoftwareSerial codeSerial(codeSerialRxPin, codeSerialTxPin);
// Used for "down" communication to receive debug counter from
// the upper block and send it to the lower one
SoftwareSerial debugSerial(debugSerialRxPin, debugSerialTxPin);
byte rxBuffer[BUFFER_SIZE]; // rx buffer for code received from lowers blocks
byte txBuffer[BUFFER_SIZE]; // tx buffer for code to send to the upper block
int rxCtn; // incremented to fill rxBuffer
int txCtn; // increment to fill txBuffer
boolean readingFrame; // indicates if we are reading a frame or not
void comInit() {
// debugSerial.begin(9600); // initialize debug serial with baud rate of 9600
codeSerial.begin(9600); // initialize code serial with baud rate of 9600
codeSerial.listen();
pinMode(LED_BUILTIN, OUTPUT); // set led built in output as output
// Initialize counters
rxCtn = 0;
txCtn = 0;
// Wait for frame a to read
readingFrame = false;
}
void resetFrame() {
txCtn = 0;
}
boolean addHeader() {
return addChar(START_SYMBOL);
}
boolean addTail() {
return addChar(END_SYMBOL);
}
boolean addChar(char data) {
// Clear buffer if too big
if (txCtn >= BUFFER_SIZE) {
ERROR_PRINTLN("TxBuffer full !");
txCtn = 0;
return false;
}
// ... fill buffer otherwise
txBuffer[txCtn] = data;
txCtn++;
return true;
}
boolean addChars(char* array, byte size) {
for(int i = 0; i < size; i++) {
if(addChar(array[i]) == false) {
return false;
}
}
return true;
}
void sendFrame() {
if(txCtn >= BUFFER_SIZE) {
ERROR_PRINT("Unable to send frame, txCtn (");
ERROR_PRINT(txCtn);
ERROR_PRINT(") is bigger than BUFFER_SIZE (");
ERROR_PRINTLN(BUFFER_SIZE);
}
INFO_PRINT("Send frame (");
INFO_PRINT(txCtn - 1); // txCtn is incremented when the last char has been added
INFO_PRINT(" chars long): ");
for(int i = 0; i < txCtn; i++) {
Serial.write(txBuffer[i]);
codeSerial.write(txBuffer[i]);
}
INFO_PRINTLN("");
}
int readCodeRx() {
return 0;
}
int processCodeRx() {
int res = codeSerial.available();
if(res == 0) {
// no data
} else if (res == -1) {
// serial unavailable
FATAL_PRINTLN("Serial unavailable");
} else {
char receivedData = codeSerial.read();
// If the start symbol is received, start frame reading
if(receivedData == START_SYMBOL) {
readingFrame = true;
rxCtn = 0;
} else if (readingFrame == true) { // If we are reading frame
// If the end symbol is received, frame is completed
if(receivedData == END_SYMBOL) {
return rxCtn;
} else if (rxCtn == BUFFER_SIZE) { // if the buffer is full (we missed the end symbol ?)
// we reset frame reading
readingFrame = false;
rxCtn = 0;
} else { // if not, we simply store data
rxBuffer[rxCtn] = receivedData;
rxCtn++;
}
}
}
return 0;
}
void processDebugSerial() {
int res = debugSerial.available();
if(res == 0) {
// no data
// INFO_PRINTLN("Debug serial - No data");
} else if (res == -1) {
// serial unavailable
//FATAL_PRINTLN("Debug serial unavailable");
} else {
byte serialDebugCounter = debugSerial.read();
if (serialDebugCounter == 0) {
// If current block receives "0", it turns it built-in led on
INFO_PRINTLN("Debug serial - Current block is addressed");
digitalWrite(LED_BUILTIN, HIGH);
} else {
// otherwise, debug counter is decremented and sent to the lower block
INFO_PRINT("Debug serial - Send to lower block: ")
INFO_PRINTLN(serialDebugCounter - 1)
digitalWrite(LED_BUILTIN, LOW);
debugSerial.write(serialDebugCounter - 1);
}
}
}
void copyFrameData(char* dataArray, int dataSize) {
for(int i = 0; i < dataSize; i ++) {
dataArray[i] = rxBuffer[i];
}
}