finik_eth_app.c 7.7 KB

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