finik_eth_app.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "finik_eth_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. #include "eth_worker.h"
  8. #define TAG "FinikEthApp"
  9. static void draw_process_selector(Canvas* canvas, DrawProcess selector, CursorPosition cursor) {
  10. uint8_t y = 0;
  11. if(selector == PROCESS_INIT) y = 11;
  12. if(selector == PROCESS_DHCP) y = 22;
  13. if(selector == PROCESS_STATIC) y = 34;
  14. if(selector == PROCESS_PING) y = 44;
  15. if(selector == PROCESS_RESET) y = 55;
  16. if(cursor != CURSOR_INSIDE_PROCESS) {
  17. canvas_draw_line(canvas, 0, y + 1, 0, y + 7);
  18. canvas_draw_line(canvas, 23, y + 1, 23, y + 7);
  19. }
  20. canvas_draw_line(canvas, 1, y, 22, y);
  21. canvas_draw_line(canvas, 1, y + 8, 22, y + 8);
  22. if(cursor == CURSOR_CLICK_PROCESS) {
  23. canvas_draw_box(canvas, 1, y, 22, 9);
  24. }
  25. }
  26. static void draw_battery_cunsumption(Canvas* canvas, double cons) {
  27. FuriString* string = furi_string_alloc_set("aaaaaaaa");
  28. if(cons >= 0) {
  29. furi_string_printf(string, "--");
  30. } else if(cons < -1) {
  31. furi_string_printf(string, "%1.1fk", -cons);
  32. } else {
  33. furi_string_printf(string, "%3.f", -(cons * 1000));
  34. }
  35. canvas_draw_str(canvas, 112, 7, furi_string_get_cstr(string));
  36. furi_string_free(string);
  37. }
  38. static void finik_eth_app_draw_callback(Canvas* canvas, void* ctx) {
  39. furi_assert(ctx);
  40. FinikEthApp* app = ctx;
  41. canvas_clear(canvas);
  42. DrawMode mode = app->draw_mode;
  43. DrawProcess process = app->draw_process;
  44. CursorPosition cursor = app->cursor_position;
  45. float consumption = app->info.current_gauge;
  46. canvas_set_font(canvas, FontSecondary);
  47. if(cursor == CURSOR_EXIT_APP) {
  48. canvas_draw_icon(canvas, 0, 0, &I_exit_128x64px);
  49. } else {
  50. canvas_draw_icon(canvas, 0, 0, &I_main_128x64px);
  51. draw_process_selector(canvas, process, cursor);
  52. draw_battery_cunsumption(canvas, (double)consumption);
  53. }
  54. }
  55. static void finik_eth_battery_info_update_model(void* ctx) {
  56. furi_assert(ctx);
  57. FinikEthApp* app = ctx;
  58. power_get_info(app->power, &app->info);
  59. }
  60. static void finik_eth_app_input_callback(InputEvent* input_event, void* ctx) {
  61. furi_assert(ctx);
  62. FuriMessageQueue* event_queue = ctx;
  63. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  64. }
  65. FinikEthApp* finik_eth_app_alloc() {
  66. FinikEthApp* app = malloc(sizeof(FinikEthApp));
  67. app->view_port = view_port_alloc();
  68. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  69. view_port_draw_callback_set(app->view_port, finik_eth_app_draw_callback, app);
  70. view_port_input_callback_set(app->view_port, finik_eth_app_input_callback, app->event_queue);
  71. app->gui = furi_record_open(RECORD_GUI);
  72. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  73. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  74. app->power = furi_record_open(RECORD_POWER);
  75. //app->eth_worker = eth_worker_alloc();
  76. //eth_worker_task(app->eth_worker);
  77. return app;
  78. }
  79. void finik_eth_app_free(FinikEthApp* app) {
  80. furi_assert(app);
  81. view_port_enabled_set(app->view_port, false);
  82. gui_remove_view_port(app->gui, app->view_port);
  83. view_port_free(app->view_port);
  84. furi_message_queue_free(app->event_queue);
  85. furi_record_close(RECORD_GUI);
  86. furi_record_close(RECORD_NOTIFICATION);
  87. }
  88. void finik_eth_update_consumtion(FinikEthApp* app) {
  89. furi_assert(app);
  90. }
  91. int32_t finik_eth_app(void* p) {
  92. UNUSED(p);
  93. FinikEthApp* app = finik_eth_app_alloc();
  94. InputEvent event;
  95. while(1) {
  96. finik_eth_battery_info_update_model(app);
  97. if(furi_message_queue_get(app->event_queue, &event, 300) == FuriStatusOk) {
  98. if(event.type == InputTypePress) {
  99. if(app->cursor_position == CURSOR_CHOOSE_PROCESS) {
  100. if(event.key == InputKeyUp) {
  101. app->draw_process =
  102. (app->draw_process + PROCESS_RESET) % (PROCESS_RESET + 1);
  103. } else if(event.key == InputKeyDown) {
  104. app->draw_process =
  105. (app->draw_process + PROCESS_RESET + 2) % (PROCESS_RESET + 1);
  106. } else if(event.key == InputKeyRight) {
  107. app->cursor_position = CURSOR_INSIDE_PROCESS;
  108. } else if(event.key == InputKeyOk) {
  109. app->cursor_position = CURSOR_CLICK_PROCESS;
  110. view_port_update(app->view_port);
  111. furi_delay_ms(150);
  112. app->cursor_position = CURSOR_INSIDE_PROCESS;
  113. } else if(event.key == InputKeyBack) {
  114. app->cursor_position = CURSOR_EXIT_APP;
  115. }
  116. } else if(app->cursor_position == CURSOR_INSIDE_PROCESS) {
  117. if(event.key == InputKeyLeft || event.key == InputKeyBack) {
  118. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  119. }
  120. } else if(app->cursor_position == CURSOR_EXIT_APP) {
  121. if(event.key == InputKeyBack) {
  122. break;
  123. } else if(event.key == InputKeyOk) {
  124. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  125. }
  126. }
  127. view_port_update(app->view_port);
  128. }
  129. }
  130. }
  131. finik_eth_app_free(app);
  132. return 0;
  133. }