2021-02-10 08:56:05 +00:00
|
|
|
#include "input_i.h"
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
#define GPIO_Read(input_pin) \
|
|
|
|
(HAL_GPIO_ReadPin((GPIO_TypeDef*)input_pin.pin->port, input_pin.pin->pin) ^ \
|
|
|
|
input_pin.pin->inverted)
|
2020-10-19 10:36:40 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
static Input* input = NULL;
|
2020-12-01 18:47:46 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
void input_press_timer_callback(void* arg) {
|
2021-02-26 10:40:54 +00:00
|
|
|
InputPinState* input_pin = arg;
|
2021-02-10 08:56:05 +00:00
|
|
|
InputEvent event;
|
2021-02-26 10:40:54 +00:00
|
|
|
event.key = input_pin->pin->key;
|
2021-02-10 08:56:05 +00:00
|
|
|
event.type = InputTypeLong;
|
|
|
|
notify_pubsub(&input->event_pubsub, &event);
|
|
|
|
}
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
void input_isr(void* _pin, void* _ctx) {
|
|
|
|
osThreadFlagsSet(input->thread, INPUT_THREAD_FLAG_ISR);
|
|
|
|
}
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-02-12 17:24:34 +00:00
|
|
|
int32_t input_task() {
|
2021-02-10 08:56:05 +00:00
|
|
|
input = furi_alloc(sizeof(Input));
|
|
|
|
input->thread = osThreadGetId();
|
|
|
|
init_pubsub(&input->event_pubsub);
|
|
|
|
furi_record_create("input_events", &input->event_pubsub);
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
const size_t pin_count = input_pins_count;
|
|
|
|
input->pin_states = furi_alloc(pin_count * sizeof(InputPinState));
|
2021-01-28 12:30:31 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
api_interrupt_add(input_isr, InterruptTypeExternalInterrupt, NULL);
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
for(size_t i = 0; i < pin_count; i++) {
|
|
|
|
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;
|
|
|
|
input->pin_states[i].press_timer =
|
|
|
|
osTimerNew(input_press_timer_callback, osTimerOnce, &input->pin_states[i], NULL);
|
2020-10-02 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
while(1) {
|
|
|
|
bool is_changing = false;
|
|
|
|
for(size_t i = 0; i < pin_count; i++) {
|
|
|
|
bool state = GPIO_Read(input->pin_states[i]);
|
|
|
|
if(input->pin_states[i].debounce > 0 &&
|
|
|
|
input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
|
|
|
|
is_changing = true;
|
|
|
|
input->pin_states[i].debounce += (state ? 1 : -1);
|
|
|
|
} else if(input->pin_states[i].state != state) {
|
|
|
|
input->pin_states[i].state = state;
|
|
|
|
// Common state info
|
|
|
|
InputEvent event;
|
|
|
|
event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
|
|
|
|
event.key = input->pin_states[i].pin->key;
|
|
|
|
// Send Press/Release event
|
|
|
|
notify_pubsub(&input->event_pubsub, &event);
|
|
|
|
// Short/Long press logic
|
|
|
|
if(state) {
|
|
|
|
osTimerStart(input->pin_states[i].press_timer, INPUT_LONG_PRESS_TICKS);
|
|
|
|
} else if(osTimerStop(input->pin_states[i].press_timer) == osOK) {
|
|
|
|
event.type = InputTypeShort;
|
|
|
|
notify_pubsub(&input->event_pubsub, &event);
|
2020-10-02 06:44:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
if(is_changing) {
|
2020-10-02 06:44:05 +00:00
|
|
|
osDelay(1);
|
2021-02-10 08:56:05 +00:00
|
|
|
} else {
|
|
|
|
osThreadFlagsWait(INPUT_THREAD_FLAG_ISR, osFlagsWaitAny, osWaitForever);
|
2020-10-02 06:44:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 17:24:34 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-10-02 06:44:05 +00:00
|
|
|
}
|