finik_eth_app.c 5.7 KB

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