app_state.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "flipper.h"
  2. #include "resistors_app.h"
  3. #include "app_state.h"
  4. #include "scenes.h"
  5. App* app_alloc() {
  6. App* app = malloc(sizeof(App));
  7. app->scene_manager = scene_manager_alloc(&resistors_scene_manager_handlers, app);
  8. app->view_dispatcher = view_dispatcher_alloc();
  9. view_dispatcher_enable_queue(app->view_dispatcher);
  10. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  11. view_dispatcher_set_custom_event_callback(app->view_dispatcher, resistors_custom_callback);
  12. view_dispatcher_set_navigation_event_callback(
  13. app->view_dispatcher, resistors_back_event_callback);
  14. app->submenu = submenu_alloc();
  15. app->widget = widget_alloc();
  16. view_dispatcher_add_view(
  17. app->view_dispatcher, ResistorsSubmenuView, submenu_get_view(app->submenu));
  18. view_dispatcher_add_view(
  19. app->view_dispatcher, ResistorsEditView, widget_get_view(app->widget));
  20. return app;
  21. }
  22. AppState* app_state_alloc() {
  23. AppState* state = malloc(sizeof(AppState));
  24. return state;
  25. }
  26. void app_init_resistor(App* app, ResistorType rtype) {
  27. app->state->resistor_type = rtype;
  28. app->state->edit_selection = 0;
  29. switch(rtype) {
  30. case R3:
  31. app->state->resistor_bands[0] = BandRed;
  32. app->state->resistor_bands[1] = BandOrange;
  33. app->state->resistor_bands[2] = BandYellow;
  34. break;
  35. case R4:
  36. app->state->resistor_bands[0] = BandRed;
  37. app->state->resistor_bands[1] = BandOrange;
  38. app->state->resistor_bands[2] = BandYellow;
  39. app->state->resistor_bands[3] = BandGreen;
  40. break;
  41. case R5:
  42. app->state->resistor_bands[0] = BandRed;
  43. app->state->resistor_bands[1] = BandOrange;
  44. app->state->resistor_bands[2] = BandYellow;
  45. app->state->resistor_bands[3] = BandGreen;
  46. app->state->resistor_bands[4] = BandBlue;
  47. break;
  48. case R6:
  49. app->state->resistor_bands[0] = BandRed;
  50. app->state->resistor_bands[1] = BandOrange;
  51. app->state->resistor_bands[2] = BandYellow;
  52. app->state->resistor_bands[3] = BandGreen;
  53. app->state->resistor_bands[4] = BandBlue;
  54. app->state->resistor_bands[5] = BandPurple;
  55. break;
  56. default:
  57. FURI_LOG_E(TAG, "Unknown resistor type in app_init_resistor");
  58. app_quit(app);
  59. break;
  60. }
  61. }
  62. void app_quit(App* app) {
  63. scene_manager_stop(app->scene_manager);
  64. }
  65. void app_free(App* app) {
  66. furi_assert(app);
  67. free(app->state);
  68. view_dispatcher_remove_view(app->view_dispatcher, ResistorsSubmenuView);
  69. view_dispatcher_remove_view(app->view_dispatcher, ResistorsEditView);
  70. scene_manager_free(app->scene_manager);
  71. view_dispatcher_free(app->view_dispatcher);
  72. submenu_free(app->submenu);
  73. widget_free(app->widget);
  74. free(app);
  75. }