pokemon_app.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <furi_hal_light.h>
  2. #include <pokemon_icons.h>
  3. #include "pokemon_app.h"
  4. #include "pokemon_data.h"
  5. #include "scenes/pokemon_menu.h"
  6. #include "views/trade.h"
  7. #include "views/select_pokemon.h"
  8. #include "pokemon_char_encode.h"
  9. #include <expansion/expansion.h>
  10. PokemonFap* pokemon_alloc() {
  11. PokemonFap* pokemon_fap = (PokemonFap*)malloc(sizeof(PokemonFap));
  12. // View dispatcher
  13. pokemon_fap->view_dispatcher = view_dispatcher_alloc();
  14. view_dispatcher_enable_queue(pokemon_fap->view_dispatcher);
  15. view_dispatcher_set_event_callback_context(pokemon_fap->view_dispatcher, pokemon_fap);
  16. view_dispatcher_attach_to_gui(
  17. pokemon_fap->view_dispatcher,
  18. (Gui*)furi_record_open(RECORD_GUI),
  19. ViewDispatcherTypeFullscreen);
  20. // Set up defaults
  21. memcpy(&pokemon_fap->pins, &common_pinouts[PINOUT_ORIGINAL], sizeof(struct gblink_pins));
  22. /* Set up gui modules used. It would be nice if these could be allocated and
  23. * freed as needed, however, the scene manager still requires pointers that
  24. * get set up as a part of the scene. Therefore, individual scene's exit
  25. * callbacks cannot free the buffer.
  26. *
  27. * In order to do this properly, I think each scene, or maybe common to all
  28. * scenes, would end up needing to set a delayed callback of some kind. But
  29. * I'm not sure how to guarantee this gets called in a reasonable amount of
  30. * time.
  31. */
  32. pokemon_fap->text_input = text_input_alloc();
  33. pokemon_fap->submenu = submenu_alloc();
  34. pokemon_fap->variable_item_list = variable_item_list_alloc();
  35. pokemon_fap->dialog_ex = dialog_ex_alloc();
  36. // Set up menu scene
  37. pokemon_fap->scene_manager = scene_manager_alloc(&pokemon_scene_manager_handlers, pokemon_fap);
  38. view_dispatcher_add_view(
  39. pokemon_fap->view_dispatcher, AppViewMainMenu, submenu_get_view(pokemon_fap->submenu));
  40. scene_manager_next_scene(pokemon_fap->scene_manager, MainMenuScene);
  41. return pokemon_fap;
  42. }
  43. void free_app(PokemonFap* pokemon_fap) {
  44. furi_assert(pokemon_fap);
  45. view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewMainMenu);
  46. view_dispatcher_free(pokemon_fap->view_dispatcher);
  47. // Free scenes
  48. scene_manager_free(pokemon_fap->scene_manager);
  49. // Free gui modules
  50. submenu_free(pokemon_fap->submenu);
  51. text_input_free(pokemon_fap->text_input);
  52. variable_item_list_free(pokemon_fap->variable_item_list);
  53. dialog_ex_free(pokemon_fap->dialog_ex);
  54. // Close records
  55. furi_record_close(RECORD_GUI);
  56. // Free rest
  57. free(pokemon_fap);
  58. pokemon_fap = NULL;
  59. }
  60. int32_t pokemon_app(void* p) {
  61. UNUSED(p);
  62. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  63. expansion_disable(expansion);
  64. PokemonFap* pokemon_fap = pokemon_alloc();
  65. furi_hal_light_set(LightRed, 0x00);
  66. furi_hal_light_set(LightGreen, 0x00);
  67. furi_hal_light_set(LightBlue, 0x00);
  68. //switch view and run dispatcher
  69. view_dispatcher_run(pokemon_fap->view_dispatcher);
  70. // Free resources
  71. free_app(pokemon_fap);
  72. expansion_enable(expansion);
  73. furi_record_close(RECORD_EXPANSION);
  74. return 0;
  75. }