- TIMSK3 ^= _BV(OCIE3A);
- dprintf("TIMER3 toggled.\n");
-}
-
-ISR(TIMER3_COMPA_vect) {
- // Mode = 1, static light, do nothing here
- if (rgblight_config.mode>=2 && rgblight_config.mode<=5) {
- // mode = 2 to 5, breathing mode
- rgblight_effect_breathing(rgblight_config.mode-2);
-
- } else if (rgblight_config.mode>=6 && rgblight_config.mode<=8) {
- rgblight_effect_rainbow_mood(rgblight_config.mode-6);
- } else if (rgblight_config.mode>=9 && rgblight_config.mode<=14) {
- rgblight_effect_rainbow_swirl(rgblight_config.mode-9);
- } else if (rgblight_config.mode>=15 && rgblight_config.mode<=20) {
- rgblight_effect_snake(rgblight_config.mode-15);
- } else if (rgblight_config.mode>=21 && rgblight_config.mode<=23) {
- rgblight_effect_knight(rgblight_config.mode-21);
- }
-}
-
-// effects
-void rgblight_effect_breathing(uint8_t interval) {
- static uint8_t pos = 0;
- static uint16_t last_timer = 0;
-
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) return;
- last_timer = timer_read();
-
- rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos]));
- pos = (pos+1) % 256;
-}
-
-void rgblight_effect_rainbow_mood(uint8_t interval) {
- static uint16_t current_hue=0;
- static uint16_t last_timer = 0;
-
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) return;
- last_timer = timer_read();
- rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
- current_hue = (current_hue+1) % 360;
-}
-
-void rgblight_effect_rainbow_swirl(uint8_t interval) {
- static uint16_t current_hue=0;
- static uint16_t last_timer = 0;
- uint16_t hue;
- uint8_t i;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval/2])) return;
- last_timer = timer_read();
- for (i=0; i<RGBLED_NUM; i++) {
- hue = (360/RGBLED_NUM*i+current_hue)%360;
- sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]);
- }
- rgblight_set();
-
- if (interval % 2) {
- current_hue = (current_hue+1) % 360;
- } else {
- if (current_hue -1 < 0) {
- current_hue = 359;
- } else {
- current_hue = current_hue - 1;
- }
-
- }
-}
-void rgblight_effect_snake(uint8_t interval) {
- static uint8_t pos=0;
- static uint16_t last_timer = 0;
- uint8_t i,j;
- int8_t k;
- int8_t increament = 1;
- if (interval%2) increament = -1;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval/2])) return;
- last_timer = timer_read();
- for (i=0;i<RGBLED_NUM;i++) {
- led[i].r=0;
- led[i].g=0;
- led[i].b=0;
- for (j=0;j<RGBLIGHT_EFFECT_SNAKE_LENGTH;j++) {
- k = pos+j*increament;
- if (k<0) k = k+RGBLED_NUM;
- if (i==k) {
- sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]);
- }
- }
- }
- rgblight_set();
- if (increament == 1) {
- if (pos - 1 < 0) {
- pos = RGBLED_NUM-1;
- } else {
- pos -= 1;
- }
- } else {
- pos = (pos+1)%RGBLED_NUM;
- }
-
-}
-
-void rgblight_effect_knight(uint8_t interval) {
- static int8_t pos=0;
- static uint16_t last_timer = 0;
- uint8_t i,j,cur;
- int8_t k;
- struct cRGB preled[RGBLED_NUM];
- static int8_t increament = -1;
- if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) return;
- last_timer = timer_read();
- for (i=0;i<RGBLED_NUM;i++) {
- preled[i].r=0;
- preled[i].g=0;
- preled[i].b=0;
- for (j=0;j<RGBLIGHT_EFFECT_KNIGHT_LENGTH;j++) {
- k = pos+j*increament;
- if (k<0) k = 0;
- if (k>=RGBLED_NUM) k=RGBLED_NUM-1;
- if (i==k) {
- sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]);
- }
- }
- }
- if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
- for (i=0;i<RGBLED_NUM;i++) {
- cur = (i+RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
- led[i].r = preled[cur].r;
- led[i].g = preled[cur].g;
- led[i].b = preled[cur].b;
- }
- }
- rgblight_set();
- if (increament == 1) {
- if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
- pos = 0- RGBLIGHT_EFFECT_KNIGHT_LENGTH;
- increament = -1;
- } else {
- pos -= 1;
- }
- } else {
- if (pos+1>RGBLED_NUM+RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
- pos = RGBLED_NUM+RGBLIGHT_EFFECT_KNIGHT_LENGTH-1;
- increament = 1;
- } else {
- pos += 1;
- }
- }
-
-}
-
-#endif
\ No newline at end of file
+ dprintf("rgblight timer toggle.\n");
+ if(rgblight_status.timer_enabled) {
+ rgblight_timer_disable();
+ } else {
+ rgblight_timer_enable();
+ }
+}
+
+void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
+ rgblight_enable();
+ rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
+ rgblight_setrgb(r, g, b);
+}
+
+static void rgblight_effect_dummy(animation_status_t *anim) {
+ // do nothing
+ /********
+ dprintf("rgblight_task() what happened?\n");
+ dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
+ dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
+ rgblight_config.mode, rgblight_status.base_mode,
+ rgblight_status.timer_enabled);
+ dprintf("last_timer = %d\n",anim->last_timer);
+ **/
+}
+
+void rgblight_task(void) {
+ if (rgblight_status.timer_enabled) {
+ effect_func_t effect_func = rgblight_effect_dummy;
+ uint16_t interval_time = 2000; // dummy interval
+ uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
+ animation_status.delta = delta;
+
+ // static light mode, do nothing here
+ if ( 1 == 0 ) { //dummy
+ }
+#ifdef RGBLIGHT_EFFECT_BREATHING
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
+ // breathing mode
+ interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
+ effect_func = rgblight_effect_breathing;
+ }
+#endif
+#ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
+ // rainbow mood mode
+ interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
+ effect_func = rgblight_effect_rainbow_mood;
+ }
+#endif
+#ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
+ // rainbow swirl mode
+ interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
+ effect_func = rgblight_effect_rainbow_swirl;
+ }
+#endif
+#ifdef RGBLIGHT_EFFECT_SNAKE
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
+ // snake mode
+ interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
+ effect_func = rgblight_effect_snake;
+ }
+#endif
+#ifdef RGBLIGHT_EFFECT_KNIGHT
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
+ // knight mode
+ interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
+ effect_func = rgblight_effect_knight;
+ }
+#endif
+#ifdef RGBLIGHT_EFFECT_CHRISTMAS
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
+ // christmas mode
+ interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
+ effect_func = (effect_func_t)rgblight_effect_christmas;
+ }
+#endif
+#ifdef RGBLIGHT_EFFECT_RGB_TEST
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
+ // RGB test mode
+ interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
+ effect_func = (effect_func_t)rgblight_effect_rgbtest;
+ }
+#endif
+#ifdef RGBLIGHT_EFFECT_ALTERNATING
+ else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING){
+ interval_time = 500;
+ effect_func = (effect_func_t)rgblight_effect_alternating;
+ }
+#endif
+ if (animation_status.restart) {
+ animation_status.restart = false;
+ animation_status.last_timer = timer_read() - interval_time - 1;
+ animation_status.pos16 = 0; // restart signal to local each effect
+ }
+ if (timer_elapsed(animation_status.last_timer) >= interval_time) {
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ static uint16_t report_last_timer = 0;
+ static bool tick_flag = false;
+ uint16_t oldpos16;
+ if (tick_flag) {
+ tick_flag = false;
+ //dprintf("rgblight animation tick\n");
+ if (timer_elapsed(report_last_timer) >= 30000) {
+ report_last_timer = timer_read();
+ dprintf("rgblight animation tick report to slave\n");
+ RGBLIGHT_SPLIT_ANIMATION_TICK;
+ }
+ }
+ oldpos16 = animation_status.pos16;
+ //dprintf("call effect function\n");
+#endif
+ animation_status.last_timer += interval_time;
+ effect_func(&animation_status);
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ //dprintf("pos16, oldpos16 = %d %d\n",
+ // animation_status.pos16,oldpos16);
+ if (animation_status.pos16 == 0 && oldpos16 != 0) {
+ //dprintf("flag on\n");
+ tick_flag = true;
+ }
+#endif
+ }
+ }
+}
+
+#endif /* RGBLIGHT_USE_TIMER */
+
+// Effects
+#ifdef RGBLIGHT_EFFECT_BREATHING
+
+#ifndef RGBLIGHT_EFFECT_BREATHE_CENTER
+ #ifndef RGBLIGHT_BREATHE_TABLE_SIZE
+ #define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256 or 128 or 64
+ #endif
+ #include <rgblight_breathe_table.h>
+#endif
+
+__attribute__ ((weak))
+const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
+
+void rgblight_effect_breathing(animation_status_t *anim) {
+ float val;
+
+ // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
+#ifdef RGBLIGHT_EFFECT_BREATHE_TABLE
+ val = pgm_read_byte(&rgblight_effect_breathe_table[anim->pos / table_scale]);
+#else
+ val = (exp(sin((anim->pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
+#endif
+ rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
+ anim->pos = (anim->pos + 1);
+}
+#endif
+
+#ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
+__attribute__ ((weak))
+const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
+
+void rgblight_effect_rainbow_mood(animation_status_t *anim) {
+ rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
+ anim->current_hue++;
+}
+#endif
+
+#ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
+#ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
+ #define RGBLIGHT_RAINBOW_SWIRL_RANGE 255
+#endif
+
+__attribute__ ((weak))
+const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
+
+void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
+ uint8_t hue;
+ uint8_t i;
+
+ for (i = 0; i < effect_num_leds; i++) {
+ hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / effect_num_leds * i + anim->current_hue);
+ sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
+ }
+ rgblight_set();
+
+ if (anim->delta % 2) {
+ anim->current_hue++;
+ } else {
+ anim->current_hue--;
+ }
+}
+#endif
+
+#ifdef RGBLIGHT_EFFECT_SNAKE
+__attribute__ ((weak))
+const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
+
+void rgblight_effect_snake(animation_status_t *anim) {
+ static uint8_t pos = 0;
+ uint8_t i, j;
+ int8_t k;
+ int8_t increment = 1;
+
+ if (anim->delta % 2) {
+ increment = -1;
+ }
+
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ if (anim->pos == 0) { // restart signal
+ if (increment == 1) {
+ pos = effect_num_leds - 1;
+ } else {
+ pos = 0;
+ }
+ anim->pos = 1;
+ }
+#endif
+
+ for (i = 0; i < effect_num_leds; i++) {
+ LED_TYPE *ledp = led + i + effect_start_pos;
+ ledp->r = 0;
+ ledp->g = 0;
+ ledp->b = 0;
+ for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
+ k = pos + j * increment;
+ if (k < 0) {
+ k = k + effect_num_leds;
+ }
+ if (i == k) {
+ sethsv(rgblight_config.hue, rgblight_config.sat,
+ (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH),
+ ledp);
+ }
+ }
+ }
+ rgblight_set();
+ if (increment == 1) {
+ if (pos - 1 < 0) {
+ pos = effect_num_leds - 1;
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ anim->pos = 0;
+#endif
+ } else {
+ pos -= 1;
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ anim->pos = 1;
+#endif
+ }
+ } else {
+ pos = (pos + 1) % effect_num_leds;
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ anim->pos = pos;
+#endif
+ }
+}
+#endif
+
+#ifdef RGBLIGHT_EFFECT_KNIGHT
+__attribute__ ((weak))
+const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
+
+void rgblight_effect_knight(animation_status_t *anim) {
+
+ static int8_t low_bound = 0;
+ static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
+ static int8_t increment = 1;
+ uint8_t i, cur;
+
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ if (anim->pos == 0) { // restart signal
+ anim->pos = 1;
+ low_bound = 0;
+ high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
+ increment = 1;
+ }
+#endif
+ // Set all the LEDs to 0
+ for (i = effect_start_pos; i < effect_end_pos; i++) {
+ led[i].r = 0;
+ led[i].g = 0;
+ led[i].b = 0;
+ }
+ // Determine which LEDs should be lit up
+ for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
+ cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % effect_num_leds + effect_start_pos;
+
+ if (i >= low_bound && i <= high_bound) {
+ sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
+ } else {
+ led[cur].r = 0;
+ led[cur].g = 0;
+ led[cur].b = 0;
+ }
+ }
+ rgblight_set();
+
+ // Move from low_bound to high_bound changing the direction we increment each
+ // time a boundary is hit.
+ low_bound += increment;
+ high_bound += increment;
+
+ if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
+ increment = -increment;
+#if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
+ if (increment == 1) {
+ anim->pos = 0;
+ }
+#endif
+ }
+}
+#endif
+
+#ifdef RGBLIGHT_EFFECT_CHRISTMAS
+void rgblight_effect_christmas(animation_status_t *anim) {
+ uint8_t hue;
+ uint8_t i;
+
+ anim->current_offset = (anim->current_offset + 1) % 2;
+ for (i = 0; i < effect_num_leds; i++) {
+ hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85;
+ sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
+ }
+ rgblight_set();
+}
+#endif
+
+#ifdef RGBLIGHT_EFFECT_RGB_TEST
+__attribute__ ((weak))
+const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
+
+void rgblight_effect_rgbtest(animation_status_t *anim) {
+ static uint8_t maxval = 0;
+ uint8_t g; uint8_t r; uint8_t b;
+
+ if( maxval == 0 ) {
+ LED_TYPE tmp_led;
+ sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
+ maxval = tmp_led.r;
+ }
+ g = r = b = 0;
+ switch( anim->pos ) {
+ case 0: r = maxval; break;
+ case 1: g = maxval; break;
+ case 2: b = maxval; break;
+ }
+ rgblight_setrgb(r, g, b);
+ anim->pos = (anim->pos + 1) % 3;
+}
+#endif
+
+#ifdef RGBLIGHT_EFFECT_ALTERNATING
+void rgblight_effect_alternating(animation_status_t *anim) {
+
+ for (int i = 0; i < effect_num_leds; i++) {
+ LED_TYPE *ledp = led + i + effect_start_pos;
+ if (i<effect_num_leds/2 && anim->pos) {
+ sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
+ } else if (i>=effect_num_leds/2 && !anim->pos) {
+ sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
+ } else {
+ sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp);
+ }
+ }
+ rgblight_set();
+ anim->pos = (anim->pos + 1) % 2;
+}
+#endif