Тема: TDA7419 + OLED1.3 + ENCODER + IR + Spectrum Analyzer (LGT8F328)
Основная статья - http://rcl-radio.ru/?p=131120
// TDA7419 + OLED1.3 + ENCODER + IR + Spectrum Analyzer
// rcl-radio.ru
// liman324@yandex.ru
// Alexander Liman
#define IR2 0x807FF00F // button encoder
#define MUTE_IR 0x807FF807 // mute
#define IR4 0x807F48B7 // >>>
#define IR5 0x807FC837 // <<<
#define IN1_IR 0x807F38C7 // IN1
#define IN2_IR 0x807FB847 // IN2
#define IN3_IR 0x807F7887 // IN3
#define GAIN_SPEC 2.0
#define SIG_0_SPEC 7
#define SPED_SPEC 25
#define TIME_SPEC 15
#define ENC_DT 9
#define ENC_CLK 8
#define ENC_SW 10
#define IR 12
#define IN1 2
#define IN2 3
#define IN3 4
#define SET 5
#define MUTE 6
#include <Wire.h>
#include <U8glib.h> // https://github.com/olikraus/u8glib/
#include <MsTimer2.h> // http://rcl-radio.ru/wp-content/uploads/2018/11/MsTimer2.zip
#include <TDA7419.h> // https://github.com/liman324/TDA7419.git
#include <Encoder.h> // http://rcl-radio.ru/wp-content/uploads/2019/05/Encoder.zip
#include <EEPROM.h>
#include <boarddefs.h> // входит в состав библиотеки IRremote
#include <IRremote.h> // http://rcl-radio.ru/wp-content/uploads/2019/06/IRremote.zip
TDA7419 tda;
Encoder myEnc(ENC_DT, ENC_CLK);
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_FAST);
IRrecv irrecv(IR);
decode_results ir;
long times,oldPosition = -999,newPosition, times1;
byte spektor[7];
int menu,vol_reg,in_reg,treb_reg,mid_reg,bass_reg,treb_f,gain0,gain1,gain2,gain3;
int lon,lon_f;
bool w=1,w1,gr1,gr2,www,set,mute=1;
byte ur[7],urr[7];
int fr,fl,rr,rl,sab,out_att,out,mid_f,mid_q,sab_f,bass_f,bass_q;
void setup(){
Wire.begin();Serial.begin(9600);
analogReadResolution(8);
analogReference(INTERNAL1V024);
irrecv.enableIRIn();
pinMode(A0,INPUT); // SAOUT TDA7419
pinMode(A1,OUTPUT); // SAIN TDA7419
pinMode(IN1,INPUT_PULLUP); // IN1 TDA7419
pinMode(IN2,INPUT_PULLUP); // IN2 TDA7419
pinMode(IN3,INPUT_PULLUP); // IN3 TDA7419
pinMode(SET,INPUT_PULLUP); // SET TDA7419
pinMode(MUTE,INPUT_PULLUP); // MUTE TDA7419
MsTimer2::set(1, to_Timer);MsTimer2::start();
vol_reg = EEPROM.read(0)-79;treb_reg = EEPROM.read(1)-15;mid_reg = EEPROM.read(2)-15;bass_reg = EEPROM.read(3)-15;
in_reg = EEPROM.read(4);gain1 = EEPROM.read(5);gain2 = EEPROM.read(6);gain3 = EEPROM.read(7);
lon = EEPROM.read(8);lon_f = EEPROM.read(9);
fr = EEPROM.read(10);fl = EEPROM.read(11);rl = EEPROM.read(12);rr = EEPROM.read(13);sab = EEPROM.read(14);
treb_f = EEPROM.read(15);mid_f = EEPROM.read(16);mid_q = EEPROM.read(17);
bass_f = EEPROM.read(18);bass_q = EEPROM.read(19);sab_f = EEPROM.read(20);
arr_7();
delay(500);audio();
}
void loop(){
/// IR ////////////////////////////////////////
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта
//////// BUTTON ////////////////////////////////////////////////
if(digitalRead(ENC_SW)==LOW&&menu<5||ir.value==IR2){menu++;cl();if(menu>3){menu=0;}arr_1();delay(200);}
if(digitalRead(IN1)==LOW&&menu<5||ir.value==IN1_IR){menu=4;cl();in_reg=1;arr_1();www=1;;delay(200);}
if(digitalRead(IN2)==LOW&&menu<5||ir.value==IN2_IR){menu=4;cl();in_reg=2;arr_1();www=1;delay(200);}
if(digitalRead(IN3)==LOW&&menu<5||ir.value==IN3_IR){menu=4;cl();in_reg=3;arr_1();www=1;delay(200);}
if(digitalRead(SET)==LOW&&set==0){menu=5;set=1;times1=millis();w=1;delay(200);}
if(digitalRead(SET)==LOW&&set==1){menu=0;set=0;;arr_1();www=1;delay(200);}
if(digitalRead(ENC_SW)==LOW&&menu>4){menu++;if(menu>10){menu=0;}times1=millis();w=1;set=0;delay(200);}
if((digitalRead(MUTE)==LOW||ir.value==MUTE_IR)&&mute==1){mute=0;menu=100;cl();
u8g.firstPage();do {u8g.setFont(u8g_font_profont12r);u8g.drawStr(53,30,"MUTE");} while( u8g.nextPage() );audio();delay(200);}
if((digitalRead(MUTE)==LOW||ir.value==MUTE_IR)&&mute==0){mute=1;menu=0;cl();arr_1();audio();delay(200);}
///////////////////////////////////////////////////////////////
//////// VOLUME //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==0){
if(newPosition != oldPosition){arr_2();vol_reg=vol_reg+newPosition;arr_3();arr_1();vol_func();audio();}
if(ir.value==IR4){vol_reg++;arr_4();cl();arr_1();vol_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){vol_reg++;gr2=0;cl();arr_1();vol_func();audio();}// кнопка >>>>>>
if(ir.value==IR5){vol_reg--;gr1=0;gr2=1;cl();arr_1();vol_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){vol_reg--;gr1=0;cl();arr_1();vol_func();audio();}// кнопка <<<<<<
if(w==1){w=0;
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"VOLUME");u8g.drawStr(0,18,"IN");
u8g.setPrintPos(20, 18);u8g.print(in_reg);u8g.drawStr(0,28,"GAIN");u8g.setPrintPos(30, 28);u8g.print(gain0);
arr_6();
u8g.drawStr(0,44,"TREBLE");u8g.setPrintPos(80, 44);u8g.print(treb_reg);u8g.drawStr(95,44,"dB");
u8g.drawStr(0,54,"MIDDLE");u8g.setPrintPos(80, 54);u8g.print(mid_reg);u8g.drawStr(95,54,"dB");
u8g.drawStr(0,64,"BASS");u8g.setPrintPos(80, 64);u8g.print(bass_reg);u8g.drawStr(95,64,"dB");
u8g.setFont(u8g_font_profont29r); u8g.setPrintPos(78, 22);u8g.print(vol_reg+79);
} while( u8g.nextPage() );
}}
/////////////////////////////////////////////////////////////////
//////// TREBLE //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==1){
if(newPosition != oldPosition){arr_2();treb_reg=treb_reg+newPosition;arr_3();arr_1();treb_func();audio();}
if(ir.value==IR4){treb_reg++;arr_4();cl();arr_1();treb_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){treb_reg++;gr2=0;cl();arr_1();treb_func();audio();}// кнопка >>>>>>
if(ir.value==IR5){treb_reg--;gr1=0;gr2=1;cl();arr_1();treb_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){treb_reg--;gr1=0;cl();arr_1();treb_func();audio();}// кнопка <<<<<<
if(w==1){w=0;
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont29r); u8g.setPrintPos(78, 22);u8g.print(treb_reg);
arr_6();
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"TREBLE");u8g.drawStr(0,44,"MIDDLE");u8g.setPrintPos(80, 44);u8g.print(mid_reg);u8g.drawStr(95,44,"dB");
u8g.drawStr(0,54,"BASS");u8g.setPrintPos(80, 54);u8g.print(bass_reg);u8g.drawStr(95,54,"dB");
u8g.drawStr(0,64,"VOLUME");u8g.setPrintPos(80, 64);u8g.print(vol_reg+79);u8g.drawStr(95,64,"dB");
} while( u8g.nextPage() );
}}
/////////////////////////////////////////////////////////////////
//////// MIDDLE //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==2){
if(newPosition != oldPosition){arr_2();mid_reg=mid_reg+newPosition;arr_3();arr_1();mid_func();audio();}
if(ir.value==IR4){mid_reg++;arr_4();cl();arr_1();mid_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){mid_reg++;gr2=0;cl();arr_1();mid_func();audio();}// кнопка >>>>>>
if(ir.value==IR5){mid_reg--;gr1=0;gr2=1;cl();arr_1();mid_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){mid_reg--;gr1=0;cl();arr_1();mid_func();audio();}// кнопка <<<<<<
if(w==1){w=0;
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont29r); u8g.setPrintPos(78, 22);u8g.print(mid_reg);
arr_6();
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"MIDDLE");u8g.drawStr(0,44,"BASS");u8g.setPrintPos(80, 44);u8g.print(bass_reg);u8g.drawStr(95,44,"dB");
u8g.drawStr(0,54,"VOLUME");u8g.setPrintPos(80, 54);u8g.print(vol_reg+79);u8g.drawStr(95,54,"dB");
u8g.drawStr(0,64,"TREBLE");u8g.setPrintPos(80, 64);u8g.print(treb_reg);u8g.drawStr(95,64,"dB");
} while( u8g.nextPage() );
}}
/////////////////////////////////////////////////////////////////
//////// BASS //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==3){
if(newPosition != oldPosition){arr_2();bass_reg=bass_reg+newPosition;arr_3();arr_1();bass_func();audio();}
if(ir.value==IR4){bass_reg++;arr_4();cl();arr_1();bass_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){bass_reg++;gr2=0;cl();arr_1();bass_func();audio();}// кнопка >>>>>>
if(ir.value==IR5){bass_reg--;gr1=0;gr2=1;cl();arr_1();bass_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){bass_reg--;gr1=0;cl();arr_1();bass_func();audio();}// кнопка <<<<<<
if(w==1){w=0;
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont29r); u8g.setPrintPos(78, 22);u8g.print(bass_reg);
arr_6();
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"BASS");u8g.drawStr(0,44,"VOLUME");u8g.setPrintPos(80, 44);u8g.print(vol_reg+79);u8g.drawStr(95,44,"dB");
u8g.drawStr(0,54,"TREBLE");u8g.setPrintPos(80, 54);u8g.print(mid_reg);u8g.drawStr(95,54,"dB");
u8g.drawStr(0,64,"MIDDLE");u8g.setPrintPos(80, 64);u8g.print(treb_reg);u8g.drawStr(95,64,"dB");
} while( u8g.nextPage() );
}}
/////////////////////////////////////////////////////////////////
////////////// INPUT GAIN ///////////////////////////////////////////////////////////////////
if(menu==4){
arr_7();
if (newPosition != oldPosition){arr_5();gain0=gain0+newPosition;arr_3();arr_1();www=1;gain_func();}
switch(in_reg){
case 1: gain1=gain0;break;
case 2: gain2=gain0;break;
case 3: gain3=gain0;break;
}
if(www==1){audio();www=0;}
if(w==1){w=0;
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont29r); u8g.setPrintPos(78, 22);u8g.print(in_reg);
arr_6();
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"INPUT");u8g.drawStr(0,44,"GAIN");
u8g.setFont(u8g_font_profont29r); u8g.setPrintPos(78, 60);u8g.print(gain0);
} while( u8g.nextPage() );
}}
/////////// loudness att/freq /////////////////////
if(menu==5){
if (newPosition != oldPosition){arr_5();lon=lon-newPosition;arr_3();w=1;lon_func();audio();}
if(digitalRead(IN1)==LOW){lon_f++;if(lon_f>3){lon_f=0;}w=1;;audio();delay(200);}
if(w==1){w=0;EEPROM.update(8,lon);EEPROM.update(9,lon_f);
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"LOUDNESS ATTENUATION");u8g.setPrintPos(50, 22);u8g.print(-lon);u8g.drawStr(80,22,"dB");
arr_6();
u8g.drawStr(0,44,"LOUDNESS FREQUENCY");
switch(lon_f){
case 0: u8g.drawStr(50, 60," FLAT");break;
case 1: u8g.drawStr(50, 60," 400Hz");break;
case 2: u8g.drawStr(50, 60," 800Hz");break;
case 3: u8g.drawStr(50, 60,"2400Hz");break;}
} while( u8g.nextPage() );
}}
///////////// out att ///////////////////////////////
if(menu==6){
if(digitalRead(IN1)==LOW){out++;if(out>4){out=0;}w=1;delay(200);}
switch(out){
case 0: out_att=fr;break;
case 1: out_att=fl;break;
case 2: out_att=rl;break;
case 3: out_att=rr;break;
case 4: out_att=sab;break;
}
if (newPosition != oldPosition){arr_5();out_att=out_att+newPosition;arr_3();w=1;att_func();www=1;}
switch(out){
case 0: fr=out_att;break;
case 1: fl=out_att;break;
case 2: rl=out_att;break;
case 3: rr=out_att;break;
case 4: sab=out_att;break;
}
if(www==1){audio();www=0; EEPROM.update(10,fr);EEPROM.update(11,fl);EEPROM.update(12,rl);EEPROM.update(13,rr);EEPROM.update(14,sab);}
if(w==1){w=0;
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"OUTPUT");
switch(out){
case 0: u8g.drawStr(45,8,"FR");break;
case 1: u8g.drawStr(45,8,"FL");break;
case 2: u8g.drawStr(45,8,"RL");break;
case 3: u8g.drawStr(45,8,"RR");break;
case 4: u8g.drawStr(45,8,"SAB");break;
}
u8g.setPrintPos(50, 22);u8g.print(out_att);u8g.drawStr(80,22,"dB");
} while( u8g.nextPage() );
}}
///////////////////////////////////////////////////////////////////
/////////// treb f /////////////////////
if(menu==7){
if (newPosition != oldPosition){arr_5();treb_f=treb_f+newPosition;arr_3();w=1;treb_f_func();audio();}
if(w==1){w=0;EEPROM.update(15,treb_f);
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"TREBLE CENTER FREQ");
switch(treb_f){
case 0: u8g.drawStr(40, 22,"10.0 kHz");break;
case 1: u8g.drawStr(40, 22,"12.5 kHz");break;
case 2: u8g.drawStr(40, 22,"15.0 kHz");break;
case 3: u8g.drawStr(40, 22,"17.5 kHz");break;}
} while( u8g.nextPage() );
}}
/////////// middle f/q /////////////////////
if(menu==8){
if (newPosition != oldPosition){arr_5();mid_f=mid_f+newPosition;arr_3();w=1;mid_f_func();audio();}
if(digitalRead(IN1)==LOW){mid_q++;mid_q_func();w=1;audio();delay(200);}
if(w==1){w=0;EEPROM.update(16,mid_f);EEPROM.update(17,mid_q);
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"MIDDLE CENTER FREQ");
switch(mid_f){
case 0: u8g.drawStr(40, 22,"0.5 kHz");break;
case 1: u8g.drawStr(40, 22,"1.0 kHz");break;
case 2: u8g.drawStr(40, 22,"1.5 kHz");break;
case 3: u8g.drawStr(40, 22,"2.5 kHz");break;}
arr_6();
u8g.drawStr(0,44,"MIDDLE Q FACTOR");
switch(mid_q){
case 0: u8g.drawStr(50, 60,"0.50");break;
case 1: u8g.drawStr(50, 60,"0.75");break;
case 2: u8g.drawStr(50, 60,"1.00");break;
case 3: u8g.drawStr(50, 60,"1.25");break;}
} while( u8g.nextPage() );
}}
/////////// bass f/q /////////////////////
if(menu==9){
if (newPosition != oldPosition){arr_5();bass_f=bass_f+newPosition;arr_3();w=1;bass_f_func();audio();}
if(digitalRead(IN1)==LOW){bass_q++;bass_q_func();w=1;audio();delay(200);}
if(w==1){w=0;EEPROM.update(18,bass_f);EEPROM.update(19,bass_q);
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"BASS CENTER FREQ");
switch(bass_f){
case 0: u8g.drawStr(40, 22," 60 Hz");break;
case 1: u8g.drawStr(40, 22," 80 Hz");break;
case 2: u8g.drawStr(40, 22,"100 Hz");break;
case 3: u8g.drawStr(40, 22,"200 Hz");break;}
arr_6();
u8g.drawStr(0,44,"BASS Q FACTOR");
switch(bass_q){
case 0: u8g.drawStr(50, 60,"1.00");break;
case 1: u8g.drawStr(50, 60,"1.25");break;
case 2: u8g.drawStr(50, 60,"1.50");break;
case 3: u8g.drawStr(50, 60,"2.00");break;}
} while( u8g.nextPage() );
}}
/////////// sab f /////////////////////
if(menu==10){
if (newPosition != oldPosition){arr_5();sab_f=sab_f+newPosition;arr_3();w=1;sab_f_func();audio();}
if(w==1){w=0;EEPROM.update(20,sab_f);
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);u8g.drawStr(0,8,"SUBWOOFER CENTER FREQ");
switch(sab_f){
case 0: u8g.drawStr(40, 22," FLAT ");break;
case 1: u8g.drawStr(40, 22," 80 Hz");break;
case 2: u8g.drawStr(40, 22,"120 Hz");break;
case 3: u8g.drawStr(40, 22,"160 Hz");break;}
} while( u8g.nextPage() );
}}
if(millis()-times1>TIME_SPEC*1000&&mute==1&&(menu<5||menu>50)){menu=100;spec_func();
if(newPosition != oldPosition){arr_5();myEnc.write(0);times1=millis();menu=0;w=1;}}
if(millis()-times>3000 && w1==1 && menu<5){
EEPROM.update(0,vol_reg+79);EEPROM.update(1,treb_reg+15);EEPROM.update(2,mid_reg+15);EEPROM.update(3,bass_reg+15);
EEPROM.update(4,in_reg);EEPROM.update(5,gain1);EEPROM.update(6,gain2);EEPROM.update(7,gain3);
menu=0;w1=0;w=1;myEnc.write(0);}
} // end loop
void arr_7(){switch(in_reg){
case 1: gain0=gain1;break;
case 2: gain0=gain2;break;
case 3: gain0=gain3;break;
}}
void arr_6(){u8g.drawLine(0, 32, 128, 32);}
void arr_5(){oldPosition = newPosition;}
void arr_4(){gr1=1;gr2=0;}
void arr_3(){myEnc.write(0);newPosition=0;}
void arr_2(){arr_5();if(newPosition>1){newPosition=1;}if(newPosition<-1){newPosition=-1;}}
void arr_1(){times=millis();times1=millis();w=1;w1=1;}
void cl(){ir.value=0;delay(300);}
void sab_f_func(){if(sab_f>3){sab_f=0;}if(sab_f<0){sab_f=3;}}
void bass_q_func(){if(bass_q>3){bass_q=0;}if(bass_q<0){bass_q=3;}}
void bass_f_func(){if(bass_f>3){bass_f=0;}if(bass_f<0){bass_f=3;}}
void mid_q_func(){if(mid_q>3){mid_q=0;}if(mid_q<0){mid_q=3;}}
void mid_f_func(){if(mid_f>3){mid_f=0;}if(mid_f<0){mid_f=3;}}
void treb_f_func(){if(treb_f>3){treb_f=0;}if(treb_f<0){treb_f=3;}}
void att_func(){if(out_att>15){out_att=15;}if(out_att<0){out_att=0;}}
void lon_func(){if(lon<0){lon=0;}if(lon>15){lon=15;}}
void gain_func(){if(gain0>15){gain0=15;}if(gain0<0){gain0=0;}}
void bass_func(){if(bass_reg<-15){bass_reg=-15;}if(bass_reg>15){bass_reg=15;}}
void mid_func(){if(mid_reg<-15){mid_reg=-15;}if(mid_reg>15){mid_reg=15;}}
void treb_func(){if(treb_reg<-15){treb_reg=-15;}if(treb_reg>15){treb_reg=15;}}
void vol_func(){if(vol_reg<-79){vol_reg=-79;}if(vol_reg>15){vol_reg=15;}}
void to_Timer(){newPosition = myEnc.read()/4;}
void audio(){
tda.setInput(in_reg,gain0,0); //вх1=1, вх2=2, вх3=3
tda.setInput2(0,0,0); // не используется
tda.setAtt_loudness(lon,lon_f,0,0);
tda.setSoft(mute,0,0,0,0);//mute = 1 off
tda.setVolume(vol_reg,0); // общая громкость int = -79...+15
tda.setAtt_LF(64+fl,0); // гром. лк фронт
tda.setAtt_RF(64+fr,0); // гром. пк фронт
tda.setAtt_LT(64+rl,0); // гром. лк тыл
tda.setAtt_RT(64+rr,0); // гром. пк тыл
tda.setAtt_SUB(64+sab,0); // гром. савбуфер
tda.setAtt_Mix(79,0); // не использует
tda.setFilter_Treble(treb_reg,treb_f,0);
tda.setFilter_Middle(mid_reg,mid_q,0);
tda.setFilter_Bass(bass_reg,bass_q,0);
tda.setSub_M_B(sab_f,mid_f,bass_f,0,0);
tda.setMix_Gain_Eff(1,0,0,0,0); // не используется
tda.setSpektor(2,0,1,0,1,0,0);
}
void spec_func(){
//// spectrum analyzer //////////////////////////////////////
digitalWrite(A1,LOW);digitalWrite(A1,HIGH);delay(SPED_SPEC);
for(int i=0;i<7;i++){
digitalWrite(A1,LOW);digitalWrite(A1,HIGH);
spektor[i]=analogRead(A0)*2;
if(spektor[i]<SIG_0_SPEC){spektor[i]=0;}}
u8g.firstPage(); do {
for (int pos = 0; pos < 7; pos++) {
for (int v_pos=0; v_pos<spektor[pos]+4;v_pos+=4){u8g.drawBox(pos*19, 61-v_pos, 13, 2);}}
} while( u8g.nextPage() );
}