usb_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <cmsis_os.h>
  8. typedef struct {
  9. Gui* gui;
  10. ViewDispatcher* view_dispatcher;
  11. Submenu* submenu;
  12. } UsbTestApp;
  13. typedef enum {
  14. UsbTestSubmenuIndexEnable,
  15. UsbTestSubmenuIndexDisable,
  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 == UsbTestSubmenuIndexVcpSingle) {
  29. furi_hal_usb_set_config(&usb_cdc_single);
  30. } else if(index == UsbTestSubmenuIndexVcpDual) {
  31. furi_hal_usb_set_config(&usb_cdc_dual);
  32. } else if(index == UsbTestSubmenuIndexHid) {
  33. furi_hal_usb_set_config(&usb_hid);
  34. } else if(index == UsbTestSubmenuIndexHidU2F) {
  35. //furi_hal_usb_set_config(UsbModeU2F);
  36. }
  37. }
  38. uint32_t usb_test_exit(void* context) {
  39. return VIEW_NONE;
  40. }
  41. UsbTestApp* usb_test_app_alloc() {
  42. UsbTestApp* app = furi_alloc(sizeof(UsbTestApp));
  43. // Gui
  44. app->gui = furi_record_open("gui");
  45. // View dispatcher
  46. app->view_dispatcher = view_dispatcher_alloc();
  47. view_dispatcher_enable_queue(app->view_dispatcher);
  48. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  49. // Views
  50. app->submenu = submenu_alloc();
  51. submenu_add_item(
  52. app->submenu, "Enable", UsbTestSubmenuIndexEnable, usb_test_submenu_callback, app);
  53. submenu_add_item(
  54. app->submenu, "Disable", UsbTestSubmenuIndexDisable, usb_test_submenu_callback, app);
  55. submenu_add_item(
  56. app->submenu, "Single VCP", UsbTestSubmenuIndexVcpSingle, usb_test_submenu_callback, app);
  57. submenu_add_item(
  58. app->submenu, "Dual VCP", UsbTestSubmenuIndexVcpDual, usb_test_submenu_callback, app);
  59. submenu_add_item(
  60. app->submenu, "HID KB+Mouse", UsbTestSubmenuIndexHid, usb_test_submenu_callback, app);
  61. submenu_add_item(
  62. app->submenu, "TODO: HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
  63. view_set_previous_callback(submenu_get_view(app->submenu), usb_test_exit);
  64. view_dispatcher_add_view(app->view_dispatcher, 0, submenu_get_view(app->submenu));
  65. // Switch to menu
  66. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  67. return app;
  68. }
  69. void usb_test_app_free(UsbTestApp* app) {
  70. furi_assert(app);
  71. // Free views
  72. view_dispatcher_remove_view(app->view_dispatcher, 0);
  73. submenu_free(app->submenu);
  74. view_dispatcher_free(app->view_dispatcher);
  75. // Close gui record
  76. furi_record_close("gui");
  77. app->gui = NULL;
  78. // Free rest
  79. free(app);
  80. }
  81. int32_t usb_test_app(void* p) {
  82. UsbTestApp* app = usb_test_app_alloc();
  83. view_dispatcher_run(app->view_dispatcher);
  84. usb_test_app_free(app);
  85. return 0;
  86. }