This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.cpp
189 lines (156 loc) · 5 KB
/
bluetooth.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "bluetooth.h"
#include "constants.h"
#include "led.h"
#include "logger.h"
#include "button.h"
#include <SoftwareSerial.h>
namespace bluetooth {
namespace {
SoftwareSerial BT(constants::P_SOFTWARE_SERIAL_RX, constants::P_SOFTWARE_SERIAL_TX);
const char START_OF_PACKET = 0x02; //Start of text
const char END_OF_PACKET = 0x03; //End of text
const char ACKNOWLEDGE = 0x06; //Acknowledge
const char C_BEGIN = 0x42; //"B"
const char C_END = 0x45; //"E"
const char C_TURN_OFF_LED = 0x49; //"I"
const char C_TURN_ON_LED = 0x4F; //"O"
const char C_SHIFT_OUT = 0x53; //"S"
const unsigned long PACKET_TIMEOUT = 1000;
const unsigned int ACKNOWLEDGE_TIMEOUT = 2000;
const long NO_TIMEOUT = -1;
const String BLUETOOTH_PIN = "1234";
bool isOnline = false;
volatile long acknowledgeTimeout = NO_TIMEOUT;
void acknowledgePacket() {
BT.write(ACKNOWLEDGE);
logger::log(logger::TYPE_INFO, "bluetooth", "Acknowledged packet");
}
void processPacketContent(String packetContent) {
if (packetContent.equals("")) {
return;
}
char command = packetContent[0];
String argument = packetContent.substring(1);
if (command == C_BEGIN) {
isOnline = true;
acknowledgePacket();
}
//Only analyse packet if a connection is already made
else if (isOnline) {
switch (command) {
case C_END:
isOnline = false;
break;
case C_TURN_ON_LED:
led::turnOn(argument.toInt());
break;
case C_TURN_OFF_LED:
led::turnOff(argument.toInt());
break;
case C_SHIFT_OUT:
led::shiftOut();
break;
default:
logger::log(logger::TYPE_ERROR, "bluetooth", "can't parse packet : " + packetContent);
}
acknowledgePacket();
}
}
String getPacketContent() {
String packet = "";
unsigned long timeOutTime = millis() + PACKET_TIMEOUT;
while (millis() < timeOutTime) {
if (BT.available()) {
char newByte = BT.read();
delay(10);
timeOutTime = millis() + PACKET_TIMEOUT;
if (newByte == END_OF_PACKET) {
logger::log(logger::TYPE_INFO, "bluetooth", "Received packet : " + packet);
return packet;
} else {
packet += newByte;
}
}
}
logger::log(logger::TYPE_WARNING, "bluetooth", "Timout while reading packet content: " + packet);
return ""; // If while loop ended because off timeout, end game
}
}
void checkSerial() {
while (Serial.available()) {
int value = Serial.read();
delay(10);
if (value == 80) {
String buttonNumber = "";
while (Serial.available()) {
buttonNumber += (char) Serial.read();
delay(10);
}
Serial.println("Debug button pressed : " + buttonNumber);
button::buttonPressedCallback(buttonNumber.toInt());
}
}
}
void readReceived() {
if (BT.available()) {
String unknown = "";
while (BT.available()) {
char newByte = char(BT.read());
delay(10);
switch (newByte) {
case ACKNOWLEDGE:
logger::log(logger::TYPE_INFO, "bluetooth", "Received acknowledge");
acknowledgeTimeout = NO_TIMEOUT;
break;
case START_OF_PACKET:
processPacketContent(getPacketContent());
break;
default:
unknown += newByte;
}
}
if (unknown.equals("OK+LOST")) {
isOnline = false;
} else if (unknown.equals("OK+CONN")) {
logger::log(logger::TYPE_INFO, "bluetooth", "A decive connected to the board");
} else if (unknown.equals("OK+Set:1")) {
//Response from AT command. To ignore.
} else if (not unknown.equals("")) {
logger::log(logger::TYPE_WARNING, "bluetooth", "Received unknown bytes: " + unknown);
}
}
if (constants::IS_DEBUGGING) {
checkSerial();
}
}
void listen() {
while (isOnline) {
readReceived();
if (acknowledgeTimeout != NO_TIMEOUT and millis() > acknowledgeTimeout) {
isOnline = false;
acknowledgeTimeout = NO_TIMEOUT;
logger::log(logger::TYPE_WARNING, "bluetooth", "No acknowledge received. Disconnecting");
}
}
}
void sendPacket(String packetContent) {
logger::log(logger::TYPE_INFO, "bluetooth", "Sent packet : " + packetContent);
BT.write(START_OF_PACKET);
for (int i = 0 ; i < packetContent.length(); i++) {
BT.write(packetContent.charAt(i));
}
BT.write(END_OF_PACKET);
if (acknowledgeTimeout == NO_TIMEOUT) {
acknowledgeTimeout = millis() + ACKNOWLEDGE_TIMEOUT;
}
}
bool shouldGoOnline() {
readReceived();
return isOnline;
}
void setup() {
BT.begin(9600);
BT.write("AT+NOTI1");
delay(1000); //Delay to allow chip to send response to AT command to see if device is connected
}
}