-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlePilot.cpp
62 lines (49 loc) · 1.61 KB
/
BlePilot.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
#include "BlePilot.h"
#include "Utils.h"
BlePilot::BlePilot() {
}
void BlePilot::begin() {
started = BLE.begin();
if (!started) {
if (Serial) Serial.println("Failed to initialize BLE");
}
pinMode(LED_BUILTIN, OUTPUT);
BLE.setLocalName("flollow-me");
BLE.setAdvertisedService(service);
service.addCharacteristic(headingCharacteristic);
service.addCharacteristic(switchCharacteristic);
BLE.addService(service);
BLE.advertise();
headingCharacteristic.writeValue(0.186);
if (Serial) Serial.println("BLE service started");
}
void BlePilot::update() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
if (!central && connectedToCentral) {
// central disconnected
connectedToCentral = false;
if (Serial) Serial.print("Disconnected from central");
}
if (central && !connectedToCentral) {
// central connected
connectedToCentral = true;
if (Serial) {
Serial.print("Connected to central: ");
Serial.println(central.address());
}
}
// while the central is still connected to peripheral:
if (central.connected()) {
// if the remote device wrote to the characteristic, use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
if (Serial) Serial.println("LED on");
digitalWrite(LED_BUILTIN, HIGH); // will turn the LED on
} else { // a 0 value
if (Serial) Serial.println(F("LED off"));
digitalWrite(LED_BUILTIN, LOW); // will turn the LED off
}
}
}
}