Atm_fade_WS2812.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // #ifdef FASTLED_INTERNAL
  2. #include "Atm_fade_WS2812.hpp"
  3. Atm_fade_WS2812::Atm_fade_WS2812( CRGB *_leds, int _num_leds, int _offset_leds) : Machine()
  4. {
  5. num_leds = _num_leds ;
  6. offset_leds = _offset_leds ;
  7. leds = _leds + offset_leds ;
  8. leds_buffer = new CRGB[num_leds] ;
  9. } ;
  10. Atm_fade_WS2812::~Atm_fade_WS2812 (void) {
  11. delete[] leds_buffer ;
  12. }
  13. Atm_fade_WS2812& Atm_fade_WS2812::begin( ) {
  14. // clang-format off
  15. const static state_t state_table[] PROGMEM = {
  16. /* ON_ENTER ON_LOOP ON_EXIT EVT_CNT_FADE EVT_TM_FADE EVT_TM_ON EVT_TM_OFF EVT_CNT_RPT EVT_ON EVT_OFF EVT_BLINK EVT_TOGGLE EVT_TOGGLE_BLINK ELSE */
  17. /* IDLE */ ENT_OFF, ATM_SLEEP, -1, -1, -1, -1, -1, -1,OSTARTU, IDLE, START, OSTARTU, START, -1, // LED off
  18. /* ON */ ENT_ON, ATM_SLEEP, -1, -1, -1, -1, -1, -1, -1,OSTARTD, START, OSTARTD, OSTARTD, -1, // LED on
  19. /* START */ ENT_OFF, -1, -1, -1, -1, -1, -1, -1,OSTARTU, IDLE, START, IDLE, IDLE, STARTU, // Start fading
  20. /* STARTU */ ENT_START, -1, -1, -1, -1, -1, UP, -1,OSTARTU, IDLE, START, IDLE, IDLE, -1,
  21. /* UP */ ENT_UP, -1, -1, STARTD, UP, -1, -1, -1,OSTARTU, IDLE, START, IDLE, IDLE, -1,
  22. /* STARTD */ ENT_START, -1, -1, -1, -1, DOWN, -1, -1,OSTARTU, IDLE, START, IDLE, IDLE, -1,
  23. /* DOWN */ ENT_DOWN, -1, -1, REPEAT, DOWN, -1, -1, -1,OSTARTU, IDLE, START, IDLE, IDLE, -1,
  24. /* REPEAT */ ENT_REPEAT, -1, -1, -1, -1, -1, -1, DONE,OSTARTU, IDLE, START, IDLE, IDLE, STARTU,
  25. /* DONE */ ENT_DONE, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, IDLE,
  26. /* OSTARTU*/ ENT_START, -1, -1, -1, -1, -1, -1, -1, -1, IDLE, START, IDLE, IDLE, OUP,
  27. /* OUP */ ENT_UP, -1, -1, ON, OUP, -1, -1, -1, -1, IDLE, START, IDLE, IDLE, -1,
  28. /* OSTARTD*/ ENT_START, -1, -1, -1, -1, -1, -1, -1,OSTARTU, IDLE, START, IDLE, IDLE, ODOWN,
  29. /* ODOWN */ ENT_DOWN, -1, -1, IDLE, ODOWN, -1, -1, -1,OSTARTU, IDLE, START, IDLE, IDLE, -1,
  30. };
  31. // clang-format on
  32. Machine::begin( state_table, ELSE );
  33. timer_fade.set( 0 ); // Number of ms per slope step (slope duration: rate * 32 ms)
  34. timer_on.set( 500 ); // Plateau between slopes (in which led is fully on)
  35. timer_off.set( 500 ); // Pause between slopes (in which led is fully off)
  36. // counter_fade.set( SLOPE_SIZE );
  37. // timer_update.setFromNow( this, fade_update_rate );
  38. counter_fade.set( FADE_STEPS );
  39. counter_repeat.set( ATM_COUNTER_OFF );
  40. repeat_count = ATM_COUNTER_OFF;
  41. return *this;
  42. }
  43. Atm_fade_WS2812& Atm_fade_WS2812::blink( uint32_t duration, uint32_t pause_duration, uint16_t repeat_count /* = ATM_COUNTER_OFF */ ) {
  44. blink( duration ); // Time in which led is fully on
  45. pause( pause_duration );
  46. repeat( repeat_count );
  47. return *this;
  48. }
  49. Atm_fade_WS2812& Atm_fade_WS2812::blink( uint32_t duration ) {
  50. timer_on.set( duration ); // Plateau between slopes (in which led is fully on)
  51. return *this;
  52. }
  53. Atm_fade_WS2812& Atm_fade_WS2812::blink( void ) {
  54. trigger( EVT_BLINK );
  55. return *this;
  56. }
  57. Atm_fade_WS2812& Atm_fade_WS2812::pause( uint32_t duration ) { // Pause between slopes (in which led is fully off)
  58. timer_off.set( duration ? duration : 1 ); // Make sure off_timer is never 0 (work around)
  59. return *this;
  60. }
  61. Atm_fade_WS2812& Atm_fade_WS2812::fade( int fade_length){
  62. if(fade_length>=0){
  63. fade_duration = fade_length ;
  64. FADE_STEPS = fade_length / fade_update_rate ;
  65. }
  66. else{
  67. fade_duration = ATM_TIMER_OFF;
  68. FADE_STEPS = 0;
  69. }
  70. timer_fade.set( fade_length >= 0 ? fade_update_rate : ATM_TIMER_OFF );
  71. return *this;
  72. }
  73. Atm_fade_WS2812& Atm_fade_WS2812::fade( int fade_length, bool fade_light, int fade_chase ) {
  74. //ws2812 version : fade_timer fixed at 25ms,
  75. // number of steps calculated from fade_timer to keep states logic
  76. //fade time is total fade time, using state_millis to check progression
  77. fade(fade_length);
  78. // Serial.printf("Fade duration %d, %d steps\n", fade_duration, FADE_STEPS);
  79. _fade_light = fade_light ;
  80. _fade_chase = fade_chase ;
  81. return *this;
  82. }
  83. Atm_fade_WS2812& Atm_fade_WS2812::fadeSpeed( float fade_speed){
  84. //define fade rate by leds/s
  85. fade((num_leds/fade_speed)*1000);
  86. return *this;
  87. }
  88. Atm_fade_WS2812& Atm_fade_WS2812::fadeSpeed( float fade_speed, bool fade_light, int fade_chase){
  89. //define fade rate by leds/s
  90. fade((num_leds/fade_speed)*1000, fade_light, fade_chase);
  91. return *this;
  92. }
  93. Atm_fade_WS2812& Atm_fade_WS2812::repeat( uint16_t repeat ) {
  94. counter_repeat.set( repeat_count = repeat );
  95. return *this;
  96. }
  97. int Atm_fade_WS2812::event( int id ) {
  98. switch ( id ) {
  99. case EVT_TM_FADE:
  100. return timer_fade.expired( this );
  101. case EVT_TM_ON:
  102. fade_start_millis = millis();
  103. return timer_on.expired( this );
  104. case EVT_TM_OFF:
  105. fade_start_millis = millis();
  106. return timer_off.expired( this );
  107. case EVT_CNT_FADE:
  108. return counter_fade.expired();
  109. case EVT_CNT_RPT:
  110. return counter_repeat.expired();
  111. }
  112. return 0;
  113. }
  114. void Atm_fade_WS2812::action( int id ) {
  115. switch ( id ) {
  116. case ENT_ON:
  117. update_leds(1) ;
  118. return;
  119. case ENT_REPEAT:
  120. counter_repeat.decrement();
  121. return;
  122. case ENT_OFF:
  123. counter_repeat.set( repeat_count );
  124. fade_start_millis = millis();
  125. update_leds(0);
  126. return;
  127. case ENT_START:
  128. counter_fade.set( FADE_STEPS );
  129. fade_start_millis = millis();
  130. // Serial.printf("-----------------ENT_START %d \n", fade_start_millis);
  131. return;
  132. case ENT_UP:
  133. fade_ellapsed_millis = millis() - fade_start_millis ;
  134. // Serial.printf("ellapsed %f \n", (float)fade_ellapsed_millis/fade_duration);
  135. update_leds((float)fade_ellapsed_millis/fade_duration);
  136. counter_fade.decrement();
  137. return;
  138. case ENT_DOWN:
  139. fade_ellapsed_millis = millis() - fade_start_millis ;
  140. update_leds( 1-(float) fade_ellapsed_millis/fade_duration);
  141. // value = slope[counter_fade.value] ;
  142. // leds->fadeToBlackBy(255-value);
  143. counter_fade.decrement();
  144. return;
  145. case ENT_DONE:
  146. onfinish.push( 0 );
  147. return;
  148. }
  149. }
  150. Atm_fade_WS2812& Atm_fade_WS2812::on( void ) {
  151. trigger( EVT_ON );
  152. return *this;
  153. }
  154. Atm_fade_WS2812& Atm_fade_WS2812::off( void ) {
  155. trigger( EVT_OFF );
  156. return *this;
  157. }
  158. Atm_fade_WS2812& Atm_fade_WS2812::toggle( void ) {
  159. trigger( EVT_TOGGLE );
  160. return *this;
  161. }
  162. Atm_fade_WS2812& Atm_fade_WS2812::toggleBlink( void ) {
  163. trigger( EVT_TOGGLE_BLINK );
  164. return *this;
  165. }
  166. Atm_fade_WS2812& Atm_fade_WS2812::start( void ) {
  167. trigger( EVT_BLINK );
  168. return *this;
  169. }
  170. Atm_fade_WS2812& Atm_fade_WS2812::onFinish( Machine& machine, int event /* = 0 */ ) {
  171. onfinish.set( &machine, event );
  172. return *this;
  173. }
  174. Atm_fade_WS2812& Atm_fade_WS2812::onFinish( atm_cb_push_t callback, int idx /* = 0 */ ) {
  175. onfinish.set( callback, idx );
  176. return *this;
  177. }
  178. Atm_fade_WS2812& Atm_fade_WS2812::setColor(CHSV hsvColor) {
  179. this->color = hsvColor ;
  180. return *this;
  181. }
  182. Atm_fade_WS2812& Atm_fade_WS2812::setColor(CRGB rgbColor) {
  183. this->color = rgb2hsv_approximate(rgbColor) ;
  184. return *this;
  185. }
  186. Atm_fade_WS2812& Atm_fade_WS2812::setColor(int hue, int sat, int val) {
  187. this->color.hue = hue ;
  188. this->color.sat = sat ;
  189. this->color.val = val ;
  190. return *this;
  191. }
  192. Atm_fade_WS2812& Atm_fade_WS2812::replaceValues(bool replace){
  193. _replace_array = replace ;
  194. return *this;
  195. }
  196. Atm_fade_WS2812& Atm_fade_WS2812::updateRate(int new_update_rate){
  197. // fade_update_rate = new_update_rate ;
  198. // timer_update.set( fade_update_rate );
  199. return *this;
  200. }
  201. Atm_fade_WS2812& Atm_fade_WS2812::update(){
  202. update_flag = true ;
  203. this->cycle() ;
  204. return *this;
  205. }
  206. Atm_fade_WS2812& Atm_fade_WS2812::reversed(bool _reverse){
  207. reverse = _reverse ;
  208. return *this ;
  209. }
  210. Atm_fade_WS2812& Atm_fade_WS2812::trace( Stream& stream ) {
  211. setTrace( &stream, atm_serial_debug::trace,
  212. "FADE\0EVT_CNT_FADE\0EVT_TM_FADE\0EVT_TM_ON\0EVT_TM_OFF\0EVT_CNT_RPT\0EVT_ON\0EVT_OFF\0EVT_BLINK\0EVT_TOGGLE\0EVT_TOGGLE_BLINK\0ELSE\0"
  213. "IDLE\0ON\0START\0STARTU\0UP\0STARTD\0DOWN\0REPEAT\0DONE\0OSTARTU\0OUP\0OSTARTD\0ODOWN" );
  214. return *this;
  215. }
  216. void Atm_fade_WS2812::update_leds(float fade_value){
  217. if(update_flag){
  218. CHSV temp_led[this->num_leds] ;
  219. int current_value = color.val ;
  220. if(_fade_light){ current_value = color.val*fade_value ;}
  221. current_value = constrain(current_value, 0, 255 ) ;
  222. for(int led=0; led<num_leds; led++){
  223. // Serial.println(led);
  224. fade_value = _fade_chase ? fade_value : 1. ;
  225. if(led<fade_value*num_leds){
  226. temp_led[led].value = current_value ;
  227. temp_led[led].saturation = color.saturation ;
  228. temp_led[led].hue = color.hue ;
  229. }
  230. else{
  231. temp_led[led].value = 0 ;
  232. temp_led[led].saturation = color.saturation ;
  233. temp_led[led].hue = color.hue ;
  234. }
  235. if(_replace_array){
  236. if(reverse){leds[num_leds-led-1] = CRGB(temp_led[led]);}
  237. else{leds[led] = CRGB(temp_led[led]);}
  238. }
  239. if(!_replace_array){
  240. int current_led = reverse ? num_leds-led-1 : led ;
  241. CRGB temp_led_RGB ;
  242. if(this->state() == DOWN || this->state() == ODOWN) {
  243. temp_led_RGB = leds_buffer[led]-CRGB(temp_led[led]);
  244. leds[current_led].r = qsub8 (leds[current_led].r, temp_led_RGB.r+1);
  245. leds[current_led].g = qsub8 (leds[current_led].g, temp_led_RGB.g+1);
  246. leds[current_led].b = qsub8 (leds[current_led].b, temp_led_RGB.b+1);
  247. }
  248. else{
  249. temp_led_RGB = CRGB(temp_led[led]) - leds_buffer[led];
  250. leds[current_led].r = qadd8 (leds[current_led].r, temp_led_RGB.r);
  251. leds[current_led].g = qadd8 (leds[current_led].g, temp_led_RGB.g);
  252. leds[current_led].b = qadd8 (leds[current_led].b, temp_led_RGB.b);
  253. }
  254. }
  255. }
  256. hsv2rgb_rainbow(temp_led, leds_buffer, num_leds) ;
  257. update_flag=true ;
  258. }
  259. }