This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth.cpp
138 lines (112 loc) · 3.79 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
#include <ArduinoBLE.h>
#include <vector>
#include "Arduino.h"
#include "Bluetooth.h"
void Bluetooth::initialize() {
Serial.println("Starting BLE...");
if (!BLE.begin()) {
Serial.println("BLE initialization failed!");
}
BLE.setLocalName("3181 LED Controller");
BLE.setAdvertisedService(m_ledService);
m_ledService.addCharacteristic(m_brightnessCharacteristic);
m_ledService.addCharacteristic(m_styleCharacteristic);
m_ledService.addCharacteristic(m_styleNamesCharacteristic);
m_ledService.addCharacteristic(m_speedCharacteristic);
m_ledService.addCharacteristic(m_stepCharacteristic);
m_ledService.addCharacteristic(m_patternCharacteristic);
m_ledService.addCharacteristic(m_patternNamesCharacteristic);
m_ledService.addCharacteristic(m_batteryVoltageCharacteristic);
BLE.addService(m_ledService);
BLE.advertise();
}
void Bluetooth::stop() {
// Disconnect any clients and stop advertising.
BLE.disconnect();
BLE.stopAdvertise();
}
void Bluetooth::resume() {
BLE.advertise();
}
void Bluetooth::setStyleNames(std::vector<String> styleNames) {
String allStyles = *joinStrings(styleNames);
Serial.print("All style names: ");
Serial.println(allStyles);
Serial.print("Style name string length: ");
Serial.println(allStyles.length());
m_styleNamesCharacteristic.setValue(allStyles);
}
void Bluetooth::setPatternNames(std::vector<String> patternNames) {
String allPatterns = *joinStrings(patternNames);
Serial.print("All pattern names: ");
Serial.println(allPatterns);
Serial.print("Pattern name string length: ");
Serial.println(allPatterns.length());
m_patternNamesCharacteristic.setValue(allPatterns);
}
byte Bluetooth::getBrightness() {
m_currentBrightness = readByteFromCharacteristic(m_brightnessCharacteristic, m_currentBrightness, "brightness");
return m_currentBrightness;
}
void Bluetooth::setBrightness(byte brightness) {
m_currentBrightness = brightness;
m_brightnessCharacteristic.setValue(brightness);
}
byte Bluetooth::getStyle() {
m_currentStyle = readByteFromCharacteristic(m_styleCharacteristic, m_currentStyle, "style");
return m_currentStyle;
}
void Bluetooth::setStyle(byte style) {
m_currentStyle = style;
m_styleCharacteristic.setValue(style);
}
byte Bluetooth::getSpeed() {
m_currentSpeed = readByteFromCharacteristic(m_speedCharacteristic, m_currentSpeed, "speed");
return m_currentSpeed;
}
void Bluetooth::setSpeed(byte speed) {
m_currentSpeed = speed;
m_speedCharacteristic.setValue(speed);
}
byte Bluetooth::getPattern() {
m_currentPattern = readByteFromCharacteristic(m_patternCharacteristic, m_currentPattern, "pattern");
return m_currentPattern;
}
void Bluetooth::setPattern(byte pattern) {
m_currentPattern = pattern;
m_patternCharacteristic.setValue(pattern);
}
byte Bluetooth::getStep() {
m_currentStep = readByteFromCharacteristic(m_stepCharacteristic, m_currentStep, "step");
return m_currentStep;
}
void Bluetooth::setStep(byte step) {
m_currentStep = step;
m_stepCharacteristic.setValue(step);
}
void Bluetooth::emitBatteryVoltage(float voltage) {
m_batteryVoltageCharacteristic.setValue(voltage);
}
byte Bluetooth::readByteFromCharacteristic(BLEByteCharacteristic characteristic, byte defaultValue, String name) {
if (BLE.connected()) {
if (characteristic.written()) {
Serial.print("Reading new value for ");
Serial.print(name);
byte valByte = characteristic.value();
Serial.print(". Byte received: ");
Serial.println(valByte, HEX);
return valByte;
}
}
return defaultValue;
}
String* Bluetooth::joinStrings(std::vector<String> strings) {
String* joinedStrings = new String();
for (int i = 0; i < strings.size(); i++) {
joinedStrings->concat(strings.at(i));
if (i < strings.size()-1) {
joinedStrings->concat(";");
}
}
return joinedStrings;
}