Skip to content

Commit

Permalink
LED Matrix: Fix up eeconfig code (qmk#12327)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Mar 24, 2021
1 parent 485adb3 commit 256c42d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 26 deletions.
75 changes: 49 additions & 26 deletions quantum/led_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,41 @@ led_eeconfig_t led_matrix_eeconfig;
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

#ifndef LED_DISABLE_AFTER_TIMEOUT
# define LED_DISABLE_AFTER_TIMEOUT 0
#if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT)
# define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL)
#endif

#ifndef LED_DISABLE_TIMEOUT
# define LED_DISABLE_TIMEOUT 0
#endif

#ifndef LED_DISABLE_WHEN_USB_SUSPENDED
# define LED_DISABLE_WHEN_USB_SUSPENDED false
#endif

#if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255
# define LED_MATRIX_MAXIMUM_BRIGHTNESS 255
#if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
# undef LED_MATRIX_MAXIMUM_BRIGHTNESS
# define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
#endif

#if !defined(LED_MATRIX_VAL_STEP)
# define LED_MATRIX_VAL_STEP 8
#endif

#if !defined(LED_MATRIX_SPD_STEP)
# define LED_MATRIX_SPD_STEP 16
#endif

#if !defined(LED_MATRIX_STARTUP_MODE)
# define LED_MATRIX_STARTUP_MODE LED_MATRIX_UNIFORM_BRIGHTNESS
#endif

#if !defined(LED_MATRIX_STARTUP_VAL)
# define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
#endif

#if !defined(LED_MATRIX_STARTUP_SPD)
# define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
#endif

bool g_suspend_state = false;
Expand All @@ -57,21 +82,21 @@ uint8_t g_key_hit[DRIVER_LED_TOTAL];
// Ticks since any key was last hit.
uint32_t g_any_key_hit = 0;

uint32_t eeconfig_read_led_matrix(void) { return eeprom_read_dword(EECONFIG_LED_MATRIX); }
void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }

void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); }
void eeconfig_update_led_matrix(void) { eeprom_update_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }

void eeconfig_update_led_matrix_default(void) {
dprintf("eeconfig_update_led_matrix_default\n");
led_matrix_eeconfig.enable = 1;
led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS;
led_matrix_eeconfig.val = 128;
led_matrix_eeconfig.speed = 0;
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
led_matrix_eeconfig.mode = LED_MATRIX_STARTUP_MODE;
led_matrix_eeconfig.val = LED_MATRIX_STARTUP_VAL;
led_matrix_eeconfig.speed = LED_MATRIX_STARTUP_SPD;
eeconfig_update_led_matrix();
}

void eeconfig_debug_led_matrix(void) {
dprintf("led_matrix_eeconfig eeprom\n");
dprintf("led_matrix_eeconfig EEPROM\n");
dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
Expand Down Expand Up @@ -154,7 +179,7 @@ void led_matrix_task(void) {

// Ideally we would also stop sending zeros to the LED driver PWM buffers
// while suspended and just do a software shutdown. This is a cheap hack for now.
bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20));
bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || (LED_DISABLE_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_TIMEOUT));
uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode;

// this gets ticked at 20 Hz.
Expand Down Expand Up @@ -220,12 +245,10 @@ void led_matrix_init(void) {
eeconfig_update_led_matrix_default();
}

led_matrix_eeconfig.raw = eeconfig_read_led_matrix();

eeconfig_read_led_matrix();
if (!led_matrix_eeconfig.mode) {
dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
eeconfig_update_led_matrix_default();
led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
}

eeconfig_debug_led_matrix(); // display current eeprom values
Expand Down Expand Up @@ -269,19 +292,19 @@ uint32_t led_matrix_get_tick(void) { return g_tick; }

void led_matrix_toggle(void) {
led_matrix_eeconfig.enable ^= 1;
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}

void led_matrix_enable(void) {
led_matrix_eeconfig.enable = 1;
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}

void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; }

void led_matrix_disable(void) {
led_matrix_eeconfig.enable = 0;
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}

void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; }
Expand All @@ -291,41 +314,41 @@ void led_matrix_step(void) {
if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) {
led_matrix_eeconfig.mode = 1;
}
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}

void led_matrix_step_reverse(void) {
led_matrix_eeconfig.mode--;
if (led_matrix_eeconfig.mode < 1) {
led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
}
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}

void led_matrix_increase_val(void) {
led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}

void led_matrix_decrease_val(void) {
led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}

void led_matrix_increase_speed(void) {
led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3);
eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
eeconfig_update_led_matrix(); // EECONFIG needs to be increased to support this
}

void led_matrix_decrease_speed(void) {
led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3);
eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
eeconfig_update_led_matrix(); // EECONFIG needs to be increased to support this
}

void led_matrix_mode(uint8_t mode, bool eeprom_write) {
led_matrix_eeconfig.mode = mode;
if (eeprom_write) {
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}
}

Expand All @@ -335,5 +358,5 @@ void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val;

void led_matrix_set_value(uint8_t val) {
led_matrix_set_value_noeeprom(val);
eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
eeconfig_update_led_matrix();
}
27 changes: 27 additions & 0 deletions quantum/led_matrix_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,43 @@
# pragma pack(push, 1)
#endif

#if defined(LED_MATRIX_KEYPRESSES) || defined(LED_MATRIX_KEYRELEASES)
# define LED_MATRIX_KEYREACTIVE_ENABLED
#endif

// Last led hit
#ifndef LED_HITS_TO_REMEMBER
# define LED_HITS_TO_REMEMBER 8
#endif // LED_HITS_TO_REMEMBER

#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
typedef struct PACKED {
uint8_t count;
uint8_t x[LED_HITS_TO_REMEMBER];
uint8_t y[LED_HITS_TO_REMEMBER];
uint8_t index[LED_HITS_TO_REMEMBER];
uint16_t tick[LED_HITS_TO_REMEMBER];
} last_hit_t;
#endif // LED_MATRIX_KEYREACTIVE_ENABLED

typedef enum led_task_states { STARTING, RENDERING, FLUSHING, SYNCING } led_task_states;

typedef uint8_t led_flags_t;

typedef struct PACKED {
uint8_t iter;
led_flags_t flags;
bool init;
} effect_params_t;

typedef struct PACKED {
uint8_t x;
uint8_t y;
} point_t;

#define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
#define HAS_ANY_FLAGS(bits, flags) ((bits & flags) != 0x00)

#define LED_FLAG_ALL 0xFF
#define LED_FLAG_NONE 0x00
#define LED_FLAG_MODIFIER 0x01
Expand Down

0 comments on commit 256c42d

Please sign in to comment.