Move bluetooth management in to own classes
This commit is contained in:
parent
0432071759
commit
e1a279ee75
5 changed files with 73 additions and 39 deletions
23
src/BluetoothLEDCallback.cpp
Normal file
23
src/BluetoothLEDCallback.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "LED.cpp"
|
||||
#include <BLEUtils.h>
|
||||
|
||||
class BluetoothLEDCallback : public BLECharacteristicCallbacks {
|
||||
LED *_led;
|
||||
void onWrite(BLECharacteristic *characteristic) {
|
||||
std::string value = characteristic->getValue();
|
||||
if (value.length() > 0) {
|
||||
Serial.println(value.c_str());
|
||||
if (value == "on")
|
||||
_led->on();
|
||||
else
|
||||
_led->off();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
BluetoothLEDCallback(LED* led){
|
||||
_led = led;
|
||||
}
|
||||
|
||||
};
|
33
src/BluetoothService.cpp
Normal file
33
src/BluetoothService.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <Arduino.h>
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
|
||||
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
||||
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
||||
|
||||
class BluetoothService {
|
||||
const char *_serviceName;
|
||||
BLECharacteristicCallbacks *_callbacks;
|
||||
BLEServer *_server;
|
||||
BLEService *_service;
|
||||
BLECharacteristic *_characteristic;
|
||||
|
||||
public:
|
||||
BluetoothService(const char* serviceName, BLECharacteristicCallbacks* callbacks) {
|
||||
_serviceName = serviceName;
|
||||
_callbacks = callbacks;
|
||||
}
|
||||
|
||||
void init() {
|
||||
BLEDevice::init(_serviceName);
|
||||
_server = BLEDevice::createServer();
|
||||
_service = _server->createService(SERVICE_UUID);
|
||||
_characteristic = _service->createCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
|
||||
_characteristic->setCallbacks(_callbacks);
|
||||
}
|
||||
|
||||
void start() {
|
||||
_service->start();
|
||||
_server->getAdvertising()->start();
|
||||
}
|
||||
};
|
|
@ -1,3 +1,6 @@
|
|||
#ifndef LED_cpp
|
||||
#define LED_cpp
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "LEDOutput.cpp"
|
||||
|
||||
|
@ -57,3 +60,5 @@ public:
|
|||
_output->writeFraction(getOutputMultiplier());
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#ifndef LEDOutput_cpp
|
||||
#define LEDOutput_cpp
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define LED_OUTPUT_FREQUENCY 490
|
||||
|
@ -25,3 +28,5 @@ public:
|
|||
ledcWrite(_channel, value);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
46
src/main.cpp
46
src/main.cpp
|
@ -1,40 +1,18 @@
|
|||
#include "LED.cpp"
|
||||
#include "Timer.cpp"
|
||||
#include "BluetoothLEDCallback.cpp"
|
||||
#include "BluetoothService.cpp"
|
||||
#include <Arduino.h>
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
|
||||
#define FADE_IN_DURATION 500
|
||||
#define FADE_OUT_DURATION 2000
|
||||
|
||||
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
||||
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
||||
|
||||
class BluetoothCallback : public BLECharacteristicCallbacks {
|
||||
LED *_led;
|
||||
void onWrite(BLECharacteristic *pCharacteristic) {
|
||||
std::string value = pCharacteristic->getValue();
|
||||
if (value.length() > 0) {
|
||||
Serial.println(value.c_str());
|
||||
if (value == "on")
|
||||
_led->on();
|
||||
else
|
||||
_led->off();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
BluetoothCallback(LED* led){
|
||||
_led = led;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
LEDOutput _ledOutput(0, true);
|
||||
LED _led(&_ledOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||
|
||||
BluetoothLEDCallback _btCallback(&_led);
|
||||
BluetoothService _btService("Van Lights", &_btCallback);
|
||||
|
||||
void toggle(){
|
||||
//_led.toggle();
|
||||
}
|
||||
|
@ -49,18 +27,8 @@ void setup() {
|
|||
_ledOutput.attach(18);
|
||||
_ledOutput.attach(23);
|
||||
|
||||
BLEDevice::init("Van Lights");
|
||||
BLEServer *pServer = BLEDevice::createServer();
|
||||
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||
BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
|
||||
|
||||
pCharacteristic->setCallbacks(new BluetoothCallback(&_led));
|
||||
|
||||
pCharacteristic->setValue("Hello World");
|
||||
pService->start();
|
||||
|
||||
BLEAdvertising *pAdvertising = pServer->getAdvertising();
|
||||
pAdvertising->start();
|
||||
_btService.init();
|
||||
_btService.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue