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.

pwm.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "pwm.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.print("----Status Changed! onoff: ");
  58. Serial.print(state);
  59. Serial.print(" dir: ");
  60. Serial.println(direction);
  61. }
  62. if(!finish){ // finish == 0 -> action pending
  63. if(!softstart_led(led,lastbrightness, brightness)){
  64. Serial.print("one LED finished: ");
  65. Serial.print(led);
  66. Serial.print(" last: ");
  67. Serial.print(lastbrightness);
  68. Serial.print(" curr: ");
  69. Serial.println(brightness);
  70. if(direction){
  71. led++;
  72. if(led >= stairs) {
  73. finish = 1;
  74. //lastbrightness = brightness;
  75. }
  76. }
  77. else{
  78. led--;
  79. if(led < 0){
  80. //lastbrightness = brightness;
  81. finish = 1;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. void Treppe::setup(){
  88. pwmController.resetDevices();
  89. // Deactive PCA9685 Phase Balancer due to LED Flickering
  90. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  91. // see also lib/PCA9685-Arduin/PCA9685.h:204
  92. pwmController.init(PCA9685_PhaseBalancer_None);
  93. pwmController.setPWMFrequency(200);
  94. pwmController.setAllChannelsPWM(idle_brightness);
  95. pinMode(SENSOR1, INPUT);
  96. pinMode(SENSOR2, INPUT);
  97. Serial.println("Hello from Treppe");
  98. Serial.print("Treppe: initial parameters: stairs=");
  99. Serial.println(stairs);
  100. }
  101. void Treppe::task(){
  102. if(finish){
  103. direction = switch_direction;
  104. state = switch_state;
  105. }
  106. static uint8_t last_sensor_state[2] = {0,0};
  107. uint8_t current_sensor_state[2] = {0,0};
  108. current_sensor_state[0] = digitalRead(SENSOR1);
  109. current_sensor_state[1] = digitalRead(SENSOR2);
  110. if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){
  111. setDirection(1);
  112. setState(1);
  113. }
  114. if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){
  115. setDirection(0);
  116. setState(1);
  117. }
  118. // first switch - off approach, use timer later
  119. if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){
  120. setDirection(1);
  121. setState(0);
  122. }
  123. if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){
  124. setDirection(0);
  125. setState(0);
  126. }
  127. last_sensor_state[0] = current_sensor_state[0];
  128. last_sensor_state[1] = current_sensor_state[1];
  129. ledsequence();
  130. }
  131. uint16_t Treppe::setIdle(uint16_t _idle_brightness){
  132. idle_brightness = _idle_brightness;
  133. Serial.println("Treppe: idle brightness changed!");
  134. return idle_brightness;
  135. }
  136. uint16_t Treppe::setActive(uint16_t _active_brightness){
  137. active_brightness = _active_brightness;
  138. Serial.println("Treppe: active brightness changed!");
  139. return active_brightness;
  140. }
  141. uint16_t Treppe::setTime(uint16_t _time_per_stair){
  142. time_per_stair = _time_per_stair;
  143. Serial.println("Treppe: time changed!");
  144. return time_per_stair;
  145. }
  146. uint8_t Treppe::setDirection(uint8_t _direction){
  147. switch_direction = _direction;
  148. Serial.println("Treppe: Direction changed!");
  149. if(finish) Serial.println("apply direction request immediately");
  150. else Serial.println("currently active, dir change afterwards");
  151. // to do: implement state command variable to determine dimm-state
  152. return switch_direction;
  153. }
  154. uint8_t Treppe::setState(uint8_t _state){
  155. if(state == _state) return 1;
  156. else{
  157. switch_state = _state;
  158. Serial.println("Treppe: State Request changed!");
  159. if(finish) Serial.println("apply state request immediately");
  160. else Serial.println("currently active, state changes after activity");
  161. }
  162. return 0;
  163. }