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