uart_hex_input.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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
  184. uart_hex_input_handle_up(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  185. UNUSED(uart_text_input);
  186. if(model->selected_row > 0) {
  187. model->selected_row--;
  188. if(model->selected_column > get_row_size(model->selected_row) - 6) {
  189. model->selected_column = model->selected_column + 1;
  190. }
  191. }
  192. }
  193. static void
  194. uart_hex_input_handle_down(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  195. UNUSED(uart_text_input);
  196. if(model->selected_row < keyboard_row_count - 1) {
  197. model->selected_row++;
  198. if(model->selected_column > get_row_size(model->selected_row) - 4) {
  199. model->selected_column = model->selected_column - 1;
  200. }
  201. }
  202. }
  203. static void
  204. uart_hex_input_handle_left(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  205. UNUSED(uart_text_input);
  206. if(model->selected_column > 0) {
  207. model->selected_column--;
  208. } else {
  209. model->selected_column = get_row_size(model->selected_row) - 1;
  210. }
  211. }
  212. static void
  213. uart_hex_input_handle_right(UART_TextInput* uart_text_input, UART_TextInputModel* model) {
  214. UNUSED(uart_text_input);
  215. if(model->selected_column < get_row_size(model->selected_row) - 1) {
  216. model->selected_column++;
  217. } else {
  218. model->selected_column = 0;
  219. }
  220. }
  221. static void uart_hex_input_handle_ok(
  222. UART_TextInput* uart_text_input,
  223. UART_TextInputModel* model,
  224. bool shift) {
  225. UNUSED(shift);
  226. char selected = get_selected_char(model);
  227. uint8_t text_length = strlen(model->text_buffer);
  228. if(selected == ENTER_KEY) {
  229. if(model->validator_callback &&
  230. (!model->validator_callback(
  231. model->text_buffer, model->validator_text, model->validator_callback_context))) {
  232. model->valadator_message_visible = true;
  233. furi_timer_start(uart_text_input->timer, furi_kernel_get_tick_frequency() * 4);
  234. } else if(model->callback != 0 && text_length > 0) {
  235. model->callback(model->callback_context);
  236. }
  237. } else if(selected == BACKSPACE_KEY) {
  238. uart_hex_input_backspace_cb(model);
  239. } else {
  240. if(model->clear_default_text) {
  241. text_length = 0;
  242. }
  243. if(text_length < (model->text_buffer_size - 1)) {
  244. model->text_buffer[text_length] = selected;
  245. model->text_buffer[text_length + 1] = 0;
  246. }
  247. }
  248. model->clear_default_text = false;
  249. }
  250. static bool uart_hex_input_view_input_callback(InputEvent* event, void* context) {
  251. UART_TextInput* uart_text_input = context;
  252. furi_assert(uart_text_input);
  253. bool consumed = false;
  254. // Acquire model
  255. UART_TextInputModel* model = view_get_model(uart_text_input->view);
  256. if((!(event->type == InputTypePress) && !(event->type == InputTypeRelease)) &&
  257. model->valadator_message_visible) {
  258. model->valadator_message_visible = false;
  259. consumed = true;
  260. } else if(event->type == InputTypeShort) {
  261. consumed = true;
  262. switch(event->key) {
  263. case InputKeyUp:
  264. uart_hex_input_handle_up(uart_text_input, model);
  265. break;
  266. case InputKeyDown:
  267. uart_hex_input_handle_down(uart_text_input, model);
  268. break;
  269. case InputKeyLeft:
  270. uart_hex_input_handle_left(uart_text_input, model);
  271. break;
  272. case InputKeyRight:
  273. uart_hex_input_handle_right(uart_text_input, model);
  274. break;
  275. case InputKeyOk:
  276. uart_hex_input_handle_ok(uart_text_input, model, false);
  277. break;
  278. default:
  279. consumed = false;
  280. break;
  281. }
  282. } else if(event->type == InputTypeLong) {
  283. consumed = true;
  284. switch(event->key) {
  285. case InputKeyUp:
  286. uart_hex_input_handle_up(uart_text_input, model);
  287. break;
  288. case InputKeyDown:
  289. uart_hex_input_handle_down(uart_text_input, model);
  290. break;
  291. case InputKeyLeft:
  292. uart_hex_input_handle_left(uart_text_input, model);
  293. break;
  294. case InputKeyRight:
  295. uart_hex_input_handle_right(uart_text_input, model);
  296. break;
  297. case InputKeyOk:
  298. uart_hex_input_handle_ok(uart_text_input, model, true);
  299. break;
  300. case InputKeyBack:
  301. uart_hex_input_backspace_cb(model);
  302. break;
  303. default:
  304. consumed = false;
  305. break;
  306. }
  307. } else if(event->type == InputTypeRepeat) {
  308. consumed = true;
  309. switch(event->key) {
  310. case InputKeyUp:
  311. uart_hex_input_handle_up(uart_text_input, model);
  312. break;
  313. case InputKeyDown:
  314. uart_hex_input_handle_down(uart_text_input, model);
  315. break;
  316. case InputKeyLeft:
  317. uart_hex_input_handle_left(uart_text_input, model);
  318. break;
  319. case InputKeyRight:
  320. uart_hex_input_handle_right(uart_text_input, model);
  321. break;
  322. case InputKeyBack:
  323. uart_hex_input_backspace_cb(model);
  324. break;
  325. default:
  326. consumed = false;
  327. break;
  328. }
  329. }
  330. // Commit model
  331. view_commit_model(uart_text_input->view, consumed);
  332. return consumed;
  333. }
  334. void uart_hex_input_timer_callback(void* context) {
  335. furi_assert(context);
  336. UART_TextInput* uart_text_input = context;
  337. with_view_model(
  338. uart_text_input->view,
  339. UART_TextInputModel * model,
  340. { model->valadator_message_visible = false; },
  341. true);
  342. }
  343. UART_TextInput* uart_hex_input_alloc() {
  344. UART_TextInput* uart_text_input = malloc(sizeof(UART_TextInput));
  345. uart_text_input->view = view_alloc();
  346. view_set_context(uart_text_input->view, uart_text_input);
  347. view_allocate_model(uart_text_input->view, ViewModelTypeLocking, sizeof(UART_TextInputModel));
  348. view_set_draw_callback(uart_text_input->view, uart_hex_input_view_draw_callback);
  349. view_set_input_callback(uart_text_input->view, uart_hex_input_view_input_callback);
  350. uart_text_input->timer =
  351. furi_timer_alloc(uart_hex_input_timer_callback, FuriTimerTypeOnce, uart_text_input);
  352. with_view_model(
  353. uart_text_input->view,
  354. UART_TextInputModel * model,
  355. { model->validator_text = furi_string_alloc(); },
  356. false);
  357. uart_text_input_reset(uart_text_input);
  358. return uart_text_input;
  359. }
  360. void uart_hex_input_free(UART_TextInput* uart_text_input) {
  361. furi_assert(uart_text_input);
  362. with_view_model(
  363. uart_text_input->view,
  364. UART_TextInputModel * model,
  365. { furi_string_free(model->validator_text); },
  366. false);
  367. // Send stop command
  368. furi_timer_stop(uart_text_input->timer);
  369. // Release allocated memory
  370. furi_timer_free(uart_text_input->timer);
  371. view_free(uart_text_input->view);
  372. free(uart_text_input);
  373. }
  374. void uart_hex_input_reset(UART_TextInput* uart_text_input) {
  375. furi_assert(uart_text_input);
  376. with_view_model(
  377. uart_text_input->view,
  378. UART_TextInputModel * model,
  379. {
  380. model->text_buffer_size = 0;
  381. model->header = "";
  382. model->selected_row = 0;
  383. model->selected_column = 0;
  384. model->clear_default_text = false;
  385. model->text_buffer = NULL;
  386. model->text_buffer_size = 0;
  387. model->callback = NULL;
  388. model->callback_context = NULL;
  389. model->validator_callback = NULL;
  390. model->validator_callback_context = NULL;
  391. furi_string_reset(model->validator_text);
  392. model->valadator_message_visible = false;
  393. },
  394. true);
  395. }
  396. View* uart_hex_input_get_view(UART_TextInput* uart_text_input) {
  397. furi_assert(uart_text_input);
  398. return uart_text_input->view;
  399. }
  400. void uart_hex_input_set_result_callback(
  401. UART_TextInput* uart_text_input,
  402. UART_TextInputCallback callback,
  403. void* callback_context,
  404. char* text_buffer,
  405. size_t text_buffer_size,
  406. bool clear_default_text) {
  407. with_view_model(
  408. uart_text_input->view,
  409. UART_TextInputModel * model,
  410. {
  411. model->callback = callback;
  412. model->callback_context = callback_context;
  413. model->text_buffer = text_buffer;
  414. model->text_buffer_size = text_buffer_size;
  415. model->clear_default_text = clear_default_text;
  416. if(text_buffer && text_buffer[0] != '\0') {
  417. // Set focus on Save
  418. model->selected_row = 1;
  419. model->selected_column = 8;
  420. }
  421. },
  422. true);
  423. }
  424. void uart_hex_input_set_validator(
  425. UART_TextInput* uart_text_input,
  426. UART_TextInputValidatorCallback callback,
  427. void* callback_context) {
  428. with_view_model(
  429. uart_text_input->view,
  430. UART_TextInputModel * model,
  431. {
  432. model->validator_callback = callback;
  433. model->validator_callback_context = callback_context;
  434. },
  435. true);
  436. }
  437. UART_TextInputValidatorCallback
  438. uart_hex_input_get_validator_callback(UART_TextInput* uart_text_input) {
  439. UART_TextInputValidatorCallback validator_callback = NULL;
  440. with_view_model(
  441. uart_text_input->view,
  442. UART_TextInputModel * model,
  443. { validator_callback = model->validator_callback; },
  444. false);
  445. return validator_callback;
  446. }
  447. void* uart_hex_input_get_validator_callback_context(UART_TextInput* uart_text_input) {
  448. void* validator_callback_context = NULL;
  449. with_view_model(
  450. uart_text_input->view,
  451. UART_TextInputModel * model,
  452. { validator_callback_context = model->validator_callback_context; },
  453. false);
  454. return validator_callback_context;
  455. }
  456. void uart_hex_input_set_header_text(UART_TextInput* uart_text_input, const char* text) {
  457. with_view_model(
  458. uart_text_input->view, UART_TextInputModel * model, { model->header = text; }, true);
  459. }