#define MUTE_OUT    0
#define GND5        13
#define SEG_G       2   // in51
#define DP          3   // in20
#define SEG_A       4   // out51
#define SEG_F       5   // out20
#define SEG_D       6
#define SEG_E       7  
#define SEG_B       8   // stby
#define SEG_C       9   // mute
#define ENC_CLK     10  // энкодер контакт 1
#define ENC_DT      11  // энкодер контакт 2
#define PIN_IR      12  // вход IR
#define PIN_IN      1  // кнопка INPUT
#define GND1        A0
#define GND2        A1
#define GND3        A2
#define GND4        A3
// SDA              A4 
// SCL              A5
#define VOL_UP_IR      0x33B8D22D
#define VOL_DW_IR      0x33B8F20D
#define CENTER_UP_IR   0x33B8D42B
#define CENTER_DW_IR   0x33B8906F
#define SURR_UP_IR     0x33B854AB
#define SURR_DW_IR     0x33B804FB
#define SUB_UP_IR      0x33B832CD
#define SUB_DW_IR      0x33B8C43B
#define BALL_UP_IR     0x33B8827D
#define BALL_DW_IR     0x33B8847B
#define ST_BY_IR       0x33B800FF
#define MUTE_IR        0x33B8649B
#define MODE_IR        0x33B8A45B
#define INPUT_IR       0x33B844BB
#include <Wire.h>
#include <Encoder.h>            // http://rcl-radio.ru/wp-content/uploads/2019/05/Encoder.zip
#include <MsTimer2.h>           // http://rcl-radio.ru/wp-content/uploads/2018/11/MsTimer2.zip       
#include <PT2323.h>             // http://rcl-radio.ru/wp-content/uploads/2019/06/PT2323-2.zip
#include <TDA7448.h>            // http://rcl-radio.ru/wp-content/uploads/2019/08/TDA7448-1.zip
#include <EEPROM.h>
#include <IRremote.h>           // http://rcl-radio.ru/wp-content/uploads/2019/06/IRremote.zip     
#include <boarddefs.h>          // входит в состав библиотеки IRremote
 
 PT2323 pt; 
 TDA7448 tda;
 IRrecv irrecv(PIN_IR); // указываем вывод модуля IR приемника
 Encoder myEnc(ENC_DT, ENC_CLK);// DT, CLK
 decode_results ir; 
 
 int an, segm,i,menu,input,in_data,vol;
 byte a[4];
 long oldPosition  = -999,newPosition,times,tim_mut; 
 bool mute_led=0,power_led=0,in5_1_led=0,aux_led=0,out5_1_led=0,out2_1_led=0;
 bool w,w1,dp_led,gr1,gr2,mut,power;
 int surr,center,subw,ball;
 bool out;
  
void setup(){ 
    Wire.begin();
    irrecv.enableIRIn();
  //  Serial.begin(9600);
    MsTimer2::set(2, to_Timer);MsTimer2::start();
    pinMode(PIN_IN,INPUT); // кнопка INPUT
    pinMode(SEG_A,OUTPUT);
    pinMode(SEG_B,OUTPUT);
    pinMode(SEG_C,OUTPUT);
    pinMode(SEG_D,OUTPUT);
    pinMode(SEG_E,OUTPUT);
    pinMode(SEG_F,OUTPUT);
    pinMode(SEG_G,OUTPUT);
    pinMode(DP,OUTPUT);
    pinMode(GND1,OUTPUT);
    pinMode(GND2,OUTPUT);
    pinMode(GND3,OUTPUT);
    pinMode(GND4,OUTPUT);
    pinMode(GND5,OUTPUT);
    pinMode(MUTE_OUT,OUTPUT);
   // digitalWrite(MUTE_OUT,LOW);
    digitalWrite(MUTE_OUT,HIGH);
    if(EEPROM.read(100)!=0){for(int i=0;i<101;i++){EEPROM.update(i,0);}}// очистка памяти при первом включении  
    input = EEPROM.read(0);vol = EEPROM.read(1)-79;center = EEPROM.read(2);subw = EEPROM.read(3);surr = EEPROM.read(4);ball = EEPROM.read(5)-10;out = EEPROM.read(6);
    if(out>1){out=1;}
    if(input>4){input=4;}
    if(input==0){in_data=0;}
    if(input==1){in_data=4;}
    delay(300);
    audio_pt();
    audio_tda();
}
 
void loop(){ 
/// IR ////////////////////////////////////////
   if ( irrecv.decode( &ir )) {
 // Serial.print("0x");Serial.println( ir.value,HEX);
   irrecv.resume();times=millis();w=1;w1=1;}
   if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта   
//// BUTTON + IR ////////////////////////////////////////////////
if(ir.value==ST_BY_IR && power==1){power=0;menu=0;mut=0;audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// power
if(ir.value==ST_BY_IR && power==0){power=1;menu=100;mut=1;audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// power
if(power==0){
  power_led=0;
  
if(ir.value==MUTE_IR && mut==1){mut=0;menu=0;audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// mute
if(ir.value==MUTE_IR && mut==0){mut=1;menu=100;audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// mute
if(mut==0){
if((digitalRead(PIN_IN)==LOW || ir.value==INPUT_IR) && input==1){input=0;in_data=0;audio_pt();cl();w=1;w1=1;times=millis();delay(250);}// input
if((digitalRead(PIN_IN)==LOW || ir.value==INPUT_IR) && input==0){input=1;in_data=4;audio_pt();cl();w=1;w1=1;times=millis();delay(250);}// input
if(ir.value==VOL_UP_IR && menu!=0){vol++;menu=0;gr1=1;gr2=0;cl();times=millis();w=1;w1=1;vol_func();audio_tda();}// кнопка > 
if(ir.value==VOL_DW_IR && menu!=0){vol--;menu=0;gr1=0;gr2=1;cl();times=millis();w=1;w1=1;vol_func();audio_tda();}// кнопка <
if(ir.value==MODE_IR && out==1){out=0;audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// out
if(ir.value==MODE_IR && out==0){out=1;audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// out
if(ir.value==SUB_UP_IR && menu!=1){subw++;menu=1;sub_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// subw
if(ir.value==SUB_DW_IR && menu!=1){subw--;menu=1;sub_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// subw
if(ir.value==CENTER_UP_IR && menu!=2){center++;menu=2;center_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// subw
if(ir.value==CENTER_DW_IR && menu!=2){center--;menu=2;center_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// subw
if(ir.value==SURR_UP_IR && menu!=3){surr++;menu=3;surr_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// surr
if(ir.value==SURR_DW_IR && menu!=3){surr--;menu=3;surr_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// surr
if(ir.value==BALL_UP_IR && menu!=4){ball++;menu=4;ball_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// ball
if(ir.value==BALL_DW_IR && menu!=4){ball--;menu=4;ball_func();audio_tda();cl();w=1;w1=1;times=millis();delay(250);}// ball
}
  if(out==0){out5_1_led=1;out2_1_led=0;}
  if(out==1){out5_1_led=0;out2_1_led=1;}
  if(input==0){aux_led=1;in5_1_led=0;}else{in5_1_led=1;aux_led=0;}
}
else{power_led=1;out5_1_led=0;out2_1_led=0;mute_led=0;aux_led=0;in5_1_led=0;a[0]=10;a[1]=10;a[2]=10;a[3]=10;an=5;dp_led=0;}
//// VOLUME ////////////////////////////////////////////////
if(menu==0){
  if(input==0){a[0]=2;a[1]=0;dp_led=1;}
  if(input==1){a[0]=5;a[1]=1;dp_led=1;}
   
   if(ir.value==VOL_UP_IR){vol++;gr1=1;gr2=0;cl();times=millis();w=1;w1=1;vol_func();audio_tda();}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl();times=millis();w=1;w1=1;vol_func();audio_tda();}// кнопка >>>>>>
   if(ir.value==VOL_DW_IR){vol--;gr1=0;gr2=1;cl();times=millis();w=1;w1=1;vol_func();audio_tda();}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl();times=millis();w=1;w1=1;vol_func();audio_tda();}// кнопка <<<<<<  
 
  if (newPosition != oldPosition){oldPosition = newPosition;myEnc.write(0);enc_err();vol=vol+newPosition;
   newPosition=0;w=1;w1=1;times=millis();vol_func();audio_tda();}
    a[2]=(79+vol)/10;
    a[3]=(79+vol)%10;
    //a[3]=out;
  }  
///////////////////////////////////////////////////////////
//// ATT SUBW ///////////////////////////////////////////////////////////
if(menu==1){
   if(ir.value==SUB_UP_IR){subw++;gr1=1;gr2=0;cl();times=millis();w=1;w1=1;sub_func();audio_tda();}// кнопка > 
   if(ir.value==SUB_DW_IR){subw--;gr1=0;gr2=1;cl();times=millis();w=1;w1=1;sub_func();audio_tda();}// кнопка <
       a[0]=11;
       a[1]=12;
       a[2]=(subw-5)/10%10;
       a[3]=(subw-5)%10;
      if(subw-5>=0) {a[2]=10;a[3]=abs(subw-5);}
        else{a[2]=17;a[3]=abs(subw-5);}  
  }
//// ATT CENTER ///////////////////////////////////////////////////////////
if(menu==2){
   if(ir.value==CENTER_UP_IR){center++;gr1=1;gr2=0;cl();times=millis();w=1;w1=1;center_func();audio_tda();}// кнопка > 
   if(ir.value==CENTER_DW_IR){center--;gr1=0;gr2=1;cl();times=millis();w=1;w1=1;center_func();audio_tda();}// кнопка <
       a[0]=13;
       a[1]=14;
       a[2]=(center-5)/10%10;
       a[3]=(center-5)%10;
      if(center-5>=0) {a[2]=10;a[3]=abs(center-5);}
        else{a[2]=17;a[3]=abs(center-5);}   
  }  
//// ATT SURR ///////////////////////////////////////////////////////////
if(menu==3){
   if(ir.value==SURR_UP_IR){surr++;gr1=1;gr2=0;cl();times=millis();w=1;w1=1;surr_func();audio_tda();}// кнопка > 
   if(ir.value==SURR_DW_IR){surr--;gr1=0;gr2=1;cl();times=millis();w=1;w1=1;surr_func();audio_tda();}// кнопка < 
       a[0]=11;
       a[1]=15;
       a[2]=(surr-5)/10%10;
       a[3]=(surr-5)%10;
      if(surr-5>=0) {a[2]=10;a[3]=abs(surr-5);}
        else{a[2]=17;a[3]=abs(surr-5);}    
  } 
//// BALLANCE SURR ///////////////////////////////////////////////////////////
if(menu==4){
   if(ir.value==BALL_UP_IR){ball++;gr1=1;gr2=0;cl();times=millis();w=1;w1=1;ball_func();audio_tda();}// кнопка > 
   if(ir.value==BALL_DW_IR){ball--;gr1=0;gr2=1;cl();times=millis();w=1;w1=1;ball_func();audio_tda();}// кнопка < 
       a[0]=12;
       a[1]=16;
      if(ball>=0) {a[2]=abs(ball/10);;a[3]=abs(ball%10);}
      else if(ball==-10){a[2]=18;a[3]=0;}
        else{a[2]=17;a[3]=abs(ball);}
  }  
if(mut==1 && power==0){
  if(millis()-tim_mut>500){tim_mut=millis();}
  if(millis()-tim_mut<250){a[0]=10;a[1]=10;a[2]=10;a[3]=10;dp_led=0;mute_led=0;}
  if(millis()-tim_mut>250){
          a[2]=(79+vol)/10;a[3]=(79+vol)%10;
            if(input==0){a[0]=2;a[1]=0;dp_led=1;}
            if(input==1){a[0]=5;a[1]=1;dp_led=1;}
            mute_led=1;
    }
  }      
////////////////// EEPROM //////////////////////////////////////////////////////////////
 if(millis()-times>5000 && w1==1 && power==0 && mut==0){
     EEPROM.update(0,input);EEPROM.update(1,vol+79);EEPROM.update(2,center);EEPROM.update(3,subw);EEPROM.update(4,surr);EEPROM.update(5,ball+10);EEPROM.update(6,out);
     menu=0;w1=0;w=1;}       
 }// loop
void ball_func(){if(ball<-10){ball=-10;}if(ball>10){ball=10;}}
void surr_func(){if(surr<0){surr=0;}if(surr>10){surr=10;}}
void cl(){ir.value=0;delay(100);}
void center_func(){if(center<0){center=0;}if(center>10){center=10;}}
void vol_func(){if(vol>=0){vol=0;}if(vol<-79){vol=-79;}}
void sub_func(){if(subw<0){subw=0;}if(subw>10){subw=10;}}
void enc_err(){if(newPosition>1){newPosition=1;}if(newPosition<-1){newPosition=-1;}}
void to_Timer(){newPosition = myEnc.read()/4;
  switch(i){
    case 0: segm = 10; segment();an=5;anod();delay(1);segm=a[0];ch(DP,dp_led); an=0; anod(); segment();break;
    case 1: segm = 10; segment();an=5;anod();delay(1);segm=a[1]; an=1; anod(); segment();break;
    case 2: segm = 10; segment();an=5;anod();delay(1);segm=a[2]; an=2; anod(); segment();break;
    case 3: segm = 10; segment();an=5;anod();delay(1);segm=a[3]; an=3; anod(); segment();break;
    case 4: segm = 10; segment();an=5;anod();delay(1);
       ch(SEG_C,mute_led);
       ch(SEG_B,power_led);
       ch(SEG_G,in5_1_led);
       ch(DP,aux_led);
       ch(SEG_A,out5_1_led);
       ch(SEG_F,out2_1_led);
    an=4; anod();break;
  }
   i++;if(i>4){i=0;} 
   delay(1);
  } 
 
void segment(){
  switch(segm){                                                                 
             //  A          B           C            D          E          F         G  
    case 0: ch(SEG_A,1);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,1);ch(SEG_G,0);break;// 0 
    case 1: ch(SEG_A,0);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,0);ch(SEG_E,0);ch(SEG_F,0);ch(SEG_G,0);break;// 1
    case 2: ch(SEG_A,1);ch(SEG_B,1);ch(SEG_C,0);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,0);ch(SEG_G,1);break;// 2
    case 3: ch(SEG_A,1);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,0);ch(SEG_F,0);ch(SEG_G,1);break;// 3   
    case 4: ch(SEG_A,0);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,0);ch(SEG_E,0);ch(SEG_F,1);ch(SEG_G,1);break;// 4 
    case 5: ch(SEG_A,1);ch(SEG_B,0);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,0);ch(SEG_F,1);ch(SEG_G,1);break;// 5 
    case 6: ch(SEG_A,1);ch(SEG_B,0);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,1);ch(SEG_G,1);break;// 6 
    case 7: ch(SEG_A,1);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,0);ch(SEG_E,0);ch(SEG_F,0);ch(SEG_G,0);break;// 7 
    case 8: ch(SEG_A,1);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,1);ch(SEG_G,1);break;// 8
    case 9: ch(SEG_A,1);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,0);ch(SEG_F,1);ch(SEG_G,1);break;// 9 
   case 10: ch(SEG_A,0);ch(SEG_B,0);ch(SEG_C,0);ch(SEG_D,0);ch(SEG_E,0);ch(SEG_F,0);ch(SEG_G,0);ch(DP,0);break;// пусто  
   case 11: ch(SEG_A,1);ch(SEG_B,0);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,0);ch(SEG_F,1);ch(SEG_G,1);ch(DP,0);break;// S
   case 12: ch(SEG_A,0);ch(SEG_B,0);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,1);ch(SEG_G,1);ch(DP,0);break;// b
   case 13: ch(SEG_A,1);ch(SEG_B,0);ch(SEG_C,0);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,1);ch(SEG_G,0);ch(DP,0);break;// C
   case 14: ch(SEG_A,0);ch(SEG_B,0);ch(SEG_C,1);ch(SEG_D,0);ch(SEG_E,1);ch(SEG_F,0);ch(SEG_G,1);ch(DP,0);break;// n
   case 15: ch(SEG_A,0);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,1);ch(SEG_G,0);ch(DP,0);break;// U
   case 16: ch(SEG_A,0);ch(SEG_B,0);ch(SEG_C,0);ch(SEG_D,1);ch(SEG_E,1);ch(SEG_F,1);ch(SEG_G,0);ch(DP,0);break;// L 
   case 17: ch(SEG_A,0);ch(SEG_B,0);ch(SEG_C,0);ch(SEG_D,0);ch(SEG_E,0);ch(SEG_F,0);ch(SEG_G,1);ch(DP,0);break;// - 
   case 18: ch(SEG_A,0);ch(SEG_B,1);ch(SEG_C,1);ch(SEG_D,0);ch(SEG_E,0);ch(SEG_F,0);ch(SEG_G,1);ch(DP,0);break;// -1 
  }}
void anod(){
  switch(an){                                                             
    case 0:ch(GND1,0);ch(GND2,1);ch(GND3,1);ch(GND4,1);ch(GND5,1);  break;  
    case 1:ch(GND1,1);ch(GND2,0);ch(GND3,1);ch(GND4,1);ch(GND5,1);  break; 
    case 2:ch(GND1,1);ch(GND2,1);ch(GND3,0);ch(GND4,1);ch(GND5,1);  break;  
    case 3:ch(GND1,1);ch(GND2,1);ch(GND3,1);ch(GND4,0);ch(GND5,1);  break;  
    case 4:ch(GND1,1);ch(GND2,1);ch(GND3,1);ch(GND4,1);ch(GND5,0);  break; 
    case 5:ch(GND1,1);ch(GND2,1);ch(GND3,1);ch(GND4,1);ch(GND5,1);  break;   
  }}
void ch(int pin, int logic){digitalWrite(pin,logic);}
void audio_pt(){
  pt.setInput(in_data); // входы 1...4 (0...3) или 6 моно (4)
  pt.setMute(0,0,0,0,0,0,0); // MUTE OFF для выходов (0,0,0,0,0,0,0); 
                              // MUTE ON  для выходов (1,1,1,1,1,1,1); 
                              // fl,  fr,  ct,  sub,  sl,  sr, все  выходы mute
  pt.setFeature(0,0); // дополнительные функции 0...3
                     // 0 - Enhanced Surround Function Active
                     // 1 - Enhanced Surround Function Disabled
                     // 0 - Mixed Channel (0dB) Setup
                     // 1 - Mixed Channel (+6dB) Setup  
}
void audio_tda(){// 0 макс громк, -79(79) мин громк 
if(mut==0){ 
  tda.setAtt(1,vol);// FL
  tda.setAtt(2,vol);// FR
 if(out==0){ tda.setAtt(3,vol+center);}else{tda.setAtt(3,-79);}// CT
  tda.setAtt(4,vol+subw);// SUB 
 if(out==0){ tda.setAtt(5,vol+surr+10+ball);}else{tda.setAtt(5,-79);}// SL  
 if(out==0){ tda.setAtt(6,vol+surr+10-ball);}else{tda.setAtt(6,-79);}// SR
}
else{
  tda.setAtt(1,-79);// FL
  tda.setAtt(2,-79);// FR
  tda.setAtt(3,-79);// CT
  tda.setAtt(4,-79);// SUB 
  tda.setAtt(5,-79);// SL
  tda.setAtt(6,-79);// SR
  }
  }