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.
276 lines
6.9 KiB
276 lines
6.9 KiB
9 years ago
|
#include "LEDControl.h"
|
||
|
|
||
9 years ago
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
LEDControl_::LEDControl_(void) {
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void LEDControl_::set_key_color(byte row, byte col, cRGB color) {
|
||
9 years ago
|
implementation_led_set_crgb_at(row, col, color);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
cRGB LEDControl_::get_key_color(byte row, byte col) {
|
||
9 years ago
|
return implementation_get_key_color(row, col);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void LEDControl_::initialize_led_mode(uint8_t mode) {
|
||
9 years ago
|
set_all_leds_to(led_off);
|
||
|
if (mode == LED_MODE_OFF) {
|
||
|
// set_all_leds_to(led_off);
|
||
|
} else if (mode == LED_MODE_HEATMAP) {
|
||
|
} else if (mode == LED_MODE_BREATHE) {
|
||
|
|
||
|
} else if (mode == LED_MODE_RAINBOW) {
|
||
|
} else if (mode == LED_MODE_RAINBOW_WAVE) {
|
||
|
} else if (mode == LED_MODE_CHASE) {
|
||
|
} else if (mode == LED_MODE_STEADY) {
|
||
|
set_all_leds_to(led_steady);
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void LEDControl_::set_all_leds_to(cRGB color) {
|
||
9 years ago
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
||
9 years ago
|
implementation_led_set_crgb_at(i, color);
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
|
||
9 years ago
|
void LEDControl_::next_mode() {
|
||
9 years ago
|
if (led_mode++ >= LED_MODES) {
|
||
9 years ago
|
led_mode = 0;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void LEDControl_::set_mode(uint8_t mode) {
|
||
9 years ago
|
led_mode = mode;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
9 years ago
|
void LEDControl_::update(uint8_t current_keymap) {
|
||
9 years ago
|
if (current_keymap == NUMPAD_KEYMAP) {
|
||
10 years ago
|
if (led_mode != LED_SPECIAL_MODE_NUMLOCK) {
|
||
|
stored_led_mode = led_mode;
|
||
|
}
|
||
|
led_mode = LED_SPECIAL_MODE_NUMLOCK;
|
||
|
}
|
||
9 years ago
|
if (current_keymap != NUMPAD_KEYMAP &&
|
||
9 years ago
|
led_mode == LED_SPECIAL_MODE_NUMLOCK
|
||
|
) {
|
||
10 years ago
|
led_mode = stored_led_mode;
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
|
||
9 years ago
|
if (led_mode != last_led_mode) {
|
||
|
initialize_led_mode(led_mode);
|
||
|
}
|
||
|
if (led_mode == LED_MODE_OFF) {
|
||
|
} else if (led_mode == LED_MODE_HEATMAP) {
|
||
|
} else if (led_mode == LED_MODE_BREATHE) {
|
||
9 years ago
|
effect_breathe_update();
|
||
9 years ago
|
} else if (led_mode == LED_MODE_RAINBOW) {
|
||
9 years ago
|
effect_rainbow_update();
|
||
9 years ago
|
} else if (led_mode == LED_MODE_RAINBOW_WAVE) {
|
||
9 years ago
|
effect_rainbow_wave_update();
|
||
9 years ago
|
} else if (led_mode == LED_MODE_CHASE) {
|
||
9 years ago
|
effect_chase_update();
|
||
9 years ago
|
} else if (led_mode == LED_MODE_STEADY) {
|
||
9 years ago
|
effect_steady_update();
|
||
9 years ago
|
} else if (led_mode == LED_SPECIAL_MODE_NUMLOCK) {
|
||
9 years ago
|
effect_numlock_update();
|
||
9 years ago
|
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
last_led_mode = led_mode;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
9 years ago
|
|
||
9 years ago
|
void LEDControl_::effect_numlock_update() {
|
||
9 years ago
|
for (uint8_t i = 0; i < 44; i++) {
|
||
9 years ago
|
implementation_led_set_crgb_at(i, led_off);
|
||
9 years ago
|
}
|
||
9 years ago
|
for (uint8_t i = 44; i < LED_COUNT; i++) {
|
||
9 years ago
|
implementation_led_set_crgb_at(i, led_bright_red);
|
||
9 years ago
|
}
|
||
10 years ago
|
led_compute_breath();
|
||
9 years ago
|
implementation_led_set_crgb_at(60, led_breathe); // make numlock breathe
|
||
|
implementation_led_sync();
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void LEDControl_::effect_steady_update() {
|
||
9 years ago
|
implementation_led_sync();
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void LEDControl_::led_compute_breath() {
|
||
9 years ago
|
// algorithm from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
|
||
|
breathe_brightness = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
|
||
|
// change the brightness for next time through the loop:
|
||
|
//breathe_brightness = breathe_brightness + breathe_fadeAmount;
|
||
|
|
||
|
// reverse the direction of the fading at the ends of the fade:
|
||
|
if (breathe_brightness == 0 || breathe_brightness == 150) {
|
||
|
breathe_fadeAmount = -breathe_fadeAmount ;
|
||
|
}
|
||
10 years ago
|
|
||
|
|
||
9 years ago
|
led_breathe.SetHSV(200, 255, breathe_brightness);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void LEDControl_::effect_breathe_update() {
|
||
9 years ago
|
led_compute_breath();
|
||
|
set_all_leds_to(led_breathe);
|
||
9 years ago
|
implementation_led_sync();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void LEDControl_::effect_chase_update() {
|
||
9 years ago
|
if (current_chase_counter++ < chase_threshold) {
|
||
|
return;
|
||
|
}
|
||
|
current_chase_counter = 0;
|
||
9 years ago
|
implementation_led_set_crgb_at(pos - chase_pixels, led_off);
|
||
|
implementation_led_set_crgb_at(pos, led_dark_blue);
|
||
9 years ago
|
|
||
10 years ago
|
pos += chase_pixels;
|
||
9 years ago
|
if (pos > LED_COUNT || pos < 0) {
|
||
|
chase_pixels = -chase_pixels;
|
||
|
pos += chase_pixels;
|
||
|
}
|
||
9 years ago
|
implementation_led_set_crgb_at(pos, led_blue);
|
||
|
implementation_led_sync();
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void LEDControl_::effect_rainbow_update() {
|
||
9 years ago
|
if (rainbow_current_ticks++ < rainbow_ticks) {
|
||
|
return;
|
||
|
} else {
|
||
|
rainbow_current_ticks = 0;
|
||
|
}
|
||
9 years ago
|
rainbow.SetHSV(rainbow_hue, rainbow_saturation, rainbow_value);
|
||
9 years ago
|
rainbow_hue += rainbow_steps;
|
||
|
if (rainbow_hue >= 360) {
|
||
|
rainbow_hue %= 360;
|
||
|
}
|
||
|
set_all_leds_to(rainbow);
|
||
9 years ago
|
implementation_led_sync();
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void LEDControl_::effect_rainbow_wave_update() {
|
||
9 years ago
|
if (rainbow_current_ticks++ < rainbow_wave_ticks) {
|
||
|
return;
|
||
|
} else {
|
||
|
rainbow_current_ticks = 0;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
||
|
uint8_t key_hue = rainbow_hue +16*(i/4);
|
||
9 years ago
|
if (key_hue >= 360) {
|
||
|
key_hue %= 360;
|
||
|
}
|
||
9 years ago
|
rainbow.SetHSV(key_hue, rainbow_saturation, rainbow_value);
|
||
9 years ago
|
implementation_led_set_crgb_at(i,rainbow);
|
||
9 years ago
|
}
|
||
|
rainbow_hue += rainbow_wave_steps;
|
||
|
if (rainbow_hue >= 360) {
|
||
|
rainbow_hue %= 360;
|
||
10 years ago
|
}
|
||
9 years ago
|
implementation_led_sync();
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void LEDControl_::boot_animation() {
|
||
10 years ago
|
set_all_leds_to(led_off);
|
||
|
|
||
9 years ago
|
type_letter(LED_K);
|
||
|
type_letter(LED_E);
|
||
|
type_letter(LED_Y);
|
||
|
type_letter(LED_B);
|
||
|
type_letter(LED_O);
|
||
|
type_letter(LED_A);
|
||
|
type_letter(LED_R);
|
||
|
type_letter(LED_D);
|
||
|
type_letter(LED_I);
|
||
|
type_letter(LED_O);
|
||
|
type_letter(LED_SPACE);
|
||
|
type_letter(LED_0);
|
||
|
type_letter(LED_PERIOD);
|
||
|
type_letter(LED_9);
|
||
9 years ago
|
led_mode = LED_MODE_RAINBOW_WAVE;
|
||
10 years ago
|
|
||
10 years ago
|
|
||
|
}
|
||
|
|
||
9 years ago
|
void LEDControl_::type_letter(uint8_t letter) {
|
||
9 years ago
|
implementation_led_set_crgb_at(letter,led_bright_red);
|
||
|
implementation_led_sync();
|
||
9 years ago
|
delay(250);
|
||
9 years ago
|
implementation_led_set_crgb_at(letter,led_off);
|
||
|
implementation_led_sync();
|
||
9 years ago
|
delay(10);
|
||
10 years ago
|
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
/* SetHSV from light_ws2812. Their source was:
|
||
|
* getRGB() function based on <http://www.codeproject.com/miscctrl/CPicker.asp>
|
||
|
* dim_curve idea by Jims
|
||
|
* */
|
||
9 years ago
|
void LEDControl_::SetHSV(cRGB *crgb, int hue, byte sat, byte val) {
|
||
9 years ago
|
/* convert hue, saturation and brightness ( HSB/HSV ) to RGB
|
||
|
*/
|
||
|
|
||
|
int base;
|
||
|
|
||
|
if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
|
||
9 years ago
|
crgb->r = val;
|
||
|
crgb->g = val;
|
||
|
crgb->b = val;
|
||
9 years ago
|
} else {
|
||
|
base = ((255 - sat) * val) >> 8;
|
||
|
|
||
|
switch (hue / 60) {
|
||
|
case 0:
|
||
9 years ago
|
crgb->r = val;
|
||
|
crgb->g = (((val - base)*hue) / 60) + base;
|
||
|
crgb->b = base;
|
||
9 years ago
|
break;
|
||
|
|
||
|
case 1:
|
||
9 years ago
|
crgb->r = (((val - base)*(60 - (hue % 60))) / 60) + base;
|
||
|
crgb->g = val;
|
||
|
crgb->b = base;
|
||
9 years ago
|
break;
|
||
|
|
||
|
case 2:
|
||
9 years ago
|
crgb->r = base;
|
||
|
crgb->g = val;
|
||
|
crgb->b = (((val - base)*(hue % 60)) / 60) + base;
|
||
9 years ago
|
break;
|
||
|
|
||
|
case 3:
|
||
9 years ago
|
crgb->r = base;
|
||
|
crgb->g = (((val - base)*(60 - (hue % 60))) / 60) + base;
|
||
|
crgb->b = val;
|
||
9 years ago
|
break;
|
||
|
|
||
|
case 4:
|
||
9 years ago
|
crgb->r = (((val - base)*(hue % 60)) / 60) + base;
|
||
|
crgb->g = base;
|
||
|
crgb->b = val;
|
||
9 years ago
|
break;
|
||
|
|
||
|
case 5:
|
||
9 years ago
|
crgb->r = val;
|
||
|
crgb->g = base;
|
||
|
crgb->b = (((val - base)*(60 - (hue % 60))) / 60) + base;
|
||
9 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
LEDControl_ LEDControl;
|