gpio_scene_start.c 3.4 KB

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