example_5_app.c 3.2 KB

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