working, but need to write test
This commit is contained in:
+11
-8
@@ -18,7 +18,7 @@ extern "C" {
|
||||
#define NODEMCU_LED 16
|
||||
|
||||
// PWM
|
||||
#include "pwm.h"
|
||||
#include "treppe.h"
|
||||
os_timer_t timer1;
|
||||
uint8_t timer_flag = 0;
|
||||
Treppe stairs(16);
|
||||
@@ -102,25 +102,28 @@ void setup() {
|
||||
Serial.println("PCA9685 connected !");
|
||||
|
||||
os_timer_setfn(&timer1, timerCallback, &timer_flag);
|
||||
os_timer_arm(&timer1, 200, true);
|
||||
os_timer_arm(&timer1, 20, true);
|
||||
|
||||
stairs.setState(1);
|
||||
stairs.setDirection(1);
|
||||
}
|
||||
|
||||
#include <random>
|
||||
|
||||
void loop() {
|
||||
if(stairs.getState() == 0) {
|
||||
delay(1000);
|
||||
stairs.setState(1);
|
||||
|
||||
// uint32_t t = rand() % stairs.getTicks();
|
||||
// uint32_t d = rand() % 2;
|
||||
|
||||
// stairs.setTick(t);
|
||||
// stairs.setDirection(d);
|
||||
|
||||
stairs.setDirection(!stairs.getDirection());
|
||||
stairs.setState(1);
|
||||
}
|
||||
|
||||
// if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0);
|
||||
// if(millis() > 35000 && stairs.getDirection() == 1){
|
||||
// stairs.setState(1);
|
||||
// stairs.setDirection(0);
|
||||
// }
|
||||
TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA");
|
||||
TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");
|
||||
}
|
||||
-191
@@ -1,191 +0,0 @@
|
||||
#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 = 0.5;
|
||||
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::elelel()
|
||||
{
|
||||
if(state) {
|
||||
if(direction) tick++;
|
||||
else tick--;
|
||||
|
||||
// was soll gemacht werden
|
||||
pwmController.setChannelPWM(stufe,
|
||||
(uint16_t) (differenz_pwm_pro_tick * (tick % ticks_pro_stufe) + idle_brightness)
|
||||
);
|
||||
Serial.printf("tick %04d, led %02d:%02d, pwm %4.1f\n",
|
||||
tick,
|
||||
tick / ticks_pro_stufe,
|
||||
(tick % ticks_pro_stufe) ,
|
||||
(differenz_pwm_pro_tick * (tick % ticks_pro_stufe) + idle_brightness)
|
||||
);
|
||||
|
||||
if(tick < 0) { // ziel erreicht
|
||||
tick = 0;
|
||||
state = 0;
|
||||
return;
|
||||
}
|
||||
if(tick == ticks_treppe) {
|
||||
tick = ticks_treppe-1;
|
||||
state = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if(tick % ticks_pro_stufe == 0) {
|
||||
if(direction) stufe++;
|
||||
else stufe--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) {
|
||||
// Serial.println("[Treppe] ERROR, Something went wrong !");
|
||||
// state = 0;
|
||||
// return;
|
||||
// }
|
||||
|
||||
void Treppe::setup(){
|
||||
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
|
||||
|
||||
pwmController.resetDevices();
|
||||
|
||||
// Deactivate PCA9685_PhaseBalancer due to LED Flickering
|
||||
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
|
||||
// see also lib/PCA9685-Arduin/PCA9685.h:204
|
||||
pwmController.init(PCA9685_PhaseBalancer_None);
|
||||
pwmController.setPWMFrequency(200);
|
||||
Serial.println("Hello from Treppe");
|
||||
Serial.print("Treppe: initial parameters: stairs=");
|
||||
Serial.println(stairs);
|
||||
|
||||
for(uint8_t i=0; i<stairs; i++) {
|
||||
pwmController.setChannelPWM(i, idle_brightness);
|
||||
}
|
||||
}
|
||||
void Treppe::task(){
|
||||
//ledsequence();
|
||||
elelel();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user