[FL-2627] Flipper applications: SDK, build and debug system (#1387)
* Added support for running applications from SD card (FAPs - Flipper Application Packages) * Added plugin_dist target for fbt to build FAPs * All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default * Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them * Added debugging support for FAPs with fbt debug & VSCode * Added public firmware API with automated versioning Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
10
applications/services/input/application.fam
Normal file
10
applications/services/input/application.fam
Normal file
@@ -0,0 +1,10 @@
|
||||
App(
|
||||
appid="input",
|
||||
name="InputSrv",
|
||||
apptype=FlipperAppType.SERVICE,
|
||||
entry_point="input_srv",
|
||||
cdefines=["SRV_INPUT"],
|
||||
stack_size=1 * 1024,
|
||||
order=80,
|
||||
sdk_headers=["input.h"],
|
||||
)
|
138
applications/services/input/input.c
Normal file
138
applications/services/input/input.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include "input_i.h"
|
||||
|
||||
#define GPIO_Read(input_pin) (furi_hal_gpio_read(input_pin.pin->gpio) ^ (input_pin.pin->inverted))
|
||||
|
||||
static Input* input = NULL;
|
||||
|
||||
inline static void input_timer_start(FuriTimer* 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(FuriTimer* 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) furi_delay_tick(1);
|
||||
}
|
||||
|
||||
void input_press_timer_callback(void* arg) {
|
||||
InputPinState* input_pin = arg;
|
||||
InputEvent event;
|
||||
event.sequence = input_pin->counter;
|
||||
event.key = input_pin->pin->key;
|
||||
input_pin->press_counter++;
|
||||
if(input_pin->press_counter == INPUT_LONG_PRESS_COUNTS) {
|
||||
event.type = InputTypeLong;
|
||||
furi_pubsub_publish(input->event_pubsub, &event);
|
||||
} else if(input_pin->press_counter > INPUT_LONG_PRESS_COUNTS) {
|
||||
input_pin->press_counter--;
|
||||
event.type = InputTypeRepeat;
|
||||
furi_pubsub_publish(input->event_pubsub, &event);
|
||||
}
|
||||
}
|
||||
|
||||
void input_isr(void* _ctx) {
|
||||
UNUSED(_ctx);
|
||||
furi_thread_flags_set(input->thread_id, INPUT_THREAD_FLAG_ISR);
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
int32_t input_srv(void* p) {
|
||||
UNUSED(p);
|
||||
input = malloc(sizeof(Input));
|
||||
input->thread_id = furi_thread_get_current_id();
|
||||
input->event_pubsub = furi_pubsub_alloc();
|
||||
furi_record_create(RECORD_INPUT_EVENTS, input->event_pubsub);
|
||||
|
||||
#ifdef SRV_CLI
|
||||
input->cli = furi_record_open(RECORD_CLI);
|
||||
if(input->cli) {
|
||||
cli_add_command(input->cli, "input", CliCommandFlagParallelSafe, input_cli, input);
|
||||
}
|
||||
#endif
|
||||
|
||||
input->pin_states = malloc(input_pins_count * sizeof(InputPinState));
|
||||
|
||||
for(size_t i = 0; i < input_pins_count; i++) {
|
||||
furi_hal_gpio_add_int_callback(input_pins[i].gpio, input_isr, NULL);
|
||||
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 = furi_timer_alloc(
|
||||
input_press_timer_callback, FuriTimerTypePeriodic, &input->pin_states[i]);
|
||||
input->pin_states[i].press_counter = 0;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
bool is_changing = false;
|
||||
for(size_t i = 0; i < input_pins_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.key = input->pin_states[i].pin->key;
|
||||
|
||||
// Short / Long / Repeat timer routine
|
||||
if(state) {
|
||||
input->counter++;
|
||||
input->pin_states[i].counter = input->counter;
|
||||
event.sequence = input->pin_states[i].counter;
|
||||
input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
|
||||
} else {
|
||||
event.sequence = input->pin_states[i].counter;
|
||||
input_timer_stop(input->pin_states[i].press_timer);
|
||||
if(input->pin_states[i].press_counter < INPUT_LONG_PRESS_COUNTS) {
|
||||
event.type = InputTypeShort;
|
||||
furi_pubsub_publish(input->event_pubsub, &event);
|
||||
}
|
||||
input->pin_states[i].press_counter = 0;
|
||||
}
|
||||
|
||||
// Send Press/Release event
|
||||
event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
|
||||
furi_pubsub_publish(input->event_pubsub, &event);
|
||||
}
|
||||
}
|
||||
|
||||
if(is_changing) {
|
||||
furi_delay_tick(1);
|
||||
} else {
|
||||
furi_thread_flags_wait(INPUT_THREAD_FLAG_ISR, FuriFlagWaitAny, FuriWaitForever);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
48
applications/services/input/input.h
Normal file
48
applications/services/input/input.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @file input.h
|
||||
* Input: main API
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal_resources.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RECORD_INPUT_EVENTS "input_events"
|
||||
|
||||
/** Input Types
|
||||
* Some of them are physical events and some logical
|
||||
*/
|
||||
typedef enum {
|
||||
InputTypePress, /**< Press event, emitted after debounce */
|
||||
InputTypeRelease, /**< Release event, emitted after debounce */
|
||||
InputTypeShort, /**< Short event, emitted after InputTypeRelease done withing INPUT_LONG_PRESS interval */
|
||||
InputTypeLong, /**< Long event, emmited after INPUT_LONG_PRESS interval, asynchronouse to InputTypeRelease */
|
||||
InputTypeRepeat, /**< Repeat event, emmited with INPUT_REPEATE_PRESS period after InputTypeLong event */
|
||||
} InputType;
|
||||
|
||||
/** Input Event, dispatches with FuriPubSub */
|
||||
typedef struct {
|
||||
uint32_t sequence;
|
||||
InputKey key;
|
||||
InputType type;
|
||||
} InputEvent;
|
||||
|
||||
/** Get human readable input key name
|
||||
* @param key - InputKey
|
||||
* @return string
|
||||
*/
|
||||
const char* input_get_key_name(InputKey key);
|
||||
|
||||
/** Get human readable input type name
|
||||
* @param type - InputType
|
||||
* @return string
|
||||
*/
|
||||
const char* input_get_type_name(InputType type);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
125
applications/services/input/input_cli.c
Normal file
125
applications/services/input/input_cli.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "input_i.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <cli/cli.h>
|
||||
#include <toolbox/args.h>
|
||||
|
||||
static void input_cli_usage() {
|
||||
printf("Usage:\r\n");
|
||||
printf("input <cmd> <args>\r\n");
|
||||
printf("Cmd list:\r\n");
|
||||
printf("\tdump\t\t\t - dump input events\r\n");
|
||||
printf("\tsend <key> <type>\t - send input event\r\n");
|
||||
}
|
||||
|
||||
static void input_cli_dump_events_callback(const void* value, void* ctx) {
|
||||
furi_assert(value);
|
||||
furi_assert(ctx);
|
||||
FuriMessageQueue* input_queue = ctx;
|
||||
furi_message_queue_put(input_queue, value, FuriWaitForever);
|
||||
}
|
||||
|
||||
static void input_cli_dump(Cli* cli, string_t args, Input* input) {
|
||||
UNUSED(args);
|
||||
FuriMessageQueue* input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
|
||||
FuriPubSubSubscription* input_subscription =
|
||||
furi_pubsub_subscribe(input->event_pubsub, input_cli_dump_events_callback, input_queue);
|
||||
|
||||
InputEvent input_event;
|
||||
printf("Press CTRL+C to stop\r\n");
|
||||
while(!cli_cmd_interrupt_received(cli)) {
|
||||
if(furi_message_queue_get(input_queue, &input_event, 100) == FuriStatusOk) {
|
||||
printf(
|
||||
"key: %s type: %s\r\n",
|
||||
input_get_key_name(input_event.key),
|
||||
input_get_type_name(input_event.type));
|
||||
}
|
||||
}
|
||||
|
||||
furi_pubsub_unsubscribe(input->event_pubsub, input_subscription);
|
||||
furi_message_queue_free(input_queue);
|
||||
}
|
||||
|
||||
static void input_cli_send_print_usage() {
|
||||
printf("Invalid arguments. Usage:\r\n");
|
||||
printf("\tinput send <key> <type>\r\n");
|
||||
printf("\t\t <key>\t - one of 'up', 'down', 'left', 'right', 'back', 'ok'\r\n");
|
||||
printf("\t\t <type>\t - one of 'press', 'release', 'short', 'long'\r\n");
|
||||
}
|
||||
|
||||
static void input_cli_send(Cli* cli, string_t args, Input* input) {
|
||||
UNUSED(cli);
|
||||
InputEvent event;
|
||||
string_t key_str;
|
||||
string_init(key_str);
|
||||
bool parsed = false;
|
||||
|
||||
do {
|
||||
// Parse Key
|
||||
if(!args_read_string_and_trim(args, key_str)) {
|
||||
break;
|
||||
}
|
||||
if(!string_cmp(key_str, "up")) {
|
||||
event.key = InputKeyUp;
|
||||
} else if(!string_cmp(key_str, "down")) {
|
||||
event.key = InputKeyDown;
|
||||
} else if(!string_cmp(key_str, "left")) {
|
||||
event.key = InputKeyLeft;
|
||||
} else if(!string_cmp(key_str, "right")) {
|
||||
event.key = InputKeyRight;
|
||||
} else if(!string_cmp(key_str, "ok")) {
|
||||
event.key = InputKeyOk;
|
||||
} else if(!string_cmp(key_str, "back")) {
|
||||
event.key = InputKeyBack;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
// Parse 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 {
|
||||
break;
|
||||
}
|
||||
parsed = true;
|
||||
} while(false);
|
||||
|
||||
if(parsed) {
|
||||
furi_pubsub_publish(input->event_pubsub, &event);
|
||||
} else {
|
||||
input_cli_send_print_usage();
|
||||
}
|
||||
string_clear(key_str);
|
||||
}
|
||||
|
||||
void input_cli(Cli* cli, string_t args, void* context) {
|
||||
furi_assert(cli);
|
||||
furi_assert(context);
|
||||
Input* input = context;
|
||||
string_t cmd;
|
||||
string_init(cmd);
|
||||
|
||||
do {
|
||||
if(!args_read_string_and_trim(args, cmd)) {
|
||||
input_cli_usage();
|
||||
break;
|
||||
}
|
||||
if(string_cmp_str(cmd, "dump") == 0) {
|
||||
input_cli_dump(cli, args, input);
|
||||
break;
|
||||
}
|
||||
if(string_cmp_str(cmd, "send") == 0) {
|
||||
input_cli_send(cli, args, input);
|
||||
break;
|
||||
}
|
||||
|
||||
input_cli_usage();
|
||||
} while(false);
|
||||
|
||||
string_clear(cmd);
|
||||
}
|
49
applications/services/input/input_i.h
Normal file
49
applications/services/input/input_i.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @file input_i.h
|
||||
* Input: internal API
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "input.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <furi.h>
|
||||
#include <cli/cli.h>
|
||||
#include <m-string.h>
|
||||
#include <furi_hal_gpio.h>
|
||||
|
||||
#define INPUT_DEBOUNCE_TICKS_HALF (INPUT_DEBOUNCE_TICKS / 2)
|
||||
#define INPUT_PRESS_TICKS 150
|
||||
#define INPUT_LONG_PRESS_COUNTS 2
|
||||
#define INPUT_THREAD_FLAG_ISR 0x00000001
|
||||
|
||||
/** Input pin state */
|
||||
typedef struct {
|
||||
const InputPin* pin;
|
||||
// State
|
||||
volatile bool state;
|
||||
volatile uint8_t debounce;
|
||||
FuriTimer* press_timer;
|
||||
volatile uint8_t press_counter;
|
||||
volatile uint32_t counter;
|
||||
} InputPinState;
|
||||
|
||||
/** Input state */
|
||||
typedef struct {
|
||||
FuriThreadId thread_id;
|
||||
FuriPubSub* event_pubsub;
|
||||
InputPinState* pin_states;
|
||||
Cli* cli;
|
||||
volatile uint32_t counter;
|
||||
} Input;
|
||||
|
||||
/** Input press timer callback */
|
||||
void input_press_timer_callback(void* arg);
|
||||
|
||||
/** Input interrupt handler */
|
||||
void input_isr(void* _ctx);
|
||||
|
||||
/** Input CLI command handler */
|
||||
void input_cli(Cli* cli, string_t args, void* context);
|
Reference in New Issue
Block a user