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-04-18 19:32:57 +00:00
|
|
|
inline static void input_timer_start(osTimerId_t timer_id, uint32_t ticks) {
|
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)timer_id;
|
|
|
|
furi_check(xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static void input_timer_stop(osTimerId_t timer_id) {
|
|
|
|
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
|
|
|
|
while(xTimerIsTimerActive(hTimer) == pdTRUE) osDelay(1);
|
|
|
|
}
|
|
|
|
|
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-09-01 21:05:00 +00:00
|
|
|
event.sequence = input_pin->counter;
|
2021-02-26 10:40:54 +00:00
|
|
|
event.key = input_pin->pin->key;
|
2021-04-18 19:32:57 +00:00
|
|
|
input_pin->press_counter++;
|
|
|
|
if(input_pin->press_counter == INPUT_LONG_PRESS_COUNTS) {
|
2021-04-01 17:10:12 +00:00
|
|
|
event.type = InputTypeLong;
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2021-04-18 19:32:57 +00:00
|
|
|
} else if(input_pin->press_counter > INPUT_LONG_PRESS_COUNTS) {
|
|
|
|
input_pin->press_counter--;
|
2021-04-01 17:10:12 +00:00
|
|
|
event.type = InputTypeRepeat;
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2021-04-01 17:10:12 +00:00
|
|
|
}
|
2021-02-10 08:56:05 +00:00
|
|
|
}
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-04-29 08:51:48 +00:00
|
|
|
void input_isr(void* _ctx) {
|
2021-02-10 08:56:05 +00:00
|
|
|
osThreadFlagsSet(input->thread, INPUT_THREAD_FLAG_ISR);
|
|
|
|
}
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-05-18 13:57:39 +00:00
|
|
|
void input_cli_send(Cli* cli, string_t args, void* context) {
|
2021-03-04 12:30:20 +00:00
|
|
|
InputEvent event;
|
|
|
|
|
|
|
|
// Get first word as key name
|
|
|
|
string_t key_name;
|
|
|
|
string_init(key_name);
|
|
|
|
size_t ws = string_search_char(args, ' ');
|
|
|
|
if(ws == STRING_FAILURE) {
|
2021-04-18 19:32:57 +00:00
|
|
|
printf("Invalid arguments. Use `input_send KEY TYPE`.");
|
2021-03-04 12:30:20 +00:00
|
|
|
string_clear(key_name);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
string_set_n(key_name, args, 0, ws);
|
|
|
|
string_right(args, ws);
|
|
|
|
string_strim(args);
|
|
|
|
}
|
|
|
|
// Check key name and set event key
|
|
|
|
if(!string_cmp(key_name, "up")) {
|
|
|
|
event.key = InputKeyUp;
|
|
|
|
} else if(!string_cmp(key_name, "down")) {
|
|
|
|
event.key = InputKeyDown;
|
|
|
|
} else if(!string_cmp(key_name, "left")) {
|
|
|
|
event.key = InputKeyLeft;
|
|
|
|
} else if(!string_cmp(key_name, "right")) {
|
|
|
|
event.key = InputKeyRight;
|
|
|
|
} else if(!string_cmp(key_name, "ok")) {
|
|
|
|
event.key = InputKeyOk;
|
|
|
|
} else if(!string_cmp(key_name, "back")) {
|
|
|
|
event.key = InputKeyBack;
|
|
|
|
} else {
|
2021-04-18 19:32:57 +00:00
|
|
|
printf("Invalid key name. Valid keys: `up`, `down`, `left`, `right`, `back`, `ok`.");
|
2021-03-04 12:30:20 +00:00
|
|
|
string_clear(key_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
string_clear(key_name);
|
|
|
|
// Check the rest of args string and set event type
|
|
|
|
if(!string_cmp(args, "press")) {
|
|
|
|
event.type = InputTypePress;
|
|
|
|
} else if(!string_cmp(args, "release")) {
|
|
|
|
event.type = InputTypeRelease;
|
|
|
|
} else if(!string_cmp(args, "short")) {
|
|
|
|
event.type = InputTypeShort;
|
|
|
|
} else if(!string_cmp(args, "long")) {
|
|
|
|
event.type = InputTypeLong;
|
|
|
|
} else {
|
2021-04-18 19:32:57 +00:00
|
|
|
printf("Ivalid type. Valid types: `press`, `release`, `short`, `long`.");
|
2021-03-04 12:30:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Publish input event
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2021-03-04 12:30:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 16:53:29 +00:00
|
|
|
static void input_cli_dump_events_callback(const void* value, void* ctx) {
|
|
|
|
furi_assert(value);
|
|
|
|
furi_assert(ctx);
|
|
|
|
osMessageQueueId_t input_queue = ctx;
|
|
|
|
osMessageQueuePut(input_queue, value, 0, osWaitForever);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void input_cli_dump(Cli* cli, string_t args, void* context) {
|
|
|
|
osMessageQueueId_t input_queue = osMessageQueueNew(8, sizeof(InputEvent), NULL);
|
|
|
|
FuriPubSubSubscription* input_subscription =
|
|
|
|
furi_pubsub_subscribe(input->event_pubsub, input_cli_dump_events_callback, input_queue);
|
|
|
|
|
|
|
|
bool stop = false;
|
|
|
|
InputEvent input_event;
|
|
|
|
while(!stop) {
|
|
|
|
if(osMessageQueueGet(input_queue, &input_event, NULL, 100) == osOK) {
|
|
|
|
printf(
|
|
|
|
"key: %s type: %s\r\n",
|
|
|
|
input_get_key_name(input_event.key),
|
|
|
|
input_get_type_name(input_event.type));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cli_cmd_interrupt_received(cli)) {
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_pubsub_unsubscribe(input->event_pubsub, input_subscription);
|
|
|
|
osMessageQueueDelete(input_queue);
|
|
|
|
}
|
|
|
|
|
2021-08-31 08:22:52 +00: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";
|
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2021-08-07 16:54:42 +00:00
|
|
|
int32_t input_srv() {
|
2021-02-10 08:56:05 +00:00
|
|
|
input = furi_alloc(sizeof(Input));
|
|
|
|
input->thread = osThreadGetId();
|
2021-11-01 20:35:54 +00:00
|
|
|
input->event_pubsub = furi_pubsub_alloc();
|
|
|
|
furi_record_create("input_events", input->event_pubsub);
|
2020-10-02 06:44:05 +00:00
|
|
|
|
2021-03-04 12:30:20 +00:00
|
|
|
input->cli = furi_record_open("cli");
|
|
|
|
if(input->cli) {
|
2021-07-19 00:16:15 +00:00
|
|
|
cli_add_command(
|
2021-12-09 16:53:29 +00:00
|
|
|
input->cli, "input_send", CliCommandFlagParallelSafe, input_cli_send, NULL);
|
|
|
|
cli_add_command(
|
|
|
|
input->cli, "input_dump", CliCommandFlagParallelSafe, input_cli_dump, NULL);
|
2021-03-04 12:30:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 08:22:52 +00:00
|
|
|
input->pin_states = furi_alloc(input_pins_count * sizeof(InputPinState));
|
2021-01-28 12:30:31 +00:00
|
|
|
|
2021-08-31 08:22:52 +00:00
|
|
|
for(size_t i = 0; i < input_pins_count; i++) {
|
2021-04-29 08:51:48 +00:00
|
|
|
GpioPin gpio = {(GPIO_TypeDef*)input_pins[i].port, (uint16_t)input_pins[i].pin};
|
|
|
|
hal_gpio_add_int_callback(&gpio, input_isr, NULL);
|
2021-02-10 08:56:05 +00: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;
|
|
|
|
input->pin_states[i].press_timer =
|
2021-04-18 19:32:57 +00:00
|
|
|
osTimerNew(input_press_timer_callback, osTimerPeriodic, &input->pin_states[i], NULL);
|
|
|
|
input->pin_states[i].press_counter = 0;
|
2020-10-02 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
while(1) {
|
|
|
|
bool is_changing = false;
|
2021-08-31 08:22:52 +00:00
|
|
|
for(size_t i = 0; i < input_pins_count; i++) {
|
2021-02-10 08:56:05 +00:00
|
|
|
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;
|
2021-04-18 19:32:57 +00:00
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
// Common state info
|
|
|
|
InputEvent event;
|
|
|
|
event.key = input->pin_states[i].pin->key;
|
2021-04-18 19:32:57 +00:00
|
|
|
|
|
|
|
// Short / Long / Repeat timer routine
|
2021-02-10 08:56:05 +00:00
|
|
|
if(state) {
|
2021-09-01 21:05:00 +00:00
|
|
|
input->counter++;
|
|
|
|
input->pin_states[i].counter = input->counter;
|
|
|
|
event.sequence = input->pin_states[i].counter;
|
2021-04-18 19:32:57 +00:00
|
|
|
input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
|
|
|
|
} else {
|
2021-09-01 21:05:00 +00:00
|
|
|
event.sequence = input->pin_states[i].counter;
|
2021-04-18 19:32:57 +00: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 20:35:54 +00:00
|
|
|
furi_pubsub_publish(input->event_pubsub, &event);
|
2021-04-18 19:32:57 +00:00
|
|
|
}
|
|
|
|
input->pin_states[i].press_counter = 0;
|
2020-10-02 06:44:05 +00:00
|
|
|
}
|
2021-08-30 20:05:09 +00:00
|
|
|
|
|
|
|
// Send Press/Release event
|
|
|
|
event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(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
|
|
|
}
|