Académique Documents
Professionnel Documents
Culture Documents
#ifndef PT2314_h
#define PT2314_h
class PT2314 {
private:
static unsigned char eq_table[];
int _volume;
int _attenuationL;
int _attenuationR;
bool _mute;
bool _loudness;
int _channel;
int _bass;
int _treble;
int volume_to_pt2314(int vol);
int eq_to_pt2314(int val);
int writeI2CChar(unsigned char c);
void updateVolume();
void updateAttenuation();
void updateAudioSwitch();
void updateBass();
void updateTreble();
void updateAll();
public:
int init(void);
void muteOn(void);
void muteOff(void);
void volume(int v);
void channel(int ch);
void loudnessOn();
void loudnessOff();
void attenuation(int l, int r);
void bass(int b);
void treble(int t);
};
#endif
#include <Arduino.h>
#include <Wire.h>
#include <PT2314.h>
r = PT2314::eq_table[val];
return r;
}
// helper method
int PT2314::writeI2CChar(unsigned char c) {
Wire.beginTransmission(PT2314_I2C_ADDRESS);
Wire.write(c);
int rtnCode = Wire.endTransmission();
return rtnCode;
}
// initialize PT2314
int PT2314::init(void) {
_volume = 0;
_attenuationL = 100;
_attenuationR = 100;
_mute = false;
_loudness = true;
_channel = 0;
_bass = 50;
_treble = 50;
updateAll();
return 1;
}
void PT2314::volume(int v) {
_volume = constrain(v, 0, 100);
updateVolume();
}
void PT2314::muteOn(void) {
_mute = true;
updateAttenuation();
updateVolume();
}
void PT2314::muteOff(void) {
_mute = false;
updateAttenuation();
updateVolume();
}
void PT2314::loudnessOff(void) {
_loudness = false;
updateAudioSwitch();
}
void PT2314::bass(int b) {
_bass = constrain(b, 0, 100);
updateBass();
}
void PT2314::treble(int t) {
_treble = constrain(t, 0, 100);
updateTreble();
}
void PT2314::updateVolume() {
unsigned int val = volume_to_pt2314(_volume);
writeI2CChar(val);
}
void PT2314::updateAttenuation() {
unsigned int aL = map(_attenuationL, 0, 100, 0b00011111,
0b00000000);
unsigned int aR = map(_attenuationR, 0, 100, 0b00011111,
0b00000000);
if (_mute) {
writeI2CChar(0b11011111);
writeI2CChar(0b11111111);
} else {
writeI2CChar(0b11000000 | aL);
writeI2CChar(0b11100000 | aR);
}
}
void PT2314::updateAudioSwitch() {
int audioByte = 0b01000000; // audio switch + gain +11.25dB.
if (_loudness){
audioByte |= 0x00;
} else {
audioByte |= 0x04;
}
audioByte |= _channel;
writeI2CChar(audioByte);
}
void PT2314::updateBass() {
unsigned int val = eq_to_pt2314(map(_bass, 0, 100, 0, 28));
writeI2CChar(0x60 | val);
}
void PT2314::updateTreble() {
unsigned int val = eq_to_pt2314(map(_treble, 0, 100, 0, 28));
writeI2CChar(0x70 | val);
}
void PT2314::updateAll() {
updateVolume();
updateAttenuation();
updateAudioSwitch();
updateBass();
updateTreble();
}