-
Notifications
You must be signed in to change notification settings - Fork 1
/
emitter.ino
79 lines (64 loc) · 1.37 KB
/
emitter.ino
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
#include <TimerOne.h>
#include <SoftwareSerial.h>
#include "Manchester.h"
#include "Channel.h"
#define SYMBOL_PERIOD 2000
#define NUM_SYMBOLS 20
#define SYN 0x04
#define STX 0x02
#define ETX 0x03
#define LED_PIN 12
#define BLE_RX 2
#define BLE_TX 3
Channel transmitCh, serialCh;
SoftwareSerial bleSerial(BLE_RX, BLE_TX);
unsigned long symbols = 0;
int symbolCounter = 0;
void timerInterrupt() {
if (symbols == 0 || symbolCounter == NUM_SYMBOLS) {
char ch;
if (transmitCh.get(&ch)) {
symbols = encodeWord(ch);
} else {
symbols = encodeWord(SYN);
}
symbolCounter = 0;
}
if (symbols & 0x80000) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
symbols <<= 1;
symbolCounter++;
}
void setup() {
bleSerial.begin(9600);
pinMode(LED_PIN, OUTPUT);
Timer1.initialize(SYMBOL_PERIOD);
Timer1.attachInterrupt(timerInterrupt);
}
void loop() {
if (bleSerial.available() > 0) {
int ch = bleSerial.read();
if (serialCh.available() > 0 && ch == '\n') {
char c;
while (!transmitCh.put(STX)) {
delay(1);
}
while (serialCh.get(&c)) {
while (!transmitCh.put(c)) {
delay(1);
}
}
while (!transmitCh.put(ETX)) {
delay(1);
}
} else {
if (!serialCh.put(ch)) {
serialCh.get();
serialCh.put(ch);
}
}
}
}