weitere anpassungen an pwm
This commit is contained in:
parent
c7eaffda00
commit
b47f778fc1
@ -30,7 +30,7 @@ uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){
|
|||||||
else {
|
else {
|
||||||
current_pwm += stepsize;
|
current_pwm += stepsize;
|
||||||
}
|
}
|
||||||
Serial.println((uint16_t)current_pwm);
|
// Serial.println((uint16_t)current_pwm);
|
||||||
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
|
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
|
||||||
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){
|
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){
|
||||||
if(stopval == 0) pwmController.setChannelPWM(led, 0);
|
if(stopval == 0) pwmController.setChannelPWM(led, 0);
|
||||||
@ -60,46 +60,40 @@ void Treppe::ledsequence(){
|
|||||||
lastbrightness = active_brightness;
|
lastbrightness = active_brightness;
|
||||||
}
|
}
|
||||||
status = status_build; // set parameter memory
|
status = status_build; // set parameter memory
|
||||||
Serial.print("----Status Changed! onoff: ");
|
|
||||||
Serial.print(state);
|
|
||||||
Serial.print(" dir: ");
|
|
||||||
Serial.println(direction);
|
|
||||||
|
|
||||||
|
Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction);
|
||||||
}
|
}
|
||||||
if(!finish){ // finish == 0 -> action pending
|
if(!finish){ // finish == 0 -> action pending
|
||||||
if(!softstart_led(led,lastbrightness, brightness)){
|
if(!softstart_led(led,lastbrightness, brightness)){
|
||||||
Serial.print("one LED finished: ");
|
Serial.printf("one LED finished: led: %d, last: %d, curr %d\n",
|
||||||
Serial.print(led);
|
led, lastbrightness, brightness);
|
||||||
Serial.print(" last: ");
|
|
||||||
Serial.print(lastbrightness);
|
if(direction){
|
||||||
Serial.print(" curr: ");
|
led++;
|
||||||
Serial.println(brightness);
|
if(led >= stairs)
|
||||||
if(direction){
|
finish = 1;
|
||||||
led++;
|
|
||||||
if(led >= stairs) {
|
|
||||||
finish = 1;
|
|
||||||
//lastbrightness = brightness;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
led--;
|
|
||||||
if(led < 0){
|
|
||||||
//lastbrightness = brightness;
|
|
||||||
finish = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
led--;
|
||||||
|
if(led < 0)
|
||||||
|
finish = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Treppe::rampe()
|
||||||
void Treppe::task_2()
|
|
||||||
{
|
{
|
||||||
if(state) {
|
if(state) {
|
||||||
|
finish = 0;
|
||||||
|
state = 0; // set parameter memory
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!finish) {
|
||||||
if(direction) { // aufwärts
|
if(direction) { // aufwärts
|
||||||
if(tick >= ticks_treppe-1) { // ziel erreicht
|
if(tick >= ticks_treppe-1) { // ziel erreicht
|
||||||
Serial.println("[Treppe] oberster tick !");
|
Serial.println("[Treppe] oberster tick !");
|
||||||
state = 0;
|
finish = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tick++; // eins hoch
|
tick++; // eins hoch
|
||||||
@ -107,17 +101,25 @@ void Treppe::task_2()
|
|||||||
else { // abwärts
|
else { // abwärts
|
||||||
if(tick <= 0) { // ziel erreicht
|
if(tick <= 0) { // ziel erreicht
|
||||||
Serial.println("[Treppe] unterster tick !");
|
Serial.println("[Treppe] unterster tick !");
|
||||||
state = 0;
|
finish = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tick--; // eins runter
|
tick--; // eins runter
|
||||||
}
|
}
|
||||||
|
|
||||||
stufe = tick / ticks_pro_stufe;
|
stufe = tick / ticks_pro_stufe;
|
||||||
float new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
|
|
||||||
if(direction)
|
float new_pwm = 0.0;
|
||||||
new_pwm += differenz_pwm_pro_tick;
|
if(an_aus) {
|
||||||
new_pwm += idle_brightness;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
pwmController.setChannelPWM(stufe, (uint16_t) new_pwm);
|
pwmController.setChannelPWM(stufe, (uint16_t) new_pwm);
|
||||||
Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
|
Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
|
||||||
@ -129,12 +131,6 @@ void Treppe::task_2()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
|
||||||
|
|
||||||
@ -166,30 +162,38 @@ void Treppe::task(){
|
|||||||
current_sensor_state[1] = digitalRead(SENSOR2);
|
current_sensor_state[1] = digitalRead(SENSOR2);
|
||||||
|
|
||||||
if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){
|
if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){
|
||||||
|
setTick(0);
|
||||||
|
setAnAus(1);
|
||||||
setDirection(1);
|
setDirection(1);
|
||||||
setState(1);
|
setState(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){
|
if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){
|
||||||
|
setTick(0);
|
||||||
|
setAnAus(0);
|
||||||
setDirection(0);
|
setDirection(0);
|
||||||
setState(1);
|
setState(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// first switch - off approach, use timer later
|
// first switch - off approach, use timer later
|
||||||
if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){
|
if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){
|
||||||
|
setTick(ticks_treppe);
|
||||||
|
setAnAus(1);
|
||||||
setDirection(1);
|
setDirection(1);
|
||||||
setState(0);
|
setState(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){
|
if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){
|
||||||
|
setTick(ticks_treppe);
|
||||||
|
setAnAus(1);
|
||||||
setDirection(0);
|
setDirection(0);
|
||||||
setState(0);
|
setState(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
last_sensor_state[0] = current_sensor_state[0];
|
last_sensor_state[0] = current_sensor_state[0];
|
||||||
last_sensor_state[1] = current_sensor_state[1];
|
last_sensor_state[1] = current_sensor_state[1];
|
||||||
ledsequence();
|
|
||||||
|
|
||||||
|
ledsequence();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -209,22 +213,20 @@ uint16_t Treppe::setTime(uint16_t _time_per_stair){
|
|||||||
return time_per_stair;
|
return time_per_stair;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Treppe::setDirection(uint8_t _direction){
|
void Treppe::setDirection(uint8_t _direction){
|
||||||
switch_direction = _direction;
|
switch_direction = _direction;
|
||||||
Serial.println("Treppe: Direction changed!");
|
Serial.printf("Treppe: switch_direction=%d!\n", switch_direction);
|
||||||
if(finish) Serial.println("apply direction request immediately");
|
if(finish) Serial.println("apply direction request immediately");
|
||||||
else Serial.println("currently active, dir change afterwards");
|
else Serial.println("currently active, dir change afterwards");
|
||||||
// to do: implement state command variable to determine dimm-state
|
// to do: implement state command variable to determine dimm-state
|
||||||
return switch_direction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Treppe::setState(uint8_t _state){
|
void Treppe::setState(uint8_t _state){
|
||||||
if(state == _state) return 1;
|
if(state == _state) return;
|
||||||
else{
|
else {
|
||||||
switch_state = _state;
|
switch_state = _state;
|
||||||
Serial.println("Treppe: State Request changed!");
|
Serial.printf("Treppe: switch_state=%d!\n", switch_state);
|
||||||
if(finish) Serial.println("apply state request immediately");
|
if(finish) Serial.println("apply state request immediately");
|
||||||
else Serial.println("currently active, state changes after activity");
|
else Serial.println("currently active, state changes after activity");
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
@ -20,17 +20,22 @@ private:
|
|||||||
uint8_t switch_state = 0;
|
uint8_t switch_state = 0;
|
||||||
uint8_t finish = 1;
|
uint8_t finish = 1;
|
||||||
|
|
||||||
|
// alternative
|
||||||
uint32_t tick = 0;
|
uint32_t tick = 0;
|
||||||
uint32_t stufe = 0;
|
uint32_t stufe = 0;
|
||||||
|
uint8_t an_aus = 0;
|
||||||
|
|
||||||
uint32_t ticks_treppe = 0;
|
uint32_t ticks_treppe = 0;
|
||||||
uint32_t ticks_pro_stufe = 0;
|
uint32_t ticks_pro_stufe = 0;
|
||||||
float differenz_pwm_pro_tick = 0.0;
|
float differenz_pwm_pro_tick = 0.0;
|
||||||
|
// alternative
|
||||||
|
|
||||||
|
|
||||||
// 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();
|
||||||
|
void rampe();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Treppe(uint8_t _stairs) : stairs(_stairs){
|
Treppe(uint8_t _stairs) : stairs(_stairs){
|
||||||
@ -41,10 +46,8 @@ public:
|
|||||||
/ (float) ticks_pro_stufe;
|
/ (float) ticks_pro_stufe;
|
||||||
}
|
}
|
||||||
|
|
||||||
void task(); // call periodically
|
|
||||||
|
|
||||||
void setup();
|
void setup();
|
||||||
void task_2();
|
void task(); // call periodically
|
||||||
|
|
||||||
// Parameter section
|
// Parameter section
|
||||||
uint16_t setIdle(uint16_t _idle_brightness);
|
uint16_t setIdle(uint16_t _idle_brightness);
|
||||||
@ -60,9 +63,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Runtime Parameter section
|
// Runtime Parameter section
|
||||||
uint8_t setDirection(uint8_t _direction);
|
void setDirection(uint8_t _direction);
|
||||||
uint8_t setState(uint8_t _state);
|
void setState(uint8_t _state);
|
||||||
|
void setAnAus(uint8_t _an_aus) {
|
||||||
|
an_aus = _an_aus;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t getState() { return state;};
|
uint8_t getState() { return state;};
|
||||||
|
uint8_t getFinished() { return finish;};
|
||||||
uint8_t getDirection() {return direction;};
|
uint8_t getDirection() {return direction;};
|
||||||
};
|
};
|
@ -103,9 +103,6 @@ void setup() {
|
|||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <random>
|
#include <random>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user