From 6e9b2e84e5bbb1e898e7be0a9a4e2e4596b8f46e Mon Sep 17 00:00:00 2001 From: Renze Nicolai Date: Wed, 3 Aug 2022 13:12:21 +0200 Subject: [PATCH] Read buttons only once every 30ms --- i2c_peripheral.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/i2c_peripheral.c b/i2c_peripheral.c index 80dde34..234f157 100644 --- a/i2c_peripheral.c +++ b/i2c_peripheral.c @@ -51,6 +51,7 @@ static const uint8_t i2c_controlled_gpios[] = {SAO_IO0_PIN, SAO_IO1_PIN, PROTO_0 static const uint8_t input1_gpios[] = {BUTTON_HOME, BUTTON_MENU, BUTTON_START, BUTTON_ACCEPT, BUTTON_BACK, FPGA_CDONE}; static const uint8_t input2_gpios[] = {BUTTON_JOY_A, BUTTON_JOY_B, BUTTON_JOY_C, BUTTON_JOY_D, BUTTON_JOY_E}; +static absolute_time_t next_button_poll; static absolute_time_t next_adc_read; static uint8_t next_adc_channel; @@ -133,6 +134,8 @@ void setup_i2c_registers(int param_ir_statemachine) { gpio_set_dir(BATT_CHRG_PIN, false); gpio_pull_up(BATT_CHRG_PIN); + next_button_poll = get_absolute_time(); + next_adc_read = get_absolute_time(); next_adc_channel = 0; @@ -295,25 +298,34 @@ void i2c_task() { gpio_in_value |= gpio_get(i2c_controlled_gpios[index]) << index; } - // Read inputs - uint8_t input1_value = 0; - for (uint8_t index = 0; index < sizeof(input1_gpios); index++) { - input1_value |= (!gpio_get(input1_gpios[index])) << index; - } - input1_value |= board_button_read() << 7; // Select button - if (input1_value != i2c_registers.registers[I2C_REGISTER_INPUT1]) interrupt_target = true; - i2c_registers.registers[I2C_REGISTER_INTERRUPT1] |= (input1_value ^ i2c_registers.registers[I2C_REGISTER_INPUT1]); - i2c_registers.registers[I2C_REGISTER_INPUT1] = input1_value; - - uint8_t input2_value = 0; - for (uint8_t index = 0; index < sizeof(input2_gpios); index++) { - input2_value |= (!gpio_get(input2_gpios[index])) << index; + absolute_time_t now = get_absolute_time(); + +#ifdef NDEBUG + if (now > next_button_poll) { +#else + if (now._private_us_since_boot > next_button_poll._private_us_since_boot) { +#endif + next_button_poll = delayed_by_ms(now, 30); + + // Read inputs + uint8_t input1_value = 0; + for (uint8_t index = 0; index < sizeof(input1_gpios); index++) { + input1_value |= (!gpio_get(input1_gpios[index])) << index; + } + input1_value |= board_button_read() << 7; // Select button + if (input1_value != i2c_registers.registers[I2C_REGISTER_INPUT1]) interrupt_target = true; + i2c_registers.registers[I2C_REGISTER_INTERRUPT1] |= (input1_value ^ i2c_registers.registers[I2C_REGISTER_INPUT1]); + i2c_registers.registers[I2C_REGISTER_INPUT1] = input1_value; + + uint8_t input2_value = 0; + for (uint8_t index = 0; index < sizeof(input2_gpios); index++) { + input2_value |= (!gpio_get(input2_gpios[index])) << index; + } + if (input2_value != i2c_registers.registers[I2C_REGISTER_INPUT2]) interrupt_target = true; + i2c_registers.registers[I2C_REGISTER_INTERRUPT2] |= (input2_value ^ i2c_registers.registers[I2C_REGISTER_INPUT2]); + i2c_registers.registers[I2C_REGISTER_INPUT2] = input2_value; } - if (input2_value != i2c_registers.registers[I2C_REGISTER_INPUT2]) interrupt_target = true; - i2c_registers.registers[I2C_REGISTER_INTERRUPT2] |= (input2_value ^ i2c_registers.registers[I2C_REGISTER_INPUT2]); - i2c_registers.registers[I2C_REGISTER_INPUT2] = input2_value; - absolute_time_t now = get_absolute_time(); #ifdef NDEBUG if (now > next_adc_read) { // Once every 250ms #else