uart_text_input.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #include "uart_text_input.h"
  2. #include <gui/elements.h>
  3. #include "mayhem_morseflash_icons.h"
  4. #include <furi.h>
  5. struct UART_TextInput {
  6. View* view;
  7. FuriTimer* timer;
  8. };
  9. typedef struct {
  10. const char text;
  11. const uint8_t x;
  12. const uint8_t y;
  13. } UART_TextInputKey;
  14. typedef struct {
  15. const char* header;
  16. char* text_buffer;
  17. size_t text_buffer_size;
  18. bool clear_default_text;
  19. UART_TextInputCallback callback;
  20. void* callback_context;
  21. uint8_t selected_row;
  22. uint8_t selected_column;
  23. UART_TextInputValidatorCallback validator_callback;
  24. void* validator_callback_context;
  25. FuriString* validator_text;
  26. bool valadator_message_visible;
  27. } UART_TextInputModel;
  28. static const uint8_t keyboard_origin_x = 1;
  29. static const uint8_t keyboard_origin_y = 29;
  30. static const uint8_t keyboard_row_count = 4;
  31. #define ENTER_KEY '\r'
  32. #define BACKSPACE_KEY '\b'
  33. static const UART_TextInputKey keyboard_keys_row_1[] = {
  34. {'{', 1, 0},
  35. {'(', 9, 0},
  36. {'[', 17, 0},
  37. {'|', 25, 0},
  38. {'@', 33, 0},
  39. {'&', 41, 0},
  40. {'#', 49, 0},
  41. {';', 57, 0},
  42. {'^', 65, 0},
  43. {'*', 73, 0},
  44. {'`', 81, 0},
  45. {'"', 89, 0},
  46. {'~', 97, 0},
  47. {'\'', 105, 0},
  48. {'.', 113, 0},
  49. {'/', 120, 0},
  50. };
  51. static const UART_TextInputKey keyboard_keys_row_2[] = {
  52. {'q', 1, 10},
  53. {'w', 9, 10},
  54. {'e', 17, 10},
  55. {'r', 25, 10},
  56. {'t', 33, 10},
  57. {'y', 41, 10},
  58. {'u', 49, 10},
  59. {'i', 57, 10},
  60. {'o', 65, 10},
  61. {'p', 73, 10},
  62. {'0', 81, 10},
  63. {'1', 89, 10},
  64. {'2', 97, 10},
  65. {'3', 105, 10},
  66. {'=', 113, 10},
  67. {'-', 120, 10},
  68. };
  69. static const UART_TextInputKey keyboard_keys_row_3[] = {
  70. {'a', 1, 21},
  71. {'s', 9, 21},
  72. {'d', 18, 21},
  73. {'f', 25, 21},
  74. {'g', 33, 21},
  75. {'h', 41, 21},
  76. {'j', 49, 21},
  77. {'k', 57, 21},
  78. {'l', 65, 21},
  79. {BACKSPACE_KEY, 72, 13},
  80. {'4', 89, 21},
  81. {'5', 97, 21},
  82. {'6', 105, 21},
  83. {'$', 113, 21},
  84. {'%', 120, 21},
  85. };
  86. static const UART_TextInputKey keyboard_keys_row_4[] = {
  87. {'z', 1, 33},
  88. {'x', 9, 33},
  89. {'c', 18, 33},
  90. {'v', 25, 33},
  91. {'b', 33, 33},
  92. {'n', 41, 33},
  93. {'m', 49, 33},
  94. {'_', 57, 33},
  95. {ENTER_KEY, 64, 24},
  96. {'7', 89, 33},
  97. {'8', 97, 33},
  98. {'9', 105, 33},
  99. {'!', 113, 33},
  100. {'+', 120, 33},
  101. };
  102. static uint8_t get_row_size(uint8_t row_index) {
  103. uint8_t row_size = 0;
  104. switch(row_index + 1) {
  105. case 1:
  106. row_size = sizeof(keyboard_keys_row_1) / sizeof(UART_TextInputKey);
  107. break;
  108. case 2:
  109. row_size = sizeof(keyboard_keys_row_2) / sizeof(UART_TextInputKey);
  110. break;
  111. case 3:
  112. row_size = sizeof(keyboard_keys_row_3) / sizeof(UART_TextInputKey);
  113. break;
  114. case 4:
  115. row_size = sizeof(keyboard_keys_row_4) / sizeof(UART_TextInputKey);
  116. break;
  117. }
  118. return row_size;
  119. }
  120. static const UART_TextInputKey* get_row(uint8_t row_index) {
  121. const UART_TextInputKey* row = NULL;
  122. switch(row_index + 1) {
  123. case 1:
  124. row = keyboard_keys_row_1;
  125. break;
  126. case 2:
  127. row = keyboard_keys_row_2;
  128. break;
  129. case 3:
  130. row = keyboard_keys_row_3;
  131. break;
  132. case 4:
  133. row = keyboard_keys_row_4;
  134. break;
  135. }
  136. return row;
  137. }
  138. static char get_selected_char(UART_TextInputModel* model) {
  139. return get_row(model->selected_row)[model->selected_column].text;
  140. }
  141. static bool char_is_lowercase(char letter) {
  142. return (letter >= 0x61 && letter <= 0x7A);
  143. }
  144. static char char_to_uppercase(const char letter) {
  145. switch(letter) {
  146. case '_':
  147. return 0x20;
  148. break;
  149. case '(':
  150. return 0x29;
  151. break;
  152. case '{':
  153. return 0x7d;
  154. break;
  155. case '[':
  156. return 0x5d;
  157. break;
  158. case '/':
  159. return 0x5c;
  160. break;
  161. case ';':
  162. return 0x3a;
  163. break;
  164. case '.':
  165. return 0x2c;
  166. break;
  167. case '!':
  168. return 0x3f;
  169. break;
  170. case '<':
  171. return 0x3e;
  172. break;
  173. }
  174. if(isalpha(letter)) {
  175. return (letter - 0x20);
  176. } else {
  177. return letter;
  178. }
  179. }
  180. static void uart_text_input_backspace_cb(UART_TextInputModel* model) {
  181. uint8_t text_length = model->clear_default_text ? 1 : strlen(model->text_buffer);
  182. if(text_length > 0) {
  183. model->text_buffer[text_length - 1] = 0;
  184. }
  185. }
  186. static void uart_text_input_view_draw_callback(Canvas* canvas, void* _model) {
  187. UART_TextInputModel* model = _model;
  188. uint8_t text_length = model->text_buffer ? strlen(model->text_buffer) : 0;
  189. uint8_t needed_string_width = canvas_width(canvas) - 8;
  190. uint8_t start_pos = 4;
  191. const char* text = model->text_buffer;
  192. canvas_clear(canvas);
  193. canvas_set_color(canvas, ColorBlack);
  194. canvas_draw_str(canvas, 2, 7, model->header);
  195. elements_slightly_rounded_frame(canvas, 1, 8, 126, 12);
  196. if(canvas_string_width(canvas, text) > needed_string_width) {
  197. canvas_draw_str(canvas, start_pos, 17, "...");
  198. start_pos += 6;
  199. needed_string_width -= 8;
  200. }
  201. while(text != 0 && canvas_string_width(canvas, text) > needed_string_width) {
  202. text++;
  203. }
  204. if(model->clear_default_text) {
  205. elements_slightly_rounded_box(
  206. canvas, start_pos - 1, 14, canvas_string_width(canvas, text) + 2, 10);
  207. canvas_set_color(canvas, ColorWhite);
  208. } else {
  209. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 1, 18, "|");
  210. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 2, 18, "|");
  211. }
  212. canvas_draw_str(canvas, start_pos, 17, text);
  213. canvas_set_font(canvas, FontKeyboard);
  214. for(uint8_t row = 0; row <= keyboard_row_count; row++) {
  215. const uint8_t column_count = get_row_size(row);
  216. const UART_TextInputKey* keys = get_row(row);
  217. for(size_t column = 0; column < column_count; column++) {
  218. if(keys[column].text == ENTER_KEY) {
  219. canvas_set_color(canvas, ColorBlack);
  220. if(model->selected_row == row && model->selected_column == column) {
  221. canvas_draw_icon(
  222. canvas,
  223. keyboard_origin_x + keys[column].x,
  224. keyboard_origin_y + keys[column].y,
  225. &I_KeySaveSelected_24x11);
  226. } else {
  227. canvas_draw_icon(
  228. canvas,
  229. keyboard_origin_x + keys[column].x,
  230. keyboard_origin_y + keys[column].y,
  231. &I_KeySave_24x11);
  232. }
  233. } else if(keys[column].text == BACKSPACE_KEY) {
  234. canvas_set_color(canvas, ColorBlack);
  235. if(model->selected_row == row && model->selected_column == column) {
  236. canvas_draw_icon(
  237. canvas,
  238. keyboard_origin_x + keys[column].x,
  239. keyboard_origin_y + keys[column].y,
  240. &I_KeyBackspaceSelected_16x9);
  241. } else {
  242. canvas_draw_icon(
  243. canvas,
  244. keyboard_origin_x + keys[column].x,
  245. keyboard_origin_y + keys[column].y,
  246. &I_KeyBackspace_16x9);
  247. }
  248. } else {
  249. if(model->selected_row == row && model->selected_column == column) {
  250. canvas_set_color(canvas, ColorBlack);
  251. canvas_draw_box(
  252. canvas,
  253. keyboard_origin_x + keys[column].x - 1,
  254. keyboard_origin_y + keys[column].y - 8,
  255. 7,
  256. 10);
  257. canvas_set_color(canvas, ColorWhite);
  258. } else {
  259. canvas_set_color(canvas, ColorBlack);
  260. }
  261. if(model->clear_default_text ||
  262. (text_length == 0 && char_is_lowercase(keys[column].text))) {
  263. canvas_draw_glyph(
  264. canvas,
  265. keyboard_origin_x + keys[column].x,
  266. keyboard_origin_y + keys[column].y,
  267. //char_to_uppercase(keys[column].text));
  268. keys[column].text);
  269. } else {
  270. canvas_draw_glyph(
  271. canvas,
  272. keyboard_origin_x + keys[column].x,
  273. keyboard_origin_y + keys[column].y,
  274. keys[column].text);
  275. }
  276. }
  277. }
  278. }
  279. if(model->valadator_message_visible) {
  280. canvas_set_font(canvas, FontSecondary);
  281. canvas_set_color(canvas, ColorWhite);
  282. canvas_draw_box(canvas, 8, 10, 110, 48);
  283. canvas_set_color(canvas, ColorBlack);
  284. canvas_draw_icon(canvas, 10, 14, &I_WarningDolphin_45x42);
  285. canvas_draw_rframe(canvas, 8, 8, 112, 50, 3);
  286. canvas_draw_rframe(canvas, 9, 9, 110, 48, 2);
  287. elements_multiline_text(canvas, 62, 20, furi_string_get_cstr(model->validator_text));
  288. canvas_set_font(canvas, FontKeyboard);
  289. }
  290. }
  291. static void
  292. uart_text_input_handle_up(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  293. UNUSED(uart_text_input);
  294. if(model->selected_row > 0) {
  295. model->selected_row--;
  296. if(model->selected_column > get_row_size(model->selected_row) - 6) {
  297. model->selected_column = model->selected_column + 1;
  298. }
  299. }
  300. }
  301. static void
  302. uart_text_input_handle_down(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  303. UNUSED(uart_text_input);
  304. if(model->selected_row < keyboard_row_count - 1) {
  305. model->selected_row++;
  306. if(model->selected_column > get_row_size(model->selected_row) - 4) {
  307. model->selected_column = model->selected_column - 1;
  308. }
  309. }
  310. }
  311. static void
  312. uart_text_input_handle_left(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  313. UNUSED(uart_text_input);
  314. if(model->selected_column > 0) {
  315. model->selected_column--;
  316. } else {
  317. model->selected_column = get_row_size(model->selected_row) - 1;
  318. }
  319. }
  320. static void
  321. uart_text_input_handle_right(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  322. UNUSED(uart_text_input);
  323. if(model->selected_column < get_row_size(model->selected_row) - 1) {
  324. model->selected_column++;
  325. } else {
  326. model->selected_column = 0;
  327. }
  328. }
  329. static void uart_text_input_handle_ok(
  330. UART_TextInput* uart_text_input,
  331. UART_TextInputModel* model,
  332. bool shift) {
  333. char selected = get_selected_char(model);
  334. uint8_t text_length = strlen(model->text_buffer);
  335. if(shift) {
  336. selected = char_to_uppercase(selected);
  337. }
  338. if(selected == ENTER_KEY) {
  339. if(model->validator_callback &&
  340. (!model->validator_callback(
  341. model->text_buffer, model->validator_text, model->validator_callback_context))) {
  342. model->valadator_message_visible = true;
  343. furi_timer_start(uart_text_input->timer, furi_kernel_get_tick_frequency() * 4);
  344. } else if(model->callback != 0 && text_length > 0) {
  345. model->callback(model->callback_context);
  346. }
  347. } else if(selected == BACKSPACE_KEY) {
  348. uart_text_input_backspace_cb(model);
  349. } else {
  350. if(model->clear_default_text) {
  351. text_length = 0;
  352. }
  353. if(text_length < (model->text_buffer_size - 1)) {
  354. if(text_length == 0 && char_is_lowercase(selected)) {
  355. //selected = char_to_uppercase(selected);
  356. }
  357. model->text_buffer[text_length] = selected;
  358. model->text_buffer[text_length + 1] = 0;
  359. }
  360. }
  361. model->clear_default_text = false;
  362. }
  363. static bool uart_text_input_view_input_callback(InputEvent* event, void* context) {
  364. UART_TextInput* uart_text_input = context;
  365. furi_assert(uart_text_input);
  366. bool consumed = false;
  367. // Acquire model
  368. UART_TextInputModel* model = view_get_model(uart_text_input->view);
  369. if((!(event->type == InputTypePress) && !(event->type == InputTypeRelease)) &&
  370. model->valadator_message_visible) {
  371. model->valadator_message_visible = false;
  372. consumed = true;
  373. } else if(event->type == InputTypeShort) {
  374. consumed = true;
  375. switch(event->key) {
  376. case InputKeyUp:
  377. uart_text_input_handle_up(uart_text_input, model);
  378. break;
  379. case InputKeyDown:
  380. uart_text_input_handle_down(uart_text_input, model);
  381. break;
  382. case InputKeyLeft:
  383. uart_text_input_handle_left(uart_text_input, model);
  384. break;
  385. case InputKeyRight:
  386. uart_text_input_handle_right(uart_text_input, model);
  387. break;
  388. case InputKeyOk:
  389. uart_text_input_handle_ok(uart_text_input, model, false);
  390. break;
  391. default:
  392. consumed = false;
  393. break;
  394. }
  395. } else if(event->type == InputTypeLong) {
  396. consumed = true;
  397. switch(event->key) {
  398. case InputKeyUp:
  399. uart_text_input_handle_up(uart_text_input, model);
  400. break;
  401. case InputKeyDown:
  402. uart_text_input_handle_down(uart_text_input, model);
  403. break;
  404. case InputKeyLeft:
  405. uart_text_input_handle_left(uart_text_input, model);
  406. break;
  407. case InputKeyRight:
  408. uart_text_input_handle_right(uart_text_input, model);
  409. break;
  410. case InputKeyOk:
  411. uart_text_input_handle_ok(uart_text_input, model, true);
  412. break;
  413. case InputKeyBack:
  414. uart_text_input_backspace_cb(model);
  415. break;
  416. default:
  417. consumed = false;
  418. break;
  419. }
  420. } else if(event->type == InputTypeRepeat) {
  421. consumed = true;
  422. switch(event->key) {
  423. case InputKeyUp:
  424. uart_text_input_handle_up(uart_text_input, model);
  425. break;
  426. case InputKeyDown:
  427. uart_text_input_handle_down(uart_text_input, model);
  428. break;
  429. case InputKeyLeft:
  430. uart_text_input_handle_left(uart_text_input, model);
  431. break;
  432. case InputKeyRight:
  433. uart_text_input_handle_right(uart_text_input, model);
  434. break;
  435. case InputKeyBack:
  436. uart_text_input_backspace_cb(model);
  437. break;
  438. default:
  439. consumed = false;
  440. break;
  441. }
  442. }
  443. // Commit model
  444. view_commit_model(uart_text_input->view, consumed);
  445. return consumed;
  446. }
  447. void uart_text_input_timer_callback(void* context) {
  448. furi_assert(context);
  449. UART_TextInput* uart_text_input = context;
  450. with_view_model(
  451. uart_text_input->view,
  452. UART_TextInputModel * model,
  453. { model->valadator_message_visible = false; },
  454. true);
  455. }
  456. UART_TextInput* uart_text_input_alloc() {
  457. UART_TextInput* uart_text_input = malloc(sizeof(UART_TextInput));
  458. uart_text_input->view = view_alloc();
  459. view_set_context(uart_text_input->view, uart_text_input);
  460. view_allocate_model(uart_text_input->view, ViewModelTypeLocking, sizeof(UART_TextInputModel));
  461. view_set_draw_callback(uart_text_input->view, uart_text_input_view_draw_callback);
  462. view_set_input_callback(uart_text_input->view, uart_text_input_view_input_callback);
  463. uart_text_input->timer =
  464. furi_timer_alloc(uart_text_input_timer_callback, FuriTimerTypeOnce, uart_text_input);
  465. with_view_model(
  466. uart_text_input->view,
  467. UART_TextInputModel * model,
  468. { model->validator_text = furi_string_alloc(); },
  469. false);
  470. uart_text_input_reset(uart_text_input);
  471. return uart_text_input;
  472. }
  473. void uart_text_input_free(UART_TextInput* uart_text_input) {
  474. furi_assert(uart_text_input);
  475. with_view_model(
  476. uart_text_input->view,
  477. UART_TextInputModel * model,
  478. { furi_string_free(model->validator_text); },
  479. false);
  480. // Send stop command
  481. furi_timer_stop(uart_text_input->timer);
  482. // Release allocated memory
  483. furi_timer_free(uart_text_input->timer);
  484. view_free(uart_text_input->view);
  485. free(uart_text_input);
  486. }
  487. void uart_text_input_reset(UART_TextInput* uart_text_input) {
  488. furi_assert(uart_text_input);
  489. with_view_model(
  490. uart_text_input->view,
  491. UART_TextInputModel * model,
  492. {
  493. model->text_buffer_size = 0;
  494. model->header = "";
  495. model->selected_row = 0;
  496. model->selected_column = 0;
  497. model->clear_default_text = false;
  498. model->text_buffer = NULL;
  499. model->text_buffer_size = 0;
  500. model->callback = NULL;
  501. model->callback_context = NULL;
  502. model->validator_callback = NULL;
  503. model->validator_callback_context = NULL;
  504. furi_string_reset(model->validator_text);
  505. model->valadator_message_visible = false;
  506. },
  507. true);
  508. }
  509. View* uart_text_input_get_view(UART_TextInput* uart_text_input) {
  510. furi_assert(uart_text_input);
  511. return uart_text_input->view;
  512. }
  513. void uart_text_input_set_result_callback(
  514. UART_TextInput* uart_text_input,
  515. UART_TextInputCallback callback,
  516. void* callback_context,
  517. char* text_buffer,
  518. size_t text_buffer_size,
  519. bool clear_default_text) {
  520. with_view_model(
  521. uart_text_input->view,
  522. UART_TextInputModel * model,
  523. {
  524. model->callback = callback;
  525. model->callback_context = callback_context;
  526. model->text_buffer = text_buffer;
  527. model->text_buffer_size = text_buffer_size;
  528. model->clear_default_text = clear_default_text;
  529. if(text_buffer && text_buffer[0] != '\0') {
  530. // Set focus on Save
  531. model->selected_row = 2;
  532. model->selected_column = 8;
  533. }
  534. },
  535. true);
  536. }
  537. void uart_text_input_set_validator(
  538. UART_TextInput* uart_text_input,
  539. UART_TextInputValidatorCallback callback,
  540. void* callback_context) {
  541. with_view_model(
  542. uart_text_input->view,
  543. UART_TextInputModel * model,
  544. {
  545. model->validator_callback = callback;
  546. model->validator_callback_context = callback_context;
  547. },
  548. true);
  549. }
  550. UART_TextInputValidatorCallback
  551. uart_text_input_get_validator_callback(UART_TextInput* uart_text_input) {
  552. UART_TextInputValidatorCallback validator_callback = NULL;
  553. with_view_model(
  554. uart_text_input->view,
  555. UART_TextInputModel * model,
  556. { validator_callback = model->validator_callback; },
  557. false);
  558. return validator_callback;
  559. }
  560. void* uart_text_input_get_validator_callback_context(UART_TextInput* uart_text_input) {
  561. void* validator_callback_context = NULL;
  562. with_view_model(
  563. uart_text_input->view,
  564. UART_TextInputModel * model,
  565. { validator_callback_context = model->validator_callback_context; },
  566. false);
  567. return validator_callback_context;
  568. }
  569. void uart_text_input_set_header_text(UART_TextInput* uart_text_input, const char* text) {
  570. with_view_model(
  571. uart_text_input->view, UART_TextInputModel * model, { model->header = text; }, true);
  572. }