uart_text_input.c 19 KB

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