easy_flipper.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #ifndef EASY_FLIPPER_H
  2. #define EASY_FLIPPER_H
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <furi.h>
  8. #include <furi_hal.h>
  9. #include <gui/gui.h>
  10. #include <gui/view.h>
  11. #include <gui/modules/submenu.h>
  12. #include <gui/view_dispatcher.h>
  13. #include <gui/modules/menu.h>
  14. #include <gui/modules/submenu.h>
  15. #include <gui/modules/widget.h>
  16. #include <gui/modules/text_input.h>
  17. #include <gui/modules/text_box.h>
  18. #include <gui/modules/variable_item_list.h>
  19. #include <gui/modules/dialog_ex.h>
  20. #include <gui/modules/popup.h>
  21. #include <gui/modules/loading.h>
  22. #include <uart_text_input.h>
  23. #define EASY_TAG "EasyFlipper"
  24. /**
  25. * @brief Navigation callback for exiting the application
  26. * @param context The context - unused
  27. * @return next view id (VIEW_NONE to exit the app)
  28. */
  29. uint32_t easy_flipper_callback_exit_app(void *context)
  30. {
  31. // Exit the application
  32. if (!context)
  33. {
  34. FURI_LOG_E(EASY_TAG, "Context is NULL");
  35. return VIEW_NONE;
  36. }
  37. UNUSED(context);
  38. return VIEW_NONE; // Return VIEW_NONE to exit the app
  39. }
  40. /**
  41. * @brief Initialize a buffer
  42. * @param buffer The buffer to initialize
  43. * @param buffer_size The size of the buffer
  44. * @return true if successful, false otherwise
  45. */
  46. bool easy_flipper_set_buffer(char **buffer, uint32_t buffer_size)
  47. {
  48. if (!buffer)
  49. {
  50. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_buffer");
  51. return false;
  52. }
  53. *buffer = (char *)malloc(buffer_size);
  54. if (!*buffer)
  55. {
  56. FURI_LOG_E(EASY_TAG, "Failed to allocate buffer");
  57. return false;
  58. }
  59. *buffer[0] = '\0';
  60. return true;
  61. }
  62. /**
  63. * @brief Initialize a View object
  64. * @param view The View object to initialize
  65. * @param view_id The ID/Index of the view
  66. * @param draw_callback The draw callback function (set to NULL if not needed)
  67. * @param input_callback The input callback function (set to NULL if not needed)
  68. * @param previous_callback The previous callback function (can be set to NULL)
  69. * @param view_dispatcher The ViewDispatcher object
  70. * @return true if successful, false otherwise
  71. */
  72. bool easy_flipper_set_view(
  73. View **view,
  74. int32_t view_id,
  75. void draw_callback(Canvas *, void *),
  76. bool input_callback(InputEvent *, void *),
  77. uint32_t (*previous_callback)(void *),
  78. ViewDispatcher **view_dispatcher,
  79. void *context)
  80. {
  81. if (!view || !view_dispatcher)
  82. {
  83. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_view");
  84. return false;
  85. }
  86. *view = view_alloc();
  87. if (!*view)
  88. {
  89. FURI_LOG_E(EASY_TAG, "Failed to allocate View");
  90. return false;
  91. }
  92. if (draw_callback)
  93. {
  94. view_set_draw_callback(*view, draw_callback);
  95. }
  96. if (input_callback)
  97. {
  98. view_set_input_callback(*view, input_callback);
  99. }
  100. if (context)
  101. {
  102. view_set_context(*view, context);
  103. }
  104. if (previous_callback)
  105. {
  106. view_set_previous_callback(*view, previous_callback);
  107. }
  108. view_dispatcher_add_view(*view_dispatcher, view_id, *view);
  109. return true;
  110. }
  111. /**
  112. * @brief Initialize a ViewDispatcher object
  113. * @param view_dispatcher The ViewDispatcher object to initialize
  114. * @param gui The GUI object
  115. * @param context The context to pass to the event callback
  116. * @return true if successful, false otherwise
  117. */
  118. bool easy_flipper_set_view_dispatcher(ViewDispatcher **view_dispatcher, Gui *gui, void *context)
  119. {
  120. if (!view_dispatcher)
  121. {
  122. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_view_dispatcher");
  123. return false;
  124. }
  125. *view_dispatcher = view_dispatcher_alloc();
  126. if (!*view_dispatcher)
  127. {
  128. FURI_LOG_E(EASY_TAG, "Failed to allocate ViewDispatcher");
  129. return false;
  130. }
  131. view_dispatcher_attach_to_gui(*view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  132. if (context)
  133. {
  134. view_dispatcher_set_event_callback_context(*view_dispatcher, context);
  135. }
  136. return true;
  137. }
  138. /**
  139. * @brief Initialize a Submenu object
  140. * @note This does not set the items in the submenu
  141. * @param submenu The Submenu object to initialize
  142. * @param view_id The ID/Index of the view
  143. * @param title The title/header of the submenu
  144. * @param previous_callback The previous callback function (can be set to NULL)
  145. * @param view_dispatcher The ViewDispatcher object
  146. * @return true if successful, false otherwise
  147. */
  148. bool easy_flipper_set_submenu(
  149. Submenu **submenu,
  150. int32_t view_id,
  151. char *title,
  152. uint32_t(previous_callback)(void *),
  153. ViewDispatcher **view_dispatcher)
  154. {
  155. if (!submenu)
  156. {
  157. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_submenu");
  158. return false;
  159. }
  160. *submenu = submenu_alloc();
  161. if (!*submenu)
  162. {
  163. FURI_LOG_E(EASY_TAG, "Failed to allocate Submenu");
  164. return false;
  165. }
  166. if (title)
  167. {
  168. submenu_set_header(*submenu, title);
  169. }
  170. if (previous_callback)
  171. {
  172. view_set_previous_callback(submenu_get_view(*submenu), previous_callback);
  173. }
  174. view_dispatcher_add_view(*view_dispatcher, view_id, submenu_get_view(*submenu));
  175. return true;
  176. }
  177. /**
  178. * @brief Initialize a Menu object
  179. * @note This does not set the items in the menu
  180. * @param menu The Menu object to initialize
  181. * @param view_id The ID/Index of the view
  182. * @param item_callback The item callback function
  183. * @param previous_callback The previous callback function (can be set to NULL)
  184. * @param view_dispatcher The ViewDispatcher object
  185. * @return true if successful, false otherwise
  186. */
  187. bool easy_flipper_set_menu(
  188. Menu **menu,
  189. int32_t view_id,
  190. uint32_t(previous_callback)(void *),
  191. ViewDispatcher **view_dispatcher)
  192. {
  193. if (!menu)
  194. {
  195. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_menu");
  196. return false;
  197. }
  198. *menu = menu_alloc();
  199. if (!*menu)
  200. {
  201. FURI_LOG_E(EASY_TAG, "Failed to allocate Menu");
  202. return false;
  203. }
  204. if (previous_callback)
  205. {
  206. view_set_previous_callback(menu_get_view(*menu), previous_callback);
  207. }
  208. view_dispatcher_add_view(*view_dispatcher, view_id, menu_get_view(*menu));
  209. return true;
  210. }
  211. /**
  212. * @brief Initialize a Widget object
  213. * @param widget The Widget object to initialize
  214. * @param view_id The ID/Index of the view
  215. * @param text The text to display in the widget
  216. * @param previous_callback The previous callback function (can be set to NULL)
  217. * @param view_dispatcher The ViewDispatcher object
  218. * @return true if successful, false otherwise
  219. */
  220. bool easy_flipper_set_widget(
  221. Widget **widget,
  222. int32_t view_id,
  223. char *text,
  224. uint32_t(previous_callback)(void *),
  225. ViewDispatcher **view_dispatcher)
  226. {
  227. if (!widget)
  228. {
  229. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_widget");
  230. return false;
  231. }
  232. *widget = widget_alloc();
  233. if (!*widget)
  234. {
  235. FURI_LOG_E(EASY_TAG, "Failed to allocate Widget");
  236. return false;
  237. }
  238. if (text)
  239. {
  240. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, text);
  241. }
  242. if (previous_callback)
  243. {
  244. view_set_previous_callback(widget_get_view(*widget), previous_callback);
  245. }
  246. view_dispatcher_add_view(*view_dispatcher, view_id, widget_get_view(*widget));
  247. return true;
  248. }
  249. /**
  250. * @brief Initialize a VariableItemList object
  251. * @note This does not set the items in the VariableItemList
  252. * @param variable_item_list The VariableItemList object to initialize
  253. * @param view_id The ID/Index of the view
  254. * @param enter_callback The enter callback function (can be set to NULL)
  255. * @param previous_callback The previous callback function (can be set to NULL)
  256. * @param view_dispatcher The ViewDispatcher object
  257. * @param context The context to pass to the enter callback (usually the app)
  258. * @return true if successful, false otherwise
  259. */
  260. bool easy_flipper_set_variable_item_list(
  261. VariableItemList **variable_item_list,
  262. int32_t view_id,
  263. void (*enter_callback)(void *, uint32_t),
  264. uint32_t(previous_callback)(void *),
  265. ViewDispatcher **view_dispatcher,
  266. void *context)
  267. {
  268. if (!variable_item_list)
  269. {
  270. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_variable_item_list");
  271. return false;
  272. }
  273. *variable_item_list = variable_item_list_alloc();
  274. if (!*variable_item_list)
  275. {
  276. FURI_LOG_E(EASY_TAG, "Failed to allocate VariableItemList");
  277. return false;
  278. }
  279. if (enter_callback)
  280. {
  281. variable_item_list_set_enter_callback(*variable_item_list, enter_callback, context);
  282. }
  283. if (previous_callback)
  284. {
  285. view_set_previous_callback(variable_item_list_get_view(*variable_item_list), previous_callback);
  286. }
  287. view_dispatcher_add_view(*view_dispatcher, view_id, variable_item_list_get_view(*variable_item_list));
  288. return true;
  289. }
  290. /**
  291. * @brief Initialize a TextInput object
  292. * @param text_input The TextInput object to initialize
  293. * @param view_id The ID/Index of the view
  294. * @param previous_callback The previous callback function (can be set to NULL)
  295. * @param view_dispatcher The ViewDispatcher object
  296. * @return true if successful, false otherwise
  297. */
  298. bool easy_flipper_set_text_input(
  299. TextInput **text_input,
  300. int32_t view_id,
  301. char *header_text,
  302. char *text_input_temp_buffer,
  303. uint32_t text_input_buffer_size,
  304. void (*result_callback)(void *),
  305. uint32_t(previous_callback)(void *),
  306. ViewDispatcher **view_dispatcher,
  307. void *context)
  308. {
  309. if (!text_input)
  310. {
  311. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_text_input");
  312. return false;
  313. }
  314. *text_input = text_input_alloc();
  315. if (!*text_input)
  316. {
  317. FURI_LOG_E(EASY_TAG, "Failed to allocate TextInput");
  318. return false;
  319. }
  320. if (previous_callback)
  321. {
  322. view_set_previous_callback(text_input_get_view(*text_input), previous_callback);
  323. }
  324. if (header_text)
  325. {
  326. text_input_set_header_text(*text_input, header_text);
  327. }
  328. if (text_input_temp_buffer && text_input_buffer_size && result_callback)
  329. {
  330. text_input_set_result_callback(*text_input, result_callback, context, text_input_temp_buffer, text_input_buffer_size, false);
  331. }
  332. view_dispatcher_add_view(*view_dispatcher, view_id, text_input_get_view(*text_input));
  333. return true;
  334. }
  335. /**
  336. * @brief Initialize a UART_TextInput object
  337. * @param uart_text_input The UART_TextInput object to initialize
  338. * @param view_id The ID/Index of the view
  339. * @param previous_callback The previous callback function (can be set to NULL)
  340. * @param view_dispatcher The ViewDispatcher object
  341. * @return true if successful, false otherwise
  342. */
  343. bool easy_flipper_set_uart_text_input(
  344. UART_TextInput **uart_text_input,
  345. int32_t view_id,
  346. char *header_text,
  347. char *uart_text_input_temp_buffer,
  348. uint32_t uart_text_input_buffer_size,
  349. void (*result_callback)(void *),
  350. uint32_t(previous_callback)(void *),
  351. ViewDispatcher **view_dispatcher,
  352. void *context)
  353. {
  354. if (!uart_text_input)
  355. {
  356. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_uart_text_input");
  357. return false;
  358. }
  359. *uart_text_input = uart_text_input_alloc();
  360. if (!*uart_text_input)
  361. {
  362. FURI_LOG_E(EASY_TAG, "Failed to allocate UART_TextInput");
  363. return false;
  364. }
  365. if (previous_callback)
  366. {
  367. view_set_previous_callback(uart_text_input_get_view(*uart_text_input), previous_callback);
  368. }
  369. if (header_text)
  370. {
  371. uart_text_input_set_header_text(*uart_text_input, header_text);
  372. }
  373. if (uart_text_input_temp_buffer && uart_text_input_buffer_size && result_callback)
  374. {
  375. uart_text_input_set_result_callback(*uart_text_input, result_callback, context, uart_text_input_temp_buffer, uart_text_input_buffer_size, false);
  376. }
  377. view_dispatcher_add_view(*view_dispatcher, view_id, uart_text_input_get_view(*uart_text_input));
  378. return true;
  379. }
  380. /**
  381. * @brief Initialize a DialogEx object
  382. * @param dialog_ex The DialogEx object to initialize
  383. * @param view_id The ID/Index of the view
  384. * @param header The header of the dialog
  385. * @param header_x The x coordinate of the header
  386. * @param header_y The y coordinate of the header
  387. * @param text The text of the dialog
  388. * @param text_x The x coordinate of the dialog
  389. * @param text_y The y coordinate of the dialog
  390. * @param left_button_text The text of the left button
  391. * @param right_button_text The text of the right button
  392. * @param center_button_text The text of the center button
  393. * @param result_callback The result callback function
  394. * @param previous_callback The previous callback function (can be set to NULL)
  395. * @param view_dispatcher The ViewDispatcher object
  396. * @param context The context to pass to the result callback
  397. * @return true if successful, false otherwise
  398. */
  399. bool easy_flipper_set_dialog_ex(
  400. DialogEx **dialog_ex,
  401. int32_t view_id,
  402. char *header,
  403. uint16_t header_x,
  404. uint16_t header_y,
  405. char *text,
  406. uint16_t text_x,
  407. uint16_t text_y,
  408. char *left_button_text,
  409. char *right_button_text,
  410. char *center_button_text,
  411. void (*result_callback)(DialogExResult, void *),
  412. uint32_t(previous_callback)(void *),
  413. ViewDispatcher **view_dispatcher,
  414. void *context)
  415. {
  416. if (!dialog_ex)
  417. {
  418. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_dialog_ex");
  419. return false;
  420. }
  421. *dialog_ex = dialog_ex_alloc();
  422. if (!*dialog_ex)
  423. {
  424. FURI_LOG_E(EASY_TAG, "Failed to allocate DialogEx");
  425. return false;
  426. }
  427. if (header)
  428. {
  429. dialog_ex_set_header(*dialog_ex, header, header_x, header_y, AlignLeft, AlignTop);
  430. }
  431. if (text)
  432. {
  433. dialog_ex_set_text(*dialog_ex, text, text_x, text_y, AlignLeft, AlignTop);
  434. }
  435. if (left_button_text)
  436. {
  437. dialog_ex_set_left_button_text(*dialog_ex, left_button_text);
  438. }
  439. if (right_button_text)
  440. {
  441. dialog_ex_set_right_button_text(*dialog_ex, right_button_text);
  442. }
  443. if (center_button_text)
  444. {
  445. dialog_ex_set_center_button_text(*dialog_ex, center_button_text);
  446. }
  447. if (result_callback)
  448. {
  449. dialog_ex_set_result_callback(*dialog_ex, result_callback);
  450. }
  451. if (previous_callback)
  452. {
  453. view_set_previous_callback(dialog_ex_get_view(*dialog_ex), previous_callback);
  454. }
  455. if (context)
  456. {
  457. dialog_ex_set_context(*dialog_ex, context);
  458. }
  459. view_dispatcher_add_view(*view_dispatcher, view_id, dialog_ex_get_view(*dialog_ex));
  460. return true;
  461. }
  462. /**
  463. * @brief Initialize a Popup object
  464. * @param popup The Popup object to initialize
  465. * @param view_id The ID/Index of the view
  466. * @param header The header of the dialog
  467. * @param header_x The x coordinate of the header
  468. * @param header_y The y coordinate of the header
  469. * @param text The text of the dialog
  470. * @param text_x The x coordinate of the dialog
  471. * @param text_y The y coordinate of the dialog
  472. * @param result_callback The result callback function
  473. * @param previous_callback The previous callback function (can be set to NULL)
  474. * @param view_dispatcher The ViewDispatcher object
  475. * @param context The context to pass to the result callback
  476. * @return true if successful, false otherwise
  477. */
  478. bool easy_flipper_set_popup(
  479. Popup **popup,
  480. int32_t view_id,
  481. char *header,
  482. uint16_t header_x,
  483. uint16_t header_y,
  484. char *text,
  485. uint16_t text_x,
  486. uint16_t text_y,
  487. void (*result_callback)(void *),
  488. uint32_t(previous_callback)(void *),
  489. ViewDispatcher **view_dispatcher,
  490. void *context)
  491. {
  492. if (!popup)
  493. {
  494. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_popup");
  495. return false;
  496. }
  497. *popup = popup_alloc();
  498. if (!*popup)
  499. {
  500. FURI_LOG_E(EASY_TAG, "Failed to allocate Popup");
  501. return false;
  502. }
  503. if (header)
  504. {
  505. popup_set_header(*popup, header, header_x, header_y, AlignLeft, AlignTop);
  506. }
  507. if (text)
  508. {
  509. popup_set_text(*popup, text, text_x, text_y, AlignLeft, AlignTop);
  510. }
  511. if (result_callback)
  512. {
  513. popup_set_callback(*popup, result_callback);
  514. }
  515. if (previous_callback)
  516. {
  517. view_set_previous_callback(popup_get_view(*popup), previous_callback);
  518. }
  519. if (context)
  520. {
  521. popup_set_context(*popup, context);
  522. }
  523. view_dispatcher_add_view(*view_dispatcher, view_id, popup_get_view(*popup));
  524. return true;
  525. }
  526. /**
  527. * @brief Initialize a Loading object
  528. * @param loading The Loading object to initialize
  529. * @param view_id The ID/Index of the view
  530. * @param previous_callback The previous callback function (can be set to NULL)
  531. * @param view_dispatcher The ViewDispatcher object
  532. * @return true if successful, false otherwise
  533. */
  534. bool easy_flipper_set_loading(
  535. Loading **loading,
  536. int32_t view_id,
  537. uint32_t(previous_callback)(void *),
  538. ViewDispatcher **view_dispatcher)
  539. {
  540. if (!loading)
  541. {
  542. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_loading");
  543. return false;
  544. }
  545. *loading = loading_alloc();
  546. if (!*loading)
  547. {
  548. FURI_LOG_E(EASY_TAG, "Failed to allocate Loading");
  549. return false;
  550. }
  551. if (previous_callback)
  552. {
  553. view_set_previous_callback(loading_get_view(*loading), previous_callback);
  554. }
  555. view_dispatcher_add_view(*view_dispatcher, view_id, loading_get_view(*loading));
  556. return true;
  557. }
  558. /**
  559. * @brief Set a char butter to a FuriString
  560. * @param furi_string The FuriString object
  561. * @param buffer The buffer to copy the string to
  562. * @return true if successful, false otherwise
  563. */
  564. bool easy_flipper_set_char_to_furi_string(FuriString **furi_string, char *buffer)
  565. {
  566. if (!furi_string)
  567. {
  568. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_buffer_to_furi_string");
  569. return false;
  570. }
  571. *furi_string = furi_string_alloc();
  572. if (!furi_string)
  573. {
  574. FURI_LOG_E(EASY_TAG, "Failed to allocate FuriString");
  575. return false;
  576. }
  577. furi_string_set_str(*furi_string, buffer);
  578. return true;
  579. }
  580. #endif // EASY_FLIPPER_H