Compare commits
2 Commits
bff8cdbeb7
...
9d1105cb80
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d1105cb80 | |||
| 4130b64370 |
@ -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;};
|
||||||
|
};
|
||||||
140
src/main.cpp
140
src/main.cpp
@ -22,35 +22,23 @@ extern "C" {
|
|||||||
#define NODEMCU_LED 16
|
#define NODEMCU_LED 16
|
||||||
|
|
||||||
// PWM
|
// PWM
|
||||||
|
|
||||||
|
#include "pwm.h"
|
||||||
os_timer_t timer1;
|
os_timer_t timer1;
|
||||||
uint8_t timer_flag = 0;
|
uint8_t timer_flag = 0;
|
||||||
PCA9685 pwmController;
|
|
||||||
|
|
||||||
void setup_pwm_pca9685();
|
Treppe stairs(13);
|
||||||
|
|
||||||
|
|
||||||
// WIFI
|
// WIFI
|
||||||
const char* ssid = STASSID;
|
const char* ssid = STASSID;
|
||||||
const char* password = STAPSK;
|
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)
|
void timerCallback(void *pArg)
|
||||||
{
|
{
|
||||||
*((int *) pArg) += 1;
|
*((int *) pArg) += 1;
|
||||||
|
stairs.task();
|
||||||
ledsequence(direction, onoff);
|
|
||||||
// Serial.print("[");
|
|
||||||
// Serial.print(micros()-m);
|
|
||||||
// Serial.print("] timerCallback\n");
|
|
||||||
// m = micros();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP
|
// HTTP
|
||||||
@ -80,111 +68,6 @@ void handleNotFound() {
|
|||||||
httpServer.send(404, "text/plain", message);
|
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() {
|
void setup() {
|
||||||
#ifdef WITH_DEBUGGING_ON
|
#ifdef WITH_DEBUGGING_ON
|
||||||
Serial.begin(460800);
|
Serial.begin(460800);
|
||||||
@ -222,18 +105,21 @@ void setup() {
|
|||||||
httpServer.onNotFound(handleNotFound);
|
httpServer.onNotFound(handleNotFound);
|
||||||
Serial.println("HTTP server started !");
|
Serial.println("HTTP server started !");
|
||||||
|
|
||||||
setup_pwm_pca9685();
|
stairs.setup();
|
||||||
Serial.println("PCA9685 connected !");
|
Serial.println("PCA9685 connected !");
|
||||||
|
|
||||||
os_timer_setfn(&timer1, timerCallback, &timer_flag);
|
os_timer_setfn(&timer1, timerCallback, &timer_flag);
|
||||||
os_timer_arm(&timer1, 20, true);
|
os_timer_arm(&timer1, 20, true);
|
||||||
|
|
||||||
|
stairs.setState(1);
|
||||||
|
stairs.setDirection(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
|
if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0);
|
||||||
if(millis() > 45000 && direction == 1){
|
if(millis() > 45000 && stairs.getDirection() == 1){
|
||||||
onoff = 1;
|
stairs.setState(1);
|
||||||
direction = 0;
|
stairs.setDirection(0);
|
||||||
}
|
}
|
||||||
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
|
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
|
||||||
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
|
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
|
||||||
|
|||||||
136
src/pwm.cpp
136
src/pwm.cpp
@ -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…
x
Reference in New Issue
Block a user