finik_eth_app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #include "eth_worker_i.h"
  9. #include "eth_view_process.h"
  10. #define TAG "FinikEthApp"
  11. static void draw_process_selector(Canvas* canvas, DrawProcess selector, CursorPosition cursor) {
  12. uint8_t y = 0;
  13. if(selector == PROCESS_INIT) y = 11;
  14. if(selector == PROCESS_DHCP) y = 22;
  15. if(selector == PROCESS_STATIC) y = 34;
  16. if(selector == PROCESS_PING) y = 44;
  17. if(selector == PROCESS_RESET) y = 55;
  18. if(cursor == CURSOR_INSIDE_PROCESS) {
  19. canvas_invert_color(canvas);
  20. canvas_draw_line(canvas, 25, y + 1, 25, y + 7);
  21. canvas_invert_color(canvas);
  22. canvas_draw_line(canvas, 0, y + 1, 0, y + 7);
  23. canvas_draw_line(canvas, 1, y, 25, y);
  24. canvas_draw_line(canvas, 1, y + 8, 25, y + 8);
  25. } else {
  26. canvas_draw_line(canvas, 0, y + 1, 0, y + 7);
  27. canvas_draw_line(canvas, 23, y + 1, 23, y + 7);
  28. canvas_draw_line(canvas, 1, y, 22, y);
  29. canvas_draw_line(canvas, 1, y + 8, 22, y + 8);
  30. }
  31. if(cursor == CURSOR_CLICK_PROCESS) {
  32. canvas_draw_box(canvas, 1, y, 22, 9);
  33. }
  34. }
  35. static void draw_battery_cunsumption(Canvas* canvas, double cons) {
  36. FuriString* string = furi_string_alloc_set("aaaaaaaa");
  37. if(cons >= 0) {
  38. furi_string_printf(string, "--");
  39. } else if(cons < -1) {
  40. furi_string_printf(string, "%1.1fk", -cons);
  41. } else {
  42. furi_string_printf(string, "%3.f", -(cons * 1000));
  43. }
  44. canvas_draw_str(canvas, 112, 7, furi_string_get_cstr(string));
  45. furi_string_free(string);
  46. }
  47. static void finik_eth_app_draw_callback(Canvas* canvas, void* ctx) {
  48. furi_assert(ctx);
  49. FinikEthApp* app = ctx;
  50. canvas_clear(canvas);
  51. DrawMode mode = app->draw_mode;
  52. DrawProcess process = app->draw_process;
  53. CursorPosition cursor = app->cursor_position;
  54. float consumption = app->info.current_gauge;
  55. canvas_set_font(canvas, FontSecondary);
  56. if(cursor == CURSOR_EXIT_ICON) {
  57. canvas_draw_icon(canvas, 0, 0, &I_exit_128x64px);
  58. } else {
  59. canvas_draw_icon(canvas, 0, 0, &I_main_128x64px);
  60. draw_process_selector(canvas, process, cursor);
  61. draw_battery_cunsumption(canvas, (double)consumption);
  62. ethernet_view_process_draw(app->eth_worker->active_process, canvas);
  63. }
  64. }
  65. static void finik_eth_battery_info_update_model(void* ctx) {
  66. furi_assert(ctx);
  67. FinikEthApp* app = ctx;
  68. power_get_info(app->power, &app->info);
  69. }
  70. static void finik_eth_app_input_callback(InputEvent* input_event, void* ctx) {
  71. furi_assert(ctx);
  72. FuriMessageQueue* event_queue = ctx;
  73. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  74. }
  75. FinikEthApp* finik_eth_app_alloc() {
  76. FinikEthApp* app = malloc(sizeof(FinikEthApp));
  77. app->view_port = view_port_alloc();
  78. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  79. app->eth_worker = eth_worker_alloc();
  80. view_port_draw_callback_set(app->view_port, finik_eth_app_draw_callback, app);
  81. view_port_input_callback_set(app->view_port, finik_eth_app_input_callback, app->event_queue);
  82. app->gui = furi_record_open(RECORD_GUI);
  83. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  84. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  85. app->power = furi_record_open(RECORD_POWER);
  86. //eth_worker_task(app->eth_worker);
  87. return app;
  88. }
  89. void finik_eth_app_free(FinikEthApp* app) {
  90. furi_assert(app);
  91. view_port_enabled_set(app->view_port, false);
  92. gui_remove_view_port(app->gui, app->view_port);
  93. view_port_free(app->view_port);
  94. furi_message_queue_free(app->event_queue);
  95. eth_worker_free(app->eth_worker);
  96. furi_record_close(RECORD_GUI);
  97. furi_record_close(RECORD_NOTIFICATION);
  98. furi_record_close(RECORD_POWER);
  99. }
  100. void finit_eth_app_key_handler(FinikEthApp* app, InputKey key) {
  101. if(app->cursor_position == CURSOR_CHOOSE_PROCESS) {
  102. if(key == InputKeyUp || key == InputKeyDown) {
  103. app->draw_process =
  104. (app->draw_process + PROCESS_RESET + (key == InputKeyDown ? 2 : 0)) %
  105. (PROCESS_RESET + 1);
  106. eth_worker_set_active_process(app->eth_worker, (EthWorkerProcess)app->draw_process);
  107. } else if(key == InputKeyOk) {
  108. ethernet_view_process_move(app->eth_worker->active_process, 0);
  109. app->cursor_position = CURSOR_CLICK_PROCESS;
  110. view_port_update(app->view_port);
  111. furi_delay_ms(150);
  112. char str[] = "test string 0 with some parameters";
  113. eth_worker_log(app->eth_worker, str);
  114. evp_printf(app->eth_worker->init_process, "test promt %d %s", 112, "ivan");
  115. ethernet_view_process_print(app->eth_worker->stat_process, str);
  116. ethernet_view_process_print(
  117. app->eth_worker->dhcp_process, "test dhcp process string. loooooong world");
  118. ethernet_view_process_print(app->eth_worker->ping_process, "ping");
  119. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  120. } else if(key == InputKeyRight) {
  121. eth_worker_set_active_process(app->eth_worker, (EthWorkerProcess)app->draw_process);
  122. app->cursor_position = CURSOR_INSIDE_PROCESS;
  123. } else if(key == InputKeyBack) {
  124. app->cursor_position = CURSOR_EXIT_ICON;
  125. }
  126. } else if(app->cursor_position == CURSOR_INSIDE_PROCESS) {
  127. if(app->eth_worker->active_process->editing) {
  128. ethernet_view_process_keyevent(app->eth_worker->active_process, key);
  129. } else if(key == InputKeyLeft) {
  130. app->eth_worker->active_process->editing = 0;
  131. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  132. } else if(key == InputKeyBack) {
  133. ethernet_view_process_move(app->eth_worker->active_process, 0);
  134. app->eth_worker->active_process->editing = 0;
  135. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  136. } else if(key == InputKeyUp) {
  137. ethernet_view_process_move(app->eth_worker->active_process, -1);
  138. } else if(key == InputKeyDown) {
  139. ethernet_view_process_move(app->eth_worker->active_process, 1);
  140. } else if(key == InputKeyOk || key == InputKeyRight) {
  141. app->eth_worker->active_process->editing = 1;
  142. }
  143. } else if(app->cursor_position == CURSOR_EXIT_ICON) {
  144. if(key == InputKeyBack) {
  145. app->cursor_position = CURSOR_EXIT;
  146. } else if(key == InputKeyOk) {
  147. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  148. }
  149. }
  150. }
  151. int32_t finik_eth_app(void* p) {
  152. UNUSED(p);
  153. FinikEthApp* app = finik_eth_app_alloc();
  154. InputEvent event;
  155. uint8_t long_press = 0;
  156. InputKey long_press_key = 0;
  157. while(1) {
  158. finik_eth_battery_info_update_model(app);
  159. if(furi_message_queue_get(app->event_queue, &event, 200) == FuriStatusOk) {
  160. if(event.type == InputTypePress) {
  161. finit_eth_app_key_handler(app, event.key);
  162. } else if(event.type == InputTypeLong) {
  163. long_press = 1;
  164. long_press_key = event.key;
  165. } else if(event.type == InputTypeRelease) {
  166. long_press = 0;
  167. long_press_key = 0;
  168. }
  169. }
  170. if(long_press && long_press_key != InputKeyBack) {
  171. finit_eth_app_key_handler(app, long_press_key);
  172. }
  173. if(app->cursor_position == CURSOR_EXIT) {
  174. break;
  175. }
  176. view_port_update(app->view_port);
  177. }
  178. finik_eth_app_free(app);
  179. return 0;
  180. }