2021-06-25 18:35:16 +02:00
|
|
|
#include "treppe.h"
|
2021-06-23 21:23:31 +02:00
|
|
|
|
|
|
|
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;
|
2021-06-25 19:23:02 +02:00
|
|
|
static float stepsize = 1.0;
|
2021-06-23 21:23:31 +02:00
|
|
|
if(led != lastled){
|
|
|
|
pwmController.setChannelPWM(led, (uint16_t)startval);
|
|
|
|
lastled = led;
|
|
|
|
current_pwm = startval;
|
2021-06-25 19:23:02 +02:00
|
|
|
stepsize = INT_TIME*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
|
2021-06-23 21:23:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(startval > stopval){
|
|
|
|
current_pwm -= stepsize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
current_pwm += stepsize;
|
|
|
|
}
|
2021-06-25 21:10:07 +02:00
|
|
|
// Serial.println((uint16_t)current_pwm);
|
2021-06-23 21:23:31 +02:00
|
|
|
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
|
2021-06-25 19:23:02 +02:00
|
|
|
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){
|
|
|
|
if(stopval == 0) pwmController.setChannelPWM(led, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-23 21:23:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Treppe::ledsequence(){
|
|
|
|
static int8_t led = 0;
|
|
|
|
static uint16_t brightness = 0;
|
|
|
|
static uint16_t lastbrightness = 0;
|
|
|
|
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
|
|
|
|
|
2021-06-25 21:10:07 +02:00
|
|
|
Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction);
|
2021-06-23 21:23:31 +02:00
|
|
|
}
|
|
|
|
if(!finish){ // finish == 0 -> action pending
|
2021-06-25 21:10:07 +02:00
|
|
|
if(!softstart_led(led,lastbrightness, brightness)){
|
|
|
|
Serial.printf("one LED finished: led: %d, last: %d, curr %d\n",
|
|
|
|
led, lastbrightness, brightness);
|
|
|
|
|
|
|
|
if(direction){
|
|
|
|
led++;
|
|
|
|
if(led >= stairs)
|
|
|
|
finish = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
led--;
|
|
|
|
if(led < 0)
|
2021-06-23 21:23:31 +02:00
|
|
|
finish = 1;
|
|
|
|
}
|
2021-06-25 21:10:07 +02:00
|
|
|
}
|
2021-06-23 21:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 21:10:07 +02:00
|
|
|
void Treppe::rampe()
|
2021-06-25 04:53:06 +02:00
|
|
|
{
|
|
|
|
if(state) {
|
2021-06-25 21:10:07 +02:00
|
|
|
finish = 0;
|
2021-07-03 11:21:18 +02:00
|
|
|
state = 0;// set parameter memory
|
2021-06-25 21:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!finish) {
|
2021-06-25 18:35:16 +02:00
|
|
|
if(direction) { // aufwärts
|
|
|
|
if(tick >= ticks_treppe-1) { // ziel erreicht
|
|
|
|
Serial.println("[Treppe] oberster tick !");
|
2021-06-25 21:10:07 +02:00
|
|
|
finish = 1;
|
2021-06-25 18:35:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tick++; // eins hoch
|
2021-06-25 04:53:06 +02:00
|
|
|
}
|
2021-06-25 18:35:16 +02:00
|
|
|
else { // abwärts
|
|
|
|
if(tick <= 0) { // ziel erreicht
|
|
|
|
Serial.println("[Treppe] unterster tick !");
|
2021-06-25 21:10:07 +02:00
|
|
|
finish = 1;
|
2021-06-25 18:35:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tick--; // eins runter
|
2021-06-25 04:53:06 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:35:16 +02:00
|
|
|
stufe = tick / ticks_pro_stufe;
|
2021-06-25 21:10:07 +02:00
|
|
|
|
|
|
|
float new_pwm = 0.0;
|
|
|
|
if(an_aus) {
|
|
|
|
new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
|
|
|
|
new_pwm += idle_brightness;
|
|
|
|
if(direction) new_pwm += differenz_pwm_pro_tick;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_pwm = active_brightness - differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
|
|
|
|
new_pwm += idle_brightness;
|
|
|
|
if(direction) new_pwm -= differenz_pwm_pro_tick;
|
|
|
|
}
|
2021-06-25 18:35:16 +02:00
|
|
|
|
|
|
|
pwmController.setChannelPWM(stufe, (uint16_t) new_pwm);
|
|
|
|
Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
|
|
|
|
tick,
|
|
|
|
stufe,
|
|
|
|
(tick - ticks_pro_stufe*stufe),
|
|
|
|
new_pwm
|
|
|
|
);
|
2021-06-25 04:53:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 21:23:31 +02:00
|
|
|
void Treppe::setup(){
|
2021-07-03 12:48:17 +02:00
|
|
|
|
2021-06-23 21:23:31 +02:00
|
|
|
pwmController.resetDevices();
|
2021-06-25 19:23:02 +02:00
|
|
|
// Deactive PCA9685 Phase Balancer due to LED Flickering
|
2021-06-24 16:51:38 +02:00
|
|
|
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
|
|
|
|
// see also lib/PCA9685-Arduin/PCA9685.h:204
|
2021-06-30 16:50:00 +02:00
|
|
|
pwmController.init(PCA9685_PhaseBalancer_None);
|
|
|
|
//pwmController.init(PCA9685_PhaseBalancer_Linear);
|
2021-06-30 16:09:46 +02:00
|
|
|
pwmController.setPWMFrequency(100);
|
2021-06-25 19:23:02 +02:00
|
|
|
pwmController.setAllChannelsPWM(idle_brightness);
|
2021-06-30 16:09:46 +02:00
|
|
|
|
|
|
|
pinMode(A0, INPUT);
|
2021-07-03 12:48:17 +02:00
|
|
|
pinMode(SENSOR_OBEN, INPUT);
|
|
|
|
pinMode(SENSOR_UNTEN, INPUT);
|
2021-06-28 15:47:10 +02:00
|
|
|
pinMode(OE, OUTPUT);
|
|
|
|
digitalWrite(OE, 0);
|
2021-07-03 12:48:17 +02:00
|
|
|
|
|
|
|
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
|
2021-06-23 21:23:31 +02:00
|
|
|
Serial.println("Hello from Treppe");
|
|
|
|
Serial.print("Treppe: initial parameters: stairs=");
|
|
|
|
Serial.println(stairs);
|
|
|
|
}
|
2021-06-25 18:35:16 +02:00
|
|
|
|
2021-07-03 12:48:17 +02:00
|
|
|
void Treppe::print_state_on_change() {
|
|
|
|
static FSMTreppeModelClass::ExtU_FSMTreppe_T last_in;
|
|
|
|
static FSMTreppeModelClass::ExtY_FSMTreppe_T last_out;
|
|
|
|
if(
|
|
|
|
fsm_inputs.anim_finished != last_in.anim_finished ||
|
|
|
|
fsm_inputs.sensor_oben != last_in.sensor_oben ||
|
|
|
|
fsm_inputs.sensor_unten != last_in.sensor_unten ||
|
|
|
|
fsm_outputs.anim_active != last_out.anim_active
|
|
|
|
) {
|
|
|
|
last_in.anim_finished = fsm_inputs.anim_finished;
|
|
|
|
last_in.sensor_oben = fsm_inputs.sensor_oben;
|
|
|
|
last_in.sensor_unten = fsm_inputs.sensor_unten;
|
|
|
|
last_out.anim_active = fsm_outputs.anim_active;
|
2021-06-25 19:23:02 +02:00
|
|
|
|
2021-07-03 12:48:17 +02:00
|
|
|
Serial.printf("FSM inputs: s_u: %d, s_o: %d, an_fin: %d =>",
|
|
|
|
fsm_inputs.sensor_oben, fsm_inputs.sensor_unten, fsm_inputs.anim_finished);
|
|
|
|
Serial.print(" step => ");
|
|
|
|
Serial.printf("FSM outputs: an_act: %d\n", fsm_outputs.anim_active);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Treppe::task(){
|
2021-06-25 19:23:02 +02:00
|
|
|
if(finish){
|
|
|
|
direction = switch_direction;
|
|
|
|
state = switch_state;
|
|
|
|
}
|
|
|
|
static uint8_t last_sensor_state[2] = {0,0};
|
|
|
|
uint8_t current_sensor_state[2] = {0,0};
|
2021-07-03 12:48:17 +02:00
|
|
|
current_sensor_state[0] = digitalRead(SENSOR_OBEN);
|
|
|
|
current_sensor_state[1] = digitalRead(SENSOR_UNTEN);
|
|
|
|
|
|
|
|
fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
|
|
|
|
fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
|
|
|
|
fsm_inputs.anim_finished = static_cast<bool>(finish);
|
|
|
|
|
|
|
|
FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
|
|
|
|
FSMTreppe_Obj.step();
|
|
|
|
fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
|
|
|
|
print_state_on_change();
|
|
|
|
|
2021-06-25 19:23:02 +02:00
|
|
|
if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){
|
2021-06-25 21:10:07 +02:00
|
|
|
setTick(0);
|
|
|
|
setAnAus(1);
|
2021-06-25 19:23:02 +02:00
|
|
|
setDirection(1);
|
|
|
|
setState(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){
|
2021-06-25 21:10:07 +02:00
|
|
|
setTick(0);
|
|
|
|
setAnAus(0);
|
2021-06-25 19:23:02 +02:00
|
|
|
setDirection(0);
|
|
|
|
setState(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// first switch - off approach, use timer later
|
|
|
|
if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){
|
2021-06-25 21:10:07 +02:00
|
|
|
setTick(ticks_treppe);
|
|
|
|
setAnAus(1);
|
2021-06-25 19:23:02 +02:00
|
|
|
setDirection(1);
|
|
|
|
setState(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){
|
2021-06-25 21:10:07 +02:00
|
|
|
setTick(ticks_treppe);
|
|
|
|
setAnAus(1);
|
2021-06-25 19:23:02 +02:00
|
|
|
setDirection(0);
|
|
|
|
setState(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_sensor_state[0] = current_sensor_state[0];
|
|
|
|
last_sensor_state[1] = current_sensor_state[1];
|
2021-06-28 15:47:10 +02:00
|
|
|
|
2021-06-25 19:23:02 +02:00
|
|
|
ledsequence();
|
2021-07-02 16:25:54 +02:00
|
|
|
//Serial.printf("LDR: %f\n", ((float)analogRead(A0))/1023.*3.68);
|
2021-06-23 21:23:31 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 19:23:02 +02:00
|
|
|
|
2021-06-23 21:23:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-25 21:10:07 +02:00
|
|
|
void Treppe::setDirection(uint8_t _direction){
|
2021-06-25 19:23:02 +02:00
|
|
|
switch_direction = _direction;
|
2021-06-25 21:10:07 +02:00
|
|
|
Serial.printf("Treppe: switch_direction=%d!\n", switch_direction);
|
2021-06-25 19:23:02 +02:00
|
|
|
if(finish) Serial.println("apply direction request immediately");
|
|
|
|
else Serial.println("currently active, dir change afterwards");
|
2021-06-23 21:23:31 +02:00
|
|
|
// to do: implement state command variable to determine dimm-state
|
|
|
|
}
|
|
|
|
|
2021-06-25 21:10:07 +02:00
|
|
|
void Treppe::setState(uint8_t _state){
|
|
|
|
if(state == _state) return;
|
|
|
|
else {
|
|
|
|
switch_state = _state;
|
|
|
|
Serial.printf("Treppe: switch_state=%d!\n", switch_state);
|
|
|
|
if(finish) Serial.println("apply state request immediately");
|
|
|
|
else Serial.println("currently active, state changes after activity");
|
|
|
|
}
|
2021-06-25 19:23:02 +02:00
|
|
|
}
|