Minor documentation page updates.
[pub/USBasp.git] / Demos / Device / AudioOutput / AudioOutput.c
index 0f19356..4ff2e74 100644 (file)
   this software.\r
 */\r
 \r
   this software.\r
 */\r
 \r
+/** \file\r
+ *\r
+ *  Main source file for the AudioOutput demo. This file contains the main tasks of\r
+ *  the demo and is responsible for the initial application hardware configuration.\r
+ */\r
+\r
 #include "AudioOutput.h"\r
 \r
 #include "AudioOutput.h"\r
 \r
+/** LUFA Audio Class driver interface configuration and state information. This structure is\r
+ *  passed to all Audio Class driver functions, so that multiple instances of the same class\r
+ *  within a device can be differentiated from one another.\r
+ */\r
 USB_ClassInfo_Audio_t Speaker_Audio_Interface =\r
        {\r
 USB_ClassInfo_Audio_t Speaker_Audio_Interface =\r
        {\r
-               .InterfaceNumber       = 0,\r
+               .StreamingInterfaceNumber = 1,\r
 \r
 \r
-               .DataOUTEndpointNumber = AUDIO_STREAM_EPNUM,\r
-               .DataOUTEndpointSize   = AUDIO_STREAM_EPSIZE,\r
+               .DataOUTEndpointNumber    = AUDIO_STREAM_EPNUM,\r
+               .DataOUTEndpointSize      = AUDIO_STREAM_EPSIZE,\r
        };\r
 \r
        };\r
 \r
+/** Main program entry point. This routine contains the overall program flow, including initial\r
+ *  setup of all components and the main program loop.\r
+ */\r
 int main(void)\r
 {\r
        SetupHardware();\r
 int main(void)\r
 {\r
        SetupHardware();\r
@@ -53,6 +66,7 @@ int main(void)
        }\r
 }\r
 \r
        }\r
 }\r
 \r
+/** Configures the board hardware and chip peripherals for the demo's functionality. */\r
 void SetupHardware(void)\r
 {\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
 void SetupHardware(void)\r
 {\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
@@ -67,6 +81,9 @@ void SetupHardware(void)
        USB_Init();\r
 }\r
 \r
        USB_Init();\r
 }\r
 \r
+/** Processes the next audio sample by reading the last ADC conversion and writing it to the audio\r
+ *  interface, each time the sample reload timer period elapses to give a constant sample rate.\r
+ */\r
 void ProcessNextSample(void)\r
 {\r
        if ((TIFR0 & (1 << OCF0A)) && USB_Audio_IsSampleReceived(&Speaker_Audio_Interface))\r
 void ProcessNextSample(void)\r
 {\r
        if ((TIFR0 & (1 << OCF0A)) && USB_Audio_IsSampleReceived(&Speaker_Audio_Interface))\r
@@ -82,10 +99,10 @@ void ProcessNextSample(void)
                int8_t  LeftSample_8Bit   = (LeftSample_16Bit  >> 8);\r
                int8_t  RightSample_8Bit  = (RightSample_16Bit >> 8);\r
                        \r
                int8_t  LeftSample_8Bit   = (LeftSample_16Bit  >> 8);\r
                int8_t  RightSample_8Bit  = (RightSample_16Bit >> 8);\r
                        \r
-#if defined(AUDIO_OUT_MONO)\r
                /* Mix the two channels together to produce a mono, 8-bit sample */\r
                int8_t  MixedSample_8Bit  = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);\r
 \r
                /* Mix the two channels together to produce a mono, 8-bit sample */\r
                int8_t  MixedSample_8Bit  = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);\r
 \r
+#if defined(AUDIO_OUT_MONO)\r
                /* Load the sample into the PWM timer channel */\r
                OCRxA = ((uint8_t)MixedSample_8Bit ^ (1 << 7));\r
 #elif defined(AUDIO_OUT_STEREO)\r
                /* Load the sample into the PWM timer channel */\r
                OCRxA = ((uint8_t)MixedSample_8Bit ^ (1 << 7));\r
 #elif defined(AUDIO_OUT_STEREO)\r
@@ -93,42 +110,32 @@ void ProcessNextSample(void)
                OCRxA = ((uint8_t)LeftSample_8Bit  ^ (1 << 7));\r
                OCRxB = ((uint8_t)RightSample_8Bit ^ (1 << 7));\r
 #elif defined(AUDIO_OUT_PORTC)\r
                OCRxA = ((uint8_t)LeftSample_8Bit  ^ (1 << 7));\r
                OCRxB = ((uint8_t)RightSample_8Bit ^ (1 << 7));\r
 #elif defined(AUDIO_OUT_PORTC)\r
-               /* Mix the two channels together to produce a mono, 8-bit sample */\r
-               int8_t  MixedSample_8Bit  = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);\r
-\r
                PORTC = MixedSample_8Bit;\r
 #else\r
                uint8_t LEDMask = LEDS_NO_LEDS;\r
 \r
                PORTC = MixedSample_8Bit;\r
 #else\r
                uint8_t LEDMask = LEDS_NO_LEDS;\r
 \r
-               /* Make left channel positive (absolute) */\r
-               if (LeftSample_8Bit < 0)\r
-                 LeftSample_8Bit = -LeftSample_8Bit;\r
-\r
-               /* Make right channel positive (absolute) */\r
-               if (RightSample_8Bit < 0)\r
-                 RightSample_8Bit = -RightSample_8Bit;\r
+               /* Make mixed sample value positive (absolute) */\r
+               if (MixedSample_8Bit < 0)\r
+                 MixedSample_8Bit = -MixedSample_8Bit;\r
 \r
 \r
-               /* Set first LED based on sample value */\r
-               if (LeftSample_8Bit < ((128 / 8) * 1))\r
-                 LEDMask |= LEDS_LED2;\r
-               else if (LeftSample_8Bit < ((128 / 8) * 3))\r
-                 LEDMask |= (LEDS_LED1 | LEDS_LED2);\r
-               else\r
+               if (MixedSample_8Bit > ((128 / 8) * 1))\r
                  LEDMask |= LEDS_LED1;\r
                  LEDMask |= LEDS_LED1;\r
+                 \r
+               if (MixedSample_8Bit > ((128 / 8) * 2))\r
+                 LEDMask |= LEDS_LED2;\r
+                 \r
+               if (MixedSample_8Bit > ((128 / 8) * 3))\r
+                 LEDMask |= LEDS_LED3;\r
 \r
 \r
-               /* Set second LED based on sample value */\r
-               if (RightSample_8Bit < ((128 / 8) * 1))\r
+               if (MixedSample_8Bit > ((128 / 8) * 4))\r
                  LEDMask |= LEDS_LED4;\r
                  LEDMask |= LEDS_LED4;\r
-               else if (RightSample_8Bit < ((128 / 8) * 3))\r
-                 LEDMask |= (LEDS_LED3 | LEDS_LED4);\r
-               else\r
-                 LEDMask |= LEDS_LED3;\r
-                 \r
+\r
                LEDs_SetAllLEDs(LEDMask);\r
 #endif\r
        }\r
 }\r
 \r
                LEDs_SetAllLEDs(LEDMask);\r
 #endif\r
        }\r
 }\r
 \r
+/** Event handler for the library USB Connection event. */\r
 void EVENT_USB_Connect(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
 void EVENT_USB_Connect(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
@@ -157,16 +164,16 @@ void EVENT_USB_Connect(void)
 #endif \r
 }\r
 \r
 #endif \r
 }\r
 \r
-/** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via\r
- *  the status LEDs, disables the sample update and PWM output timers and stops the USB and Audio management tasks.\r
- */\r
+/** Event handler for the library USB Disconnection event. */\r
 void EVENT_USB_Disconnect(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
 void EVENT_USB_Disconnect(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
-       /* Stop the timers */\r
+       /* Stop the sample reload timer */\r
        TCCR0B = 0;\r
        TCCR0B = 0;\r
+\r
 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))\r
 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))\r
+       /* Stop the PWM generation timer */\r
        TCCRxB = 0;\r
 #endif         \r
 \r
        TCCRxB = 0;\r
 #endif         \r
 \r
@@ -182,6 +189,7 @@ void EVENT_USB_Disconnect(void)
 #endif\r
 }\r
 \r
 #endif\r
 }\r
 \r
+/** Event handler for the library USB Configuration Changed event. */\r
 void EVENT_USB_ConfigurationChanged(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
 void EVENT_USB_ConfigurationChanged(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
@@ -190,6 +198,7 @@ void EVENT_USB_ConfigurationChanged(void)
          LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 }\r
 \r
          LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 }\r
 \r
+/** Event handler for the library USB Unhandled Control Packet event. */\r
 void EVENT_USB_UnhandledControlPacket(void)\r
 {\r
        USB_Audio_ProcessControlPacket(&Speaker_Audio_Interface);\r
 void EVENT_USB_UnhandledControlPacket(void)\r
 {\r
        USB_Audio_ProcessControlPacket(&Speaker_Audio_Interface);\r