2019-12-03 23:00:54 +00:00
|
|
|
#include "avrlib/adc.h"
|
|
|
|
#include "avrlib/avrlib.h"
|
2019-12-05 21:30:07 +00:00
|
|
|
#include "avrlib/boot.h"
|
2019-12-03 23:00:54 +00:00
|
|
|
#include "avrlib/devices/mcp492x.h"
|
|
|
|
#include "avrlib/gpio.h"
|
|
|
|
#include "avrlib/spi.h"
|
2020-01-07 17:29:55 +00:00
|
|
|
#include "avrlib/time.h"
|
2019-12-03 23:00:54 +00:00
|
|
|
#include "avrlib/watchdog_timer.h"
|
|
|
|
#include "stereo_mix/resources.h"
|
|
|
|
|
|
|
|
using namespace avrlib;
|
|
|
|
using namespace stereo_mix;
|
|
|
|
|
|
|
|
typedef SpiMaster<NumberedGpio<0>, MSB_FIRST, 2> dac1Spi;
|
|
|
|
typedef Dac<dac1Spi, BUFFERED_REFERENCE, 1> Dac1;
|
|
|
|
typedef SpiMaster<NumberedGpio<1>, MSB_FIRST, 2> dac2Spi;
|
|
|
|
typedef Dac<dac2Spi, BUFFERED_REFERENCE, 1> Dac2;
|
|
|
|
typedef SpiMaster<NumberedGpio<2>, MSB_FIRST, 2> dac3Spi;
|
|
|
|
typedef Dac<dac3Spi, BUFFERED_REFERENCE, 1> Dac3;
|
|
|
|
typedef SpiMaster<NumberedGpio<3>, MSB_FIRST, 2> dac4Spi;
|
|
|
|
typedef Dac<dac4Spi, BUFFERED_REFERENCE, 1> Dac4;
|
|
|
|
|
2020-01-07 17:29:55 +00:00
|
|
|
typedef SpiMaster<NumberedGpio<4>, MSB_FIRST, 2> oDac1Spi;
|
2020-01-15 23:07:12 +00:00
|
|
|
typedef Dac<oDac1Spi, BUFFERED_REFERENCE, 0> oDac1;
|
2020-01-07 17:29:55 +00:00
|
|
|
typedef SpiMaster<NumberedGpio<5>, MSB_FIRST, 2> oDac2Spi;
|
2020-01-15 23:07:12 +00:00
|
|
|
typedef Dac<oDac2Spi, BUFFERED_REFERENCE, 0> oDac2;
|
2020-01-07 17:29:55 +00:00
|
|
|
typedef SpiMaster<NumberedGpio<6>, MSB_FIRST, 2> oDac3Spi;
|
2020-01-15 23:07:12 +00:00
|
|
|
typedef Dac<oDac3Spi, BUFFERED_REFERENCE, 0> oDac3;
|
2020-01-07 17:29:55 +00:00
|
|
|
typedef SpiMaster<NumberedGpio<7>, MSB_FIRST, 2> oDac4Spi;
|
2020-01-15 23:07:12 +00:00
|
|
|
typedef Dac<oDac4Spi, BUFFERED_REFERENCE, 0> oDac4;
|
|
|
|
|
2019-12-03 23:00:54 +00:00
|
|
|
typedef AdcInputScanner AnalogInputs;
|
|
|
|
|
|
|
|
#define NUM_CHANNELS 4
|
|
|
|
|
2020-01-07 17:29:55 +00:00
|
|
|
uint32_t volume[NUM_CHANNELS];
|
2020-01-15 23:07:12 +00:00
|
|
|
uint16_t pan[NUM_CHANNELS * 2];
|
2019-12-03 23:00:54 +00:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2019-12-05 21:30:07 +00:00
|
|
|
Boot(true);
|
2019-12-03 23:00:54 +00:00
|
|
|
Dac1::Init();
|
|
|
|
Dac2::Init();
|
|
|
|
Dac3::Init();
|
|
|
|
Dac4::Init();
|
|
|
|
|
2020-01-07 17:29:55 +00:00
|
|
|
oDac1::Init();
|
|
|
|
oDac2::Init();
|
|
|
|
oDac3::Init();
|
|
|
|
oDac4::Init();
|
|
|
|
|
|
|
|
AnalogInputs::Init();
|
|
|
|
|
2019-12-03 23:00:54 +00:00
|
|
|
AnalogInputs::set_num_inputs(8);
|
2020-01-15 23:07:12 +00:00
|
|
|
|
2020-02-18 11:05:27 +00:00
|
|
|
#define WRITE(DAC, ODAC, N) \
|
|
|
|
DAC::Write((volume[N] * pan[N * NUM_CHANNELS]) >> 12, 0); \
|
|
|
|
ODAC::Write((volume[N] * pan[N * NUM_CHANNELS]) >> 12, 0); \
|
|
|
|
DAC::Write((volume[N] * pan[N * NUM_CHANNELS + 1]) >> 12, 1); \
|
|
|
|
ODAC::Write((volume[N] * pan[N * NUM_CHANNELS + 1]) >> 12, 1);
|
2020-01-15 23:07:12 +00:00
|
|
|
|
2019-12-03 23:00:54 +00:00
|
|
|
while (true) {
|
|
|
|
ResetWatchdog();
|
|
|
|
|
2020-02-18 00:56:45 +00:00
|
|
|
int i = AnalogInputs::current_pin() % NUM_CHANNELS;
|
2020-02-18 11:05:27 +00:00
|
|
|
volume[i] = pgm_read_word_near(lut_res_linear_to_exp + (AnalogInputs::Read(i) >> 1));
|
|
|
|
pan[i * NUM_CHANNELS] = pgm_read_word(lut_res_left_sin_pan + (AnalogInputs::Read(i + NUM_CHANNELS) >> 1));
|
|
|
|
pan[i * NUM_CHANNELS + 1] = pgm_read_word(lut_res_right_cos_pan + (AnalogInputs::Read(i + NUM_CHANNELS) >> 1));
|
2020-02-18 00:56:45 +00:00
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
WRITE(Dac1, oDac1, i);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
WRITE(Dac2, oDac2, i);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
WRITE(Dac3, oDac3, i);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
WRITE(Dac4, oDac4, i);
|
|
|
|
break;
|
2019-12-05 21:30:07 +00:00
|
|
|
}
|
2020-02-18 00:56:45 +00:00
|
|
|
AnalogInputs::Scan();
|
2019-12-03 23:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 21:30:07 +00:00
|
|
|
|
2020-01-07 17:29:55 +00:00
|
|
|
TIMER_0_TICK
|
|
|
|
{
|
2019-12-05 21:30:07 +00:00
|
|
|
TickSystemClock();
|
|
|
|
}
|