Add new Audio Class Driver Host demos.
[pub/USBasp.git] / Demos / Host / LowLevel / AudioOutputHost / AudioOutputHost.c
index 15d00c0..1fb64a1 100644 (file)
@@ -68,6 +68,8 @@ void SetupHardware(void)
        /* Hardware Initialization */\r
        Serial_Init(9600, false);\r
        Buttons_Init();\r
+       ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);\r
+       ADC_SetupChannel(MIC_IN_ADC_CHANNEL);   \r
        LEDs_Init();\r
        USB_Init();\r
 \r
@@ -234,17 +236,30 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
        /* Check if the current pipe can be written to (device ready for more data) */\r
        if (Pipe_IsOUTReady())\r
        {\r
-               static uint8_t SquareWaveSampleCount;\r
-               static int16_t CurrentWaveValue;\r
+               int16_t AudioSample;            \r
+       \r
+               #if defined(USE_TEST_TONE)\r
+                       static uint8_t SquareWaveSampleCount;\r
+                       static int16_t CurrentWaveValue;\r
                        \r
-               if (SquareWaveSampleCount++ == 0xFF)\r
-                 CurrentWaveValue ^= 0x8000;\r
+                       /* In test tone mode, generate a square wave at 1/256 of the sample rate */\r
+                       if (SquareWaveSampleCount++ == 0xFF)\r
+                         CurrentWaveValue ^= 0x8000;\r
                        \r
-               /* Only generate audio if the board button is being pressed */\r
-               int16_t Sample_16Bit = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;\r
+                       /* Only generate audio if the board button is being pressed */\r
+                       AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;\r
+               #else\r
+                       /* Audio sample is ADC value scaled to fit the entire range */\r
+                       AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());\r
+\r
+                       #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)\r
+                       /* Microphone is biased to half rail voltage, subtract the bias from the sample value */\r
+                       AudioSample -= (SAMPLE_MAX_RANGE / 2);\r
+                       #endif          \r
+               #endif\r
                \r
-               Pipe_Write_16_LE(Sample_16Bit);\r
-               Pipe_Write_16_LE(Sample_16Bit);\r
+               Pipe_Write_16_LE(AudioSample);\r
+               Pipe_Write_16_LE(AudioSample);\r
                \r
                if (!(Pipe_IsReadWriteAllowed()))\r
                  Pipe_ClearOUT();\r