RGBKBD: Consolidate grids' states into one
Currently, grids track states individually. This patch consolidates
grids' states into a single state common to all grids.
BRANCH=None
TEST=Vell
@@ -36,6 +36,8 @@ test_export_static enum rgbkbd_demo demo =
test_export_static
uint8_t rgbkbd_table[EC_RGBKBD_MAX_KEY_COUNT];
+static enum rgbkbd_state rgbkbd_state;
+
static int set_color_single(struct rgb_s color, int x, int y)
{
struct rgbkbd *ctx = &rgbkbds[0];
@@ -279,16 +281,12 @@ static int rgbkbd_init(void)
{
int rv = EC_SUCCESS;
int e, i;
- bool updated = false;
rgbkbd_init_lookup_table();
for (i = 0; i < rgbkbd_count; i++) {
struct rgbkbd *ctx = &rgbkbds[i];
- if (ctx->state >= RGBKBD_STATE_INITIALIZED)
- continue;
-
e = ctx->cfg->drv->init(ctx);
if (e) {
CPRINTS("Failed to init GRID%d (%d)", i, e);
@@ -296,20 +294,18 @@ static int rgbkbd_init(void)
continue;
}
- ctx->state = RGBKBD_STATE_INITIALIZED;
- updated = true;
-
e = ctx->cfg->drv->set_scale(ctx, 0, 0x80, get_grid_size(ctx));
if (e) {
CPRINTS("Failed to set scale of GRID%d (%d)", i, e);
rv = e;
}
+
+ CPRINTS("Initialized GRID%d", i);
}
- if (updated)
- CPRINTS("Initialized (%d)", rv);
+ if (rv == EC_SUCCESS)
+ rgbkbd_state = RGBKBD_STATE_INITIALIZED;
- /* Return EC_SUCCESS or the last error. */
return rv;
}
@@ -317,14 +313,18 @@ static int rgbkbd_enable(int enable)
{
int rv = EC_SUCCESS;
int e, i;
- bool updated = false;
+
+ if (enable) {
+ if (rgbkbd_state == RGBKBD_STATE_ENABLED)
+ return EC_SUCCESS;
+ } else {
+ if (rgbkbd_state == RGBKBD_STATE_DISABLED)
+ return EC_SUCCESS;
+ }
for (i = 0; i < rgbkbd_count; i++) {
struct rgbkbd *ctx = &rgbkbds[i];
- if (ctx->state >= RGBKBD_STATE_ENABLED && enable)
- continue;
-
e = ctx->cfg->drv->enable(ctx, enable);
if (e) {
CPRINTS("Failed to %s GRID%d (%d)",
@@ -333,13 +333,13 @@ static int rgbkbd_enable(int enable)
continue;
}
- ctx->state = enable ?
- RGBKBD_STATE_ENABLED : RGBKBD_STATE_DISABLED;
- updated = true;
+ CPRINTS("%s GRID%d", enable ? "Enabled" : "Disabled", i);
}
- if (updated)
- CPRINTS("%s (%d)", enable ? "Enabled" : "Disabled", rv);
+ if (rv == EC_SUCCESS) {
+ rgbkbd_state = enable ?
+ RGBKBD_STATE_ENABLED : RGBKBD_STATE_DISABLED;
+ }
/* Return EC_SUCCESS or the last error. */
return rv;
@@ -353,13 +353,14 @@ static int rgbkbd_kblight_set(int percent)
static int rgbkbd_get_enabled(void)
{
- return rgbkbds[0].state >= RGBKBD_STATE_ENABLED;
+ return rgbkbd_state >= RGBKBD_STATE_ENABLED;
}
static void rgbkbd_reset(void)
{
board_kblight_shutdown();
board_kblight_init();
+ rgbkbd_state = RGBKBD_STATE_RESET;
}
const struct kblight_drv kblight_rgbkbd = {
