From 60e3d01e0d29049296eff78cf0eb30994383b85f Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Thu, 9 May 2019 02:44:04 -0400 Subject: [PATCH] refactor --- fido2/device.h | 1 + targets/stm32l432/src/device.c | 40 ++++++++++------------------------ targets/stm32l432/src/sense.c | 33 +++++++++++----------------- targets/stm32l432/src/sense.h | 5 ++--- 4 files changed, 28 insertions(+), 51 deletions(-) diff --git a/fido2/device.h b/fido2/device.h index e13ea51..5d9950e 100644 --- a/fido2/device.h +++ b/fido2/device.h @@ -103,5 +103,6 @@ void device_set_clock_rate(DEVICE_CLOCK_RATE param); // 0 otherwise. bool device_is_nfc(); +void device_init_button(); #endif diff --git a/targets/stm32l432/src/device.c b/targets/stm32l432/src/device.c index 1c4e8cd..1f387dd 100644 --- a/targets/stm32l432/src/device.c +++ b/targets/stm32l432/src/device.c @@ -105,6 +105,7 @@ void device_set_status(uint32_t status) int device_is_button_pressed() { + return IS_BUTTON_PRESSED(); } @@ -119,25 +120,17 @@ void device_reboot() NVIC_SystemReset(); } - - -static int does_device_have_touch_sensor() +void device_init_button() { - int does; - LL_GPIO_InitTypeDef GPIO_InitStruct; - GPIO_InitStruct.Pin = LL_GPIO_PIN_1; - GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; - GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.OutputType = 0; - GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; - LL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - does = (LL_GPIO_ReadInputPort(GPIOB) & 1) == 0; - - GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; - LL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - return does; + if (tsc_sensor_exists()) + { + tsc_init(); + IS_BUTTON_PRESSED = is_touch_button_pressed; + } + else + { + IS_BUTTON_PRESSED = is_physical_button_pressed; + } } void device_init(int argc, char *argv[]) @@ -156,16 +149,7 @@ void device_init(int argc, char *argv[]) printf1(TAG_NFC, "Have NO NFC\r\n"); hw_init(HIGH_FREQUENCY); isLowFreq = 0; - - if (does_device_have_touch_sensor()) - { - tsc_init(); - IS_BUTTON_PRESSED = is_touch_button_pressed; - } - else - { - IS_BUTTON_PRESSED = is_physical_button_pressed; - } + device_init_button(); } usbhid_init(); diff --git a/targets/stm32l432/src/sense.c b/targets/stm32l432/src/sense.c index 803ab1d..9d8ee46 100644 --- a/targets/stm32l432/src/sense.c +++ b/targets/stm32l432/src/sense.c @@ -127,28 +127,21 @@ uint32_t tsc_read_button(uint32_t index) return tsc_read(1) < 50; } -void sense_run() +int tsc_sensor_exists() { - static uint32_t tlim = 0; - uint32_t t1,t2; - uint32_t but0,but1; + int does; + LL_GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.Pin = LL_GPIO_PIN_1; + GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; + GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.OutputType = 0; + GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; + LL_GPIO_Init(GPIOB, &GPIO_InitStruct); - if (!_has_init) - { - tsc_init(); - _has_init = 1; - } + does = (LL_GPIO_ReadInputPort(GPIOB) & 1) == 0; - if ((millis() - tlim) > 200) - { - t1 = millis(); - but0 = tsc_read_button(0); - but1 = tsc_read_button(1); - t2 = millis(); + GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; + LL_GPIO_Init(GPIOB, &GPIO_InitStruct); - printf1(TAG_GREEN, "but0: %02d but1: %02d (%d ms)\r\n", but0, but1, t2-t1); - t1 = millis(); - - tlim = millis(); - } + return does; } diff --git a/targets/stm32l432/src/sense.h b/targets/stm32l432/src/sense.h index 98f9839..0b73ec6 100644 --- a/targets/stm32l432/src/sense.h +++ b/targets/stm32l432/src/sense.h @@ -7,9 +7,8 @@ extern int _run_sense_app; void tsc_init(); +int tsc_sensor_exists(); + uint32_t tsc_read_button(uint32_t index); -// For testing -void sense_run(); - #endif