easy_flipper.h 17 KB

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