start with object oriented approach
This commit is contained in:
parent
bf6c241c04
commit
aded5832ea
355
src/Hanglamp.cpp
355
src/Hanglamp.cpp
@ -1,345 +1,76 @@
|
||||
#define INCLUDE_LED
|
||||
#ifdef INCLUDE_LED
|
||||
#include "Led.h"
|
||||
#endif
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
|
||||
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
||||
|
||||
#define ENABLE_MQTT
|
||||
#define ENABLE_WIFI
|
||||
// #define ENABLE_FS
|
||||
// #define ENABLE_PINFINDER
|
||||
#define ENABLE_COLORS
|
||||
// #define ENABLE_PIN_SENSING
|
||||
#define ENABLE_CONTROLS
|
||||
|
||||
// #include <TaskScheduler.h>
|
||||
|
||||
#ifdef ENABLE_WIFI
|
||||
#include "Wifi.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MQTT
|
||||
#include "MQTT.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_FS
|
||||
#include "FS.h"
|
||||
#include <LittleFS.h>
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_PINFINDER
|
||||
#include "PinFinder.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_CONTROLS
|
||||
#include "Controls.h"
|
||||
#endif
|
||||
#include "Hanglamp.hpp"
|
||||
|
||||
|
||||
|
||||
#ifdef ENABLE_COLORS
|
||||
CRGB colorTable[] = {
|
||||
CRGB(255, 243, 218),
|
||||
CRGB(255, 250, 237),
|
||||
CRGB(255, 255, 255),
|
||||
CRGB(250, 233, 213),
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
CRGB::Red,
|
||||
CRGB::Blue,
|
||||
CRGB::Green,
|
||||
CRGB::Purple,
|
||||
CRGB::Turquoise,
|
||||
CRGB::Yellow,
|
||||
|
||||
};
|
||||
|
||||
int8_t colorIndex = 0;
|
||||
|
||||
void nextColor(){
|
||||
colorIndex++;
|
||||
Hanglamp::Hanglamp(){
|
||||
|
||||
}
|
||||
|
||||
void Hanglamp::setup(){
|
||||
|
||||
}
|
||||
|
||||
void Hanglamp::setColor(CRGB color){
|
||||
jumpTo(color);
|
||||
}
|
||||
|
||||
void Hanglamp::nextColor(){
|
||||
this->colorIndex++;
|
||||
if(colorIndex >= (sizeof(colorTable) / sizeof(CRGB))){
|
||||
colorIndex = 0;
|
||||
}
|
||||
Serial.printf("colorIndex: %d\n", colorIndex);
|
||||
jumpTo(colorTable[colorIndex]);
|
||||
Serial.printf("colorIndex: %d\n", this->colorIndex);
|
||||
|
||||
setColor(colorTable[this->colorIndex]);
|
||||
}
|
||||
|
||||
void colors(void *pvParameters){
|
||||
while(true){
|
||||
nextColor();
|
||||
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_PIN_SENSING
|
||||
gpio_num_t sensePins[] = {
|
||||
(gpio_num_t) 0,
|
||||
(gpio_num_t) 1,
|
||||
(gpio_num_t) 2,
|
||||
(gpio_num_t) 3,
|
||||
(gpio_num_t) 4,
|
||||
(gpio_num_t) 5,
|
||||
(gpio_num_t) 6,
|
||||
(gpio_num_t) 7,
|
||||
(gpio_num_t) 8,
|
||||
(gpio_num_t) 9,
|
||||
(gpio_num_t) 10,
|
||||
(gpio_num_t) 20,
|
||||
(gpio_num_t) 21,
|
||||
};
|
||||
|
||||
long prevStates[sizeof(sensePins)];
|
||||
#endif
|
||||
|
||||
int16_t brightness = 128;
|
||||
|
||||
#ifdef ENABLE_CONTROLS
|
||||
|
||||
void newBrightness(){
|
||||
uint8_t newBrightness =
|
||||
void Hanglamp::setBrightness(char brightness){
|
||||
uint8_t newBrightness =
|
||||
brightness < 0 ? 0 :
|
||||
brightness > 255 ? 255 :
|
||||
brightness;
|
||||
Serial.printf("new brightness: %d\n", newBrightness);
|
||||
delay(100);
|
||||
FastLED.setBrightness(newBrightness);
|
||||
FastLED.show();
|
||||
#ifdef ENABLE_MQTT
|
||||
MQTT_publish("hanglamp/brightness", String(newBrightness));
|
||||
#endif
|
||||
this->brightness = newBrightness;
|
||||
Serial.printf("new brightness: %d\n", newBrightness);
|
||||
FastLED.setBrightness(newBrightness);
|
||||
FastLED.show();
|
||||
#ifdef ENABLE_MQTT
|
||||
MQTT_publish("hanglamp/brightness", String(newBrightness));
|
||||
#endif
|
||||
}
|
||||
|
||||
void controlsCallback(ControlEvent event){
|
||||
Serial.printf("ControlEvent: %d\n", event);
|
||||
void Hanglamp::adjustBrightness(int add){
|
||||
this->setBrightness(this->brightness + add);
|
||||
}
|
||||
|
||||
void Hanglamp::controlsCallback(ControlEvent event){
|
||||
Serial.printf("ControlEvent: %d\n", event);
|
||||
switch(event){
|
||||
case Clockwise:
|
||||
if(brightness < 256){
|
||||
brightness += 8;
|
||||
newBrightness();
|
||||
}
|
||||
adjustBrightness(1);
|
||||
break;
|
||||
case Counterclockwise:
|
||||
if(brightness > 0){
|
||||
brightness -= 8 ;
|
||||
newBrightness();
|
||||
}
|
||||
adjustBrightness(-1);
|
||||
break;
|
||||
case Key:
|
||||
nextColor();
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_LED
|
||||
|
||||
#endif
|
||||
|
||||
void setup() {
|
||||
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("Waiting...");
|
||||
delay(1500);
|
||||
Serial.println("That's long enough.");
|
||||
|
||||
|
||||
#ifdef ENABLE_PIN_SENSING
|
||||
for(int i=0; i<sizeof(sensePins)/sizeof(gpio_num_t); i++){
|
||||
auto pin = sensePins[i];
|
||||
Serial.printf("Setting pin %d to input\n", pin);
|
||||
// gpio_reset_pin(pin);
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_FS
|
||||
if(!LittleFS.begin()){
|
||||
Serial.println("LittleFS Mount Failed");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef ENABLE_PINFINDER
|
||||
PinFinder_setup();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_CONTROLS
|
||||
Controls_setup();
|
||||
|
||||
Controls_handler(controlsCallback);
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
Led_setup();
|
||||
|
||||
FastLED.setBrightness(brightness);
|
||||
|
||||
jumpTo(CRGB(0xFF00FF));
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_WIFI
|
||||
Wifi_setup();
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
jumpTo(CRGB(0x00FFFF));
|
||||
|
||||
jumpTo(CRGB(0x000000));
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MQTT
|
||||
MQTT_setup();
|
||||
#endif
|
||||
#ifdef ENABLE_OTA
|
||||
OTA_setup();
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
jumpTo(CRGB(0x00FFFF));
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef ENABLE_COLORS
|
||||
TaskHandle_t taskColors = NULL;
|
||||
|
||||
|
||||
Serial.println("Scheduling tasks");
|
||||
|
||||
|
||||
// Create the task, storing the handle.
|
||||
// xTaskCreate(colors, "NAME", 1000, NULL, tskIDLE_PRIORITY, &taskColors);
|
||||
Serial.println("Scheduled tasks");
|
||||
|
||||
#endif
|
||||
|
||||
// ESP.wdtEnable(10000);
|
||||
Serial.println("Initialized scheduler");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
#ifdef ENABLE_OTA
|
||||
OTA_loop();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_PINFINDER
|
||||
PinFinder_loop();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_PIN_SENSING
|
||||
for(int i=0; i<sizeof(sensePins)/sizeof(gpio_num_t); i++){
|
||||
auto pin = sensePins[i];
|
||||
pinMode(pin, INPUT);
|
||||
auto currentState = analogRead(pin);
|
||||
long diff = currentState - prevStates[i];
|
||||
// if(abs(diff) > 10){
|
||||
Serial.printf("pin#%02d %d->%d\n", pin, prevStates[i], currentState);
|
||||
// }
|
||||
prevStates[i] = currentState;
|
||||
}
|
||||
|
||||
|
||||
for(int i=0; i<sizeof(sensePins)/sizeof(gpio_num_t); i++){
|
||||
break;
|
||||
auto pin = sensePins[i];
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, HIGH);
|
||||
Serial.printf("pin#%02d HIGH\n", pin);
|
||||
delay(1000);
|
||||
digitalWrite(pin, LOW);
|
||||
}
|
||||
#endif
|
||||
|
||||
delay(1000);
|
||||
|
||||
String command;
|
||||
|
||||
while(Serial.available()) {
|
||||
|
||||
command = Serial.readString();// read the incoming data as string
|
||||
|
||||
Serial.print("Have command: ");
|
||||
Serial.println(command);
|
||||
|
||||
int index = command.indexOf(":");
|
||||
if(index >= 0){
|
||||
String name = command.substring(0, index);
|
||||
Serial.print("Have command name: ");
|
||||
Serial.println(name);
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
if(name.equals("c")){
|
||||
//c:0xff4000
|
||||
//c:0xff0000
|
||||
//c:0x00ff00
|
||||
//c:0x0000ff
|
||||
String color = command.substring(index+1);
|
||||
if(color.startsWith("0x")){
|
||||
color = color.substring(2);
|
||||
}
|
||||
|
||||
int value = strtol(color.c_str(), NULL, 16);
|
||||
|
||||
CRGB target;
|
||||
target.r = (value >> 16) & 0xFF;
|
||||
target.g = (value >> 8) & 0xFF;
|
||||
target.b = (value >> 0) & 0xFF;
|
||||
|
||||
jumpTo(target);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
void Hanglamp::status(CRGB color){
|
||||
Led_status(color);
|
||||
}
|
||||
24
src/Hanglamp.hpp
Normal file
24
src/Hanglamp.hpp
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <FastLED.h>
|
||||
|
||||
#include "Led.h"
|
||||
#include "Controls.h"
|
||||
|
||||
class Hanglamp {
|
||||
public:
|
||||
Hanglamp();
|
||||
void setup();
|
||||
void setBrightness(char brightness);
|
||||
void adjustBrightness(int add);
|
||||
void nextColor();
|
||||
void controlsCallback(ControlEvent event);
|
||||
void status(CRGB color);
|
||||
void setColor(CRGB color);
|
||||
|
||||
|
||||
private:
|
||||
char colorIndex = 0;
|
||||
int brightness = 128;
|
||||
|
||||
};
|
||||
10
src/Led.cpp
10
src/Led.cpp
@ -123,8 +123,8 @@ void Led_setup(){
|
||||
// analogWriteRange(255);
|
||||
|
||||
#ifdef LED_FASTLED
|
||||
FastLED.addLeds<WS2812B, PIN_STRIP1>(strip2, NUM_LEDS);
|
||||
FastLED.addLeds<WS2812B, PIN_STRIP2>(strip2, NUM_LEDS);
|
||||
FastLED.addLeds<WS2812B, PIN_STRIP1, GRB>(strip2, NUM_LEDS);
|
||||
FastLED.addLeds<WS2812B, PIN_STRIP2, GRB>(strip2, NUM_LEDS);
|
||||
strip1[0] = CRGB::Black;
|
||||
strip2[0] = CRGB::Black;
|
||||
#endif
|
||||
@ -156,4 +156,10 @@ void Led_loop(){
|
||||
strip2[15] = CRGB(50, 255, 20);
|
||||
FastLED.show();
|
||||
delay(500);
|
||||
}
|
||||
|
||||
void Led_status(CRGB color){
|
||||
strip1[0] = color;
|
||||
strip2[0] = color;
|
||||
FastLED.show();
|
||||
}
|
||||
@ -35,4 +35,6 @@ void Led_loop();
|
||||
void jumpTo(const CRGB & dest);
|
||||
|
||||
void fadeTo(const CRGB & dest);
|
||||
void fadeTask();
|
||||
void fadeTask();
|
||||
|
||||
void Led_status(CRGB color);
|
||||
187
src/main.cpp
Normal file
187
src/main.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
#define INCLUDE_LED
|
||||
#ifdef INCLUDE_LED
|
||||
#include "Led.h"
|
||||
#endif
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
|
||||
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
||||
|
||||
#define ENABLE_MQTT
|
||||
#define ENABLE_WIFI
|
||||
// #define ENABLE_FS
|
||||
// #define ENABLE_PINFINDER
|
||||
#define ENABLE_COLORS
|
||||
// #define ENABLE_PIN_SENSING
|
||||
#define ENABLE_CONTROLS
|
||||
|
||||
#ifdef ENABLE_WIFI
|
||||
#include "Wifi.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MQTT
|
||||
#include "MQTT.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_FS
|
||||
#include "FS.h"
|
||||
#include <LittleFS.h>
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_PINFINDER
|
||||
#include "PinFinder.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_CONTROLS
|
||||
#include "Controls.h"
|
||||
#endif
|
||||
|
||||
#include "Hanglamp.hpp"
|
||||
|
||||
Hanglamp hanglamp;
|
||||
|
||||
|
||||
#ifdef ENABLE_CONTROLS
|
||||
|
||||
void controlsCallback(ControlEvent event){
|
||||
hanglamp.controlsCallback(event);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_LED
|
||||
|
||||
#endif
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("Waiting...");
|
||||
delay(1500);
|
||||
Serial.println("That's long enough.");
|
||||
|
||||
|
||||
|
||||
#ifdef ENABLE_PIN_SENSING
|
||||
for(int i=0; i<sizeof(sensePins)/sizeof(gpio_num_t); i++){
|
||||
auto pin = sensePins[i];
|
||||
Serial.printf("Setting pin %d to input\n", pin);
|
||||
// gpio_reset_pin(pin);
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_FS
|
||||
if(!LittleFS.begin()){
|
||||
Serial.println("LittleFS Mount Failed");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef ENABLE_PINFINDER
|
||||
PinFinder_setup();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_CONTROLS
|
||||
Controls_setup();
|
||||
|
||||
Controls_handler(controlsCallback);
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
Led_setup();
|
||||
|
||||
FastLED.setBrightness(128);
|
||||
|
||||
|
||||
hanglamp.status(CRGB(0xFF00FF));
|
||||
#endif
|
||||
|
||||
hanglamp.setup();
|
||||
|
||||
#ifdef ENABLE_WIFI
|
||||
Wifi_setup();
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
hanglamp.status(CRGB(0x00FFFF));
|
||||
|
||||
hanglamp.status(CRGB(0x000000));
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MQTT
|
||||
MQTT_setup();
|
||||
#endif
|
||||
#ifdef ENABLE_OTA
|
||||
OTA_setup();
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
hanglamp.status(CRGB(0x00FFFF));
|
||||
#endif
|
||||
|
||||
delay(1000);
|
||||
|
||||
hanglamp.status(CRGB::Black);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
#ifdef ENABLE_OTA
|
||||
OTA_loop();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_PINFINDER
|
||||
PinFinder_loop();
|
||||
#endif
|
||||
|
||||
delay(1000);
|
||||
|
||||
String command;
|
||||
|
||||
while(Serial.available()) {
|
||||
|
||||
command = Serial.readString();// read the incoming data as string
|
||||
|
||||
Serial.print("Have command: ");
|
||||
Serial.println(command);
|
||||
|
||||
int index = command.indexOf(":");
|
||||
if(index >= 0){
|
||||
String name = command.substring(0, index);
|
||||
Serial.print("Have command name: ");
|
||||
Serial.println(name);
|
||||
|
||||
#ifdef INCLUDE_LED
|
||||
if(name.equals("c")){
|
||||
//c:0xff4000
|
||||
//c:0xff0000
|
||||
//c:0x00ff00
|
||||
//c:0x0000ff
|
||||
String color = command.substring(index+1);
|
||||
if(color.startsWith("0x")){
|
||||
color = color.substring(2);
|
||||
}
|
||||
|
||||
int value = strtol(color.c_str(), NULL, 16);
|
||||
|
||||
CRGB target;
|
||||
target.r = (value >> 16) & 0xFF;
|
||||
target.g = (value >> 8) & 0xFF;
|
||||
target.b = (value >> 0) & 0xFF;
|
||||
|
||||
hanglamp.setColor(target);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user