-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathopenmili.ino
140 lines (127 loc) · 3.25 KB
/
openmili.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
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
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <printf.h>
#include "PL1167_nRF24.h"
#include "MiLightRadio.h"
#define CE_PIN 8
#define CSN_PIN 53
RF24 radio(CE_PIN, CSN_PIN);
PL1167_nRF24 prf(radio);
MiLightRadio mlr(prf);
void setup()
{
Serial.begin(115200);
printf_begin();
delay(1000);
Serial.println("# OpenMiLight Receiver/Transmitter starting");
mlr.begin();
}
static int dupesPrinted = 0;
static bool receiving = false;
static bool escaped = false;
static uint8_t outgoingPacket[7];
static uint8_t outgoingPacketPos = 0;
static uint8_t nibble;
static enum {
IDLE,
HAVE_NIBBLE,
COMPLETE,
} state;
void loop()
{
if (receiving) {
if (mlr.available()) {
printf("\n");
uint8_t packet[7];
size_t packet_length = sizeof(packet);
mlr.read(packet, packet_length);
for (int i = 0; i < packet_length; i++) {
printf("%02X ", packet[i]);
}
}
int dupesReceived = mlr.dupesReceived();
for (; dupesPrinted < dupesReceived; dupesPrinted++) {
printf(".");
}
}
while (Serial.available()) {
char inChar = (char)Serial.read();
uint8_t val = 0;
bool have_val = true;
if (inChar >= '0' && inChar <= '9') {
val = inChar - '0';
} else if (inChar >= 'a' && inChar <= 'f') {
val = inChar - 'a' + 10;
} else if (inChar >= 'A' && inChar <= 'F') {
val = inChar - 'A' + 10;
} else {
have_val = false;
}
if (!escaped) {
if (have_val) {
switch (state) {
case IDLE:
nibble = val;
state = HAVE_NIBBLE;
break;
case HAVE_NIBBLE:
if (outgoingPacketPos < sizeof(outgoingPacket)) {
outgoingPacket[outgoingPacketPos++] = (nibble << 4) | (val);
} else {
Serial.println("# Error: outgoing packet buffer full/packet too long");
}
if (outgoingPacketPos >= sizeof(outgoingPacket)) {
state = COMPLETE;
} else {
state = IDLE;
}
break;
case COMPLETE:
Serial.println("# Error: outgoing packet complete. Press enter to send.");
break;
}
} else {
switch (inChar) {
case ' ':
case '\n':
case '\r':
case '.':
if (state == COMPLETE) {
mlr.write(outgoingPacket, sizeof(outgoingPacket));
}
if(inChar != ' ') {
outgoingPacketPos = 0;
state = IDLE;
}
if (inChar == '.') {
mlr.resend();
delay(1);
}
break;
case 'x':
Serial.println("# Escaped to extended commands: r - Toggle receiver; Press enter to return to normal mode.");
escaped = true;
break;
}
}
} else {
switch (inChar) {
case '\n':
case '\r':
outgoingPacketPos = 0;
state = IDLE;
escaped = false;
break;
case 'r':
receiving = !receiving;
if (receiving) {
Serial.println("# Now receiving");
} else {
Serial.println("# Now not receiving");
}
break;
}
}
}
}