2021-02-10 11:56:05 +03:00
|
|
|
#include "input_i.h"
|
2020-10-02 09:44:05 +03:00
|
|
|
|
2022-11-12 11:46:04 +03:00
|
|
|
// #define INPUT_DEBUG
|
|
|
|
|
2022-03-30 18:23:40 +03:00
|
|
|
#define GPIO_Read(input_pin) (furi_hal_gpio_read(input_pin.pin->gpio) ^ (input_pin.pin->inverted))
|
2020-10-19 13:36:40 +03:00
|
|
|
|
2021-02-10 11:56:05 +03:00
|
|
|
static Input* input = NULL;
|
2020-12-01 21:47:46 +03:00
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
inline static void input_timer_start(FuriTimer* timer_id, uint32_t ticks) {
|
2021-04-18 22:32:57 +03:00
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)timer_id;
|
|
|
|
furi_check(xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS);
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
inline static void input_timer_stop(FuriTimer* timer_id) {
|
2021-04-18 22:32:57 +03:00
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)timer_id;
|
|
|
|
furi_check(xTimerStop(hTimer, portMAX_DELAY) == pdPASS);
|
|
|
|
// xTimerStop is not actually stopping timer,
|
|
|
|
// Instead it places stop event into timer queue
|
|
|
|
// This code ensures that timer is stopped
|
2022-07-20 13:56:33 +03:00
|
|
|
while(xTimerIsTimerActive(hTimer) == pdTRUE) furi_delay_tick(1);
|
2021-04-18 22:32:57 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 11:56:05 +03:00
|
|
|
void input_press_timer_callback(void* arg) {
|
2021-02-26 13:40:54 +03:00
|
|
|
InputPinState* input_pin = arg;
|
2021-02-10 11:56:05 +03:00
|
|
|
InputEvent event;
|
2023-03-09 20:13:18 +03:00
|
|
|
event.sequence_source = INPUT_SEQUENCE_SOURCE_HARDWARE;
|
|
|
|
event.sequence_counter = input_pin->counter;
|
2021-02-26 13:40:54 +03:00
|
|
|
event.key = input_pin->pin->key;
|
2021-04-18 22:32:57 +03:00
|
|
|
input_pin->press_counter++;
|
|
|
|
if(input_pin->press_counter == INPUT_LONG_PRESS_COUNTS) {
|
2021-04-01 20:10:12 +03:00
|
|
|
event.type = InputTypeLong;
|
2021-11-01 23:35:54 +03:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2021-04-18 22:32:57 +03:00
|
|
|
} else if(input_pin->press_counter > INPUT_LONG_PRESS_COUNTS) {
|
|
|
|
input_pin->press_counter--;
|
2021-04-01 20:10:12 +03:00
|
|
|
event.type = InputTypeRepeat;
|
2021-11-01 23:35:54 +03:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2021-04-01 20:10:12 +03:00
|
|
|
}
|
2021-02-10 11:56:05 +03:00
|
|
|
}
|
2020-10-02 09:44:05 +03:00
|
|
|
|
2021-04-29 11:51:48 +03:00
|
|
|
void input_isr(void* _ctx) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(_ctx);
|
2022-06-20 17:54:48 +03:00
|
|
|
furi_thread_flags_set(input->thread_id, INPUT_THREAD_FLAG_ISR);
|
2021-02-10 11:56:05 +03:00
|
|
|
}
|
2020-10-02 09:44:05 +03:00
|
|
|
|
2021-08-31 11:22:52 +03:00
|
|
|
const char* input_get_key_name(InputKey key) {
|
|
|
|
for(size_t i = 0; i < input_pins_count; i++) {
|
|
|
|
if(input_pins[i].key == key) {
|
|
|
|
return input_pins[i].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* input_get_type_name(InputType type) {
|
|
|
|
switch(type) {
|
|
|
|
case InputTypePress:
|
|
|
|
return "Press";
|
|
|
|
case InputTypeRelease:
|
|
|
|
return "Release";
|
|
|
|
case InputTypeShort:
|
|
|
|
return "Short";
|
|
|
|
case InputTypeLong:
|
|
|
|
return "Long";
|
|
|
|
case InputTypeRepeat:
|
|
|
|
return "Repeat";
|
2022-11-02 17:36:17 +03:00
|
|
|
default:
|
|
|
|
return "Unknown";
|
2021-08-31 11:22:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 18:47:10 +03:00
|
|
|
int32_t input_srv(void* p) {
|
|
|
|
UNUSED(p);
|
2022-02-18 22:53:46 +03:00
|
|
|
input = malloc(sizeof(Input));
|
2022-06-20 17:54:48 +03:00
|
|
|
input->thread_id = furi_thread_get_current_id();
|
2021-11-01 23:35:54 +03:00
|
|
|
input->event_pubsub = furi_pubsub_alloc();
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_create(RECORD_INPUT_EVENTS, input->event_pubsub);
|
2020-10-02 09:44:05 +03:00
|
|
|
|
2022-11-12 11:46:04 +03:00
|
|
|
#if INPUT_DEBUG
|
|
|
|
furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
|
|
|
|
#endif
|
|
|
|
|
2021-12-24 21:47:48 +03:00
|
|
|
#ifdef SRV_CLI
|
2022-07-26 15:21:51 +03:00
|
|
|
input->cli = furi_record_open(RECORD_CLI);
|
2022-12-26 15:13:30 +03:00
|
|
|
cli_add_command(input->cli, "input", CliCommandFlagParallelSafe, input_cli, input);
|
2021-12-24 21:47:48 +03:00
|
|
|
#endif
|
2021-03-04 15:30:20 +03:00
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
input->pin_states = malloc(input_pins_count * sizeof(InputPinState));
|
2021-01-28 15:30:31 +03:00
|
|
|
|
2021-08-31 11:22:52 +03:00
|
|
|
for(size_t i = 0; i < input_pins_count; i++) {
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_add_int_callback(input_pins[i].gpio, input_isr, NULL);
|
2021-02-10 11:56:05 +03:00
|
|
|
input->pin_states[i].pin = &input_pins[i];
|
|
|
|
input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
|
|
|
|
input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
|
2022-07-20 13:56:33 +03:00
|
|
|
input->pin_states[i].press_timer = furi_timer_alloc(
|
|
|
|
input_press_timer_callback, FuriTimerTypePeriodic, &input->pin_states[i]);
|
2021-04-18 22:32:57 +03:00
|
|
|
input->pin_states[i].press_counter = 0;
|
2020-10-02 09:44:05 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 11:56:05 +03:00
|
|
|
while(1) {
|
|
|
|
bool is_changing = false;
|
2021-08-31 11:22:52 +03:00
|
|
|
for(size_t i = 0; i < input_pins_count; i++) {
|
2021-02-10 11:56:05 +03:00
|
|
|
bool state = GPIO_Read(input->pin_states[i]);
|
2022-11-12 11:46:04 +03:00
|
|
|
if(state) {
|
|
|
|
if(input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS)
|
|
|
|
input->pin_states[i].debounce += 1;
|
|
|
|
} else {
|
|
|
|
if(input->pin_states[i].debounce > 0) input->pin_states[i].debounce -= 1;
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:56:05 +03:00
|
|
|
if(input->pin_states[i].debounce > 0 &&
|
|
|
|
input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
|
|
|
|
is_changing = true;
|
|
|
|
} else if(input->pin_states[i].state != state) {
|
|
|
|
input->pin_states[i].state = state;
|
2021-04-18 22:32:57 +03:00
|
|
|
|
2021-02-10 11:56:05 +03:00
|
|
|
// Common state info
|
|
|
|
InputEvent event;
|
2023-03-09 20:13:18 +03:00
|
|
|
event.sequence_source = INPUT_SEQUENCE_SOURCE_HARDWARE;
|
2021-02-10 11:56:05 +03:00
|
|
|
event.key = input->pin_states[i].pin->key;
|
2021-04-18 22:32:57 +03:00
|
|
|
|
|
|
|
// Short / Long / Repeat timer routine
|
2021-02-10 11:56:05 +03:00
|
|
|
if(state) {
|
2021-09-02 00:05:00 +03:00
|
|
|
input->counter++;
|
|
|
|
input->pin_states[i].counter = input->counter;
|
2023-03-09 20:13:18 +03:00
|
|
|
event.sequence_counter = input->pin_states[i].counter;
|
2021-04-18 22:32:57 +03:00
|
|
|
input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
|
|
|
|
} else {
|
2023-03-09 20:13:18 +03:00
|
|
|
event.sequence_counter = input->pin_states[i].counter;
|
2021-04-18 22:32:57 +03:00
|
|
|
input_timer_stop(input->pin_states[i].press_timer);
|
|
|
|
if(input->pin_states[i].press_counter < INPUT_LONG_PRESS_COUNTS) {
|
|
|
|
event.type = InputTypeShort;
|
2021-11-01 23:35:54 +03:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2021-04-18 22:32:57 +03:00
|
|
|
}
|
|
|
|
input->pin_states[i].press_counter = 0;
|
2020-10-02 09:44:05 +03:00
|
|
|
}
|
2021-08-30 23:05:09 +03:00
|
|
|
|
|
|
|
// Send Press/Release event
|
|
|
|
event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
|
2021-11-01 23:35:54 +03:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2020-10-02 09:44:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:56:05 +03:00
|
|
|
if(is_changing) {
|
2022-11-12 11:46:04 +03:00
|
|
|
#if INPUT_DEBUG
|
|
|
|
furi_hal_gpio_write(&gpio_ext_pa4, 1);
|
|
|
|
#endif
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_tick(1);
|
2021-02-10 11:56:05 +03:00
|
|
|
} else {
|
2022-11-12 11:46:04 +03:00
|
|
|
#if INPUT_DEBUG
|
|
|
|
furi_hal_gpio_write(&gpio_ext_pa4, 0);
|
|
|
|
#endif
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_thread_flags_wait(INPUT_THREAD_FLAG_ISR, FuriFlagWaitAny, FuriWaitForever);
|
2020-10-02 09:44:05 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 20:24:34 +03:00
|
|
|
|
|
|
|
return 0;
|
2020-10-02 09:44:05 +03:00
|
|
|
}
|