#include "mxss_frontled.h"
#include "tmk_core/common/eeprom.h"
#include "rgblight.h"
#include "via.h"
#include "version.h"
uint8_t fled_mode; uint8_t fled_val; LED_TYPE fleds[2];
__attribute__ ((weak))
hs_set layer_colors[FRONTLED_COLOR_MAXCNT];
__attribute__ ((weak))
hs_set caps_color;
__attribute__ ((weak))
size_t lc_size = sizeof(layer_colors) / sizeof(hs_set);
void fled_init(void) {
if (via_eeprom_is_valid()) {
fled_load_conf();
} else {
fled_mode = FLED_RGB;
fled_val = 10 * FLED_VAL_STEP;
caps_color.hue = 0;
caps_color.sat = 255;
layer_colors[0].hue = 0;
layer_colors[0].sat = 0;
layer_colors[1].hue = 86;
layer_colors[1].sat = 255;
layer_colors[2].hue = 36;
layer_colors[2].sat = 255;
layer_colors[3].hue = 185;
layer_colors[3].sat = 255;
fled_update_conf(); }
setrgb(0, 0, 0, &fleds[0]);
setrgb(0, 0, 0, &fleds[1]);
if (fled_mode == FLED_INDI) {
fled_lock_update(host_keyboard_led_state());
fled_layer_update(layer_state);
}
}
void process_record_fled(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case FLED_MOD: if (record->event.pressed)
fled_mode_cycle();
break;
case FLED_VAI: if (record->event.pressed)
fled_val_increase();
break;
case FLED_VAD: if (record->event.pressed)
fled_val_decrease();
break;
default:
break; }
return;
}
void fled_load_conf(void) {
fled_config fled_conf;
fled_conf.raw = eeprom_read_byte(FRONTLED_CONF_ADDR);
fled_mode = fled_conf.mode;
fled_val = fled_conf.val * FLED_VAL_STEP;
uint8_t stored_cnt = eeprom_read_byte(FRONTLED_COLOR_CNT_ADDR);
uint16_t *color_ptr = FRONTLED_COLOR_ADDR;
caps_color.raw = eeprom_read_word(color_ptr); for (uint8_t i = 1; i < stored_cnt; i++) {
if (i == lc_size) break;
layer_colors[i].raw = eeprom_read_word(&color_ptr[i]);
}
layer_colors[0].raw = 0; }
void fled_update_conf(void)
{
fled_config conf;
conf.mode = fled_mode;
if (fled_val == 255)
conf.val = 256 / FLED_VAL_STEP;
else
conf.val = fled_val / FLED_VAL_STEP;
eeprom_update_byte(FRONTLED_CONF_ADDR, conf.raw);
uint16_t *color_ptr = FRONTLED_COLOR_ADDR;
eeprom_update_word(color_ptr, caps_color.raw);
uint8_t i = 1;
for (; i < lc_size; i++) {
if (i == FRONTLED_COLOR_MAXCNT) break;
eeprom_update_word(&color_ptr[i], layer_colors[i].raw);
}
eeprom_update_byte(FRONTLED_COLOR_CNT_ADDR, i); }
void fled_mode_cycle(void)
{
switch (fled_mode) {
case FLED_OFF:
fled_mode = FLED_RGB;
rgblight_timer_enable();
break;
case FLED_RGB:
fled_mode = FLED_INDI;
break;
case FLED_INDI:
fled_mode = FLED_OFF;
break;
}
fled_update_conf();
rgblight_set();
}
void fled_val_increase(void)
{
if (fled_val + FLED_VAL_STEP > 255)
fled_val = 255;
else
fled_val += FLED_VAL_STEP;
fled_update_conf();
if (fled_mode == FLED_INDI) {
fled_layer_update(layer_state);
fled_lock_update(host_keyboard_led_state());
} else {
rgblight_set();
}
}
void fled_val_decrease(void)
{
if (fled_val - FLED_VAL_STEP > 255)
fled_val = 255;
else
fled_val -= FLED_VAL_STEP;
fled_update_conf();
if (fled_mode == FLED_INDI) {
fled_layer_update(layer_state);
fled_lock_update(host_keyboard_led_state());
} else {
rgblight_set();
}
}
void fled_layer_update(layer_state_t state) {
uint8_t layer = get_highest_layer(state);
if (layer < lc_size && !(layer_colors[layer].hue == 0 && layer_colors[layer].sat == 0)) {
sethsv(layer_colors[layer].hue, layer_colors[layer].sat, fled_val, &fleds[1]);
} else {
setrgb(0, 0, 0, &fleds[1]);
}
}
void fled_lock_update(led_t led_state) {
if (led_state.caps_lock) {
sethsv(caps_color.hue, caps_color.sat, fled_val, &fleds[0]);
} else {
setrgb(0, 0, 0, &fleds[0]);
}
rgblight_set();
}
void set_fled_layer_color(uint8_t layer, hs_set hs) {
layer_colors[layer] = hs;
fled_layer_update(layer_state);
fled_update_conf();
}
hs_set get_fled_layer_color(uint8_t layer) {
return layer_colors[layer];
}
void set_fled_caps_color(hs_set hs) {
caps_color = hs;
fled_lock_update(host_keyboard_led_state());
fled_update_conf();
}
hs_set get_fled_caps_color(void) {
return caps_color;
}
#ifndef VIA_ENABLE
bool via_eeprom_is_valid(void)
{
char *p = QMK_BUILDDATE; uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
}
#endif