easy_flipper.h 17 KB

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