Skip to content

Commit

Permalink
Impl. Volume control.
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Jul 9, 2020
1 parent bec39d4 commit 90ad8f4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
64 changes: 51 additions & 13 deletions BoxDAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void BoxDAC::begin() {
//Excel 219
// Extract END
send(ADDR::PAGE_CONTROL, PAGE::DAC_OUT_VOL);
send(ADDR_P1_DAC_OUT::L_VOL_TO_SPK, 128+32+16);
send(ADDR_P1_DAC_OUT::L_VOL_TO_SPK, 128);
/*
for (uint32_t i = 0; i<5; i++) {
beep();
Expand Down Expand Up @@ -329,7 +329,11 @@ void BoxDAC::beepTest() {
}

void BoxDAC::beepRaw(uint16_t sin, uint16_t cos, uint32_t length) {
Log.info("beep sin=%i, cos=%i, len=%l", sin, cos, length);
beepRaw(sin, cos, length, convertDacVol2BeepVol(current_volume));
}
void BoxDAC::beepRaw(uint16_t sin, uint16_t cos, uint32_t length, uint8_t volume) {
Log.info("beep sin=%i, cos=%i, len=%l, vol=%X", sin, cos, length, volume);
logBeepVolume(volume);

send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);

Expand All @@ -349,8 +353,8 @@ void BoxDAC::beepRaw(uint16_t sin, uint16_t cos, uint32_t length) {
send(ADDR_P0_SERIAL::BEEP_COS_MSB, (cos>>8)&0xFF);
send(ADDR_P0_SERIAL::BEEP_COS_LSB, cos);

send(ADDR_P0_SERIAL::BEEP_R_GEN, 0x00);
send(ADDR_P0_SERIAL::BEEP_L_GEN, 0x80); //enable beep generator with left channel volume = 0dB,
send(ADDR_P0_SERIAL::BEEP_R_GEN, 0x80);
send(ADDR_P0_SERIAL::BEEP_L_GEN, 0x80|(volume&0x3F)); //enable beep generator with right channel volume,

send(ADDR_P0_SERIAL::DAC_NDAC_VAL, 0x84); //power up NDAC divider

Expand Down Expand Up @@ -419,42 +423,76 @@ uint8_t BoxDAC::readByte(ADDR_P3_MCLK source_register) {
}

bool BoxDAC::increaseVolume() {
bool result = false;
if (current_volume < VOL_MAX) {
current_volume += VOL_STEP;
setVolume(current_volume);
beepRaw(0x278A, 0x79BD, 0x000140); //16kHz
//beepMidi(78,50,true);
return true;
result = true;
} else {
beepRaw(0x30F9, 0x763F, 0x000140); //16kHz
delay(50);
beepRaw(0x30F9, 0x763F, 0x000140); //16kHz
//beepMidi(84,50,true);
Log.info("Maximum volume %X reached.", current_volume);
Log.info("Maximum volume reached.");
}
return false;
logVolume();
return result;
}
bool BoxDAC::decreaseVolume() {
bool result = false;
if (current_volume > VOL_MIN) {
current_volume -= VOL_STEP;
setVolume(current_volume);
beepRaw(0x18F5, 0x7D87, 0x000140); //16kHz
//beepMidi(70, 50, true);
return true;
result = true;
} else {
beepRaw(0x0F0A, 0x7F1A, 0x000140); //16kHz
delay(50);
beepRaw(0x0F0A, 0x7F1A, 0x000140); //16kHz
//beepMidi(62, 50, true);
Log.info("Minimal volume %X reached.", current_volume);
Log.info("Minimal volume reached.");
}
return false;
logVolume();
return result;
}

void BoxDAC::setVolume(uint8_t volume) {
int8_t volumeConv = (int8_t)(volume-0x7F);
send(ADDR::PAGE_CONTROL, PAGE::SERIAL_IO);
send(ADDR_P0_SERIAL::DAC_VOL_L_CTRL, volume);
send(ADDR_P0_SERIAL::DAC_VOL_R_CTRL, volume);
Log.info("Set volume to %X", volume);
send(ADDR_P0_SERIAL::DAC_VOL_L_CTRL, volumeConv);
send(ADDR_P0_SERIAL::DAC_VOL_R_CTRL, volumeConv);
while ((readByte(ADDR_P0_SERIAL::DAC_FLAG_REG) & 0b00010001) != 0b00010001) { }
}

void BoxDAC::logVolume() {
uint8_t volume = current_volume;
uint8_t volumeConv = (volume-0x7F)&0xFF;

int8_t dbVal = (volume-64*2)/2;
int8_t dbDotVal = 10*((volume-64*2)/2)-(10*dbVal);

Log.info("Volume %X(%X) means %i.%idB", volumeConv, volume, dbVal, dbDotVal);
}
void BoxDAC::logBeepVolume(uint8_t volume) {
int8_t dbVal = 2-volume;
Log.info("Beep volume %X means %i.0dB", volume, dbVal);
}

uint8_t BoxDAC::convertDacVol2BeepVol(uint8_t dacVol) {
//DAC Range -63.5dB - +24.0dB
//BEEP Range -61.0dB - +02.0dB
int8_t dbVal = (dacVol-64*2)/2;

if (dbVal>2) {
dbVal = 2;
} else if (dbVal<-61) {
dbVal = -61;
}

int8_t beepVol = 2-dbVal;

return beepVol & 0x3F;
}
34 changes: 31 additions & 3 deletions BoxDAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BoxDAC : public EnhancedThread {
void beep();
void beepMidi(uint8_t midiId, uint16_t lengthMs, bool async=false);
void beepRaw(uint16_t sin, uint16_t cos, uint32_t length);
void beepRaw(uint16_t sin, uint16_t cos, uint32_t length, uint8_t volume);

void dmaPingPingComplete();
const static uint16_t I2S_PACKET_SIZE = 2 * 256; //TODO
Expand All @@ -34,14 +35,41 @@ class BoxDAC : public EnhancedThread {
int16_t sample[2] { amplitude, amplitude }; // current sample value
int count = 0;

const static uint8_t VOL_MIN = 0x0A;
const static uint8_t VOL_MAX = 0xB0;
uint8_t VOL_STEP = 0x06;

const static uint8_t VOL_MIN = 0xB0+0x7F; //0xB0=-40.0dB /min allowed value 0x81=-63.5dB
const static uint8_t VOL_MAX = 0x0A+0x7F; //0x0A=+04.0dB /max allowed value 0x30=+24.0dB
const static uint8_t VOL_STEP = 0x06; //3dB
uint8_t current_volume = VOL_MIN;

//const static uint8_t VOL_BEEP_MIN = 0x2A; //0x2A=-40dB /min allowed value 0x3F=-61dB
//const static uint8_t VOL_BEEP_MAX = 0x00; //0x00=+02dB /max allowed value 0x00=+02dB

//0x2A
//0x27
//0x24
//0x21
//0x1E
//0x1B
//0x18
//0x14
//0x11
//0x0E
//0x0B
//0x08
//0x05
//0x02
//0x00




bool increaseVolume();
bool decreaseVolume();

void setVolume(uint8_t volume);
uint8_t convertDacVol2BeepVol(uint8_t dacVol);
void logVolume();
void logBeepVolume(uint8_t volume);


private:
Expand Down

0 comments on commit 90ad8f4

Please sign in to comment.