LEDNotifier.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 LEDNotfier project. This file contains the main tasks of
  29. * the project and is responsible for the initial application hardware configuration.
  30. */
  31. #include "LEDNotifier.h"
  32. /** LUFA CDC Class driver interface configuration and state information. This structure is
  33. * passed to all CDC Class driver functions, so that multiple instances of the same class
  34. * within a device can be differentiated from one another.
  35. */
  36. USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
  37. {
  38. .Config =
  39. {
  40. .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
  41. .DataINEndpoint =
  42. {
  43. .Address = CDC_TX_EPADDR,
  44. .Size = CDC_TXRX_EPSIZE,
  45. .Banks = 1,
  46. },
  47. .DataOUTEndpoint =
  48. {
  49. .Address = CDC_RX_EPADDR,
  50. .Size = CDC_TXRX_EPSIZE,
  51. .Banks = 1,
  52. },
  53. .NotificationEndpoint =
  54. {
  55. .Address = CDC_NOTIFICATION_EPADDR,
  56. .Size = CDC_NOTIFICATION_EPSIZE,
  57. .Banks = 1,
  58. },
  59. },
  60. };
  61. /** Counter for the software PWM. */
  62. static volatile uint8_t SoftPWM_Count;
  63. /** Duty cycle for the first software PWM channel. */
  64. static volatile uint8_t SoftPWM_Channel1_Duty;
  65. /** Duty cycle for the second software PWM channel. */
  66. static volatile uint8_t SoftPWM_Channel2_Duty;
  67. /** Duty cycle for the third software PWM channel. */
  68. static volatile uint8_t SoftPWM_Channel3_Duty;
  69. /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
  70. * used like any regular character stream in the C APIs.
  71. */
  72. static FILE USBSerialStream;
  73. /** Interrupt handler for managing the software PWM channels for the LEDs */
  74. ISR(TIMER0_COMPA_vect, ISR_BLOCK)
  75. {
  76. uint8_t LEDMask = LEDS_ALL_LEDS;
  77. if (++SoftPWM_Count == 0b00011111)
  78. SoftPWM_Count = 0;
  79. if (SoftPWM_Count >= SoftPWM_Channel1_Duty)
  80. LEDMask &= ~LEDS_LED1;
  81. if (SoftPWM_Count >= SoftPWM_Channel2_Duty)
  82. LEDMask &= ~LEDS_LED2;
  83. if (SoftPWM_Count >= SoftPWM_Channel3_Duty)
  84. LEDMask &= ~LEDS_LED3;
  85. LEDs_SetAllLEDs(LEDMask);
  86. }
  87. /** Main program entry point. This routine contains the overall program flow, including initial
  88. * setup of all components and the main program loop.
  89. */
  90. int main(void)
  91. {
  92. SetupHardware();
  93. /* Create a regular blocking character stream for the interface so that it can be used with the stdio.h functions */
  94. CDC_Device_CreateBlockingStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
  95. GlobalInterruptEnable();
  96. for (;;)
  97. {
  98. /* Read in next LED colour command from the host */
  99. uint8_t ColourUpdate = fgetc(&USBSerialStream);
  100. /* Top 3 bits select the LED, bottom 5 control the brightness */
  101. uint8_t Channel = (ColourUpdate & 0b11100000);
  102. uint8_t Duty = (ColourUpdate & 0b00011111);
  103. if (Channel & (1 << 5))
  104. SoftPWM_Channel1_Duty = Duty;
  105. if (Channel & (1 << 6))
  106. SoftPWM_Channel2_Duty = Duty;
  107. if (Channel & (1 << 7))
  108. SoftPWM_Channel3_Duty = Duty;
  109. fputc(&USBSerialStream, ColourUpdate);
  110. CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
  111. USB_USBTask();
  112. }
  113. }
  114. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  115. void SetupHardware(void)
  116. {
  117. #if (ARCH == ARCH_AVR8)
  118. /* Disable watchdog if enabled by bootloader/fuses */
  119. MCUSR &= ~(1 << WDRF);
  120. wdt_disable();
  121. /* Disable clock division */
  122. clock_prescale_set(clock_div_1);
  123. #endif
  124. /* Hardware Initialization */
  125. LEDs_Init();
  126. USB_Init();
  127. /* Timer Initialization */
  128. OCR0A = 100;
  129. TCCR0A = (1 << WGM01);
  130. TCCR0B = (1 << CS00);
  131. TIMSK0 = (1 << OCIE0A);
  132. }
  133. /** Event handler for the library USB Configuration Changed event. */
  134. void EVENT_USB_Device_ConfigurationChanged(void)
  135. {
  136. CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
  137. }
  138. /** Event handler for the library USB Control Request reception event. */
  139. void EVENT_USB_Device_ControlRequest(void)
  140. {
  141. CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
  142. }