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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** Car stereo control using 
00002  *  TDA7419 for input select, audio control.
00003  *  Adafruit 96x64 monochrome 0.96" display to show the audio selections
00004  *  four buttons to control selections.
00005  *
00006  *  Next steps:  take input from the car steering wheel controls!
00007  *
00008  * @Author: Dan Cohen
00009  *
00010  */
00011 
00012 
00013 
00014 #include "mbed.h"
00015 #include "PinDetect.h"
00016 #include "Adafruit_SSD1306.h"
00017 #include "PreampTDA7419.h"
00018 
00019 #define NUM_OPTIONS 12 // how many different options does the stereo have?
00020 char option;                               
00021 
00022 PinDetect  PinUp    ( PTA1  );
00023 PinDetect  PinLeft  ( PTD4  );
00024 PinDetect  PinRight ( PTA2  );
00025 PinDetect  PinDown  ( PTA12 );
00026 
00027 class SPI2 : public SPI
00028 {
00029 public:
00030     SPI2(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk)
00031     {
00032         format(8,3);
00033         frequency(2000000);
00034     };
00035 };
00036 
00037 SPI2 gSpi(PTD2,NC,PTD1);
00038 Adafruit_SSD1306 display (gSpi, PTD0, PTD5, PTA13);  // SPI obect, DC, RST, CS
00039 PreampTDA7419    Preamp  (PTE0, PTE1);
00040 
00041 /////////////////////////////////////////////
00042 // Helper functions for the serial display // 
00043 /////////////////////////////////////////////
00044 void testfillrect(void) {
00045   uint8_t color = 1;
00046   for (int16_t i=0; i<display.height()/2; i+=3) {
00047     // alternate colors
00048     wait(0.05);
00049     display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
00050     display.display();
00051     color++;
00052   }
00053 }
00054 
00055 void displayWrite(char firstLine[], int value)
00056 {
00057   display.clearDisplay();
00058   display.setCursor(0,0);
00059   display.setTextSize(2);
00060   display.printf("%s\r\n", firstLine);
00061   display.setCursor(0,30);
00062   display.setTextSize(5);
00063   display.printf("  %d\r\n", value);
00064   display.display();    
00065 }
00066 
00067 void displayWrite(char firstLine[], char secondLine[])
00068 {
00069   display.clearDisplay();
00070   display.setCursor(0,0);
00071   display.setTextSize(2);
00072   display.printf("%s\r\n", firstLine);
00073   display.printf("%s\r\n", secondLine);  
00074   display.display();    
00075 }
00076 
00077 void processButtonPress (int button) {
00078 
00079   if (button == 0) {
00080     if (option < (NUM_OPTIONS-1)) {
00081       option++;
00082     } else {
00083       option = 0;
00084     }
00085   }
00086 
00087   if (button == 1) {
00088     if (option > 0) {
00089       option--;
00090     } else {
00091       option = (NUM_OPTIONS-1);
00092     }
00093   }
00094 
00095   switch (option) {
00096   case (0):  // if volume option is selected change volume when button 1 and 2 are pressed
00097     if (button == 2) {
00098       Preamp.decreaseVolume();
00099     }
00100     if (button == 3) {
00101       Preamp.increaseVolume();
00102     }
00103     displayWrite("Volume",  Preamp.readVolume() );
00104   break;
00105   case (1):  // manage the input - 1,2,3 are the standard single ended inputs
00106       int input = Preamp.readInput();
00107       if (button == 2) {
00108         input--;
00109       }
00110       if (button == 3) {
00111         input++;
00112       }
00113       Preamp.setInput(input);
00114       displayWrite("Input",  Preamp.readInput() );
00115   break;
00116   case (2):  // manage the treble value
00117     if (button == 2) {
00118       Preamp.decreaseTreble();
00119     }
00120     if (button == 3) {
00121       Preamp.increaseTreble();
00122     }
00123     displayWrite("Treble", Preamp.readTreble());
00124   break;
00125   case (3):  // manage the treble value
00126     if (button == 2) {
00127       Preamp.decreaseMiddle();
00128     }
00129     if (button == 3) {
00130       Preamp.increaseMiddle();
00131     }
00132     displayWrite("Middle", Preamp.readMiddle());
00133   break;
00134   case (4):  // manage the treble value
00135     if (button == 2) {
00136       Preamp.decreaseBass();
00137     }
00138     if (button == 3) {
00139       Preamp.increaseBass();
00140     }
00141     displayWrite("Bass", Preamp.readBass());
00142   break;
00143 
00144   // Manage the attenuators
00145   case (5):  // manage the atten_lf value
00146     if (button == 2) {
00147         displayWrite("LF",   Preamp.decreaseSpeaker(1) );
00148     } 
00149     else if (button == 3) {
00150         displayWrite("LF",   Preamp.increaseSpeaker(1) );
00151     } 
00152     else {
00153         displayWrite("LF",   Preamp.readSpeaker(1) );
00154     }
00155   break;
00156   case (6):  // manage the atten_rf value
00157     if (button == 2) {
00158         displayWrite("RF",   Preamp.decreaseSpeaker(2) );
00159     } 
00160     else if (button == 3) {
00161         displayWrite("RF",   Preamp.increaseSpeaker(2) );
00162     }
00163     else {
00164         displayWrite("RF",   Preamp.readSpeaker(2) );
00165     }   
00166   break;
00167   case (7):  // manage the atten_lr value
00168     if (button == 2) {
00169         displayWrite("LR",   Preamp.decreaseSpeaker(3) );
00170     }
00171     else if (button == 3) {
00172         displayWrite("LR",   Preamp.increaseSpeaker(3) );
00173     } 
00174     else {
00175         displayWrite("LR",   Preamp.readSpeaker(3) );
00176     }           
00177   break;
00178   case (8):  // manage the atten_rr value
00179     if (button == 2) {
00180         displayWrite("RR",   Preamp.decreaseSpeaker(4) );
00181     } 
00182     else if (button == 3) {
00183         displayWrite("RR",   Preamp.increaseSpeaker(4) );
00184     }
00185     else {
00186         displayWrite("RR",   Preamp.readSpeaker(4) );
00187     }     
00188   break;
00189   case (9):  // manage the atten_sub value
00190     if (button == 2) {
00191         displayWrite("SUB",  Preamp.decreaseSpeaker(5) );
00192     }
00193     else if (button == 3) {
00194         displayWrite("SUB",  Preamp.increaseSpeaker(5) );
00195     }
00196     else {
00197         displayWrite("SUB",   Preamp.readSpeaker(5) );
00198     }      
00199   break;
00200   case (10):  // manage the atten_mix value
00201     if (button == 2) {
00202         displayWrite("Mix Level",  Preamp.decreaseSpeaker(6) );
00203     }
00204     else if (button == 3) {
00205         displayWrite("Mix Level",  Preamp.increaseSpeaker(6) );
00206     }
00207     else {
00208         displayWrite("Mix Level",   Preamp.readSpeaker(6) );
00209     }         
00210   break;
00211   case (11):  // manage the atten_sub value
00212     if ( (button == 2) || (button == 3) ) {
00213       Preamp.toggleMix();
00214     } 
00215     if (Preamp.readMix()) {
00216       displayWrite("Mix",  "Enabled");
00217     }
00218     else {
00219       displayWrite("Mix", "Disabled");
00220     }
00221   break;  
00222   
00223   }
00224 }
00225 
00226 void UpPressed( void )
00227 {
00228     processButtonPress(0);
00229 }
00230 
00231 void LeftPressed( void )
00232 {
00233     processButtonPress(2);
00234 }
00235 
00236 void RightPressed( void )
00237 {
00238     processButtonPress(3);
00239 }
00240 
00241 void DownPressed( void )
00242 {
00243     processButtonPress(1);
00244 }
00245 
00246 int main()
00247 {
00248     PinUp   .mode( PullUp );
00249     PinLeft .mode( PullUp );
00250     PinRight.mode( PullUp );
00251     PinDown .mode( PullUp );
00252 
00253     PinUp   .attach_asserted( &UpPressed    );
00254     PinLeft .attach_asserted( &LeftPressed  );
00255     PinRight.attach_asserted( &RightPressed );
00256     PinDown .attach_asserted( &DownPressed  );
00257 
00258     display.clearDisplay();
00259     // draw multiple rectangles
00260     testfillrect();
00261     wait(0.3);
00262     display.display();
00263     display.clearDisplay();
00264     display.display();
00265   
00266 
00267     // Sampling does not begin until you set a frequency.
00268     // The default is 20ms. If you want a different frequency
00269     // then pass the period in microseconds for example, for 10ms :-
00270     //     pin.setSampleFrequency( 10000 );
00271     //
00272     PinUp   .setSampleFrequency(10000); // Defaults to 20ms.
00273     PinLeft .setSampleFrequency(10000); // Defaults to 20ms.
00274     PinRight.setSampleFrequency(10000); // Defaults to 20ms.
00275     PinDown .setSampleFrequency(10000); // Defaults to 20ms.
00276 
00277     // option = NUM_OPTIONS;
00278     processButtonPress(0);
00279 
00280     while (1) {
00281         wait(0.2);
00282     }
00283 
00284 }
00285 
00286 // EOF