-
Notifications
You must be signed in to change notification settings - Fork 925
Open
Description
The ticks returned by iot_button_get_ticks_time are off by factor of 10.
Seems it doesn't take the freertos tick rate into account:
Example:
#define CONFIG_FREERTOS_HZ 100 // Set in sdkconfig
static void btn_init()
{
const button_config_t btn_cfg = {
.long_press_time = 5000, // 5S
.short_press_time = BUTTON_SHORT_PRESS_TIME_MS, // 1mS
};
const button_gpio_config_t btn_gpio_cfg = {
.gpio_num = PIN_BTN,
.active_level = 0,
.disable_pull = true, // input-only pad has no internal PU, external PU present
};
button_handle_t btn = NULL;
if (iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btn) != ESP_OK)
{
ESP_LOGE(TAG, "Failed to create button");
}
else
{
iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, NULL, button_long_press_start_cb, NULL);
iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, NULL, button_long_press_up_cb, NULL);
}
}
TickType_t startTimeTicks = 0;
static void button_long_press_start_cb(void *arg, void *usr_data)
{
ESP_LOGI(TAG, "BUTTON_LONG_PRESS_START");
ESP_LOGW(TAG, "Realease for calibration\nHold for factory reset");
startTimeTicks = xTaskGetTickCount();
}
static void button_long_press_up_cb(void *arg, void *usr_data)
{
ESP_LOGI(TAG, "BUTTON_LONG_PRESS_UP");
// Time since button is pressed, !!!! BUG off by factor of 10
uint32_t hold_time_ticks = iot_button_get_ticks_time((button_handle_t)arg);
ESP_LOGI(TAG, "Btn hold for %d ticks = %d s", hold_time_ticks, pdTICKS_TO_MS(hold_time_ticks) / 1000);
// Manual calculation of long press time
ESP_LOGI(TAG, "TICKRATE is 100HZ so pdTICKS_TO_MS(100) = %d ms", pdTICKS_TO_MS(100)); // Sanity check
TickType_t endTime = xTaskGetTickCount();
TickType_t timeDiff = endTime - startTimeTicks;
ESP_LOGI(TAG, "manual Time diff ticks: %d = %d ms", timeDiff, pdTICKS_TO_MS(timeDiff));
}
Output (when holding the button for ~7 seconds:
W (25098) main.c: Realease for calibration
Hold for factory reset
I (27128) main.c: BUTTON_LONG_PRESS_UP
I (27128) main.c: Btn hold for 7030 ticks = 70 s
I (27128) main.c: TICKRATE is 100HZ so pdTICKS_TO_MS(100) = 1000 ms
I (27138) main.c: manual Time diff ticks: 204 = 2040 ms
Expected output:
this: I (27128) main.c: Btn hold for 7030 ticks = 70 s should be I (27128) main.c: Btn hold for 703 ticks = 7 s
Workaround:
change pdTICKS_TO_MS(hold_time_ticks) / 1000 to pdTICKS_TO_MS(hold_time_ticks) / 10000
By changing the CONFIG_FREERTOS_HZ to 1000 instead of the default 100HZ the iot_button_get_ticks_time function return the correct amount of ticks:
I (65818) main.c: BUTTON_LONG_PRESS_UP
I (65818) main.c: Btn hold for 6780 ticks = 6 s
I (65818) main.c: TICKRATE is 1000HZ so pdTICKS_TO_MS(100) = 100 ms
I (65823) main.c: manual Time diff ticks: 1785 = 1785 ms
Metadata
Metadata
Assignees
Labels
No labels