#define IR_1 0x2FDD02F // Кнопка вверх
#define IR_2 0x2FD32CD // Кнопка вниз
#define IR_3 0x2FD906F // Кнопка >
#define IR_4 0x2FDF20D // Кнопка <
#define IR_5 0x2FD708F // Кнопка IN
#include <Wire.h>
#include <SPI.h>
#include <PCD8544.h>
#include <EEPROM.h>
#include <MsTimer2.h>
#include <Encoder.h>
#include <TDA7313.h>
#include <boarddefs.h>
#include <IRremote.h>
TDA7313 tda;
IRrecv irrecv(12); // указываем вывод модуля IR приемника
decode_results ir;
Encoder myEnc(9, 8);//CLK, DT подключение энкодера
PCD8544 lcd; // RST,CE,DC,DIN,CLK VCC,BL,GND (3,4,5,6,7 +3.3V,200 OM +3.3V, GND)
static const byte asd[] = { B00111100,B00000000,B00000000,B00000000,B00000000 };
static const byte asd1[] = { B00111100,B00111100,B00111100,B0000000,B11111111 };
static const byte asd2[] = { B00111100,B00111100,B00111100,B0000000,B00111100 };
unsigned long newPosition,time,oldPosition = -999;
int i,w=1,w1=1,w2=1,menu,vol,lr,rr,lf,rf,in,loud,gain,bass,treb,eeprom,n,menu2,ccc;
int gr1,gr2;
void setup() {
irrecv.enableIRIn();Serial.begin(9600);Wire.begin();
lcd.begin(84, 48);
lcd.setContrast(60);// контрастность 0-127
pinMode(10,INPUT);// SW кнопка энкодера
lcd.createChar(0, asd); lcd.createChar(1, asd1);lcd.createChar(2, asd2);
MsTimer2::set(1, to_Timer);MsTimer2::start();
vol = EEPROM.read(0);bass = EEPROM.read(1)-7;treb = EEPROM.read(2)-7;in = EEPROM.read(3);
loud = EEPROM.read(7);lr = EEPROM.read(8);rr = EEPROM.read(9);lf = EEPROM.read(10);rf = EEPROM.read(11);
audio();
}
void loop() {
if(digitalRead(10)==LOW&&menu<100){menu++;n++;w=1;w1=1;w2=1;myEnc.write(0);time=millis();eeprom=1;delay(200);if(menu>3){menu=0;}}
if(digitalRead(10)==LOW&&n>4){menu2++;menu=100;myEnc.write(0);time=millis();eeprom=1;cl();if(menu2>5){menu2=100;menu=0;n=0;}
if(menu2==1){lcd.setCursor(0,1);lcd.print(" MENU 2 ");delay(2000);cl();}}
if(digitalRead(10)==HIGH&&menu2==100){n=0;menu2=0;}
////////////////////// IR /////////////////////////////////////////////////
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();time=millis();eeprom=1;}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта
if(ir.value==IR_2){menu++;gr1=0;gr2=0;cl1();time=millis();myEnc.write(0);w=1;w1=1;w2=1;if(menu>2){menu=0;}}// меню кнопка вверх
if(ir.value==IR_1){menu--;gr1=0;gr2=0;cl1();time=millis();myEnc.write(0);w=1;w1=1;w2=1;if(menu<0){menu=2;}}// меню кнопка вниз
if(ir.value==IR_5){menu=3;in++;gr1=0;gr2=0;cl1();time=millis();myEnc.write(0);w=1;w1=1;w2=1;if(in>2){in=0;}if(in<0){in=2;}audio();}// КНОПКА ВЫБОРА ВХОДА IN
//////////////////////////////////////////////////////////////
if(menu<100){
lcd.setCursor(0,0);lcd.print("--------------");
////////////////////////////// vol ///////////////////////////////////////////////////////
if(menu==0){
if(ir.value==IR_3){vol++;gr1=1;gr2=0;cl1();w=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl1();w=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка >>>>>>
if(ir.value==IR_4){vol--;gr1=0;gr2=1;cl1();w=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl1();w=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
vol=vol+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}
}
if(w==1){w=0;
lcd.setCursor(0,1);lcd.print("}P");
for(i=15;i<68;i++){lcd.setCursor(i,1);lcd.write(0);}
lcd.setCursor(vol/1.3+15,1);if(menu==0){lcd.write(1);}else{lcd.write(2);}lcd.setCursor(72,1);if(vol<10){lcd.print(" ");}lcd.print(vol);
}
///////////////////////////////////////// bass ///////////////////////////////////
if(menu==1){
if(ir.value==IR_3){bass++;gr1=1;gr2=0;cl1();w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl1();w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка >>>>>>
if(ir.value==IR_4){bass--;gr1=0;gr2=1;cl1();w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl1();w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
bass=bass+newPosition;myEnc.write(0);newPosition=0;time=millis();w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}
}
if(w1==1){w1=0;
lcd.setCursor(0,2);lcd.print("H'");
for(i=8;i<60;i=i+1){lcd.setCursor(i+7,2);lcd.write(0);}
lcd.setCursor(bass*3+37,2);if(menu==1){lcd.write(1);}else{lcd.write(2);}lcd.setCursor(72,2);if(bass<10&&bass>=0){lcd.print(" ");}lcd.print(bass);
}
//////////////////////////////////////////// treble /////////////////////////////
if(menu==2){
if(ir.value==IR_3){treb++;gr1=1;gr2=0;cl1();w2=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl1();w2=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка >>>>>>
if(ir.value==IR_4){treb--;gr1=0;gr2=1;cl1();w2=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl1();w2=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
treb=treb+newPosition;myEnc.write(0);newPosition=0;time=millis();w2=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}
}
if(w2==1){w2=0;
lcd.setCursor(0,3);lcd.print("B'");
for(i=8;i<60;i=i+1){lcd.setCursor(i+7,3);lcd.write(0);}
lcd.setCursor(treb*3+37,3);if(menu==2){lcd.write(1);}else{lcd.write(2);}lcd.setCursor(72,3);if(treb<10&&treb>=0){lcd.print(" ");}lcd.print(treb);
}
lcd.setCursor(0,4);lcd.print("--------------");
///////////////////////IN //////////////////////////////////////////////////////
if(menu==3){
if (newPosition != oldPosition) {
oldPosition = newPosition;
in=in+newPosition;myEnc.write(0);newPosition=0;time=millis();if(in>2){in=2;}if(in<0){in=0;}audio();}
lcd.setCursor(0,5);lcd.print(" +INPUT ");lcd.print(in+1);lcd.print(" ");}
else{lcd.setCursor(0,5);lcd.print(" INPUT ");lcd.print(in+1);lcd.print(" ");}
}
//////////////// MENU2 //////////////////////////////////////////////////////////////
if(menu==100){
/////////////////////// att LR //////////////////////////////////////////////////////
if(menu2==1){
if (newPosition != oldPosition) {
oldPosition = newPosition;
lr=lr+newPosition;myEnc.write(0);newPosition=0;time=millis();if(lr>31){lr=31;}if(lr<0){lr=0;}audio();}
lcd.setCursor(0,0);lcd.print("+OUT LR ");lcd.print(lr);lcd.print(" ");}else{lcd.setCursor(0,0);lcd.print(" OUT LR ");lcd.print(lr);lcd.print(" ");}
/////////////////////// att RR //////////////////////////////////////////////////////
if(menu2==2){
if (newPosition != oldPosition) {
oldPosition = newPosition;
rr=rr+newPosition;myEnc.write(0);newPosition=0;time=millis();if(rr>31){rr=31;}if(rr<0){rr=0;}audio();}
lcd.setCursor(0,1);lcd.print("+OUT RR ");lcd.print(rr);lcd.print(" ");}else{lcd.setCursor(0,1);lcd.print(" OUT RR ");lcd.print(rr);lcd.print(" ");}
/////////////////////// att LF //////////////////////////////////////////////////////
if(menu2==3){
if (newPosition != oldPosition) {
oldPosition = newPosition;
lf=lf+newPosition;myEnc.write(0);newPosition=0;time=millis();if(lf>31){lf=31;}if(lf<0){lf=0;}audio();}
lcd.setCursor(0,2);lcd.print("+OUT LF ");lcd.print(lf);lcd.print(" ");}else{lcd.setCursor(0,2);lcd.print(" OUT LF ");lcd.print(lf);lcd.print(" ");}
/////////////////////// att RF //////////////////////////////////////////////////////
if(menu2==4){
if (newPosition != oldPosition) {
oldPosition = newPosition;
rf=rf+newPosition;myEnc.write(0);newPosition=0;time=millis();if(rf>31){rf=31;}if(rf<0){rf=0;}audio();}
lcd.setCursor(0,3);lcd.print("+OUT RF ");lcd.print(rf);lcd.print(" ");}else{lcd.setCursor(0,3);lcd.print(" OUT RF ");lcd.print(rf);lcd.print(" ");}
/////////////////////// LOUDNESS //////////////////////////////////////////////////////
if(menu2==5){
if (newPosition != oldPosition) {
oldPosition = newPosition;
loud=loud+newPosition;myEnc.write(0);newPosition=0;time=millis();if(loud>1){loud=1;}if(loud<0){loud=0;}audio();}
lcd.setCursor(0,4);lcd.print("+LOUDNESS ");lcd.print(loud);lcd.print(" ");}else{lcd.setCursor(0,4);lcd.print(" LOUDNESS ");lcd.print(loud);lcd.print(" ");}
///////////////////////////////////////////////////////////////////////////////////////
}
if(millis()-time>10000 && eeprom==1){
EEPROM.update(0,vol);
EEPROM.update(1,bass+7);
EEPROM.update(2,treb+7);
EEPROM.update(3,in);
EEPROM.update(7,loud);
EEPROM.update(8,lr);EEPROM.update(9,rr);EEPROM.update(10,lf);EEPROM.update(11,rf);
cl();menu=0;menu2=100;eeprom=0;w=1;w1=1;w2=1;myEnc.write(0);newPosition=0;time=millis();}
}
void cl(){delay(300);lcd.clear();}
void cl1(){ir.value=0;delay(200);}
void audio(){
tda.setVolume(vol); // громкость 0...63
tda.setAttLR(lr); // аттенюатор LR 0...31
tda.setAttRR(rr); // аттенюатор RR 0...31
tda.setAttLF(lf); // аттенюатор LF 0...31
tda.setAttRF(rf); // аттенюатор RF 0...31
tda.setSwitch(in,loud,3); // вход 0...2, тонкомпенсация 1 вкл 0 выкл, усиление 0...3
tda.setBass(bass); // тембр НЧ -7...+7
tda.setTreble(treb); // тембр ВЧ -7...+7
}
void to_Timer(){newPosition = myEnc.read()/4;}