From 8c50498c7de15be760b584d30f432057cc602431 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Thu, 29 Jul 2021 16:21:25 +0100 Subject: [PATCH] Initial commit --- .gitignore | 5 +++ .vscode/extensions.json | 7 +++++ include/README | 39 +++++++++++++++++++++++ lib/README | 46 +++++++++++++++++++++++++++ platformio.ini | 30 ++++++++++++++++++ src/LED.cpp | 60 +++++++++++++++++++++++++++++++++++ src/LEDOutput.cpp | 22 +++++++++++++ src/Timer.cpp | 23 ++++++++++++++ src/main.cpp | 69 +++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++++++ 10 files changed, 312 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/LED.cpp create mode 100644 src/LEDOutput.cpp create mode 100644 src/Timer.cpp create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..c286675 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,30 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +; [env:lolin32_lite] +; platform = espressif32 +; board = lolin32_lite +; framework = arduino + +[env:lolin32] +platform = espressif32 +board = lolin32 +framework = arduino +monitor_speed = 115200 +upload_port = /dev/ttyUSB0 +monitor_port = /dev/ttyUSB0 + +; [env:nanoatmega328new] +; platform = atmelavr +; board = nanoatmega328new +; framework = arduino +; upload_port = /dev/ttyUSB0 +; monitor_speed = 115200 +; monitor_port = /dev/ttyUSB0 diff --git a/src/LED.cpp b/src/LED.cpp new file mode 100644 index 0000000..3b09dbe --- /dev/null +++ b/src/LED.cpp @@ -0,0 +1,60 @@ +#include +#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); + } +}; diff --git a/src/LEDOutput.cpp b/src/LEDOutput.cpp new file mode 100644 index 0000000..fe4a0ae --- /dev/null +++ b/src/LEDOutput.cpp @@ -0,0 +1,22 @@ +#include + +#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); + } +}; diff --git a/src/Timer.cpp b/src/Timer.cpp new file mode 100644 index 0000000..a742877 --- /dev/null +++ b/src/Timer.cpp @@ -0,0 +1,23 @@ +#include + +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; + } + } +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..23e43d6 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,69 @@ +#include "LED.cpp" +#include "Timer.cpp" +#include +#include +#include +#include + +#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(); +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html