volume control added

This commit is contained in:
Sebastian
2025-06-06 02:07:30 +02:00
parent 8cd690edbd
commit 55bcb2c389
2 changed files with 123 additions and 15 deletions

View File

@ -16,6 +16,8 @@
// };
uint8_t logain[36] = {58,59,60,61,62,63,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29};
class TLV320AIC3204{
public:
@ -23,10 +25,12 @@ class TLV320AIC3204{
TLV320AIC3204(TwoWire *wire) : wire(wire) {}
void begin(){init();};
void begin(TwoWire *_wire) {
wire = _wire;
init();
}
void begin(uint8_t i2cAddress, TwoWire *_wire) {
i2cAddress = i2cAddress;
wire = _wire;
@ -142,15 +146,86 @@ class TLV320AIC3204{
void setMicPgaGainL(int gain);
void setMicPgaGainR(int gain);
void setLineOutVolume(int volumeLeft, int volumeRight);
void setLineOutVolumeL(int volume);
void setLineOutVolumeR(int volume);
void mute(bool muteState){
muteL(muteState);
muteR(muteState);
}
void muteL(bool muteState){
uint8_t mutevol = (muteState << 7) + logain[volumeL];
cw(0x00, 0x01);
cw(0x12, mutevol);
muteStateL = muteState;
}
void muteR(bool muteState){
uint8_t mutevol = (muteState << 7) + logain[volumeR];
cw(0x00, 0x01);
cw(0x13, mutevol);
muteStateR = muteState;
}
// set LOL and LOR gain from 0 (-6dB) to 35 (29dB)
void setLineOutVolume(int volume){
setLineOutVolumeL(volume);
setLineOutVolumeR(volume);
cw(0x00, 0x01);
crd(0x12, 1);
}
void setLineOutVolumeL(int volume){
volume = max(0, min(35, volume));
uint8_t mutevol = (muteStateL << 7) + logain[volume];
cw(0x00, 0x01);
cw(0x12, mutevol); // LOL gain 0dB
volumeL = volume;
}
void setLineOutVolumeR(int volume){
volume = max(0, min(35, volume));
uint8_t mutevol = (muteStateR << 7) + logain[volume];
cw(0x00, 0x01);
cw(0x13, mutevol); // LOR gain 0dB
volumeR = volume;
}
void volumeUp(){
volumeL++;
volumeR++;
setLineOutVolumeL(volumeL);
setLineOutVolumeR(volumeR);
}
void volumeDown(){
volumeL--;
volumeR--;
setLineOutVolumeL(volumeL);
setLineOutVolumeR(volumeR);
}
uint8_t getVolumeL(){
uint8_t vol[1];
cw(0x00, 0x01);
cr(0x12, vol, 1);
return vol[0];
}
uint8_t getVolumeR(){
uint8_t vol[1];
cw(0x00, 0x01);
cr(0x13, vol, 1);
return vol[0];
}
private:
TwoWire *wire;
uint8_t i2cAddress = 0x18;
bool muteStateL = 0;
bool muteStateR = 0;
uint8_t volumeL = 0;
uint8_t volumeR = 0;
void cw(unsigned char first, unsigned char second){
wire->beginTransmission(i2cAddress);
@ -195,6 +270,26 @@ class TLV320AIC3204{
}
}
void crd(unsigned char first, size_t len){
wire->beginTransmission(0x18);
wire->write(first); // set register for read
wire->endTransmission(false); // false to not release the line
wire->requestFrom(0x18, len, true); // request bytes from register XY
Serial.print(first, HEX);
Serial.print(" ");
byte buff[len];
wire->readBytes(buff, len);
for (int i = 0; i < len; i++) {
Serial.print(buff[i], HEX);
Serial.print(" ");
Serial.println(buff[i], BIN);
}
}
// TLV320AIC3204_Settings settings;
};

View File

@ -152,8 +152,8 @@ void speaker(bool state){
}
void setup() {
//Serial.begin();
//delay(1000);
Serial.begin();
delay(1000);
i2s.setFrequency(48000);
@ -181,21 +181,18 @@ void setup() {
if(sdInitialized && SD.exists("/click.wav")) {
File click = SD.open("/click.wav");
click.seek(44, SeekSet);
int click_bytes = 0;
int16_t maxv = 0;
while (click.available() || click_bytes == UI_SAMPLERATE-1) {
int16_t sample = click.read() * 32;
if(abs(sample) > maxv) maxv = sample;
uint8_t samplebyte[2];
click.read(samplebyte, 2);
int16_t sample = (samplebyte[1] << 8) + samplebyte[0];
beep[click_bytes] = sample;
click_bytes++;
}
click.close();
click.close();
beep_length = click_bytes;
Serial.print("Read ");
Serial.print(beep_length);
Serial.print(" bytes from click.wav into beep array. maxV was ");
Serial.println(maxv);
} else {
for(int i = 0; i < UI_SAMPLERATE; i++){
float sine_pos = (2.0f * M_PI * 8000.0f * (float)i) / (float)UI_SAMPLERATE;
@ -295,8 +292,13 @@ void setup() {
// }
digitalWrite(6, HIGH);
delay(50);
delay(25);
digitalWrite(6, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(25);
digitalWrite(6, LOW);
}
void loop() {
@ -367,11 +369,22 @@ void loop() {
if(active == 13) active = 0;
if(active == -1) active = 12;
if(buttons[CVINL]) {
Serial.println("vol down");
codec.volumeDown();
}
if(buttons[CVOUTL]) {
Serial.println("vol up");
codec.volumeUp();
}
if(buttons[DEBUG3]) {
speakerToggle = !speakerToggle;
speaker(speakerToggle);
}
Serial.println(codec.getVolumeL());
buttonChanged = false;
}