|
| 1 | + |
| 2 | +#include "example_5_app.h" |
| 3 | + |
| 4 | +#include <furi.h> |
| 5 | +#include <gui/gui.h> |
| 6 | +#include <gui/elements.h> |
| 7 | +#include <input/input.h> |
| 8 | +#include <notification/notification_messages.h> |
| 9 | + |
| 10 | +static void example_5_app_draw_callback(Canvas* canvas, void* ctx) { |
| 11 | + furi_assert(ctx); |
| 12 | + Example5App* app = ctx; |
| 13 | + |
| 14 | + canvas_clear(canvas); |
| 15 | + |
| 16 | + DrawMode mode = app->draw_mode; |
| 17 | + if(mode == DRAW_ONLY_PICTURES || mode == DRAW_ALL) |
| 18 | + canvas_draw_icon(canvas, 0, 29, &I_amperka_ru_logo_128x35px); |
| 19 | + if(mode == DRAW_ONLY_TEXT || mode == DRAW_ALL) { |
| 20 | + canvas_set_font(canvas, FontPrimary); |
| 21 | + canvas_draw_str(canvas, 4, 8, "This is an example app!"); |
| 22 | + canvas_set_font(canvas, FontSecondary); |
| 23 | + elements_multiline_text_aligned( |
| 24 | + canvas, |
| 25 | + 127, |
| 26 | + 15, |
| 27 | + AlignRight, |
| 28 | + AlignTop, |
| 29 | + "Some long long long long \n aligned multiline text"); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +static void example_5_app_input_callback(InputEvent* input_event, void* ctx) { |
| 34 | + furi_assert(ctx); |
| 35 | + |
| 36 | + FuriMessageQueue* event_queue = ctx; |
| 37 | + furi_message_queue_put(event_queue, input_event, FuriWaitForever); |
| 38 | +} |
| 39 | + |
| 40 | +Example5App* example_5_app_alloc() { |
| 41 | + Example5App* app = malloc(sizeof(Example5App)); |
| 42 | + |
| 43 | + app->view_port = view_port_alloc(); |
| 44 | + app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent)); |
| 45 | + |
| 46 | + view_port_draw_callback_set(app->view_port, example_5_app_draw_callback, app); |
| 47 | + view_port_input_callback_set(app->view_port, example_5_app_input_callback, app->event_queue); |
| 48 | + |
| 49 | + app->gui = furi_record_open(RECORD_GUI); |
| 50 | + gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen); |
| 51 | + |
| 52 | + app->notifications = furi_record_open(RECORD_NOTIFICATION); |
| 53 | + |
| 54 | + return app; |
| 55 | +} |
| 56 | + |
| 57 | +void example_5_app_free(Example5App* app) { |
| 58 | + furi_assert(app); |
| 59 | + |
| 60 | + view_port_enabled_set(app->view_port, false); |
| 61 | + gui_remove_view_port(app->gui, app->view_port); |
| 62 | + view_port_free(app->view_port); |
| 63 | + |
| 64 | + furi_message_queue_free(app->event_queue); |
| 65 | + |
| 66 | + furi_record_close(RECORD_GUI); |
| 67 | + furi_record_close(RECORD_NOTIFICATION); |
| 68 | +} |
| 69 | + |
| 70 | +int32_t example_5_app(void* p) { |
| 71 | + UNUSED(p); |
| 72 | + Example5App* app = example_5_app_alloc(); |
| 73 | + |
| 74 | + InputEvent event; |
| 75 | + |
| 76 | + while(1) { |
| 77 | + if(furi_message_queue_get(app->event_queue, &event, 100) == FuriStatusOk) { |
| 78 | + if(event.type == InputTypePress) { |
| 79 | + if(event.key == InputKeyBack) |
| 80 | + break; |
| 81 | + else if(event.key == InputKeyUp) |
| 82 | + notification_message(app->notifications, &example_led_sequence); |
| 83 | + else if(event.key == InputKeyDown) |
| 84 | + notification_message(app->notifications, &example_vibro_sequence); |
| 85 | + else if(event.key == InputKeyOk) |
| 86 | + notification_message(app->notifications, &example_sound_sequence); |
| 87 | + |
| 88 | + } else if(event.type == InputTypeLong) { |
| 89 | + DrawMode mode = app->draw_mode; |
| 90 | + if(event.key == InputKeyLeft) |
| 91 | + app->draw_mode = (mode - 1 + TOTAL_DRAW_MODES) % TOTAL_DRAW_MODES; |
| 92 | + else if(event.key == InputKeyRight) |
| 93 | + app->draw_mode = (mode + 1) % TOTAL_DRAW_MODES; |
| 94 | + |
| 95 | + view_port_update(app->view_port); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + example_5_app_free(app); |
| 101 | + return 0; |
| 102 | +} |
0 commit comments