main.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. /* This example current worked and tested with following controller
  26. * - Sony DualShock 4 [CUH-ZCT2x] VID = 0x054c, PID = 0x09cc
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "bsp/board.h"
  32. #include "tusb.h"
  33. //--------------------------------------------------------------------+
  34. // MACRO CONSTANT TYPEDEF PROTYPES
  35. //--------------------------------------------------------------------+
  36. void led_blinking_task(void);
  37. extern void cdc_task(void);
  38. extern void hid_app_task(void);
  39. /*------------- MAIN -------------*/
  40. int main(void)
  41. {
  42. board_init();
  43. printf("TinyUSB Host HID Controller Example\r\n");
  44. printf("Note: Events only displayed for explictly supported controllers\r\n");
  45. tusb_init();
  46. while (1)
  47. {
  48. // tinyusb host task
  49. tuh_task();
  50. led_blinking_task();
  51. }
  52. return 0;
  53. }
  54. //--------------------------------------------------------------------+
  55. // TinyUSB Callbacks
  56. //--------------------------------------------------------------------+
  57. // Invoked when device is mounted (configured)
  58. void tuh_mount_cb (uint8_t dev_addr)
  59. {
  60. printf("Device attached, address = %d\r\n", dev_addr);
  61. }
  62. /// Invoked when device is unmounted (bus reset/unplugged)
  63. void tuh_umount_cb(uint8_t dev_addr)
  64. {
  65. printf("Device removed, address = %d\r\n", dev_addr);
  66. }
  67. //--------------------------------------------------------------------+
  68. // Blinking Task
  69. //--------------------------------------------------------------------+
  70. void led_blinking_task(void)
  71. {
  72. const uint32_t interval_ms = 1000;
  73. static uint32_t start_ms = 0;
  74. static bool led_state = false;
  75. // Blink every interval ms
  76. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  77. start_ms += interval_ms;
  78. board_led_write(led_state);
  79. led_state = 1 - led_state; // toggle
  80. }