Browse Source

object oriented stairlight control

tags/LEDFlickr
Dominik Bartsch 3 years ago
parent
commit
4130b64370
8 changed files with 193 additions and 191 deletions
  1. 1
    5
      compress/favicon.png.gz.h
  2. 2
    44
      compress/index.html.gz.h
  3. 1
    5
      compress/input.js.gz.h
  4. 1
    5
      compress/light.png.gz.h
  5. 1
    5
      compress/style.css.gz.h
  6. 38
    0
      include/pwm.h
  7. 13
    127
      src/main.cpp
  8. 136
    0
      src/pwm.cpp

+ 1
- 5
compress/favicon.png.gz.h View File

@@ -1,11 +1,7 @@
// filename: favicon.png.gz.h
#define favicon_png_gz_len 554
const char favicon_png_gz[] = {
<<<<<<< HEAD
0x1F, 0x8B, 0x08, 0x00, 0x3F, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
=======
0x1F, 0x8B, 0x08, 0x00, 0x06, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
>>>>>>> b62f4f9f95b7086fc97d9242705342fa1bebf0a9
0x1F, 0x8B, 0x08, 0x00, 0xCE, 0x89, 0xD3, 0x60, 0x02, 0xFF,
0x01, 0x13, 0x02, 0xEC, 0xFD, 0x89, 0x50, 0x4E, 0x47, 0x0D,
0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
0x52, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x08,

+ 2
- 44
compress/index.html.gz.h View File

@@ -1,48 +1,7 @@
// filename: index.html.gz.h
#define index_html_gz_len 407
const char index_html_gz[] = {
<<<<<<< HEAD
0x1F, 0x8B, 0x08, 0x00, 0x3F, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
0xC5, 0x53, 0x3D, 0x4F, 0xC3, 0x30, 0x10, 0xDD, 0x91, 0xF8,
0x0F, 0x57, 0xEF, 0x69, 0x52, 0x86, 0x0A, 0x81, 0x93, 0x05,
0x2A, 0x31, 0x20, 0x51, 0x89, 0x2E, 0x4C, 0xC8, 0x49, 0x2E,
0xC9, 0x51, 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F,
0xE5, 0x94, 0xC1, 0x4A, 0x03, 0x31, 0x10, 0x86, 0xEF, 0x82,
0xEF, 0x30, 0xE6, 0xBE, 0x4D, 0xEB, 0x41, 0x44, 0x77, 0x0B,
0x62, 0x0B, 0x1E, 0x04, 0x05, 0x7B, 0xD1, 0x8B, 0x64, 0x77,
@@ -82,5 +41,4 @@ const char index_html_gz[] = {
0xB8, 0xE0, 0x71, 0x5E, 0x38, 0x69, 0x0A, 0x4D, 0x9D, 0x05,
0xA3, 0x0B, 0x37, 0x80, 0x7C, 0xB1, 0xC1, 0x8B, 0x1B, 0x2F,
0x29, 0x0F, 0x81, 0x90, 0xC4, 0xFD, 0x14, 0x7D, 0x07, 0x40,
0x6E, 0x4D, 0x01, 0x5D, 0x05, 0x00, 0x00};
>>>>>>> b62f4f9f95b7086fc97d9242705342fa1bebf0a9
0x6E, 0x4D, 0x01, 0x5D, 0x05, 0x00, 0x00};

+ 1
- 5
compress/input.js.gz.h View File

@@ -1,11 +1,7 @@
// filename: input.js.gz.h
#define input_js_gz_len 205
const char input_js_gz[] = {
<<<<<<< HEAD
0x1F, 0x8B, 0x08, 0x00, 0x3F, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
=======
0x1F, 0x8B, 0x08, 0x00, 0x06, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
>>>>>>> b62f4f9f95b7086fc97d9242705342fa1bebf0a9
0x1F, 0x8B, 0x08, 0x00, 0xCE, 0x89, 0xD3, 0x60, 0x02, 0xFF,
0x7D, 0xCF, 0x31, 0x0B, 0xC2, 0x30, 0x10, 0x05, 0xE0, 0x3D,
0x90, 0xFF, 0x70, 0x38, 0xB5, 0x4B, 0x8B, 0xB3, 0xB8, 0x88,
0x82, 0x82, 0x6E, 0x3A, 0xCB, 0xD1, 0x9C, 0x6D, 0x20, 0xBD,

+ 1
- 5
compress/light.png.gz.h View File

@@ -1,11 +1,7 @@
// filename: light.png.gz.h
#define light_png_gz_len 11408
const char light_png_gz[] = {
<<<<<<< HEAD
0x1F, 0x8B, 0x08, 0x00, 0x3F, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
=======
0x1F, 0x8B, 0x08, 0x00, 0x06, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
>>>>>>> b62f4f9f95b7086fc97d9242705342fa1bebf0a9
0x1F, 0x8B, 0x08, 0x00, 0xCE, 0x89, 0xD3, 0x60, 0x02, 0xFF,
0xED, 0x7A, 0x57, 0x5C, 0x13, 0xD1, 0xB7, 0xEE, 0xD0, 0x04,
0x44, 0x8A, 0x80, 0xA0, 0x94, 0x80, 0xD2, 0x04, 0xA4, 0x37,
0x21, 0x34, 0x0B, 0x25, 0x74, 0x01, 0xE9, 0x01, 0x29, 0x52,

+ 1
- 5
compress/style.css.gz.h View File

@@ -1,11 +1,7 @@
// filename: style.css.gz.h
#define style_css_gz_len 1091
const char style_css_gz[] = {
<<<<<<< HEAD
0x1F, 0x8B, 0x08, 0x00, 0x3F, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
=======
0x1F, 0x8B, 0x08, 0x00, 0x06, 0x4E, 0xD3, 0x60, 0x02, 0xFF,
>>>>>>> b62f4f9f95b7086fc97d9242705342fa1bebf0a9
0x1F, 0x8B, 0x08, 0x00, 0xCE, 0x89, 0xD3, 0x60, 0x02, 0xFF,
0xBD, 0x56, 0x4D, 0x6F, 0xDB, 0x46, 0x10, 0xBD, 0x1B, 0xF0,
0x7F, 0x18, 0x24, 0x08, 0x60, 0xA5, 0xA2, 0x43, 0x29, 0xB2,
0xAD, 0xD0, 0xE8, 0xC1, 0x70, 0x93, 0xFA, 0xD0, 0x20, 0x40,

+ 38
- 0
include/pwm.h View File

@@ -0,0 +1,38 @@
#pragma once

#include "PCA9685.h"

class Treppe {
private:
uint8_t stairs;
uint16_t time_per_stair = 300; // dimmtime per stair [ms]
uint16_t idle_brightness = 200;
uint16_t active_brightness = 2048;

uint8_t direction = 0;
uint8_t state = 0;
uint8_t switch_state = 0;


PCA9685 pwmController;
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
void ledsequence();

public:
Treppe(uint8_t _stairs) : stairs(_stairs){}
void task(); // call periodically

void setup();

// Parameter section
uint16_t setIdle(uint16_t _idle_brightness);
uint16_t setActive(uint16_t _active_brightness);
uint16_t setTime(uint16_t _time_per_stair);

// Runtime Parameter section
uint8_t setDirection(uint8_t _direction);
uint8_t setState(uint8_t _state);

uint8_t getState() { return state;};
uint8_t getDirection() {return direction;};
};

+ 13
- 127
src/main.cpp View File

@@ -22,35 +22,23 @@ extern "C" {
#define NODEMCU_LED 16

// PWM

#include "pwm.h"
os_timer_t timer1;
uint8_t timer_flag = 0;
PCA9685 pwmController;

void setup_pwm_pca9685();
Treppe stairs(13);


// WIFI
const char* ssid = STASSID;
const char* password = STAPSK;

// PWM
uint32_t dimmtimer = 0;
uint16_t time_per_stair = 500; // global parameter: dimmtime per stair [ms]
uint8_t direction = 1;
uint8_t onoff = 1;


void ledsequence(uint8_t direction, uint8_t onoff);
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);

void timerCallback(void *pArg)
{
*((int *) pArg) += 1;
ledsequence(direction, onoff);
// Serial.print("[");
// Serial.print(micros()-m);
// Serial.print("] timerCallback\n");
// m = micros();
stairs.task();
}

// HTTP
@@ -80,111 +68,6 @@ void handleNotFound() {
httpServer.send(404, "text/plain", message);
}

void setup_pwm_pca9685() {
pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line

pwmController.init(B000000); // Address pins A5-A0 set to B000000
pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz

Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
}


uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){

/*
softstart task

- get's called at regular intervals (1ms at the moment)
- dimms single led (0 - 15, PCA9685 outputs) with linear intervals vom startval to stopval
- calculates pwm steps depending on startval, stopval and timeinterval
- -> results in constanst speed
- returns 1 if led dimming is running
- returns 0 if led dimming is finished

*/

static uint8_t lastled = 255;
static float current_pwm = 0;
static float stepsize = 1.0;
if(led != lastled){
pwmController.setChannelPWM(led, (uint16_t)startval);
lastled = led;
current_pwm = startval;
stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
return 1;
}
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0;
// todo: duty cycle zero!
if(startval > stopval){
current_pwm -= stepsize;
}
else {
current_pwm += stepsize;
}
Serial.println((uint16_t)current_pwm);
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
return 1;
}

#define LEDCOUNT 16

void ledsequence(uint8_t direction, uint8_t onoff){
static int8_t led = 0;
static uint16_t brightness = 0;
static uint16_t lastbrightness = 0;
static uint8_t finish = 1;
static uint16_t status = 0;
uint16_t status_build = 0;
status_build |= direction << 8;
status_build |= onoff;
if(status_build != status){ // check if any parameter changed
finish = 0; // set state unfinished -> start action
if(direction) led = 0; // reset led counter depending of direction
else led = LEDCOUNT-1;
if(onoff){
brightness = 2048; // set brightness value depending of on/off
lastbrightness = 200;
}
else{
brightness = 200;
lastbrightness = 2048;
}
status = status_build; // set parameter memory
Serial.print("----Status Changed! onoff: ");
Serial.print(onoff);
Serial.print(" dir: ");
Serial.println(direction);

}
if(!finish){ // finish == 0 -> action pending
if(!softstart_led(led,lastbrightness, brightness)){
Serial.print("one LED finished: ");
Serial.print(led);
Serial.print(" last: ");
Serial.print(lastbrightness);
Serial.print(" curr: ");
Serial.println(brightness);
if(direction){
led++;
if(led >= LEDCOUNT) {
finish = 1;
//lastbrightness = brightness;
}
}
else{
led--;
if(led < 0){
//lastbrightness = brightness;
finish = 1;
}
}
}
}
}


void setup() {
#ifdef WITH_DEBUGGING_ON
Serial.begin(460800);
@@ -222,18 +105,21 @@ void setup() {
httpServer.onNotFound(handleNotFound);
Serial.println("HTTP server started !");

setup_pwm_pca9685();
stairs.setup();
Serial.println("PCA9685 connected !");

os_timer_setfn(&timer1, timerCallback, &timer_flag);
os_timer_arm(&timer1, 20, true);

stairs.setState(1);
stairs.setDirection(1);
}

void loop() {
if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
if(millis() > 45000 && direction == 1){
onoff = 1;
direction = 0;
if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0);
if(millis() > 45000 && stairs.getDirection() == 1){
stairs.setState(1);
stairs.setDirection(0);
}
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");

+ 136
- 0
src/pwm.cpp View File

@@ -0,0 +1,136 @@
#include "pwm.h"

uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){
/*
softstart task

- get's called at regular intervals (1ms at the moment)
- dimms single led (0 - 15, PCA9685 outputs) with linear intervals vom startval to stopval
- calculates pwm steps depending on startval, stopval and timeinterval
- -> results in constanst speed
- returns 1 if led dimming is running
- returns 0 if led dimming is finished

*/

static uint8_t lastled = 255;
static float current_pwm = 0;
static float stepsize = 1.0;
if(led != lastled){
pwmController.setChannelPWM(led, (uint16_t)startval);
lastled = led;
current_pwm = startval;
stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
return 1;
}
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0;
// todo: duty cycle zero!
if(startval > stopval){
current_pwm -= stepsize;
}
else {
current_pwm += stepsize;
}
Serial.println((uint16_t)current_pwm);
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
return 1;
}

void Treppe::ledsequence(){
static int8_t led = 0;
static uint16_t brightness = 0;
static uint16_t lastbrightness = 0;
static uint8_t finish = 1;
static uint16_t status = 0;
uint16_t status_build = 0;
status_build |= direction << 8;
status_build |= state;
if(status_build != status){ // check if any parameter changed
finish = 0; // set state unfinished -> start action
if(direction) led = 0; // reset led counter depending of direction
else led = stairs-1;
if(state){
brightness = active_brightness; // set brightness value depending of on/off
lastbrightness = idle_brightness;
}
else{
brightness = idle_brightness;
lastbrightness = active_brightness;
}
status = status_build; // set parameter memory
Serial.print("----Status Changed! onoff: ");
Serial.print(state);
Serial.print(" dir: ");
Serial.println(direction);

}
if(!finish){ // finish == 0 -> action pending
if(!softstart_led(led,lastbrightness, brightness)){
Serial.print("one LED finished: ");
Serial.print(led);
Serial.print(" last: ");
Serial.print(lastbrightness);
Serial.print(" curr: ");
Serial.println(brightness);
if(direction){
led++;
if(led >= stairs) {
finish = 1;
//lastbrightness = brightness;
}
}
else{
led--;
if(led < 0){
//lastbrightness = brightness;
finish = 1;
}
}
}
}
}

void Treppe::setup(){
pwmController.resetDevices();
pwmController.init(B000000);
pwmController.setPWMFrequency(200);
Serial.println("Hello from Treppe");
Serial.print("Treppe: initial parameters: stairs=");
Serial.println(stairs);
}
void Treppe::task(){
ledsequence();
}

uint16_t Treppe::setIdle(uint16_t _idle_brightness){
idle_brightness = _idle_brightness;
Serial.println("Treppe: idle brightness changed!");
return idle_brightness;
}
uint16_t Treppe::setActive(uint16_t _active_brightness){
active_brightness = _active_brightness;
Serial.println("Treppe: active brightness changed!");
return active_brightness;
}
uint16_t Treppe::setTime(uint16_t _time_per_stair){
time_per_stair = _time_per_stair;
Serial.println("Treppe: time changed!");
return time_per_stair;
}

uint8_t Treppe::setDirection(uint8_t _direction){
direction = _direction;
Serial.println("Treppe: Direction changed!");
// to do: implement state command variable to determine dimm-state
return direction;
}

uint8_t Treppe::setState(uint8_t _state){
if(state == _state) return 1;
else{
state = _state;
Serial.println("Treppe: State changed!");
}
return 0;
}

Loading…
Cancel
Save