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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = 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::setup(){
  87. pwmController.resetDevices();
  88. // Deactive PCA9685 due to LED Flickering
  89. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  90. // see also lib/PCA9685-Arduin/PCA9685.h:204
  91. pwmController.init(PCA9685_PhaseBalancer_None);
  92. pwmController.setPWMFrequency(200);
  93. Serial.println("Hello from Treppe");
  94. Serial.print("Treppe: initial parameters: stairs=");
  95. Serial.println(stairs);
  96. }
  97. void Treppe::task(){
  98. ledsequence();
  99. }
  100. uint16_t Treppe::setIdle(uint16_t _idle_brightness){
  101. idle_brightness = _idle_brightness;
  102. Serial.println("Treppe: idle brightness changed!");
  103. return idle_brightness;
  104. }
  105. uint16_t Treppe::setActive(uint16_t _active_brightness){
  106. active_brightness = _active_brightness;
  107. Serial.println("Treppe: active brightness changed!");
  108. return active_brightness;
  109. }
  110. uint16_t Treppe::setTime(uint16_t _time_per_stair){
  111. time_per_stair = _time_per_stair;
  112. Serial.println("Treppe: time changed!");
  113. return time_per_stair;
  114. }
  115. uint8_t Treppe::setDirection(uint8_t _direction){
  116. direction = _direction;
  117. Serial.println("Treppe: Direction changed!");
  118. // to do: implement state command variable to determine dimm-state
  119. return direction;
  120. }
  121. uint8_t Treppe::setState(uint8_t _state){
  122. if(state == _state) return 1;
  123. else{
  124. state = _state;
  125. Serial.println("Treppe: State changed!");
  126. }
  127. return 0;
  128. }