usb_test.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/view.h>
  4. #include <gui/view_dispatcher.h>
  5. #include <gui/modules/submenu.h>
  6. #include <gui/gui.h>
  7. typedef struct {
  8. Gui* gui;
  9. ViewDispatcher* view_dispatcher;
  10. Submenu* submenu;
  11. } UsbTestApp;
  12. typedef enum {
  13. UsbTestSubmenuIndexEnable,
  14. UsbTestSubmenuIndexDisable,
  15. UsbTestSubmenuIndexRestart,
  16. UsbTestSubmenuIndexVcpSingle,
  17. UsbTestSubmenuIndexVcpDual,
  18. UsbTestSubmenuIndexHid,
  19. UsbTestSubmenuIndexHidU2F,
  20. } SubmenuIndex;
  21. void usb_test_submenu_callback(void* context, uint32_t index) {
  22. furi_assert(context);
  23. //UsbTestApp* app = context;
  24. if(index == UsbTestSubmenuIndexEnable) {
  25. furi_hal_usb_enable();
  26. } else if(index == UsbTestSubmenuIndexDisable) {
  27. furi_hal_usb_disable();
  28. } else if(index == UsbTestSubmenuIndexRestart) {
  29. furi_hal_usb_reinit();
  30. } else if(index == UsbTestSubmenuIndexVcpSingle) {
  31. furi_hal_usb_set_config(&usb_cdc_single);
  32. } else if(index == UsbTestSubmenuIndexVcpDual) {
  33. furi_hal_usb_set_config(&usb_cdc_dual);
  34. } else if(index == UsbTestSubmenuIndexHid) {
  35. furi_hal_usb_set_config(&usb_hid);
  36. } else if(index == UsbTestSubmenuIndexHidU2F) {
  37. furi_hal_usb_set_config(&usb_hid_u2f);
  38. }
  39. }
  40. uint32_t usb_test_exit(void* context) {
  41. return VIEW_NONE;
  42. }
  43. UsbTestApp* usb_test_app_alloc() {
  44. UsbTestApp* app = malloc(sizeof(UsbTestApp));
  45. // Gui
  46. app->gui = furi_record_open("gui");
  47. // View dispatcher
  48. app->view_dispatcher = view_dispatcher_alloc();
  49. view_dispatcher_enable_queue(app->view_dispatcher);
  50. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  51. // Views
  52. app->submenu = submenu_alloc();
  53. submenu_add_item(
  54. app->submenu, "Enable", UsbTestSubmenuIndexEnable, usb_test_submenu_callback, app);
  55. submenu_add_item(
  56. app->submenu, "Disable", UsbTestSubmenuIndexDisable, usb_test_submenu_callback, app);
  57. submenu_add_item(
  58. app->submenu, "Restart", UsbTestSubmenuIndexRestart, usb_test_submenu_callback, app);
  59. submenu_add_item(
  60. app->submenu, "Single VCP", UsbTestSubmenuIndexVcpSingle, usb_test_submenu_callback, app);
  61. submenu_add_item(
  62. app->submenu, "Dual VCP", UsbTestSubmenuIndexVcpDual, usb_test_submenu_callback, app);
  63. submenu_add_item(
  64. app->submenu, "HID KB+Mouse", UsbTestSubmenuIndexHid, usb_test_submenu_callback, app);
  65. submenu_add_item(
  66. app->submenu, "HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
  67. view_set_previous_callback(submenu_get_view(app->submenu), usb_test_exit);
  68. view_dispatcher_add_view(app->view_dispatcher, 0, submenu_get_view(app->submenu));
  69. // Switch to menu
  70. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  71. return app;
  72. }
  73. void usb_test_app_free(UsbTestApp* app) {
  74. furi_assert(app);
  75. // Free views
  76. view_dispatcher_remove_view(app->view_dispatcher, 0);
  77. submenu_free(app->submenu);
  78. view_dispatcher_free(app->view_dispatcher);
  79. // Close gui record
  80. furi_record_close("gui");
  81. app->gui = NULL;
  82. // Free rest
  83. free(app);
  84. }
  85. int32_t usb_test_app(void* p) {
  86. UsbTestApp* app = usb_test_app_alloc();
  87. view_dispatcher_run(app->view_dispatcher);
  88. usb_test_app_free(app);
  89. return 0;
  90. }