From d99bc90631e9d9762d6646372c71e29359cf8947 Mon Sep 17 00:00:00 2001 From: Vijay Hiremath Date: Thu, 1 Sep 2016 14:23:48 -0700 Subject: [PATCH] button: Add console command to simulate button press BUG=chrome-os-partner:56878 BRANCH=none TEST=Using console command 'button' verified that volume button icon slides on reef. Change-Id: I8051194fd91f989f8887cebce6ea2af1b8c9f731 Signed-off-by: Vijay Hiremath Reviewed-on: https://chromium-review.googlesource.com/380315 Commit-Ready: Bill Richardson Tested-by: Vijay P Hiremath Tested-by: Bill Richardson Reviewed-by: Bill Richardson --- common/button.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++- include/config.h | 1 + 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/common/button.c b/common/button.c index deace7f2f1..fb47984ad9 100644 --- a/common/button.c +++ b/common/button.c @@ -26,12 +26,29 @@ static struct button_state_t __bss_slow state[CONFIG_BUTTON_COUNT]; static uint64_t __bss_slow next_deferred_time; +#ifdef CONFIG_CMD_BUTTON +static int siml_btn_presd; + +static int simulated_button_pressed(void) +{ + static int button = 1; + + button = !button; + return button; +} +#endif + /* * Whether a button is currently pressed. */ static int raw_button_pressed(const struct button_config *button) { - int raw_value = gpio_get_level(button->gpio); + int raw_value = +#ifdef CONFIG_CMD_BUTTON + siml_btn_presd ? + simulated_button_pressed() : +#endif + gpio_get_level(button->gpio); return button->flags & BUTTON_FLAG_ACTIVE_HIGH ? raw_value : !raw_value; @@ -130,3 +147,70 @@ void button_interrupt(enum gpio_signal signal) break; } } + +#ifdef CONFIG_CMD_BUTTON +static int button_present(enum keyboard_button_type type) +{ + int i; + + for (i = 0; i < CONFIG_BUTTON_COUNT; i++) + if (buttons[i].type == type) + break; + + return i; +} + +static void button_interrupt_simulate(int button) +{ + button_interrupt(buttons[button].gpio); + usleep(buttons[button].debounce_us >> 2); + button_interrupt(buttons[button].gpio); +} + +static int console_command_button(int argc, char **argv) +{ + int button; + int press_ms = 50; + char *e; + + if (argc < 2) + return EC_ERROR_PARAM_COUNT; + + if (!strcasecmp(argv[1], "vup")) + button = button_present(KEYBOARD_BUTTON_VOLUME_UP); + else if (!strcasecmp(argv[1], "vdown")) + button = button_present(KEYBOARD_BUTTON_VOLUME_DOWN); + else + return EC_ERROR_PARAM1; + + if (button == CONFIG_BUTTON_COUNT) + return EC_ERROR_PARAM1; + + if (argc > 2) { + press_ms = strtoi(argv[2], &e, 0); + if (*e) + return EC_ERROR_PARAM2; + } + + siml_btn_presd = 1; + + /* Press the button */ + button_interrupt_simulate(button); + + /* Hold the button */ + msleep(press_ms); + + /* Release the button */ + button_interrupt_simulate(button); + + /* Wait till button processing is finished */ + msleep(100); + + siml_btn_presd = 0; + + return EC_SUCCESS; +} +DECLARE_CONSOLE_COMMAND(button, console_command_button, + "vup|vdown msec", + "Simulate button press"); +#endif diff --git a/include/config.h b/include/config.h index 5ccb5ce39e..1cb5b83108 100644 --- a/include/config.h +++ b/include/config.h @@ -560,6 +560,7 @@ #define CONFIG_CMD_APTHROTTLE #undef CONFIG_CMD_BATDEBUG #define CONFIG_CMD_BATTFAKE +#undef CONFIG_CMD_BUTTON #define CONFIG_CMD_CHARGER #undef CONFIG_CMD_CHARGER_ADC_AMON_BMON #undef CONFIG_CMD_CHARGER_PSYS