easy_flipper.c 17 KB

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