easy_flipper.h 17 KB

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