Initial commit

This commit is contained in:
Robert Marshall 2021-07-29 16:21:25 +01:00
commit 8c50498c7d
10 changed files with 312 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

7
.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}

39
include/README Normal file
View file

@ -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

46
lib/README Normal file
View file

@ -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 <Foo.h>
#include <Bar.h>
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

30
platformio.ini Normal file
View file

@ -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

60
src/LED.cpp Normal file
View 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
View 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
View 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
View 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();
}

11
test/README Normal file
View file

@ -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