gpio_scene_start.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "../gpio_app_i.h"
  2. #include "furi_hal_power.h"
  3. #include "furi_hal_usb.h"
  4. #include <dolphin/dolphin.h>
  5. enum GpioItem {
  6. GpioItemUsbUart,
  7. GpioItemTest,
  8. GpioItemReader,
  9. GpioItemOtg,
  10. };
  11. enum GpioOtg {
  12. GpioOtgOff,
  13. GpioOtgOn,
  14. GpioOtgSettingsNum,
  15. };
  16. const char* const gpio_otg_text[GpioOtgSettingsNum] = {
  17. "OFF",
  18. "ON",
  19. };
  20. static void gpio_scene_start_var_list_enter_callback(void* context, uint32_t index) {
  21. furi_assert(context);
  22. GpioApp* app = context;
  23. if(index == GpioItemTest) {
  24. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventManualControl);
  25. } else if(index == GpioItemUsbUart) {
  26. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventUsbUart);
  27. } else if(index == GpioItemReader) {
  28. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventReader);
  29. }
  30. }
  31. static void gpio_scene_start_var_list_change_callback(VariableItem* item) {
  32. GpioApp* app = variable_item_get_context(item);
  33. uint8_t index = variable_item_get_current_value_index(item);
  34. variable_item_set_current_value_text(item, gpio_otg_text[index]);
  35. if(index == GpioOtgOff) {
  36. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOff);
  37. } else if(index == GpioOtgOn) {
  38. view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOn);
  39. }
  40. }
  41. void gpio_scene_start_on_enter(void* context) {
  42. GpioApp* app = context;
  43. VariableItemList* var_item_list = app->var_item_list;
  44. VariableItem* item;
  45. variable_item_list_set_enter_callback(
  46. var_item_list, gpio_scene_start_var_list_enter_callback, app);
  47. variable_item_list_add(var_item_list, "USB-UART Bridge", 0, NULL, NULL);
  48. variable_item_list_add(var_item_list, "GPIO Manual Control", 0, NULL, NULL);
  49. variable_item_list_add(var_item_list, "GPIO Manual Read", 0, NULL, NULL);
  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_set_selected_item(
  64. var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioSceneStart));
  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 == GpioStartEventOtgOn) {
  72. furi_hal_power_enable_otg();
  73. } else if(event.event == GpioStartEventOtgOff) {
  74. furi_hal_power_disable_otg();
  75. } else if(event.event == GpioStartEventManualControl) {
  76. scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemTest);
  77. scene_manager_next_scene(app->scene_manager, GpioSceneTest);
  78. } else if(event.event == GpioStartEventReader) {
  79. scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemReader);
  80. scene_manager_next_scene(app->scene_manager, GpioSceneReader);
  81. } else if(event.event == GpioStartEventUsbUart) {
  82. scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemUsbUart);
  83. if(!furi_hal_usb_is_locked()) {
  84. dolphin_deed(DolphinDeedGpioUartBridge);
  85. scene_manager_next_scene(app->scene_manager, GpioSceneUsbUart);
  86. } else {
  87. scene_manager_next_scene(app->scene_manager, GpioSceneUsbUartCloseRpc);
  88. }
  89. }
  90. consumed = true;
  91. }
  92. return consumed;
  93. }
  94. void gpio_scene_start_on_exit(void* context) {
  95. GpioApp* app = context;
  96. variable_item_list_reset(app->var_item_list);
  97. }