-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLECommunicator.cpp
168 lines (142 loc) · 5.82 KB
/
BLECommunicator.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
#include "BLECommunicator.h"
#include "BLEConstants.h"
BLECommunicator::BLECommunicator(Sensors *sensors, MultiMotorControl *motors)
{
_sensors = sensors;
_motors = motors;
// begin initialization
if (BLE.begin())
{
BLEService sensorsService(BLEConstants::SENSORS_SERVICE_UUID);
BLEService commandsService(BLEConstants::COMMANDS_SERVICE_UUID);
BLETypedCharacteristic<int8_t> temperatureCharacteristic(BLEConstants::TEMPERATURE_CHAR_UUID, BLERead | BLENotify);
BLETypedCharacteristic<int8_t> humidityCharacteristic(BLEConstants::HUMIDITY_CHAR_UUID, BLERead | BLENotify);
BLETypedCharacteristic<float> pressureCharacteristic(BLEConstants::PRESSUERE_CHAR_UUID, BLERead | BLENotify);
BLETypedCharacteristic<Coordinates> accCoordinatesCharacteristic(BLEConstants::ACC_COORDS_UUID, BLERead | BLENotify);
BLETypedCharacteristic<Coordinates> magCoordinatesCharacteristic(BLEConstants::MAG_COORDS_UUID, BLERead | BLENotify);
BLETypedCharacteristic<Coordinates> gyroCoordinatesCharacteristic(BLEConstants::GYRO_COORDS_UUID, BLERead | BLENotify);
BLECharacteristic motorSpeedCharacteristic(BLEConstants::MOTORS_SPEED_UUID, BLERead | BLEWrite, 1, true);
BLE.setLocalName(BLEConstants::BLUETOOTH_NAME);
BLE.setDeviceName(BLEConstants::BLUETOOTH_NAME);
BLE.setAppearance(BLEConstants::GENERIC_AIRCRAFT);
BLE.setAdvertisedService(sensorsService);
sensorsService.addCharacteristic(temperatureCharacteristic);
sensorsService.addCharacteristic(humidityCharacteristic);
sensorsService.addCharacteristic(pressureCharacteristic);
sensorsService.addCharacteristic(accCoordinatesCharacteristic);
sensorsService.addCharacteristic(magCoordinatesCharacteristic);
sensorsService.addCharacteristic(gyroCoordinatesCharacteristic);
// add service
BLE.addService(sensorsService);
commandsService.addCharacteristic(motorSpeedCharacteristic);
//motorSpeedCharacteristic.setEventHandler(BLEWritten, BLECommunicator::motorsThrottled);
BLE.addService(commandsService);
// set the initial value for the characeristic:
temperatureCharacteristic.writeValue(_temperature);
humidityCharacteristic.writeValue(_humidity);
pressureCharacteristic.writeValue(_pressure);
accCoordinatesCharacteristic.writeValue(_accCoords);
magCoordinatesCharacteristic.writeValue(_magCoords);
gyroCoordinatesCharacteristic.writeValue(_gyroCoords);
uint8_t motor = 0;
motorSpeedCharacteristic.writeValue(motor);
_temperatureCharacteristic = &temperatureCharacteristic;
_humidityCharacteristic = &humidityCharacteristic;
_pressureCharacteristic = &pressureCharacteristic;
_accCoordinatesCharacteristic = &accCoordinatesCharacteristic;
_magCoordinatesCharacteristic = &magCoordinatesCharacteristic;
_gyroCoordinatesCharacteristic = &gyroCoordinatesCharacteristic;
// start advertising
BLE.advertise();
}
}
// void BLECommunicator::motorsThrottled(BLEDevice central, BLECharacteristic characteristic)
// {
// uint8_t throttle = characteristic.value<uint8_t>();
// rampAllMotors(throttle);
// }
void BLECommunicator::listenForConnections()
{
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central)
{
// while the central is still connected to peripheral:
while (central.connected())
{
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (_motorSpeedCharacteristic->written())
{
uint8_t motorPercentage = 0;
_motorSpeedCharacteristic->readValue(motorPercentage);
}
if (_temperature != _sensors->GetTemperature())
{
_temperature = _sensors->GetTemperature();
_temperatureCharacteristic->writeValue(_temperature);
}
if (_pressure != _sensors->GetPressure())
{
_pressure = _sensors->GetPressure();
_pressureCharacteristic->writeValue(_pressure);
}
if (_humidity != _sensors->GetHumidity())
{
_humidity = _sensors->GetHumidity();
_humidityCharacteristic->writeValue(_humidity);
}
delay(500);
}
}
}
void BLECommunicator::rampAllMotors(uint8_t throttle)
{
_motors->getMotor(0)->ChangeThrottle(throttle);
_motors->getMotor(1)->ChangeThrottle(throttle);
_motors->getMotor(2)->ChangeThrottle(throttle);
_motors->getMotor(3)->ChangeThrottle(throttle);
}
void BLECommunicator::setTemperature(int8_t temperature)
{
if (temperature != _temperature)
{
this->_temperatureCharacteristic->writeValue(temperature);
}
}
void BLECommunicator::setPressure(float pressure)
{
if (pressure != _pressure)
{
this->_humidityCharacteristic->writeValue(pressure);
}
}
void BLECommunicator::setHumidity(int8_t humidity)
{
if (humidity != _humidity)
{
this->_pressureCharacteristic->writeValue(humidity);
}
}
void BLECommunicator::setAccerelationDirection(Coordinates coords)
{
_accCoordinatesCharacteristic->writeValue(coords);
}
void BLECommunicator::setGyroscopeDirection(Coordinates coords)
{
_gyroCoordinatesCharacteristic->writeValue(coords);
}
void BLECommunicator::setMagnetometerDirection(Coordinates coords)
{
_magCoordinatesCharacteristic->writeValue(coords);
}
bool BLECommunicator::tryReadMotorSpeeds(uint8_t &motorPercent)
{
if (_motorSpeedCharacteristic->valueUpdated())
{
_motorSpeedCharacteristic->readValue(motorPercent);
return true;
}
return false;
}