usb_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. UsbTestSubmenuIndexVcpSingle,
  16. UsbTestSubmenuIndexVcpDual,
  17. UsbTestSubmenuIndexHid,
  18. UsbTestSubmenuIndexHidU2F,
  19. } SubmenuIndex;
  20. void usb_test_submenu_callback(void* context, uint32_t index) {
  21. furi_assert(context);
  22. //UsbTestApp* app = context;
  23. if(index == UsbTestSubmenuIndexEnable) {
  24. furi_hal_usb_enable();
  25. } else if(index == UsbTestSubmenuIndexDisable) {
  26. furi_hal_usb_disable();
  27. } else if(index == UsbTestSubmenuIndexVcpSingle) {
  28. furi_hal_usb_set_config(UsbModeVcpSingle);
  29. } else if(index == UsbTestSubmenuIndexVcpDual) {
  30. furi_hal_usb_set_config(UsbModeVcpDual);
  31. } else if(index == UsbTestSubmenuIndexHid) {
  32. furi_hal_usb_set_config(UsbModeHid);
  33. } else if(index == UsbTestSubmenuIndexHidU2F) {
  34. //furi_hal_usb_set_config(UsbModeU2F);
  35. }
  36. }
  37. uint32_t usb_test_exit(void* context) {
  38. return VIEW_NONE;
  39. }
  40. UsbTestApp* usb_test_app_alloc() {
  41. UsbTestApp* app = furi_alloc(sizeof(UsbTestApp));
  42. // Gui
  43. app->gui = furi_record_open("gui");
  44. // View dispatcher
  45. app->view_dispatcher = view_dispatcher_alloc();
  46. view_dispatcher_enable_queue(app->view_dispatcher);
  47. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  48. // Views
  49. app->submenu = submenu_alloc();
  50. submenu_add_item(
  51. app->submenu, "Enable", UsbTestSubmenuIndexEnable, usb_test_submenu_callback, app);
  52. submenu_add_item(
  53. app->submenu, "Disable", UsbTestSubmenuIndexDisable, usb_test_submenu_callback, app);
  54. submenu_add_item(
  55. app->submenu, "Single VCP", UsbTestSubmenuIndexVcpSingle, usb_test_submenu_callback, app);
  56. submenu_add_item(
  57. app->submenu, "Dual VCP", UsbTestSubmenuIndexVcpDual, usb_test_submenu_callback, app);
  58. submenu_add_item(
  59. app->submenu, "HID KB+Mouse", UsbTestSubmenuIndexHid, usb_test_submenu_callback, app);
  60. submenu_add_item(
  61. app->submenu, "TODO: HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
  62. view_set_previous_callback(submenu_get_view(app->submenu), usb_test_exit);
  63. view_dispatcher_add_view(app->view_dispatcher, 0, submenu_get_view(app->submenu));
  64. // Switch to menu
  65. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  66. return app;
  67. }
  68. void usb_test_app_free(UsbTestApp* app) {
  69. furi_assert(app);
  70. // Free views
  71. view_dispatcher_remove_view(app->view_dispatcher, 0);
  72. submenu_free(app->submenu);
  73. view_dispatcher_free(app->view_dispatcher);
  74. // Close gui record
  75. furi_record_close("gui");
  76. app->gui = NULL;
  77. // Free rest
  78. free(app);
  79. }
  80. int32_t usb_test_app(void* p) {
  81. UsbTestApp* app = usb_test_app_alloc();
  82. view_dispatcher_run(app->view_dispatcher);
  83. usb_test_app_free(app);
  84. return 0;
  85. }