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

3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "treppe.h"
  2. /*
  3. dimm_stufe
  4. - dimmt stufe (0 - 15, PCA9685 outputs) mit linearen ticks
  5. von idle bis active brightness
  6. - return false solange gedimmt wird
  7. - return true bei nächster stufe
  8. */
  9. bool Treppe::dimm_stufe(uint8_t stufe)
  10. {
  11. if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN)
  12. current_pwm += differenz_pwm_pro_tick;
  13. else
  14. current_pwm -= differenz_pwm_pro_tick;
  15. pwmController.setChannelPWM(stufe, static_cast<uint16_t> (current_pwm));
  16. current_tick++;
  17. if (current_tick >= ticks_pro_stufe)
  18. return false;
  19. return true;
  20. }
  21. /*
  22. animation tick
  23. - nach dem dimmen einer stufe wird die stufe weitergezählt
  24. - abbruch am ende => anim_beendet = true;
  25. */
  26. void Treppe::anim_tick()
  27. {
  28. if (!dimm_stufe(stufe))
  29. {
  30. Serial.printf("anim_tick(): stufe: %d, start: %d, ziel: %d, current %f\n",
  31. stufe, start_pwm, ziel_pwm, current_pwm);
  32. if (fsm_outputs.laufrichtung == LR_HOCH)
  33. {
  34. if (stufe >= stufen-1) {
  35. anim_beendet = true;
  36. return;
  37. }
  38. stufe++;
  39. }
  40. else
  41. {
  42. if (stufe <= 0) {
  43. anim_beendet = true;
  44. return;
  45. }
  46. stufe--;
  47. }
  48. current_tick = 0;
  49. current_pwm = start_pwm;
  50. }
  51. }
  52. // startbedingunen für animation
  53. void Treppe::start_animation() {
  54. anim_beendet = false;
  55. if(fsm_outputs.laufrichtung == LR_HOCH)
  56. stufe = 0;
  57. else
  58. stufe = stufen-1;
  59. if(fsm_outputs.dimmrichtung == DR_AUFDIMMEN) {
  60. start_pwm = idle_brightness;
  61. ziel_pwm = active_brightness;
  62. } else {
  63. start_pwm = active_brightness;
  64. ziel_pwm = idle_brightness;
  65. }
  66. current_tick = 0;
  67. current_pwm = start_pwm;
  68. }
  69. void Treppe::print_state_on_change()
  70. {
  71. static FSMTreppeModelClass::ExtU_FSMTreppe_T last_in;
  72. static FSMTreppeModelClass::ExtY_FSMTreppe_T last_out;
  73. if (
  74. fsm_inputs.anim_beendet != last_in.anim_beendet ||
  75. fsm_inputs.sensor_oben != last_in.sensor_oben ||
  76. fsm_inputs.sensor_unten != last_in.sensor_unten ||
  77. fsm_outputs.dimmrichtung != last_out.dimmrichtung ||
  78. fsm_outputs.laufrichtung != last_out.laufrichtung ||
  79. fsm_outputs.status != last_out.status)
  80. {
  81. last_in.anim_beendet = fsm_inputs.anim_beendet;
  82. last_in.sensor_oben = fsm_inputs.sensor_oben;
  83. last_in.sensor_unten = fsm_inputs.sensor_unten;
  84. last_out.dimmrichtung = fsm_outputs.dimmrichtung;
  85. last_out.laufrichtung = fsm_outputs.laufrichtung;
  86. last_out.status = fsm_outputs.status;
  87. Serial.printf("FSM IN: s_u: %d, s_o: %d, beendet: %d =>",
  88. fsm_inputs.sensor_oben, fsm_inputs.sensor_unten, fsm_inputs.anim_beendet);
  89. Serial.print(" step => ");
  90. Serial.printf("OUT: LR: %d DR: %d ST: %d\n",
  91. fsm_outputs.laufrichtung, fsm_outputs.dimmrichtung, fsm_outputs.status);
  92. }
  93. }
  94. void Treppe::task()
  95. {
  96. //Serial.printf("LDR: %f\n", ((float)analogRead(A0))/1023.*3.68);
  97. fsm_inputs.ldr_schwelle = true; // <=== LDR implementierung !!
  98. fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
  99. fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
  100. fsm_inputs.anim_beendet = static_cast<bool>(anim_beendet);
  101. FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
  102. FSMTreppe_Obj.step();
  103. fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
  104. print_state_on_change();
  105. if(fsm_outputs.status > ST_RUHEZUSTAND) {
  106. if( anim_beendet == true &&
  107. ( fsm_outputs.status == ST_AUFDIMMEN_HOCH || fsm_outputs.status == ST_ABDIMMEN_HOCH ||
  108. fsm_outputs.status == ST_AUFDIMMEN_RUNTER || fsm_outputs.status == ST_ABDIMMEN_RUNTER ))
  109. {
  110. start_animation();
  111. }
  112. if( !anim_beendet )
  113. anim_tick();
  114. }
  115. }
  116. void Treppe::berechne_dimmer() {
  117. ticks_pro_stufe = time_per_stair / INT_TIME; // [ms]
  118. differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness)
  119. / (float) ticks_pro_stufe;
  120. }
  121. void Treppe::setup()
  122. {
  123. pwmController.resetDevices();
  124. // Deactive PCA9685 Phase Balancer due to LED Flickering
  125. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  126. // see also lib/PCA9685-Arduin/PCA9685.h:204
  127. pwmController.init(PCA9685_PhaseBalancer_None);
  128. //pwmController.init(PCA9685_PhaseBalancer_Linear);
  129. pwmController.setPWMFrequency(100);
  130. pwmController.setAllChannelsPWM(idle_brightness);
  131. pinMode(A0, INPUT);
  132. pinMode(SENSOR_OBEN, INPUT);
  133. pinMode(SENSOR_UNTEN, INPUT);
  134. pinMode(OE, OUTPUT);
  135. digitalWrite(OE, 0);
  136. Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
  137. Serial.printf("Treppe: initial parameters: stufen=%d\n", stufen);
  138. }
  139. // ###################################################################################################################
  140. // GEBUFFERT => Erst im Ruhezustand übernehmen !!!!
  141. void Treppe::set_idle_pwm(uint16_t _idle_brightness)
  142. {
  143. idle_brightness = _idle_brightness;
  144. berechne_dimmer();
  145. Serial.printf("Treppe: idle_brightness=%d\n", idle_brightness);
  146. }
  147. void Treppe::set_active_pwm(uint16_t _active_brightness)
  148. {
  149. active_brightness = _active_brightness;
  150. berechne_dimmer();
  151. Serial.printf("Treppe: active_brightness=%d\n", active_brightness);
  152. }
  153. void Treppe::set_time_per_stair(uint16_t _time_per_stair)
  154. {
  155. time_per_stair = _time_per_stair;
  156. berechne_dimmer();
  157. Serial.printf("Treppe: time_per_stair=%d\n", time_per_stair);
  158. }