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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 = 1.0;
  15. if(led != lastled){
  16. pwmController.setChannelPWM(led, (uint16_t)startval);
  17. lastled = led;
  18. current_pwm = startval;
  19. stepsize = INT_TIME*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
  20. return 1;
  21. }
  22. if(startval > stopval){
  23. current_pwm -= stepsize;
  24. }
  25. else {
  26. current_pwm += stepsize;
  27. }
  28. // Serial.println((uint16_t)current_pwm);
  29. pwmController.setChannelPWM(led, (uint16_t)current_pwm);
  30. if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){
  31. if(stopval == 0) pwmController.setChannelPWM(led, 0);
  32. return 0;
  33. }
  34. return 1;
  35. }
  36. void Treppe::ledsequence(){
  37. static int8_t led = 0;
  38. static uint16_t brightness = 0;
  39. static uint16_t lastbrightness = 0;
  40. static uint16_t status = 0;
  41. uint16_t status_build = 0;
  42. status_build |= direction << 8;
  43. status_build |= state;
  44. if(status_build != status){ // check if any parameter changed
  45. finish = 0; // set state unfinished -> start action
  46. if(direction) led = 0; // reset led counter depending of direction
  47. else led = stairs-1;
  48. if(state){
  49. brightness = active_brightness; // set brightness value depending of on/off
  50. lastbrightness = idle_brightness;
  51. }
  52. else{
  53. brightness = idle_brightness;
  54. lastbrightness = active_brightness;
  55. }
  56. status = status_build; // set parameter memory
  57. Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction);
  58. }
  59. if(!finish){ // finish == 0 -> action pending
  60. if(!softstart_led(led,lastbrightness, brightness)){
  61. Serial.printf("one LED finished: led: %d, last: %d, curr %d\n",
  62. led, lastbrightness, brightness);
  63. if(direction){
  64. led++;
  65. if(led >= stairs)
  66. finish = 1;
  67. }
  68. else{
  69. led--;
  70. if(led < 0)
  71. finish = 1;
  72. }
  73. }
  74. }
  75. }
  76. void Treppe::rampe()
  77. {
  78. if(state) {
  79. finish = 0;
  80. state = 0;// set parameter memory
  81. }
  82. if(!finish) {
  83. if(direction) { // aufwärts
  84. if(tick >= ticks_treppe-1) { // ziel erreicht
  85. Serial.println("[Treppe] oberster tick !");
  86. finish = 1;
  87. return;
  88. }
  89. tick++; // eins hoch
  90. }
  91. else { // abwärts
  92. if(tick <= 0) { // ziel erreicht
  93. Serial.println("[Treppe] unterster tick !");
  94. finish = 1;
  95. return;
  96. }
  97. tick--; // eins runter
  98. }
  99. stufe = tick / ticks_pro_stufe;
  100. float new_pwm = 0.0;
  101. if(an_aus) {
  102. new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
  103. new_pwm += idle_brightness;
  104. if(direction) new_pwm += differenz_pwm_pro_tick;
  105. }
  106. else {
  107. new_pwm = active_brightness - differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
  108. new_pwm += idle_brightness;
  109. if(direction) new_pwm -= differenz_pwm_pro_tick;
  110. }
  111. pwmController.setChannelPWM(stufe, (uint16_t) new_pwm);
  112. Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
  113. tick,
  114. stufe,
  115. (tick - ticks_pro_stufe*stufe),
  116. new_pwm
  117. );
  118. }
  119. }
  120. void Treppe::setup(){
  121. pwmController.resetDevices();
  122. // Deactive PCA9685 Phase Balancer due to LED Flickering
  123. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  124. // see also lib/PCA9685-Arduin/PCA9685.h:204
  125. pwmController.init(PCA9685_PhaseBalancer_None);
  126. //pwmController.init(PCA9685_PhaseBalancer_Linear);
  127. pwmController.setPWMFrequency(100);
  128. pwmController.setAllChannelsPWM(idle_brightness);
  129. pinMode(A0, INPUT);
  130. pinMode(SENSOR_OBEN, INPUT);
  131. pinMode(SENSOR_UNTEN, INPUT);
  132. pinMode(OE, OUTPUT);
  133. digitalWrite(OE, 0);
  134. Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
  135. Serial.println("Hello from Treppe");
  136. Serial.print("Treppe: initial parameters: stairs=");
  137. Serial.println(stairs);
  138. }
  139. void Treppe::print_state_on_change() {
  140. static FSMTreppeModelClass::ExtU_FSMTreppe_T last_in;
  141. static FSMTreppeModelClass::ExtY_FSMTreppe_T last_out;
  142. if(
  143. fsm_inputs.anim_finished != last_in.anim_finished ||
  144. fsm_inputs.sensor_oben != last_in.sensor_oben ||
  145. fsm_inputs.sensor_unten != last_in.sensor_unten ||
  146. fsm_outputs.anim_active != last_out.anim_active
  147. ) {
  148. last_in.anim_finished = fsm_inputs.anim_finished;
  149. last_in.sensor_oben = fsm_inputs.sensor_oben;
  150. last_in.sensor_unten = fsm_inputs.sensor_unten;
  151. last_out.anim_active = fsm_outputs.anim_active;
  152. Serial.printf("FSM inputs: s_u: %d, s_o: %d, an_fin: %d =>",
  153. fsm_inputs.sensor_oben, fsm_inputs.sensor_unten, fsm_inputs.anim_finished);
  154. Serial.print(" step => ");
  155. Serial.printf("FSM outputs: an_act: %d\n", fsm_outputs.anim_active);
  156. }
  157. }
  158. void Treppe::task(){
  159. if(finish){
  160. direction = switch_direction;
  161. state = switch_state;
  162. }
  163. static uint8_t last_sensor_state[2] = {0,0};
  164. uint8_t current_sensor_state[2] = {0,0};
  165. current_sensor_state[0] = digitalRead(SENSOR_OBEN);
  166. current_sensor_state[1] = digitalRead(SENSOR_UNTEN);
  167. fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN);
  168. fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN);
  169. fsm_inputs.anim_finished = static_cast<bool>(finish);
  170. FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
  171. FSMTreppe_Obj.step();
  172. fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
  173. print_state_on_change();
  174. if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){
  175. setTick(0);
  176. setAnAus(1);
  177. setDirection(1);
  178. setState(1);
  179. }
  180. if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){
  181. setTick(0);
  182. setAnAus(0);
  183. setDirection(0);
  184. setState(1);
  185. }
  186. // first switch - off approach, use timer later
  187. if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){
  188. setTick(ticks_treppe);
  189. setAnAus(1);
  190. setDirection(1);
  191. setState(0);
  192. }
  193. if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){
  194. setTick(ticks_treppe);
  195. setAnAus(1);
  196. setDirection(0);
  197. setState(0);
  198. }
  199. last_sensor_state[0] = current_sensor_state[0];
  200. last_sensor_state[1] = current_sensor_state[1];
  201. ledsequence();
  202. //Serial.printf("LDR: %f\n", ((float)analogRead(A0))/1023.*3.68);
  203. }
  204. uint16_t Treppe::setIdle(uint16_t _idle_brightness){
  205. idle_brightness = _idle_brightness;
  206. Serial.println("Treppe: idle brightness changed!");
  207. return idle_brightness;
  208. }
  209. uint16_t Treppe::setActive(uint16_t _active_brightness){
  210. active_brightness = _active_brightness;
  211. Serial.println("Treppe: active brightness changed!");
  212. return active_brightness;
  213. }
  214. uint16_t Treppe::setTime(uint16_t _time_per_stair){
  215. time_per_stair = _time_per_stair;
  216. Serial.println("Treppe: time changed!");
  217. return time_per_stair;
  218. }
  219. void Treppe::setDirection(uint8_t _direction){
  220. switch_direction = _direction;
  221. Serial.printf("Treppe: switch_direction=%d!\n", switch_direction);
  222. if(finish) Serial.println("apply direction request immediately");
  223. else Serial.println("currently active, dir change afterwards");
  224. // to do: implement state command variable to determine dimm-state
  225. }
  226. void Treppe::setState(uint8_t _state){
  227. if(state == _state) return;
  228. else {
  229. switch_state = _state;
  230. Serial.printf("Treppe: switch_state=%d!\n", switch_state);
  231. if(finish) Serial.println("apply state request immediately");
  232. else Serial.println("currently active, state changes after activity");
  233. }
  234. }