aboutsummaryrefslogtreecommitdiffstats
path: root/Demos/Host/LowLevel
diff options
context:
space:
mode:
Diffstat (limited to 'Demos/Host/LowLevel')
-rw-r--r--Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.c31
-rw-r--r--Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.h13
2 files changed, 36 insertions, 8 deletions
diff --git a/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.c b/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.c
index 15d00c0e1..1fb64a1a4 100644
--- a/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.c
+++ b/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.c
@@ -68,6 +68,8 @@ void SetupHardware(void)
/* Hardware Initialization */
Serial_Init(9600, false);
Buttons_Init();
+ ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
+ ADC_SetupChannel(MIC_IN_ADC_CHANNEL);
LEDs_Init();
USB_Init();
@@ -234,17 +236,30 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
/* Check if the current pipe can be written to (device ready for more data) */
if (Pipe_IsOUTReady())
{
- static uint8_t SquareWaveSampleCount;
- static int16_t CurrentWaveValue;
+ int16_t AudioSample;
+
+ #if defined(USE_TEST_TONE)
+ static uint8_t SquareWaveSampleCount;
+ static int16_t CurrentWaveValue;
- if (SquareWaveSampleCount++ == 0xFF)
- CurrentWaveValue ^= 0x8000;
+ /* In test tone mode, generate a square wave at 1/256 of the sample rate */
+ if (SquareWaveSampleCount++ == 0xFF)
+ CurrentWaveValue ^= 0x8000;
- /* Only generate audio if the board button is being pressed */
- int16_t Sample_16Bit = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
+ /* Only generate audio if the board button is being pressed */
+ AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
+ #else
+ /* Audio sample is ADC value scaled to fit the entire range */
+ AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
+
+ #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
+ /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
+ AudioSample -= (SAMPLE_MAX_RANGE / 2);
+ #endif
+ #endif
- Pipe_Write_16_LE(Sample_16Bit);
- Pipe_Write_16_LE(Sample_16Bit);
+ Pipe_Write_16_LE(AudioSample);
+ Pipe_Write_16_LE(AudioSample);
if (!(Pipe_IsReadWriteAllowed()))
Pipe_ClearOUT();
diff --git a/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.h b/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.h
index 4772bb3ea..b151f2283 100644
--- a/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.h
+++ b/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.h
@@ -48,12 +48,25 @@
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Peripheral/Serial.h>
+ #include <LUFA/Drivers/Peripheral/ADC.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include <LUFA/Drivers/Board/Buttons.h>
#include "ConfigDescriptor.h"
/* Macros: */
+ /** ADC channel number for the microphone input. */
+ #define MIC_IN_ADC_CHANNEL 2
+
+ /** ADC channel MUX mask for the microphone input. */
+ #define MIC_IN_ADC_MUX_MASK ADC_CHANNEL2
+
+ /** Maximum audio sample value for the microphone input. */
+ #define SAMPLE_MAX_RANGE 0xFFFF
+
+ /** Maximum ADC range for the microphone input. */
+ #define ADC_MAX_RANGE 0x3FF
+
/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
#define LEDMASK_USB_NOTREADY LEDS_LED1