2021-08-07 16:54:42 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "KeypadTest"
|
|
|
|
|
2021-08-07 16:54:42 +00:00
|
|
|
typedef struct {
|
|
|
|
bool press[5];
|
|
|
|
uint16_t up;
|
|
|
|
uint16_t down;
|
|
|
|
uint16_t left;
|
|
|
|
uint16_t right;
|
|
|
|
uint16_t ok;
|
2023-03-07 13:09:45 +00:00
|
|
|
FuriMutex* mutex;
|
2021-08-07 16:54:42 +00:00
|
|
|
} KeypadTestState;
|
|
|
|
|
|
|
|
static void keypad_test_reset_state(KeypadTestState* state) {
|
|
|
|
state->left = 0;
|
|
|
|
state->right = 0;
|
|
|
|
state->up = 0;
|
|
|
|
state->down = 0;
|
|
|
|
state->ok = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void keypad_test_render_callback(Canvas* canvas, void* ctx) {
|
2023-03-07 13:09:45 +00:00
|
|
|
KeypadTestState* state = ctx;
|
|
|
|
furi_mutex_acquire(state->mutex, FuriWaitForever);
|
2021-08-07 16:54:42 +00:00
|
|
|
canvas_clear(canvas);
|
|
|
|
char strings[5][20];
|
|
|
|
|
2022-08-03 16:00:17 +00:00
|
|
|
snprintf(strings[0], 20, "Ok: %d", state->ok);
|
|
|
|
snprintf(strings[1], 20, "L: %d", state->left);
|
|
|
|
snprintf(strings[2], 20, "R: %d", state->right);
|
|
|
|
snprintf(strings[3], 20, "U: %d", state->up);
|
|
|
|
snprintf(strings[4], 20, "D: %d", state->down);
|
2021-08-07 16:54:42 +00:00
|
|
|
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
canvas_draw_str(canvas, 0, 10, "Keypad test");
|
|
|
|
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
canvas_draw_str(canvas, 0, 24, strings[1]);
|
|
|
|
canvas_draw_str(canvas, 35, 24, strings[2]);
|
|
|
|
canvas_draw_str(canvas, 0, 36, strings[3]);
|
|
|
|
canvas_draw_str(canvas, 35, 36, strings[4]);
|
|
|
|
canvas_draw_str(canvas, 0, 48, strings[0]);
|
|
|
|
canvas_draw_circle(canvas, 100, 26, 25);
|
|
|
|
|
|
|
|
if(state->press[0]) canvas_draw_disc(canvas, 118, 26, 5);
|
|
|
|
if(state->press[1]) canvas_draw_disc(canvas, 82, 26, 5);
|
|
|
|
if(state->press[2]) canvas_draw_disc(canvas, 100, 8, 5);
|
|
|
|
if(state->press[3]) canvas_draw_disc(canvas, 100, 44, 5);
|
|
|
|
if(state->press[4]) canvas_draw_disc(canvas, 100, 26, 5);
|
|
|
|
|
|
|
|
canvas_draw_str(canvas, 10, 63, "[back] - reset, hold to exit");
|
|
|
|
|
2023-03-07 13:09:45 +00:00
|
|
|
furi_mutex_release(state->mutex);
|
2021-08-07 16:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void keypad_test_input_callback(InputEvent* input_event, void* ctx) {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMessageQueue* event_queue = ctx;
|
|
|
|
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
|
2021-08-07 16:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t keypad_test_app(void* p) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(p);
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMessageQueue* event_queue = furi_message_queue_alloc(32, sizeof(InputEvent));
|
2021-08-07 16:54:42 +00:00
|
|
|
furi_check(event_queue);
|
|
|
|
|
2023-03-07 13:09:45 +00:00
|
|
|
KeypadTestState state = {{false, false, false, false, false}, 0, 0, 0, 0, 0, NULL};
|
|
|
|
state.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
2021-08-07 16:54:42 +00:00
|
|
|
|
2023-03-07 13:09:45 +00:00
|
|
|
if(!state.mutex) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "cannot create mutex");
|
2021-08-07 16:54:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewPort* view_port = view_port_alloc();
|
|
|
|
|
2023-03-07 13:09:45 +00:00
|
|
|
view_port_draw_callback_set(view_port, keypad_test_render_callback, &state);
|
2021-08-07 16:54:42 +00:00
|
|
|
view_port_input_callback_set(view_port, keypad_test_input_callback, event_queue);
|
|
|
|
|
|
|
|
// Open GUI and register view_port
|
2022-07-26 12:21:51 +00:00
|
|
|
Gui* gui = furi_record_open(RECORD_GUI);
|
2021-08-07 16:54:42 +00:00
|
|
|
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
|
|
|
|
2021-12-15 12:23:16 +00:00
|
|
|
InputEvent event;
|
2022-07-20 10:56:33 +00:00
|
|
|
while(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
|
2023-03-07 13:09:45 +00:00
|
|
|
furi_mutex_acquire(state.mutex, FuriWaitForever);
|
2021-12-15 12:23:16 +00:00
|
|
|
FURI_LOG_I(
|
|
|
|
TAG,
|
|
|
|
"key: %s type: %s",
|
|
|
|
input_get_key_name(event.key),
|
|
|
|
input_get_type_name(event.type));
|
|
|
|
|
|
|
|
if(event.key == InputKeyRight) {
|
|
|
|
if(event.type == InputTypePress) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[0] = true;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeRelease) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[0] = false;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeShort) {
|
2023-03-07 13:09:45 +00:00
|
|
|
++state.right;
|
2021-12-15 12:23:16 +00:00
|
|
|
}
|
|
|
|
} else if(event.key == InputKeyLeft) {
|
|
|
|
if(event.type == InputTypePress) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[1] = true;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeRelease) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[1] = false;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeShort) {
|
2023-03-07 13:09:45 +00:00
|
|
|
++state.left;
|
2021-12-15 12:23:16 +00:00
|
|
|
}
|
|
|
|
} else if(event.key == InputKeyUp) {
|
|
|
|
if(event.type == InputTypePress) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[2] = true;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeRelease) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[2] = false;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeShort) {
|
2023-03-07 13:09:45 +00:00
|
|
|
++state.up;
|
2021-12-15 12:23:16 +00:00
|
|
|
}
|
|
|
|
} else if(event.key == InputKeyDown) {
|
|
|
|
if(event.type == InputTypePress) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[3] = true;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeRelease) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[3] = false;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeShort) {
|
2023-03-07 13:09:45 +00:00
|
|
|
++state.down;
|
2021-12-15 12:23:16 +00:00
|
|
|
}
|
|
|
|
} else if(event.key == InputKeyOk) {
|
|
|
|
if(event.type == InputTypePress) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[4] = true;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeRelease) {
|
2023-03-07 13:09:45 +00:00
|
|
|
state.press[4] = false;
|
2021-12-15 12:23:16 +00:00
|
|
|
} else if(event.type == InputTypeShort) {
|
2023-03-07 13:09:45 +00:00
|
|
|
++state.ok;
|
2021-12-15 12:23:16 +00:00
|
|
|
}
|
|
|
|
} else if(event.key == InputKeyBack) {
|
|
|
|
if(event.type == InputTypeLong) {
|
2023-03-07 13:09:45 +00:00
|
|
|
furi_mutex_release(state.mutex);
|
2021-12-15 12:23:16 +00:00
|
|
|
break;
|
|
|
|
} else if(event.type == InputTypeShort) {
|
2023-03-07 13:09:45 +00:00
|
|
|
keypad_test_reset_state(&state);
|
2021-08-07 16:54:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 12:23:16 +00:00
|
|
|
|
2023-03-07 13:09:45 +00:00
|
|
|
furi_mutex_release(state.mutex);
|
2021-12-15 12:23:16 +00:00
|
|
|
view_port_update(view_port);
|
2021-08-07 16:54:42 +00:00
|
|
|
}
|
2021-12-15 12:23:16 +00:00
|
|
|
|
2021-08-07 16:54:42 +00:00
|
|
|
// remove & free all stuff created by app
|
|
|
|
gui_remove_view_port(gui, view_port);
|
|
|
|
view_port_free(view_port);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_message_queue_free(event_queue);
|
2023-03-07 13:09:45 +00:00
|
|
|
furi_mutex_free(state.mutex);
|
2021-08-07 16:54:42 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_GUI);
|
2021-12-15 12:23:16 +00:00
|
|
|
|
2021-08-07 16:54:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|