Car stereo control using TDA7419 for input select, audio control. Adafruit 96x64 monochrome 0.96" display to show the audio selections four buttons to control selections. Next steps: take input from the car steering wheel controls!

Dependencies:   Adafruit_GFX PinDetect_KL25Z PreampTDA7419 mbed

main.cpp

Committer:
danielashercohen
Date:
2014-10-20
Revision:
3:8d3cc3488cd8
Parent:
2:2b0cf4d80668
Child:
4:46da1eff72bf

File content as of revision 3:8d3cc3488cd8:

#include "mbed.h"
#include "PinDetect.h"
#include "Adafruit_SSD1306.h"
#include "PreampTDA7419.h"

#define NUM_OPTIONS 12 // how many different options does the stereo have?
char option;                               

PinDetect  PinUp    ( PTA1  );
PinDetect  PinLeft  ( PTD4  );
PinDetect  PinRight ( PTA2  );
PinDetect  PinDown  ( PTA12 );

class SPI2 : public SPI
{
public:
    SPI2(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk)
    {
        format(8,3);
        frequency(2000000);
    };
};

SPI2 gSpi(PTD2,NC,PTD1);
Adafruit_SSD1306 display (gSpi, PTD0, PTD5, PTA13);  // SPI obect, DC, RST, CS
PreampTDA7419    Preamp  (PTE0, PTE1);

void testfillrect(void) {
  uint8_t color = 1;
  for (int16_t i=0; i<display.height()/2; i+=3) {
    // alternate colors
    wait(0.05);
    display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
    display.display();
    color++;
  }
}

void displayWrite(char firstLine[], int value)
{
  display.clearDisplay();
  display.setCursor(0,0);
  display.setTextSize(2);
  display.printf("%s\r\n", firstLine);
  display.setCursor(0,30);
  display.setTextSize(5);
  display.printf("  %d\r\n", value);
  display.display();    
}

/////////////////////////////////////////////
// Helper functions for the serial display // 
/////////////////////////////////////////////
void processButtonPress (int button) {

  if (button == 0) {
    if (option < (NUM_OPTIONS-1)) {
      option++;
    } else {
      option = 0;
    }
  }

  if (button == 1) {
    if (option > 0) {
      option--;
    } else {
      option = (NUM_OPTIONS-1);
    }
  }

  switch (option) {
  case (0):  // if volume option is selected change volume when button 1 and 2 are pressed
    if (button == 2) {
      Preamp.decreaseVolume();
    }
    if (button == 3) {
      Preamp.increaseVolume();
    }
    displayWrite("Volume",  Preamp.readVolume() );
  break;
  case (1):  // manage the input - 1,2,3 are the standard single ended inputs
      int input = Preamp.readInput();
      if (button == 2) {
        input--;
      }
      if (button == 3) {
        input++;
      }
      Preamp.setInput(input);
      displayWrite("Input",  Preamp.readInput() );
  break;
  case (2):  // manage the treble value
    if (button == 2) {
      Preamp.decreaseTreble();
    }
    if (button == 3) {
      Preamp.increaseTreble();
    }
    displayWrite("Treble", Preamp.readTreble());
  break;
  case (3):  // manage the middle value
    displayWrite("Middle", 42);
  break;
  }
}
/*
  case (3):  // manage the middle value
    if (button == 2) {
      if (middle > -5) {
        middle--;
      }
    }
    if (button == 3) {
      if (middle < 5) {
        middle++;
      }
    }
    displayWrite("Middle",  middle );
    break;
  case (4):  // manage the bass value
    if (button == 2) {
      if (bass > -5) {
        bass--;
      }
    }
    if (button == 3) {
      if (bass < 5) {
        bass++;
      }
    }
    displayWrite("Bass",  bass );
  break;

  // Manage the attenuators
  case (5):  // manage the atten_lf value
    if (button == 2) {
      if (atten_lf > 0) {
        atten_lf--;
      }
    }
    if (button == 3) {
      if (atten_lf < 11) {
        atten_lf++;
      }
    }
    displayWrite("LF",  atten_lf );
  break;
  case (6):  // manage the atten_rf value
    if (button == 2) {
      if (atten_rf > 0) {
        atten_rf--;
      }
    }
    if (button == 3) {
      if (atten_rf < 11) {
        atten_rf++;
      }
    }
    displayWrite("RF",  atten_rf );
  break;
  case (7):  // manage the atten_lr value
    if (button == 2) {
      if (atten_lr > 0) {
        atten_lr--;
      }
    }
    if (button == 3) {
      if (atten_lr < 11) {
        atten_lr++;
      }
    }
    displayWrite("LR",  atten_lr );
  break;
  case (8):  // manage the atten_rr value
    if (button == 2) {
      if (atten_rr > 0) {
        atten_rr--;
      }
    }
    if (button == 3) {
      if (atten_rr < 11) {
        atten_rr++;
      }
    }
    displayWrite("RR",  atten_rr );
  break;
  case (9):  // manage the atten_mix value
    if (button == 2) {
      if (atten_mix > 0) {
        atten_mix--;
      }
    }
    if (button == 3) {
      if (atten_mix < 11) {
        atten_mix++;
      }
    }
    displayWrite("Mix",  atten_mix );
  break;
  case (10):  // manage the atten_sub value
    if (button == 2) {
      if (atten_sub > 0) {
        atten_sub--;
      }
    }
    if (button == 3) {
      if (atten_sub < 11) {
        atten_sub++;
      }
    }
    displayWrite("SUB",  atten_sub );
  break;
  case (11):  // manage the atten_sub value
    if (button == 2) {
      mix = 0;
    }
    if (button == 3) {
      mix = 1;
    }
    displayWrite("Mix",  mix );
  break;

  }
}
*/

void UpPressed( void )
{
    processButtonPress(0);
}

void LeftPressed( void )
{
    processButtonPress(2);
}

void RightPressed( void )
{
    processButtonPress(3);
}

void DownPressed( void )
{
    processButtonPress(1);
}

int main()
{
    PinUp   .mode( PullUp );
    PinLeft .mode( PullUp );
    PinRight.mode( PullUp );
    PinDown .mode( PullUp );

    PinUp   .attach_asserted( &UpPressed    );
    PinLeft .attach_asserted( &LeftPressed  );
    PinRight.attach_asserted( &RightPressed );
    PinDown .attach_asserted( &DownPressed  );

    display.clearDisplay();
    // draw multiple rectangles
    testfillrect();
    wait(0.5);
    display.display();
    display.clearDisplay();
    display.display();
  

    // Sampling does not begin until you set a frequency.
    // The default is 20ms. If you want a different frequency
    // then pass the period in microseconds for example, for 10ms :-
    //     pin.setSampleFrequency( 10000 );
    //
    PinUp   .setSampleFrequency(10000); // Defaults to 20ms.
    PinLeft .setSampleFrequency(10000); // Defaults to 20ms.
    PinRight.setSampleFrequency(10000); // Defaults to 20ms.
    PinDown .setSampleFrequency(10000); // Defaults to 20ms.

    // option = NUM_OPTIONS;
    processButtonPress(0);

    while (1) {
        wait(0.2);
    }

}

// EOF