-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication.cpp
More file actions
84 lines (66 loc) · 2.58 KB
/
communication.cpp
File metadata and controls
84 lines (66 loc) · 2.58 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
#include <SPI.h> // BLEPeripheral depends on SPI
#include <BLEPeripheral.h>
#include "BLESerial.h"
BLESerial BLESerial;
#include "pulse.h"
#include "pinout.h" // important to keep it last for the undefs to work
#include "communication.h"
#include "firmware.h"
int LEDpins[] = {14, 12}; // G, B
int LEDNum = sizeof LEDpins / sizeof LEDpins[0];
void pinsSetup() {
// LEDs & debug pins
for (int i=0; i<LEDNum; i++) {
pinMode(LEDpins[i], OUTPUT);
digitalWrite(LEDpins[i], 0); // 0 = ON (inverted logic)
delay(100);
digitalWrite(LEDpins[i], 1); // 1 = OFF (inverted logic)
}
pinMode(PIN_SERIAL_RX, OUTPUT); // TODO REMOVE (TMP)
}
void serialSetup() {
Serial.setPins(0, PIN_SERIAL_TX); // RX is not used here
Serial.begin(230400);
while (!Serial); // wait
Serial.println("Serial OK!");
}
void wirelessSetup() {
Serial.println("Connect to BLE-UART...");
BLESerial.setLocalName("HT-UART");
BLESerial.begin();
}
void sendPulseData() {
// Send binary data:
DEBUG_WRITE(0xFF); DEBUG_WRITE(0xFF); // send start headers
uint8_t base_axis = pulse_data.baseID << 1 | pulse_data.axis;
DEBUG_WRITE(base_axis); // send base ID and axis
if (BLESerial)
BLESerial.print(base_axis);
for (int t = 0; t < 2; t++) { // send captures
for (int c = 0; c < sensors_num; c += 2) {
// Invalid pulse by default
int pulseStart = pulse_data.sweep_captures[t][c];
int pulseEnd = pulse_data.sweep_captures[t][c+1];
int pulseWidthTicks16 = pulseEnd - pulseStart;
if ( pulseWidthTicks16 < minSweepPulseWidth || // ticks
pulseWidthTicks16 > maxSweepPulseWidth || // ticks
pulseStart < sweepStartTicks || // ticks
pulseEnd > sweepEndTicks ) { // ticks
// mark the measures if they are invalid
pulseStart = 0;
pulseEnd = 0;
}
// get centroid + remove 2 LSb (non-significant) to stay in 16bit
int centroid = ((pulseEnd+pulseStart)/2) >> 2;
DEBUG_WRITE((centroid >> 0) & 0xFF); // LSB first
DEBUG_WRITE((centroid >> 8) & 0xFF); // MSB last
if (BLESerial) {
BLESerial.print(' ');
BLESerial.print((centroid >> 0) & 0xFF); // LSB first
BLESerial.print((centroid >> 8) & 0xFF); // MSB last
}
}
}
if (BLESerial)
BLESerial.print('\n');
}