//======================================================================
// DSP.cpp
//======================================================================

#include "DSP.h"

#include <Audio.h>
#include <i2c_t3.h>
//#include <Wire.h>
//#include <SPI.h>
//#include <SD.h>
//#include <SerialFlash.h>

#define RX_AUDIO_CH 0

#define TX_MIC_IN_CH  0
#define TX_LINE_IN_CH 0
#define TX_USB_IN_CH1 1
#define TX_USB_IN_CH2 2

UBitxDSP DSP;

//static struct {

// GUItool: begin automatically generated code
AudioInputI2S            lineIn;         //xy=385.9371643066406,1001.9600830078125
AudioInputUSB            usbIn;          //xy=390.9371643066406,1107.9600830078125
AudioMixer4              rxAudio;        //xy=642.9371643066406,915.9600830078125
AudioMixer4              txAudio;        //xy=646.9371643066406,1103.9600830078125
AudioOutputUSB           usbOut;         //xy=1022.9371643066406,919.9600830078125
AudioOutputI2S           lineOut;        //xy=1024.9371643066406,1017.9600830078125
AudioConnection          patchCord1(lineIn, 0, rxAudio, 0);
AudioConnection          patchCord2(lineIn, 1, txAudio, 0);
AudioConnection          patchCord3(usbIn, 0, txAudio, 1);
AudioConnection          patchCord4(usbIn, 1, txAudio, 2);
AudioConnection          patchCord5(rxAudio, 0, lineOut, 0);
AudioConnection          patchCord6(rxAudio, 0, usbOut, 0);
AudioConnection          patchCord7(rxAudio, 0, usbOut, 1);
AudioConnection          patchCord8(txAudio, 0, lineOut, 1);
AudioControlSGTL5000     audioCtrl;      //xy=651.9371643066406,1244.9600830078125
// GUItool: end automatically generated code
  
//} audio;

void UBitxDSP::begin() {
  AudioMemory(16);
  audioCtrl.enable();
  audioCtrl.volume(0.0);      // headphone volume...
  audioCtrl.muteHeadphone();  // ...not used by UBitxDSP

  for (int i = 0; i < 4; i++) {
    if (i == RX_AUDIO_CH)
      rxAudio.gain(i, 1.0);
    else
      rxAudio.gain(i, 0.0);
  }

  for (int i = 0; i < 4; i++) {
    txAudio.gain(i, 0.0);
  }

  // SETUP THE AUDIO INPUTS

  // Rig (Line) Input (RX)
  audioCtrl.inputSelect(AUDIO_INPUT_LINEIN);
  audioCtrl.unmuteLineout();
  audioCtrl.lineInLevel(9, 5);  // RX, TX
  audioCtrl.lineOutLevel(29, 31); //RX, TX

  // Mic Input (TX)
  audioCtrl.micGain(0); // TODO: set value

  // Line Input (TX)

  // USB Input (TX)

  // SETUP THE AUDIO OUTPUTS

  // Line Output (RX)

  // USB Output (RX)

  // Rig (Line) Output (TX)

  // Default to RX.
  rx();
}

void UBitxDSP::end() {
}

void UBitxDSP::rx() {
  // mute all tx audio
  audioCtrl.micGain(0);
  for (int i = 0; i < 4; i++) {
    txAudio.gain(i, 0.0);
  }
  // select line in for rx
  audioCtrl.inputSelect(AUDIO_INPUT_LINEIN);
  // restore rx audio
  rxAudio.gain(RX_AUDIO_CH, 1.0); // restore the RX audio
}

void UBitxDSP::txMicIn() {
  // mute the rx audio
  rxAudio.gain(RX_AUDIO_CH, 0.0);
  // restore the tx mic audio
  audioCtrl.inputSelect(AUDIO_INPUT_MIC);  
  audioCtrl.micGain(12);
  for (int i = 0; i < 4; i++) {
    if (i == TX_MIC_IN_CH)
      txAudio.gain(i, 0.1);
    else
      txAudio.gain(i, 0.0);
  }
}

void UBitxDSP::txLineIn() {
  // mute the rx audio
  rxAudio.gain(RX_AUDIO_CH, 0.0);
  // restore the tx line in audio
  audioCtrl.inputSelect(AUDIO_INPUT_LINEIN);
  for (int i = 0; i < 4; i++) {
    if (i == TX_LINE_IN_CH)
      txAudio.gain(i, 0.1);
    else
      txAudio.gain(i, 0.0);
  }
}

void UBitxDSP::txUSBIn() {
    // mute the rx audio
  rxAudio.gain(RX_AUDIO_CH, 0.0);
  // restore the tx usb in audio
  audioCtrl.inputSelect(AUDIO_INPUT_LINEIN);
  for (int i = 0; i < 4; i++) {
    if (i == TX_USB_IN_CH1 || i == TX_USB_IN_CH2)
      txAudio.gain(i, 0.1);
    else
      txAudio.gain(i, 0.0);
  }
}

//======================================================================
// EOF
//======================================================================
