Browse Source

test

tags/FSM1.0
Simon Schmidt 3 years ago
parent
commit
55ebd21975
4 changed files with 92 additions and 16 deletions
  1. 3
    0
      doku.md
  2. 21
    6
      include/pwm.h
  3. 14
    7
      src/main.cpp
  4. 54
    3
      src/pwm.cpp

+ 3
- 0
doku.md View File

https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15 https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15


PlatformIO Library veraltet PlatformIO Library veraltet


###TIMER in OTA unterbrechen !!!!!

+ 21
- 6
include/pwm.h View File

#include "PCA9685.h" #include "PCA9685.h"


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


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


uint32_t tick = 0;
uint32_t stufe = 0;

uint32_t ticks_treppe = 0;
uint32_t ticks_pro_stufe = 0;
float differenz_pwm_pro_tick = 0.0;

// initialize with i2c-Address 0, use Wire Library // initialize with i2c-Address 0, use Wire Library
PCA9685 pwmController; PCA9685 pwmController;
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval); uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
void ledsequence(); void ledsequence();


public:
Treppe(uint8_t _stairs) : stairs(_stairs){}
public:
Treppe(uint8_t _stairs) : stairs(_stairs){
ticks_pro_stufe = time_per_stair / 20; // [ms]
ticks_treppe = ticks_pro_stufe * stairs;

differenz_pwm_pro_tick = (active_brightness - idle_brightness)
/ ticks_pro_stufe;

}
void task(); // call periodically void task(); // call periodically


void setup(); void setup();
void elelel();


// Parameter section // Parameter section
uint16_t setIdle(uint16_t _idle_brightness); uint16_t setIdle(uint16_t _idle_brightness);

+ 14
- 7
src/main.cpp View File

#include "pwm.h" #include "pwm.h"
os_timer_t timer1; os_timer_t timer1;
uint8_t timer_flag = 0; uint8_t timer_flag = 0;
Treppe stairs(13);
Treppe stairs(16);


// WIFI // WIFI
const char* ssid = STASSID; const char* ssid = STASSID;
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, 200, true);


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



void loop() { void loop() {
if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0);
if(millis() > 45000 && stairs.getDirection() == 1){
if(stairs.getState() == 0) {
delay(1000);
stairs.setState(1); stairs.setState(1);
stairs.setDirection(0);
stairs.setDirection(!stairs.getDirection());
} }
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
// 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");
} }

+ 54
- 3
src/pwm.cpp View File



static uint8_t lastled = 255; static uint8_t lastled = 255;
static float current_pwm = 0; static float current_pwm = 0;
static float stepsize = 1.0;
static float stepsize = 0.5;
if(led != lastled){ if(led != lastled){
pwmController.setChannelPWM(led, (uint16_t)startval); pwmController.setChannelPWM(led, (uint16_t)startval);
lastled = led; lastled = led;
static uint8_t finish = 1; static uint8_t finish = 1;
static uint16_t status = 0; static uint16_t status = 0;
uint16_t status_build = 0; uint16_t status_build = 0;

status_build |= direction << 8; status_build |= direction << 8;
status_build |= state; status_build |= state;
if(status_build != status){ // check if any parameter changed if(status_build != status){ // check if any parameter changed
} }
} }




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(){ void Treppe::setup(){
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
pwmController.resetDevices(); pwmController.resetDevices();
// Deactive PCA9685 due to LED Flickering
// Deactivate PCA9685_PhaseBalancer due to LED Flickering
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15 // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
// see also lib/PCA9685-Arduin/PCA9685.h:204 // see also lib/PCA9685-Arduin/PCA9685.h:204
pwmController.init(PCA9685_PhaseBalancer_None); pwmController.init(PCA9685_PhaseBalancer_None);
Serial.println("Hello from Treppe"); Serial.println("Hello from Treppe");
Serial.print("Treppe: initial parameters: stairs="); Serial.print("Treppe: initial parameters: stairs=");
Serial.println(stairs); Serial.println(stairs);
for(uint8_t i=0; i<stairs; i++) {
pwmController.setChannelPWM(i, idle_brightness);
}
} }
void Treppe::task(){ void Treppe::task(){
ledsequence();
//ledsequence();
elelel();
} }


uint16_t Treppe::setIdle(uint16_t _idle_brightness){ uint16_t Treppe::setIdle(uint16_t _idle_brightness){

Loading…
Cancel
Save