easy_flipper.h 18 KB

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