Initial commit
This commit is contained in:
commit
8c50498c7d
10 changed files with 312 additions and 0 deletions
60
src/LED.cpp
Normal file
60
src/LED.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include <Arduino.h>
|
||||
#include "LEDOutput.cpp"
|
||||
|
||||
class LED{
|
||||
LEDOutput* _output;
|
||||
bool _on;
|
||||
unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd;
|
||||
|
||||
unsigned long getFadeDuration(){
|
||||
return _on ? _fadeDurationOn : _fadeDurationOff;
|
||||
}
|
||||
|
||||
unsigned long getRemainingFadeTime(){
|
||||
unsigned long now = millis();
|
||||
return _fadeEnd >= now ? constrain(_fadeEnd - now, 0, getFadeDuration()) : 0;
|
||||
}
|
||||
|
||||
float getMultiplier() {
|
||||
float value = getRemainingFadeTime() / (float)getFadeDuration();
|
||||
return 1.0f - value;
|
||||
}
|
||||
|
||||
float getOutputMultiplier(){
|
||||
float value = getMultiplier();
|
||||
return _on ? value : 1.0f - value;
|
||||
}
|
||||
|
||||
void reset(bool on){
|
||||
if (on == _on)
|
||||
return;
|
||||
float oldMultiplier = getMultiplier();
|
||||
_on = on;
|
||||
_fadeStart = millis();
|
||||
_fadeEnd = _fadeStart + (getFadeDuration() * oldMultiplier);
|
||||
}
|
||||
|
||||
public:
|
||||
LED(LEDOutput* output, unsigned long fadeDurationOn, unsigned long fadeDurationOff) {
|
||||
_output = output;
|
||||
_fadeDurationOn = fadeDurationOn;
|
||||
_fadeDurationOff = fadeDurationOff;
|
||||
}
|
||||
|
||||
void on(){
|
||||
reset(true);
|
||||
}
|
||||
|
||||
void off(){
|
||||
reset(false);
|
||||
}
|
||||
|
||||
void toggle(){
|
||||
reset(!_on);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float multiplier = getOutputMultiplier();
|
||||
_output->writeFraction(1 - multiplier);
|
||||
}
|
||||
};
|
22
src/LEDOutput.cpp
Normal file
22
src/LEDOutput.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#define LED_OUTPUT_FREQUENCY 490
|
||||
#define LED_OUTPUT_RESOLUTION 15
|
||||
#define LED_OUTPUT_PWM_RANGE 32767
|
||||
|
||||
class LEDOutput {
|
||||
unsigned int _channel;
|
||||
|
||||
public:
|
||||
LEDOutput(unsigned int channel) {
|
||||
_channel = channel;
|
||||
ledcSetup(channel, LED_OUTPUT_FREQUENCY, LED_OUTPUT_RESOLUTION);
|
||||
}
|
||||
|
||||
void attach(unsigned int pin){
|
||||
ledcAttachPin(pin, _channel);
|
||||
}
|
||||
void writeFraction(float fraction){
|
||||
ledcWrite(_channel, fraction * LED_OUTPUT_PWM_RANGE);
|
||||
}
|
||||
};
|
23
src/Timer.cpp
Normal file
23
src/Timer.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
class Timer{
|
||||
private:
|
||||
unsigned long _interval, _lastTick;
|
||||
void (*_callback)(void);
|
||||
|
||||
public:
|
||||
Timer(unsigned long interval, void (*callback)(void)){
|
||||
_interval = interval;
|
||||
_callback = callback;
|
||||
_lastTick = millis();
|
||||
}
|
||||
|
||||
void loop(){
|
||||
unsigned long tick = millis();
|
||||
|
||||
if (tick - _lastTick >= _interval){
|
||||
_callback();
|
||||
_lastTick = tick;
|
||||
}
|
||||
}
|
||||
};
|
69
src/main.cpp
Normal file
69
src/main.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include "LED.cpp"
|
||||
#include "Timer.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);
|
||||
LED _led(&_ledOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||
|
||||
void toggle(){
|
||||
//_led.toggle();
|
||||
}
|
||||
|
||||
Timer _timer(3000, &toggle);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
_ledOutput.attach(16);
|
||||
_ledOutput.attach(17);
|
||||
_ledOutput.attach(5);
|
||||
_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();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
_led.loop();
|
||||
_timer.loop();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue