ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

treppe.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "treppe.h"
  2. uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){
  3. /*
  4. softstart task
  5. - get's called at regular intervals (1ms at the moment)
  6. - dimms single led (0 - 15, PCA9685 outputs) with linear intervals vom startval to stopval
  7. - calculates pwm steps depending on startval, stopval and timeinterval
  8. - -> results in constanst speed
  9. - returns 1 if led dimming is running
  10. - returns 0 if led dimming is finished
  11. */
  12. static uint8_t lastled = 255;
  13. static float current_pwm = 0;
  14. static float stepsize = 0.5;
  15. if(led != lastled){
  16. pwmController.setChannelPWM(led, (uint16_t)startval);
  17. lastled = led;
  18. current_pwm = startval;
  19. stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
  20. return 1;
  21. }
  22. if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0;
  23. // todo: duty cycle zero!
  24. if(startval > stopval){
  25. current_pwm -= stepsize;
  26. }
  27. else {
  28. current_pwm += stepsize;
  29. }
  30. Serial.println((uint16_t)current_pwm);
  31. pwmController.setChannelPWM(led, (uint16_t)current_pwm);
  32. return 1;
  33. }
  34. void Treppe::ledsequence(){
  35. static int8_t led = 0;
  36. static uint16_t brightness = 0;
  37. static uint16_t lastbrightness = 0;
  38. static uint8_t finish = 1;
  39. static uint16_t status = 0;
  40. uint16_t status_build = 0;
  41. status_build |= direction << 8;
  42. status_build |= state;
  43. if(status_build != status){ // check if any parameter changed
  44. finish = 0; // set state unfinished -> start action
  45. if(direction) led = 0; // reset led counter depending of direction
  46. else led = stairs-1;
  47. if(state){
  48. brightness = active_brightness; // set brightness value depending of on/off
  49. lastbrightness = idle_brightness;
  50. }
  51. else{
  52. brightness = idle_brightness;
  53. lastbrightness = active_brightness;
  54. }
  55. status = status_build; // set parameter memory
  56. Serial.print("----Status Changed! onoff: ");
  57. Serial.print(state);
  58. Serial.print(" dir: ");
  59. Serial.println(direction);
  60. }
  61. if(!finish){ // finish == 0 -> action pending
  62. if(!softstart_led(led,lastbrightness, brightness)){
  63. Serial.print("one LED finished: ");
  64. Serial.print(led);
  65. Serial.print(" last: ");
  66. Serial.print(lastbrightness);
  67. Serial.print(" curr: ");
  68. Serial.println(brightness);
  69. if(direction){
  70. led++;
  71. if(led >= stairs) {
  72. finish = 1;
  73. //lastbrightness = brightness;
  74. }
  75. }
  76. else{
  77. led--;
  78. if(led < 0){
  79. //lastbrightness = brightness;
  80. finish = 1;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. void Treppe::task_2()
  87. {
  88. if(state) {
  89. if(direction) { // aufwärts
  90. if(tick >= ticks_treppe-1) { // ziel erreicht
  91. Serial.println("[Treppe] oberster tick !");
  92. state = 0;
  93. return;
  94. }
  95. tick++; // eins hoch
  96. }
  97. else { // abwärts
  98. if(tick <= 0) { // ziel erreicht
  99. Serial.println("[Treppe] unterster tick !");
  100. state = 0;
  101. return;
  102. }
  103. tick--; // eins runter
  104. }
  105. stufe = tick / ticks_pro_stufe;
  106. float new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
  107. if(direction)
  108. new_pwm += differenz_pwm_pro_tick;
  109. new_pwm += idle_brightness;
  110. pwmController.setChannelPWM(stufe, (uint16_t) new_pwm);
  111. Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
  112. tick,
  113. stufe,
  114. (tick - ticks_pro_stufe*stufe),
  115. new_pwm
  116. );
  117. }
  118. }
  119. // if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) {
  120. // Serial.println("[Treppe] ERROR, Something went wrong !");
  121. // state = 0;
  122. // return;
  123. // }
  124. void Treppe::setup(){
  125. Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
  126. pwmController.resetDevices();
  127. // Deactivate PCA9685_PhaseBalancer due to LED Flickering
  128. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  129. // see also lib/PCA9685-Arduin/PCA9685.h:204
  130. pwmController.init(PCA9685_PhaseBalancer_None);
  131. pwmController.setPWMFrequency(200);
  132. Serial.println("Hello from Treppe");
  133. Serial.print("Treppe: initial parameters: stairs=");
  134. Serial.println(stairs);
  135. for(uint8_t i=0; i<stairs; i++) {
  136. pwmController.setChannelPWM(i, idle_brightness);
  137. }
  138. }
  139. void Treppe::task(){
  140. //ledsequence();
  141. task_2();
  142. }
  143. uint16_t Treppe::setIdle(uint16_t _idle_brightness){
  144. idle_brightness = _idle_brightness;
  145. Serial.println("Treppe: idle brightness changed!");
  146. return idle_brightness;
  147. }
  148. uint16_t Treppe::setActive(uint16_t _active_brightness){
  149. active_brightness = _active_brightness;
  150. Serial.println("Treppe: active brightness changed!");
  151. return active_brightness;
  152. }
  153. uint16_t Treppe::setTime(uint16_t _time_per_stair){
  154. time_per_stair = _time_per_stair;
  155. Serial.println("Treppe: time changed!");
  156. return time_per_stair;
  157. }
  158. uint8_t Treppe::setDirection(uint8_t _direction){
  159. direction = _direction;
  160. Serial.printf("Treppe: Direction: %d!\n",direction);
  161. // to do: implement state command variable to determine dimm-state
  162. return direction;
  163. }
  164. uint8_t Treppe::setState(uint8_t _state){
  165. if(state == _state) return 1;
  166. else{
  167. state = _state;
  168. Serial.printf("Treppe: State: %d!\n",state);
  169. }
  170. return 0;
  171. }