gpio_scene_start.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "../gpio_app_i.h"
  2. #include "furi_hal_power.h"
  3. enum GpioItem {
  4. GpioItemUsbUart,
  5. GpioItemTest,
  6. GpioItemOtg,
  7. };
  8. enum GpioOtg {
  9. GpioOtgOff,
  10. GpioOtgOn,
  11. GpioOtgSettingsNum,
  12. };
  13. const char* const gpio_otg_text[GpioOtgSettingsNum] = {
  14. "Off",
  15. "On",
  16. };
  17. static void gpio_scene_start_var_list_enter_callback(void* context, uint32_t index) {
  18. furi_assert(context);
  19. GpioApp* app = context;
  20. if(index == GpioItemTest) {
  21. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventManualConrol);
  22. } else if(index == GpioItemUsbUart) {
  23. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventUsbUart);
  24. }
  25. }
  26. static void gpio_scene_start_var_list_change_callback(VariableItem* item) {
  27. GpioApp* app = variable_item_get_context(item);
  28. uint8_t index = variable_item_get_current_value_index(item);
  29. variable_item_set_current_value_text(item, gpio_otg_text[index]);
  30. if(index == GpioOtgOff) {
  31. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOff);
  32. } else if(index == GpioOtgOn) {
  33. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOn);
  34. }
  35. }
  36. void gpio_scene_start_on_enter(void* context) {
  37. GpioApp* app = context;
  38. VariableItemList* var_item_list = app->var_item_list;
  39. VariableItem* item;
  40. variable_item_list_set_enter_callback(
  41. var_item_list, gpio_scene_start_var_list_enter_callback, app);
  42. variable_item_list_add(var_item_list, "USB-UART bridge", 0, NULL, NULL);
  43. variable_item_list_add(var_item_list, "GPIO manual control", 0, NULL, NULL);
  44. item = variable_item_list_add(
  45. var_item_list,
  46. "5V on GPIO",
  47. GpioOtgSettingsNum,
  48. gpio_scene_start_var_list_change_callback,
  49. app);
  50. if(furi_hal_power_is_otg_enabled()) {
  51. variable_item_set_current_value_index(item, GpioOtgOn);
  52. variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOn]);
  53. } else {
  54. variable_item_set_current_value_index(item, GpioOtgOff);
  55. variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOff]);
  56. }
  57. variable_item_list_set_selected_item(
  58. var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioSceneStart));
  59. view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewVarItemList);
  60. }
  61. bool gpio_scene_start_on_event(void* context, SceneManagerEvent event) {
  62. GpioApp* app = context;
  63. bool consumed = false;
  64. if(event.type == SceneManagerEventTypeCustom) {
  65. if(event.event == GpioStartEventOtgOn) {
  66. furi_hal_power_enable_otg();
  67. } else if(event.event == GpioStartEventOtgOff) {
  68. furi_hal_power_disable_otg();
  69. } else if(event.event == GpioStartEventManualConrol) {
  70. scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemTest);
  71. scene_manager_next_scene(app->scene_manager, GpioSceneTest);
  72. } else if(event.event == GpioStartEventUsbUart) {
  73. scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemUsbUart);
  74. scene_manager_next_scene(app->scene_manager, GpioSceneUsbUart);
  75. }
  76. consumed = true;
  77. }
  78. return consumed;
  79. }
  80. void gpio_scene_start_on_exit(void* context) {
  81. GpioApp* app = context;
  82. variable_item_list_reset(app->var_item_list);
  83. }