MouseHostDevice.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Main source file for the MouseHostDevice demo. This file contains the main tasks of
  29. * the demo and is responsible for the overall control flow of the demo.
  30. */
  31. #include "MouseHostDevice.h"
  32. /** Main program entry point. This routine configures the hardware required by the application, then
  33. * enters a loop to run the application tasks in sequence.
  34. */
  35. int main(void)
  36. {
  37. SetupHardware();
  38. puts_P(PSTR(ESC_FG_CYAN "Mouse Host/Device Demo running.\r\n" ESC_FG_WHITE));
  39. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  40. GlobalInterruptEnable();
  41. for (;;)
  42. {
  43. /* Determine which USB mode we are currently in */
  44. if (USB_CurrentMode == USB_MODE_Host)
  45. {
  46. MouseHost_Task();
  47. HID_Host_USBTask(&Mouse_HID_Host_Interface);
  48. }
  49. else
  50. {
  51. HID_Device_USBTask(&Mouse_HID_Device_Interface);
  52. }
  53. USB_USBTask();
  54. }
  55. }
  56. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  57. void SetupHardware(void)
  58. {
  59. #if (ARCH == ARCH_AVR8)
  60. /* Disable watchdog if enabled by bootloader/fuses */
  61. MCUSR &= ~(1 << WDRF);
  62. wdt_disable();
  63. /* Disable clock division */
  64. clock_prescale_set(clock_div_1);
  65. #endif
  66. /* Hardware Initialization */
  67. Serial_Init(9600, false);
  68. LEDs_Init();
  69. Joystick_Init();
  70. Buttons_Init();
  71. USB_Init(USB_MODE_UID);
  72. /* Create a stdio stream for the serial port for stdin and stdout */
  73. Serial_CreateStream(NULL);
  74. }
  75. /** Event handler for the library USB mode change event. */
  76. void EVENT_USB_UIDChange(void)
  77. {
  78. printf_P(PSTR(ESC_FG_YELLOW "UID Change to %S mode\r\n" ESC_FG_WHITE),
  79. (USB_CurrentMode == USB_MODE_Device) ? PSTR("Device") : PSTR("Host"));
  80. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  81. }