gpio_scene_start.c 3.5 KB

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