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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. pwmController.init();
  89. pwmController.setPWMFrequency(200);
  90. Serial.println("Hello from Treppe");
  91. Serial.print("Treppe: initial parameters: stairs=");
  92. Serial.println(stairs);
  93. }
  94. void Treppe::task(){
  95. ledsequence();
  96. }
  97. uint16_t Treppe::setIdle(uint16_t _idle_brightness){
  98. idle_brightness = _idle_brightness;
  99. Serial.println("Treppe: idle brightness changed!");
  100. return idle_brightness;
  101. }
  102. uint16_t Treppe::setActive(uint16_t _active_brightness){
  103. active_brightness = _active_brightness;
  104. Serial.println("Treppe: active brightness changed!");
  105. return active_brightness;
  106. }
  107. uint16_t Treppe::setTime(uint16_t _time_per_stair){
  108. time_per_stair = _time_per_stair;
  109. Serial.println("Treppe: time changed!");
  110. return time_per_stair;
  111. }
  112. uint8_t Treppe::setDirection(uint8_t _direction){
  113. direction = _direction;
  114. Serial.println("Treppe: Direction changed!");
  115. // to do: implement state command variable to determine dimm-state
  116. return direction;
  117. }
  118. uint8_t Treppe::setState(uint8_t _state){
  119. if(state == _state) return 1;
  120. else{
  121. state = _state;
  122. Serial.println("Treppe: State changed!");
  123. }
  124. return 0;
  125. }