This library controls a ST TDA7419 audio control IC. This is part of a project to implement an mbed controlled car stereo. The TDA7419 will take in stereo and output four channels of audio plus a subwoofer channel.

Dependents:   car_stereo

Files at this revision

API Documentation at this revision

Comitter:
danielashercohen
Date:
Sun Oct 26 22:49:34 2014 +0000
Parent:
1:69c37f1ab7df
Commit message:
Basic tone control features all implemented, working with FRDM-KL25Z

Changed in this revision

PreampTDA7419.cpp Show annotated file Show diff for this revision Revisions of this file
PreampTDA7419.h Show annotated file Show diff for this revision Revisions of this file
diff -r 69c37f1ab7df -r 34a58356394c PreampTDA7419.cpp
--- a/PreampTDA7419.cpp	Mon Oct 20 05:54:52 2014 +0000
+++ b/PreampTDA7419.cpp	Sun Oct 26 22:49:34 2014 +0000
@@ -1,19 +1,17 @@
 /** PreampTDA7419 Library
  *
  * @Author: Dan Cohen
+ *
  */
- 
+
 #include "mbed.h"
 #include "PreampTDA7419.h"
 #include <stdio.h>
 #include <string.h>
-#include <inttypes.h>
 
 PreampTDA7419::PreampTDA7419(PinName sda, PinName scl):
     _device(sda, scl)
 {
-    _address = (TDA7419_ADDRESS<<1);
- 
     _volume           = 6;
     _input            = 1;
 
@@ -29,67 +27,65 @@
     _middleSoftStep   = 0;
     _middleQ          = 0;
     _middle           = 0;
-}
-
-void PreampTDA7419::setI2CAddress(uint8_t add) 
-{
-    _address = (add<<1);
+    
+    _atten_lf         =  9;
+    _atten_rf         =  9;
+    _atten_lr         =  9;
+    _atten_rr         =  9;
+    _atten_mix        =  9;
+    _atten_sub        =  9;
+    
 }
 
-int PreampTDA7419::i2c_write(int command, int value) {
-  int transmissionSuccessful;
-  _device.start();
-  transmissionSuccessful  = _device.write(_address);
-  transmissionSuccessful |= _device.write(command);
-  transmissionSuccessful |= _device.write(value);
-  _device.stop();
-  return (transmissionSuccessful);
-} 
-
-void PreampTDA7419::writeToTDA7419 (int address, int value) {
-  i2c_write(address, value);
-}  
+void PreampTDA7419::writeToTDA7419 (int command, int value)
+{
+    int transmissionSuccessful;
+    _device.start();
+    transmissionSuccessful  = _device.write(TDA7419_ADDRESS<<1);
+    transmissionSuccessful |= _device.write(command);
+    transmissionSuccessful |= _device.write(value);
+    _device.stop();
+    // return (transmissionSuccessful);
+}
 
 /////////////////////////////////
 // set the speaker attenuators //
 /////////////////////////////////
-// attenuation can be set from 0 to 11 and this is mapped to the 
+// attenuation can be set from 0 to 11 and this is mapped to the
 // values that the TDA7419 uses for it's attenuation (0->h60)
 
 //
 //  (FL/FR/RL/RR/SWL/SWR) (13-18)
-int PreampTDA7419::calcAttenuationReg(int attenuation) {
-  int regAtten;
-  if (attenuation == 11) {  
-    regAtten = 13;
-  } else if (attenuation == 10) {
-    regAtten = 6;
-  } else {
-    regAtten = (99-(attenuation*9));
-  }
-  return (regAtten);
+void PreampTDA7419::setAttenuationReg(int regAddr, int attenuation)
+{
+    int regAtten;
+    if (attenuation == 11) {
+        regAtten = 13;
+    } else if (attenuation == 10) {
+        regAtten = 6;
+    } else {
+        regAtten = (99-(attenuation*9));
+    }
+    writeToTDA7419(regAddr, regAtten);
 }
 
-int PreampTDA7419::calcToneAttenuationReg(int attenuation) {
-  int regAtten;
-  if (attenuation > 0) {
-    regAtten = 16 + (attenuation * 3);
-  } else if (attenuation == 0) {
-    regAtten = 0;
-  } else if (attenuation  < 0) {
-    regAtten = 0 - (attenuation * 3); 
-  }  
-  return (regAtten);
+int PreampTDA7419::calcToneAttenuationReg(int attenuation)
+{
+    int regAtten;
+    if (attenuation > 0) {
+        regAtten = 16 + (attenuation * 3);
+    } else if (attenuation == 0) {
+        regAtten = 0;
+    } else if (attenuation  < 0) {
+        regAtten = 0 - (attenuation * 3);
+    }
+    return (regAtten);
 }
 
 // update all of the registers in the TDA7419
-void PreampTDA7419::updateTDA7419Reg() {
-  
-  int regVolume;
-  int regTreble;
-  int regMiddle;
-  int regBass;
-  
+void PreampTDA7419::updateTDA7419Reg()
+{
+
     int s_main_source    = 0;
     int s_main_loud      = 1   | 0x40;
     int s_softmute       = 2   | 0x40;
@@ -107,166 +103,231 @@
     int s_atten_mix      = 14  | 0x40;
     int s_atten_sub      = 15  | 0x40;
     int s_spectrum       = 16  | 0x40;
-    int s_test           = 17  | 0x40;
+    // int s_test           = 17  | 0x40;
+
+    //////////////////////////////////////////////////////////////////
+    // Calculate actual register values from the variables that the //
+    // buttons control                                              //
+    //////////////////////////////////////////////////////////////////
 
-  //////////////////////////////////////////////////////////////////
-  // Calculate actual register values from the variables that the //
-  // buttons control                                              //
-  //////////////////////////////////////////////////////////////////
-
+    //////////////////////////
+    // update the registers //
+    //////////////////////////
+    writeToTDA7419(s_main_source,  ( (0x78) | (_input & 0x3) ) );
+    writeToTDA7419(s_main_loud,      (0xc0));
+    writeToTDA7419(s_softmute,       (0xa7));
+    setAttenuationReg(s_volume, _volume);
 
 
-/*
-  // set the tone controls //
-  // Expect treble to have the values -5 to +5 //
-  // Expect trebleCenterFreq to be 0 to 3      //
-  // Expect referenceInE to be 0 or 1          //
-  // we define treble as -5 to +5 the TDA7419 register value is more complex
-  if (treble > 0) {
-    regTreble = 16 + (treble * 3);
-  } else if (treble == 0) {
-    regTreble = 0;
-  } else if (treble  < 0) {
-    regTreble = 0 - (treble * 3); 
-  }  
+    // tone register attenuation isn't simple so moving that
+    // calculation to a separate function
+    // locking softwtep as '0' because that is on and I think we
+    // want soft step!
+    writeToTDA7419(s_treble,
+                   ( (0                                      &  0x1 ) << 7 ) |
+                   ( (1                                      &  0x3 ) << 5 ) |
+                   ( (calcToneAttenuationReg(_treble)        & 0x1f )      ) );
 
-  if (middle > 0) {
-    regMiddle = 16 + (middle * 3);
-  } else if (middle == 0) {
-    regMiddle = 0;
-  } else if (middle  < 0) {
-    regMiddle = 0 - (middle * 3); 
-  }  
+    writeToTDA7419(s_middle,
+                   ( (0                                      &  0x1 ) << 7 ) |
+                   ( (1                                      &  0x3 ) << 5 ) |
+                   ( (calcToneAttenuationReg(_middle)        & 0x1f )      ) );
 
-  if (bass > 0) {
-    regBass = 16 + (bass * 3);
-  } else if (bass == 0) {
-    regBass = 0;
-  } else if (bass  < 0) {
-    regBass = 0 - (bass * 3); 
-  }  
-*/
-  //////////////////////////  
-  // update the registers //
-  //////////////////////////  
-  writeToTDA7419(s_main_source,  ( (0x78) | (_input & 0x3) ) );
-  writeToTDA7419(s_main_loud,      (0xc0));
-  writeToTDA7419(s_softmute,       (0xa7));
-  writeToTDA7419(s_volume, calcAttenuationReg(_volume));
+    writeToTDA7419(s_bass,
+                   ( (0                                      &  0x1 ) << 7 ) |
+                   ( (1                                      &  0x3 ) << 5 ) |
+                   ( (calcToneAttenuationReg(_bass)          & 0x1f )      ) );
+
 
-  // tone register attenuation isn't simple so moving that 
-  // calculation to a separate function
-  // locking softwtep as '0' because that is on and I think we 
-  // want soft step!
-  writeToTDA7419(s_treble,        
-     ( (0                                      &  0x1 ) << 7 ) | 
-     ( (1                                      &  0x3 ) << 5 ) |
-     ( (calcToneAttenuationReg(_treble)        & 0x1f )      ) );
+    // this register allows the second source to be routed to the rear speakers
+    // not useful in the context of this project
+    writeToTDA7419(s_second_source, (0x07));
 
-  writeToTDA7419(s_middle,      
-     ( (0                                      &  0x1 ) << 7 ) | 
-     ( (1                                      &  0x3 ) << 5 ) |
-     ( (calcToneAttenuationReg(_middle)        & 0x1f )      ) );
-
-  writeToTDA7419(s_bass,   
-     ( (0                                      &  0x1 ) << 7 ) | 
-     ( (1                                      &  0x3 ) << 5 ) |
-     ( (calcToneAttenuationReg(_bass)          & 0x1f )      ) );
+    // this is the subwoofer cut-off frequency
+    // 11 which is 160Khz)
+    writeToTDA7419(s_sub_mid_bass,  (0x63));
 
-  // this register allows the second source to be routed to the rear speakers
-  // not useful in the context of this project
-  writeToTDA7419(s_second_source, (0x07));
-
-  // this is the subwoofer cut-off frequency
-  // 11 which is 160Khz)
-  writeToTDA7419(s_sub_mid_bass,  (0x63));
+    // mix to the front speakers,  enable the sub,  no gain
+    if (_mix == 1) {
+        writeToTDA7419(s_mix_gain,    (0xf7));
+    } else {
+        writeToTDA7419(s_mix_gain,    (0xf0));
+    }
 
-  // mix to the front speakers,  enable the sub,  no gain
-  if (_mix == 1) {
-    writeToTDA7419(s_mix_gain,    (0xf7));
-  } else {
-    writeToTDA7419(s_mix_gain,    (0xf0));
-  }
+    setAttenuationReg(s_atten_lf  , _atten_lf  );
+    setAttenuationReg(s_atten_rf  , _atten_rf  );
+    setAttenuationReg(s_atten_lr  , _atten_lr  );
+    setAttenuationReg(s_atten_rr  , _atten_rr  );
 
-  s_atten_lf  = calcAttenuationReg(_atten_lf   );
-  s_atten_rf  = calcAttenuationReg(_atten_rf   );
-  s_atten_lr  = calcAttenuationReg(_atten_lr   );
-  s_atten_rr  = calcAttenuationReg(_atten_rr   );
+    setAttenuationReg(s_atten_mix , _atten_mix );
+    setAttenuationReg(s_atten_sub , _atten_sub );
 
-  s_atten_mix = calcAttenuationReg(_atten_mix  );
-  s_atten_sub = calcAttenuationReg(_atten_sub  );
-
-  writeToTDA7419       (s_spectrum,      (0x09));  
+    writeToTDA7419       (s_spectrum,      (0x09));
 
 }
 
-/* setVolume: This sets the volume within the valid range of 0->11 
+/* setVolume: This sets the volume within the valid range of 0->11
   return indicates it was successfully set */
-void PreampTDA7419::setVolume(int volume) {
-  if (volume > 11) {
-    _volume = 11;
-  } 
-  else if (volume < 0) {
-    volume = 0;
-  }
-  else {
-    _volume = volume;
-  }
-  updateTDA7419Reg();
+void PreampTDA7419::setVolume(int volume)
+{
+    if (volume > 11) {
+        _volume = 11;
+    } else if (volume < 0) {
+        volume = 0;
+    } else {
+        _volume = volume;
+    }
+    updateTDA7419Reg();
 }
 
 /* readVolume:  return the volume level that is currently set */
-int PreampTDA7419::readVolume() {
-  return (_volume);
+int PreampTDA7419::readVolume()
+{
+    return (_volume);
+}
+/* readVolume:  return the volume level that is currently set */
+int PreampTDA7419::increaseVolume()
+{
+    _volume++;
+    setVolume(_volume);
+    return (_volume);
 }
 /* readVolume:  return the volume level that is currently set */
-int PreampTDA7419::increaseVolume() {
-  _volume++;
-  setVolume(_volume);
-  return (_volume);
+int PreampTDA7419::decreaseVolume()
+{
+    _volume--;
+    setVolume(_volume);
+    return (_volume);
+}
+
+void PreampTDA7419::setInput(int input)
+{
+    if (input > 3) {
+        _input = 3;
+    } else if (input < 0) {
+        input = 0;
+    } else {
+        _input = input;
+    }
+    updateTDA7419Reg();
+}
+
+int PreampTDA7419::readInput()
+{
+    return (_input);
 }
-/* readVolume:  return the volume level that is currently set */
-int PreampTDA7419::decreaseVolume() {
-  _volume--;
-  setVolume(_volume);
-  return (_volume);
+
+int  PreampTDA7419::increaseTreble()
+{
+    if (_treble < 5) {
+        _treble++;
+    }
+    updateTDA7419Reg();
+    return(_treble);
+}
+
+int  PreampTDA7419::decreaseTreble()
+{
+    if (_treble > -5) {
+        _treble--;
+    }
+    updateTDA7419Reg();
+    return(_treble);
+}
+
+int PreampTDA7419::readTreble()
+{
+    return (_treble);
+}
+
+int  PreampTDA7419::increaseMiddle()
+{
+    if (_middle < 5) {
+        _middle++;
+    }
+    updateTDA7419Reg();
+    return(_middle);
 }
 
-void PreampTDA7419::setInput(int input) {
-  if (input > 3) {
-    _input = 3;
-  } 
-  else if (input < 0) {
-    input = 0;
-  }
-  else {
-    _input = input;
-  }
-  updateTDA7419Reg();
+int  PreampTDA7419::decreaseMiddle()
+{
+    if (_middle > -5) {
+        _middle--;
+    }
+    updateTDA7419Reg();
+    return(_middle);
+}
+
+int PreampTDA7419::readMiddle()
+{
+    return (_middle);
 }
 
-int PreampTDA7419::readInput() {
-  return (_input);
+int  PreampTDA7419::increaseBass()
+{
+    if (_bass < 5) {
+        _bass++;
+    }
+    updateTDA7419Reg();
+    return(_bass);
+}
+
+int  PreampTDA7419::decreaseBass()
+{
+    if (_bass > -5) {
+        _bass--;
+    }
+    updateTDA7419Reg();
+    return(_bass);
+}
+
+int PreampTDA7419::readBass()
+{
+    return (_bass);
 }
 
-int  PreampTDA7419::increaseTreble() {
-  if (_treble < 5) {
-    _treble++;
+int  PreampTDA7419::increaseSpeaker (int speakerNumber) {
+  switch (speakerNumber) {
+    case (1): if (_atten_lf  < 11) { _atten_lf++;  }; updateTDA7419Reg(); return( _atten_lf  );  
+    case (2): if (_atten_rf  < 11) { _atten_rf++;  }; updateTDA7419Reg(); return( _atten_rf  );  
+    case (3): if (_atten_lr  < 11) { _atten_lr++;  }; updateTDA7419Reg(); return( _atten_lr  );  
+    case (4): if (_atten_rr  < 11) { _atten_rr++;  }; updateTDA7419Reg(); return( _atten_rr  );  
+    case (5): if (_atten_sub < 11) { _atten_sub++; }; updateTDA7419Reg(); return( _atten_sub );    
+    case (6): if (_atten_mix < 11) { _atten_mix++; }; updateTDA7419Reg(); return( _atten_mix );   
   }
-  updateTDA7419Reg();
-  return(_treble);
-}   
-
-int  PreampTDA7419::decreaseTreble() {
-  if (_treble > -5) {
-    _treble--;
-  }
-  updateTDA7419Reg();
-  return(_treble);
-}   
-
-int PreampTDA7419::readTreble() {
-  return (_treble);
+  return (_atten_lf );
 }
 
+int  PreampTDA7419::decreaseSpeaker (int speakerNumber) {
+  switch (speakerNumber) {
+    case (1): if (_atten_lf  >  0) { _atten_lf--;  }; updateTDA7419Reg(); return( _atten_lf  );  
+    case (2): if (_atten_rf  >  0) { _atten_rf--;  }; updateTDA7419Reg(); return( _atten_rf  );  
+    case (3): if (_atten_lr  >  0) { _atten_lr--;  }; updateTDA7419Reg(); return( _atten_lr  );  
+    case (4): if (_atten_rr  >  0) { _atten_rr--;  }; updateTDA7419Reg(); return( _atten_rr  );  
+    case (5): if (_atten_sub >  0) { _atten_sub--; }; updateTDA7419Reg(); return( _atten_sub );    
+    case (6): if (_atten_mix >  0) { _atten_mix--; }; updateTDA7419Reg(); return( _atten_mix );   
+  }
+  return (_atten_lf );
+}
 
+int  PreampTDA7419::readSpeaker (int speakerNumber) {
+  switch (speakerNumber) {
+    case (1):  return( _atten_lf  );  
+    case (2):  return( _atten_rf  ); 
+    case (3):  return( _atten_lr  ); 
+    case (4):  return( _atten_rr  ); 
+    case (5):  return( _atten_sub ); 
+    case (6):  return( _atten_mix ); 
+  }
+  return (_atten_lf );
+}
+
+int PreampTDA7419::toggleMix() {
+  _mix = !_mix;   
+  updateTDA7419Reg();
+  return (_mix);   
+}
+    
+int PreampTDA7419::readMix() {
+  return (_mix);   
+}
diff -r 69c37f1ab7df -r 34a58356394c PreampTDA7419.h
--- a/PreampTDA7419.h	Mon Oct 20 05:54:52 2014 +0000
+++ b/PreampTDA7419.h	Sun Oct 26 22:49:34 2014 +0000
@@ -14,11 +14,6 @@
 // I2C address for TDA7419
 #define TDA7419_ADDRESS      0x44 
  
-#define DEC 10
-#define HEX 16
-#define OCT 8
-#define BIN 2
- 
  /** TDA7419 PreAmp library
  *
  * Includes the commands for volume, fader, subwoofer and tone controls
@@ -27,44 +22,118 @@
 class PreampTDA7419 {
 public:
  
-    /** Create a new Digole Serial Display interface
+    /** Create a new TDA7419 communication interface
      *
      * @param sda is the pin for I2C SDA
      * @param scl is the pin for I2C SCL
-     * @param address is the 7-bit address (default is 0x27 for the device)
      */
     PreampTDA7419(PinName sda, PinName scl);
- 
-    /** Sets a new I2C address for the Preamp board (perhaps not useful as it is fixed for the TDA4719)
-     * @param address is the the new address 
-     */
-    void setI2CAddress(uint8_t add);
-    
-    /** Set up the TDA7419 to default values that will allow audio to pass through the device
-     * 
-     */
-    void initialize();
-    int i2c_write(int command, int value);
-    
-     /** Set up the TDA7419 to default values that will allow audio to pass through the device
-     * 
-     */
+
+    /** Directly set the TDA7419 Master volume (0->11)
+    * 
+    */
     void setVolume  (int volume);
+    /** Read the the TDA7419 Master volume (0->11)
+    * 
+    */    
     int  readVolume ();
+    /** Increase the current volume (if below 11) 
+    * 
+    */        
     int  increaseVolume();
+    /** Decrease the current volume (if more than 0) 
+    * 
+    */         
     int  decreaseVolume();
+    /** Select the input (1->4) 
+    *   Stereo inputs are 1->3 
+    */          
     void setInput   (int input);
+    /** Read currently selected input
+    *  
+    */           
     int  readInput  ();
 
+    /** Increase treble level (-5 -> 5)
+    *  
+    */        
     int  increaseTreble();
+    /** Decrease treble level (-5 -> 5)
+    *  
+    */        
     int  decreaseTreble();
+    /** Read currently set treble level
+    *  
+    */            
     int  readTreble    ();
+    
+    /** Increase middle level (-5 -> 5)
+    *  
+    */       
+    int  increaseMiddle();
+    /** Decrease middle level (-5 -> 5)
+    *  
+    */      
+    int  decreaseMiddle();
+    /** Read currently set middle level
+    *  
+    */        
+    int  readMiddle    ();
+
+    /** Increase Bass level (-5 -> 5)
+    *  
+    */           
+    int  increaseBass();
+    /** Decrease bass level (-5 -> 5)
+    *  
+    */        
+    int  decreaseBass();
+    /** Read currently set middle level
+    *  
+    */    
+    int  readBass    ();    
+    
+    /** Adjust the volume of each channel attenuator
+    * 
+    *  @param speakerNumber  
+    *        1 - left front,
+    *        2 - right front,
+    *        3 - back left,
+    *        4 - back right,
+    *        5 - subwoofer,
+    *        6 - volume of mix channel
+    */          
+    int  increaseSpeaker (int speakerNumber);
+    /** Adjust the volume of each channel attenuator
+    * 
+    *  @param speakerNumber  
+    *        1 - left front,
+    *        2 - right front,
+    *        3 - back left,
+    *        4 - back right,
+    *        5 - subwoofer,
+    *        6 - volume of mix channel
+    */              
+    int  decreaseSpeaker (int speakerNumber);
+    
+    /** Read the value that is currently set for each attenuator
+    *  @param speakerNumber  match the increase and decrease parameter values
+    */
+    int  readSpeaker     (int speakerNumber);
+    
+    /** Enable the mix input (by default this mixes to the front two channels 
+    * @return The return is the state of the mix (1 = on, 0 = 0ff) after this call
+    */
+    int toggleMix();
+    
+    /** Read if Mix is enabled or not
+    * @return The return is the state of the mix (1 = on, 0 = 0ff) after this call
+    */    
+    int readMix();
 
 private:
     // Signals related to I2C communication
     I2C _device;
-    uint8_t _address;
-    uint8_t _Comdelay;
     
     ////////////////////////////////////
     // register addresses for TDA7419 //
@@ -97,9 +166,9 @@
     int _atten_rr;
     int _atten_mix;
     int _atten_sub;
-
-    void writeToTDA7419     (int address, int value);
-    int  calcAttenuationReg (int attenuation);
+    
+    void writeToTDA7419     (int command, int value);
+    void setAttenuationReg  (int regAddr, int attenuation);
     int  calcToneAttenuationReg(int attenuation);
     void updateTDA7419Reg();