uart_hex_input.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include "uart_hex_input.h"
  2. #include <gui/elements.h>
  3. #include "uart_terminal_icons.h"
  4. #include "uart_terminal_app_i.h"
  5. #include <furi.h>
  6. struct UART_TextInput {
  7. View* view;
  8. FuriTimer* timer;
  9. };
  10. typedef struct {
  11. const char text;
  12. const uint8_t x;
  13. const uint8_t y;
  14. } UART_TextInputKey;
  15. typedef struct {
  16. const char* header;
  17. char* text_buffer;
  18. size_t text_buffer_size;
  19. bool clear_default_text;
  20. UART_TextInputCallback callback;
  21. void* callback_context;
  22. uint8_t selected_row;
  23. uint8_t selected_column;
  24. UART_TextInputValidatorCallback validator_callback;
  25. void* validator_callback_context;
  26. FuriString* validator_text;
  27. bool valadator_message_visible;
  28. } UART_TextInputModel;
  29. static const uint8_t keyboard_origin_x = 5;
  30. static const uint8_t keyboard_origin_y = 28;
  31. static const uint8_t keyboard_row_count = 2;
  32. #define ENTER_KEY '\r'
  33. #define BACKSPACE_KEY '\b'
  34. static const UART_TextInputKey keyboard_keys_row_1[] = {
  35. {'0', 0, 12},
  36. {'1', 11, 12},
  37. {'2', 22, 12},
  38. {'3', 33, 12},
  39. {'4', 44, 12},
  40. {'5', 55, 12},
  41. {'6', 66, 12},
  42. {'7', 77, 12},
  43. {BACKSPACE_KEY, 103, 4},
  44. };
  45. static const UART_TextInputKey keyboard_keys_row_2[] = {
  46. {'8', 0, 26},
  47. {'9', 11, 26},
  48. {'A', 22, 26},
  49. {'B', 33, 26},
  50. {'C', 44, 26},
  51. {'D', 55, 26},
  52. {'E', 66, 26},
  53. {'F', 77, 26},
  54. {ENTER_KEY, 95, 17},
  55. };
  56. static uint8_t get_row_size(uint8_t row_index) {
  57. uint8_t row_size = 0;
  58. switch(row_index + 1) {
  59. case 1:
  60. row_size = sizeof(keyboard_keys_row_1) / sizeof(UART_TextInputKey);
  61. break;
  62. case 2:
  63. row_size = sizeof(keyboard_keys_row_2) / sizeof(UART_TextInputKey);
  64. break;
  65. }
  66. return row_size;
  67. }
  68. static const UART_TextInputKey* get_row(uint8_t row_index) {
  69. const UART_TextInputKey* row = NULL;
  70. switch(row_index + 1) {
  71. case 1:
  72. row = keyboard_keys_row_1;
  73. break;
  74. case 2:
  75. row = keyboard_keys_row_2;
  76. break;
  77. }
  78. return row;
  79. }
  80. static char get_selected_char(UART_TextInputModel* model) {
  81. return get_row(model->selected_row)[model->selected_column].text;
  82. }
  83. static void uart_hex_input_backspace_cb(UART_TextInputModel* model) {
  84. uint8_t text_length = model->clear_default_text ? 1 : strlen(model->text_buffer);
  85. if(text_length > 0) {
  86. model->text_buffer[text_length - 1] = 0;
  87. }
  88. }
  89. static void uart_hex_input_view_draw_callback(Canvas* canvas, void* _model) {
  90. UART_TextInputModel* model = _model;
  91. uint8_t needed_string_width = canvas_width(canvas) - 8;
  92. uint8_t start_pos = 4;
  93. const char* text = model->text_buffer;
  94. canvas_clear(canvas);
  95. canvas_set_color(canvas, ColorBlack);
  96. canvas_draw_str(canvas, 2, 7, model->header);
  97. elements_slightly_rounded_frame(canvas, 1, 8, 126, 12);
  98. if(canvas_string_width(canvas, text) > needed_string_width) {
  99. canvas_draw_str(canvas, start_pos, 17, "...");
  100. start_pos += 6;
  101. needed_string_width -= 8;
  102. }
  103. while(text != 0 && canvas_string_width(canvas, text) > needed_string_width) {
  104. text++;
  105. }
  106. if(model->clear_default_text) {
  107. elements_slightly_rounded_box(
  108. canvas, start_pos - 1, 14, canvas_string_width(canvas, text) + 2, 10);
  109. canvas_set_color(canvas, ColorWhite);
  110. } else {
  111. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 1, 18, "|");
  112. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 2, 18, "|");
  113. }
  114. canvas_draw_str(canvas, start_pos, 17, text);
  115. canvas_set_font(canvas, FontKeyboard);
  116. for(uint8_t row = 0; row <= keyboard_row_count; row++) {
  117. const uint8_t column_count = get_row_size(row);
  118. const UART_TextInputKey* keys = get_row(row);
  119. for(size_t column = 0; column < column_count; column++) {
  120. if(keys[column].text == ENTER_KEY) {
  121. canvas_set_color(canvas, ColorBlack);
  122. if(model->selected_row == row && model->selected_column == column) {
  123. canvas_draw_icon(
  124. canvas,
  125. keyboard_origin_x + keys[column].x,
  126. keyboard_origin_y + keys[column].y,
  127. &I_KeySaveSelected_24x11);
  128. } else {
  129. canvas_draw_icon(
  130. canvas,
  131. keyboard_origin_x + keys[column].x,
  132. keyboard_origin_y + keys[column].y,
  133. &I_KeySave_24x11);
  134. }
  135. } else if(keys[column].text == BACKSPACE_KEY) {
  136. canvas_set_color(canvas, ColorBlack);
  137. if(model->selected_row == row && model->selected_column == column) {
  138. canvas_draw_icon(
  139. canvas,
  140. keyboard_origin_x + keys[column].x,
  141. keyboard_origin_y + keys[column].y,
  142. &I_KeyBackspaceSelected_16x9);
  143. } else {
  144. canvas_draw_icon(
  145. canvas,
  146. keyboard_origin_x + keys[column].x,
  147. keyboard_origin_y + keys[column].y,
  148. &I_KeyBackspace_16x9);
  149. }
  150. } else {
  151. if(model->selected_row == row && model->selected_column == column) {
  152. canvas_set_color(canvas, ColorBlack);
  153. canvas_draw_box(
  154. canvas,
  155. keyboard_origin_x + keys[column].x - 1,
  156. keyboard_origin_y + keys[column].y - 8,
  157. 7,
  158. 10);
  159. canvas_set_color(canvas, ColorWhite);
  160. } else {
  161. canvas_set_color(canvas, ColorBlack);
  162. }
  163. canvas_draw_glyph(
  164. canvas,
  165. keyboard_origin_x + keys[column].x,
  166. keyboard_origin_y + keys[column].y,
  167. keys[column].text);
  168. }
  169. }
  170. }
  171. if(model->valadator_message_visible) {
  172. canvas_set_font(canvas, FontSecondary);
  173. canvas_set_color(canvas, ColorWhite);
  174. canvas_draw_box(canvas, 8, 10, 110, 48);
  175. canvas_set_color(canvas, ColorBlack);
  176. canvas_draw_icon(canvas, 10, 14, &I_WarningDolphin_45x42);
  177. canvas_draw_rframe(canvas, 8, 8, 112, 50, 3);
  178. canvas_draw_rframe(canvas, 9, 9, 110, 48, 2);
  179. elements_multiline_text(canvas, 62, 20, furi_string_get_cstr(model->validator_text));
  180. canvas_set_font(canvas, FontKeyboard);
  181. }
  182. }
  183. static void uart_hex_input_handle_up(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  184. UNUSED(uart_text_input);
  185. if(model->selected_row > 0) {
  186. model->selected_row--;
  187. if(model->selected_column > get_row_size(model->selected_row) - 6) {
  188. model->selected_column = model->selected_column + 1;
  189. }
  190. }
  191. }
  192. static void
  193. uart_hex_input_handle_down(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  194. UNUSED(uart_text_input);
  195. if(model->selected_row < keyboard_row_count - 1) {
  196. model->selected_row++;
  197. if(model->selected_column > get_row_size(model->selected_row) - 4) {
  198. model->selected_column = model->selected_column - 1;
  199. }
  200. }
  201. }
  202. static void
  203. uart_hex_input_handle_left(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  204. UNUSED(uart_text_input);
  205. if(model->selected_column > 0) {
  206. model->selected_column--;
  207. } else {
  208. model->selected_column = get_row_size(model->selected_row) - 1;
  209. }
  210. }
  211. static void
  212. uart_hex_input_handle_right(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  213. UNUSED(uart_text_input);
  214. if(model->selected_column < get_row_size(model->selected_row) - 1) {
  215. model->selected_column++;
  216. } else {
  217. model->selected_column = 0;
  218. }
  219. }
  220. static void uart_hex_input_handle_ok(
  221. UART_TextInput* uart_text_input,
  222. UART_TextInputModel* model,
  223. bool shift) {
  224. UNUSED(shift);
  225. char selected = get_selected_char(model);
  226. uint8_t text_length = strlen(model->text_buffer);
  227. if(selected == ENTER_KEY) {
  228. if(model->validator_callback &&
  229. (!model->validator_callback(
  230. model->text_buffer, model->validator_text, model->validator_callback_context))) {
  231. model->valadator_message_visible = true;
  232. furi_timer_start(uart_text_input->timer, furi_kernel_get_tick_frequency() * 4);
  233. } else if(model->callback != 0 && text_length > 0) {
  234. model->callback(model->callback_context);
  235. }
  236. } else if(selected == BACKSPACE_KEY) {
  237. uart_hex_input_backspace_cb(model);
  238. } else {
  239. if(model->clear_default_text) {
  240. text_length = 0;
  241. }
  242. if(text_length < (model->text_buffer_size - 1)) {
  243. model->text_buffer[text_length] = selected;
  244. model->text_buffer[text_length + 1] = 0;
  245. }
  246. }
  247. model->clear_default_text = false;
  248. }
  249. static bool uart_hex_input_view_input_callback(InputEvent* event, void* context) {
  250. UART_TextInput* uart_text_input = context;
  251. furi_assert(uart_text_input);
  252. bool consumed = false;
  253. // Acquire model
  254. UART_TextInputModel* model = view_get_model(uart_text_input->view);
  255. if((!(event->type == InputTypePress) && !(event->type == InputTypeRelease)) &&
  256. model->valadator_message_visible) {
  257. model->valadator_message_visible = false;
  258. consumed = true;
  259. } else if(event->type == InputTypeShort) {
  260. consumed = true;
  261. switch(event->key) {
  262. case InputKeyUp:
  263. uart_hex_input_handle_up(uart_text_input, model);
  264. break;
  265. case InputKeyDown:
  266. uart_hex_input_handle_down(uart_text_input, model);
  267. break;
  268. case InputKeyLeft:
  269. uart_hex_input_handle_left(uart_text_input, model);
  270. break;
  271. case InputKeyRight:
  272. uart_hex_input_handle_right(uart_text_input, model);
  273. break;
  274. case InputKeyOk:
  275. uart_hex_input_handle_ok(uart_text_input, model, false);
  276. break;
  277. default:
  278. consumed = false;
  279. break;
  280. }
  281. } else if(event->type == InputTypeLong) {
  282. consumed = true;
  283. switch(event->key) {
  284. case InputKeyUp:
  285. uart_hex_input_handle_up(uart_text_input, model);
  286. break;
  287. case InputKeyDown:
  288. uart_hex_input_handle_down(uart_text_input, model);
  289. break;
  290. case InputKeyLeft:
  291. uart_hex_input_handle_left(uart_text_input, model);
  292. break;
  293. case InputKeyRight:
  294. uart_hex_input_handle_right(uart_text_input, model);
  295. break;
  296. case InputKeyOk:
  297. uart_hex_input_handle_ok(uart_text_input, model, true);
  298. break;
  299. case InputKeyBack:
  300. uart_hex_input_backspace_cb(model);
  301. break;
  302. default:
  303. consumed = false;
  304. break;
  305. }
  306. } else if(event->type == InputTypeRepeat) {
  307. consumed = true;
  308. switch(event->key) {
  309. case InputKeyUp:
  310. uart_hex_input_handle_up(uart_text_input, model);
  311. break;
  312. case InputKeyDown:
  313. uart_hex_input_handle_down(uart_text_input, model);
  314. break;
  315. case InputKeyLeft:
  316. uart_hex_input_handle_left(uart_text_input, model);
  317. break;
  318. case InputKeyRight:
  319. uart_hex_input_handle_right(uart_text_input, model);
  320. break;
  321. case InputKeyBack:
  322. uart_hex_input_backspace_cb(model);
  323. break;
  324. default:
  325. consumed = false;
  326. break;
  327. }
  328. }
  329. // Commit model
  330. view_commit_model(uart_text_input->view, consumed);
  331. return consumed;
  332. }
  333. void uart_hex_input_timer_callback(void* context) {
  334. furi_assert(context);
  335. UART_TextInput* uart_text_input = context;
  336. with_view_model(
  337. uart_text_input->view,
  338. UART_TextInputModel * model,
  339. { model->valadator_message_visible = false; },
  340. true);
  341. }
  342. UART_TextInput* uart_hex_input_alloc() {
  343. UART_TextInput* uart_text_input = malloc(sizeof(UART_TextInput));
  344. uart_text_input->view = view_alloc();
  345. view_set_context(uart_text_input->view, uart_text_input);
  346. view_allocate_model(uart_text_input->view, ViewModelTypeLocking, sizeof(UART_TextInputModel));
  347. view_set_draw_callback(uart_text_input->view, uart_hex_input_view_draw_callback);
  348. view_set_input_callback(uart_text_input->view, uart_hex_input_view_input_callback);
  349. uart_text_input->timer =
  350. furi_timer_alloc(uart_hex_input_timer_callback, FuriTimerTypeOnce, uart_text_input);
  351. with_view_model(
  352. uart_text_input->view,
  353. UART_TextInputModel * model,
  354. { model->validator_text = furi_string_alloc(); },
  355. false);
  356. uart_text_input_reset(uart_text_input);
  357. return uart_text_input;
  358. }
  359. void uart_hex_input_free(UART_TextInput* uart_text_input) {
  360. furi_assert(uart_text_input);
  361. with_view_model(
  362. uart_text_input->view,
  363. UART_TextInputModel * model,
  364. { furi_string_free(model->validator_text); },
  365. false);
  366. // Send stop command
  367. furi_timer_stop(uart_text_input->timer);
  368. // Release allocated memory
  369. furi_timer_free(uart_text_input->timer);
  370. view_free(uart_text_input->view);
  371. free(uart_text_input);
  372. }
  373. void uart_hex_input_reset(UART_TextInput* uart_text_input) {
  374. furi_assert(uart_text_input);
  375. with_view_model(
  376. uart_text_input->view,
  377. UART_TextInputModel * model,
  378. {
  379. model->text_buffer_size = 0;
  380. model->header = "";
  381. model->selected_row = 0;
  382. model->selected_column = 0;
  383. model->clear_default_text = false;
  384. model->text_buffer = NULL;
  385. model->text_buffer_size = 0;
  386. model->callback = NULL;
  387. model->callback_context = NULL;
  388. model->validator_callback = NULL;
  389. model->validator_callback_context = NULL;
  390. furi_string_reset(model->validator_text);
  391. model->valadator_message_visible = false;
  392. },
  393. true);
  394. }
  395. View* uart_hex_input_get_view(UART_TextInput* uart_text_input) {
  396. furi_assert(uart_text_input);
  397. return uart_text_input->view;
  398. }
  399. void uart_hex_input_set_result_callback(
  400. UART_TextInput* uart_text_input,
  401. UART_TextInputCallback callback,
  402. void* callback_context,
  403. char* text_buffer,
  404. size_t text_buffer_size,
  405. bool clear_default_text) {
  406. with_view_model(
  407. uart_text_input->view,
  408. UART_TextInputModel * model,
  409. {
  410. model->callback = callback;
  411. model->callback_context = callback_context;
  412. model->text_buffer = text_buffer;
  413. model->text_buffer_size = text_buffer_size;
  414. model->clear_default_text = clear_default_text;
  415. if(text_buffer && text_buffer[0] != '\0') {
  416. // Set focus on Save
  417. model->selected_row = 1;
  418. model->selected_column = 8;
  419. }
  420. },
  421. true);
  422. }
  423. void uart_hex_input_set_validator(
  424. UART_TextInput* uart_text_input,
  425. UART_TextInputValidatorCallback callback,
  426. void* callback_context) {
  427. with_view_model(
  428. uart_text_input->view,
  429. UART_TextInputModel * model,
  430. {
  431. model->validator_callback = callback;
  432. model->validator_callback_context = callback_context;
  433. },
  434. true);
  435. }
  436. UART_TextInputValidatorCallback
  437. uart_hex_input_get_validator_callback(UART_TextInput* uart_text_input) {
  438. UART_TextInputValidatorCallback validator_callback = NULL;
  439. with_view_model(
  440. uart_text_input->view,
  441. UART_TextInputModel * model,
  442. { validator_callback = model->validator_callback; },
  443. false);
  444. return validator_callback;
  445. }
  446. void* uart_hex_input_get_validator_callback_context(UART_TextInput* uart_text_input) {
  447. void* validator_callback_context = NULL;
  448. with_view_model(
  449. uart_text_input->view,
  450. UART_TextInputModel * model,
  451. { validator_callback_context = model->validator_callback_context; },
  452. false);
  453. return validator_callback_context;
  454. }
  455. void uart_hex_input_set_header_text(UART_TextInput* uart_text_input, const char* text) {
  456. with_view_model(
  457. uart_text_input->view, UART_TextInputModel * model, { model->header = text; }, true);
  458. }