pokemon_app.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* The MALVEKE board has an esp32 which is set to TX on the flipper's default
  10. * UART pins. If this pin shows signs of something connected, assume a MALVEKE
  11. * board is being used.
  12. */
  13. static bool detect_malveke(void) {
  14. bool rc;
  15. furi_hal_gpio_init(&gpio_usart_rx, GpioModeInput, GpioPullDown, GpioSpeedVeryHigh);
  16. //furi_hal_gpio_init(&gpio_swdio, GpioModeInput, GpioPullDown, GpioSpeedVeryHigh);
  17. /* Delay a tick to let the IO pin changes settle */
  18. furi_delay_tick(1);
  19. rc = furi_hal_gpio_read(&gpio_usart_rx);
  20. /* XXX: HACK: Need to clean this up later, but, newer MALVEKE boards use the
  21. * original pinout. Using a second pin to detect if there is a pullup to
  22. * determine if this is the board in use. In the future, it looks like the
  23. * GPIO module auto-detect support might be the better way here.
  24. */
  25. if(furi_hal_gpio_read(&gpio_swdio)) rc = 0;
  26. furi_hal_gpio_init_simple(&gpio_usart_rx, GpioModeAnalog);
  27. //furi_hal_gpio_init_simple(&gpio_swdio, GpioModeAnalog);
  28. return rc;
  29. }
  30. PokemonFap* pokemon_alloc() {
  31. PokemonFap* pokemon_fap = (PokemonFap*)malloc(sizeof(PokemonFap));
  32. // View dispatcher
  33. pokemon_fap->view_dispatcher = view_dispatcher_alloc();
  34. view_dispatcher_enable_queue(pokemon_fap->view_dispatcher);
  35. view_dispatcher_set_event_callback_context(pokemon_fap->view_dispatcher, pokemon_fap);
  36. view_dispatcher_attach_to_gui(
  37. pokemon_fap->view_dispatcher,
  38. (Gui*)furi_record_open(RECORD_GUI),
  39. ViewDispatcherTypeFullscreen);
  40. // Set up defaults
  41. pokemon_fap->malveke_detected = detect_malveke();
  42. memcpy(
  43. &pokemon_fap->pins,
  44. &common_pinouts[pokemon_fap->malveke_detected],
  45. sizeof(struct gblink_pins));
  46. /* Set up gui modules used. It would be nice if these could be allocated and
  47. * freed as needed, however, the scene manager still requires pointers that
  48. * get set up as a part of the scene. Therefore, individual scene's exit
  49. * callbacks cannot free the buffer.
  50. *
  51. * In order to do this properly, I think each scene, or maybe common to all
  52. * scenes, would end up needing to set a delayed callback of some kind. But
  53. * I'm not sure how to guarantee this gets called in a reasonable amount of
  54. * time.
  55. */
  56. pokemon_fap->text_input = text_input_alloc();
  57. pokemon_fap->submenu = submenu_alloc();
  58. pokemon_fap->variable_item_list = variable_item_list_alloc();
  59. pokemon_fap->dialog_ex = dialog_ex_alloc();
  60. // Set up menu scene
  61. pokemon_fap->scene_manager = scene_manager_alloc(&pokemon_scene_manager_handlers, pokemon_fap);
  62. view_dispatcher_add_view(
  63. pokemon_fap->view_dispatcher, AppViewMainMenu, submenu_get_view(pokemon_fap->submenu));
  64. scene_manager_next_scene(pokemon_fap->scene_manager, MainMenuScene);
  65. return pokemon_fap;
  66. }
  67. void free_app(PokemonFap* pokemon_fap) {
  68. furi_assert(pokemon_fap);
  69. view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewMainMenu);
  70. view_dispatcher_free(pokemon_fap->view_dispatcher);
  71. // Free scenes
  72. scene_manager_free(pokemon_fap->scene_manager);
  73. // Free gui modules
  74. submenu_free(pokemon_fap->submenu);
  75. text_input_free(pokemon_fap->text_input);
  76. variable_item_list_free(pokemon_fap->variable_item_list);
  77. dialog_ex_free(pokemon_fap->dialog_ex);
  78. // Close records
  79. furi_record_close(RECORD_GUI);
  80. // Free rest
  81. free(pokemon_fap);
  82. pokemon_fap = NULL;
  83. }
  84. int32_t pokemon_app(void* p) {
  85. UNUSED(p);
  86. PokemonFap* pokemon_fap = pokemon_alloc();
  87. furi_hal_light_set(LightRed, 0x00);
  88. furi_hal_light_set(LightGreen, 0x00);
  89. furi_hal_light_set(LightBlue, 0x00);
  90. //switch view and run dispatcher
  91. view_dispatcher_run(pokemon_fap->view_dispatcher);
  92. // Free resources
  93. free_app(pokemon_fap);
  94. return 0;
  95. }