Тема: PT2258 + OLED 0.96 + encoder + IR
Основная статья - http://rcl-radio.ru/?p=128120
D10 - MUTE КНОПКА SW энкодера
D2 - КНОПКА SET
D4 - КНОПКА POWER
D5 - Выход управления STANDBY усилителя
Если нужна инверсия выхода STANDBY, то замените строку:
if(power==1){digitalWrite(5,LOW);}else{digitalWrite(5,HIGH);}
на
if(power==0){digitalWrite(5,LOW);}else{digitalWrite(5,HIGH);}
#define POWER 0x2FD00FF
#define DW 0x2FD906F
#define UP 0x2FDF20D
#define MUT 0x2FDEA15
#define SET 0x2FD6A95
#include <Wire.h>
#include <OLED_I2C.h> // http://rcl-radio.ru/wp-content/uploads/2022/01/OLED_I2C.zip
#include <Encoder.h> // http://rcl-radio.ru/wp-content/uploads/2019/05/Encoder.zip
#include <EEPROM.h>
#include <MsTimer2.h> // http://rcl-radio.ru/wp-content/uploads/2018/11/MsTimer2.zip
#include <boarddefs.h> // входит в состав библиотеки IRremote
#include <IRremote.h> // http://rcl-radio.ru/wp-content/uploads/2019/06/IRremote.zip
#include <PT2258.h> // https://github.com/liman324/PT2258.git
PT2258 pt;
OLED myOLED(SDA, SCL, 8);
extern uint8_t SmallFont[],BigNumbers[];
IRrecv irrecv(12); // указываем вывод модуля IR приемника
Encoder myEnc(9, 8);// DT, CLK
decode_results ir;
unsigned long times,oldPosition = -999,newPosition;
int vol,ch1,ch2,ch3,ch4,ch5,ch6,menu,mute;
bool w,w2,gr1,gr2,power;
void setup(){
Wire.begin();
Wire.setClock(400000L);
Serial.begin(9600);
irrecv.enableIRIn();
myOLED.begin();
myOLED.setBrightness(100);
pinMode(10,INPUT); // MUTE КНОПКА SW энкодера
pinMode(2,INPUT_PULLUP); // КНОПКА SET
pinMode(4,INPUT_PULLUP); // КНОПКА POWER
pinMode(5,OUTPUT); // Выход управления STANDBY усилителя
MsTimer2::set(3, to_Timer);MsTimer2::start();
if(EEPROM.read(100)!=0){for(int i=0;i<101;i++){EEPROM.update(i,0);}}// очистка памяти при первом включении
myOLED.clrScr();
myOLED.setFont(SmallFont);
myOLED.print(F("Sound Processors"), CENTER, 10);
myOLED.print(F("PT2258"), CENTER, 30);
myOLED.update();
delay(2000);
vol = EEPROM.read(0);ch1 = EEPROM.read(1);ch2 = EEPROM.read(2);ch3 = EEPROM.read(3);
ch4 = EEPROM.read(4);ch5 = EEPROM.read(5);ch6 = EEPROM.read(6);
audio();
}
void loop(){
////////////////////////////////// IR ////////////////////////////////////////
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();times=millis();w=1;w2=1;}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта
///////// BUTTON /////////////////////////////////////////////////////////////
if(power==0){
if((digitalRead(10)==LOW||ir.value==MUT)&&mute==0){mute=1;w=1;menu=100;cl();audio();myOLED.clrScr();myOLED.setFont(SmallFont);myOLED.print(F("MUTE"), CENTER, 25);myOLED.update();}
if((digitalRead(10)==LOW||ir.value==MUT)&&mute==1){mute=0;w=1;menu=0;cl();audio();}
if(digitalRead(2)==LOW||ir.value==SET){menu++;if(menu>6){menu=0;}times=millis();w=1;w2=1;cl();}
}
/// POWER ////////////////////////////////////
if((digitalRead(4)==LOW||ir.value==POWER)&&power==0){power=1;menu=100;myOLED.setBrightness(1);cl();myEnc.write(0);mute=1;
myOLED.clrScr();myOLED.setFont(SmallFont);myOLED.print(F("POWER OFF"), CENTER, 10);myOLED.update();
audio();delay(2000);cl();}
if((digitalRead(4)==LOW||ir.value==POWER)&&power==1&&digitalRead(10)==HIGH){power=0;menu=0;myOLED.setBrightness(100);cl();myEnc.write(0);mute=0;w=1;
myOLED.clrScr();myOLED.setFont(SmallFont);myOLED.print(F("POWER ON"), CENTER, 10);myOLED.update();
audio();delay(2000);cl();}
/// standby output
if(power==1){digitalWrite(5,LOW);}else{digitalWrite(5,HIGH);}
//////// VOLUME //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==0){
if(newPosition != oldPosition){oldPosition = newPosition;vol=vol-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;vol_func();audio();}
if(ir.value==DW){vol--;gr1=1;gr2=0;cl();times=millis();w=1;w2=1;vol_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){vol--;gr2=0;cl();times=millis();w=1;w2=1;vol_func();audio();}// кнопка >>>>>>
if(ir.value==UP){vol++;gr1=0;gr2=1;cl();times=millis();w=1;w2=1;vol_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){vol++;gr1=0;cl();times=millis();w=1;w2=1;vol_func();audio();}// кнопка <<<<<<
if(w==1){w=0;
myOLED.clrScr();
myOLED.setFont(SmallFont);myOLED.print(F("VOLUME"), LEFT, 0);myOLED.setFont(BigNumbers);myOLED.printNumI(64-vol, 75, 0);
myOLED.drawLine(0, 30, 128, 30);
myOLED.setFont(SmallFont);myOLED.print(F("LF"), LEFT, 35);myOLED.printNumI(-ch1, 40, 35);
myOLED.setFont(SmallFont);myOLED.print(F("RF"), 70, 35);myOLED.printNumI(-ch2, 110, 35);
myOLED.setFont(SmallFont);myOLED.print(F("LR"), LEFT, 45);myOLED.printNumI(-ch3, 40, 45);
myOLED.setFont(SmallFont);myOLED.print(F("RR"), 70, 45);myOLED.printNumI(-ch4, 110, 45);
myOLED.setFont(SmallFont);myOLED.print(F("CEN"), LEFT, 55);myOLED.printNumI(-ch5, 40, 55);
myOLED.setFont(SmallFont);myOLED.print(F("SUB"), 70, 55);myOLED.printNumI(-ch6, 110, 55);
myOLED.update();
}}
//////// ATT LF //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==1){
if(newPosition != oldPosition){oldPosition = newPosition;ch1=ch1-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ch1_conf();audio();}
if(ir.value==DW){ch1--;gr1=1;gr2=0;cl();times=millis();w=1;w2=1;ch1_conf();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){ch1--;gr2=0;cl();times=millis();w=1;w2=1;ch1_conf();audio();}// кнопка >>>>>>
if(ir.value==UP){ch1++;gr1=0;gr2=1;cl();times=millis();w=1;w2=1;ch1_conf();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){ch1++;gr1=0;cl();times=millis();w=1;w2=1;ch1_conf();audio();}// кнопка <<<<<<
if(w==1){w=0;
myOLED.clrScr();
myOLED.setFont(SmallFont);myOLED.print(F("ATT_LF"), LEFT, 0);myOLED.setFont(BigNumbers);myOLED.printNumI(-ch1, 65, 0);
myOLED.setFont(SmallFont);myOLED.print(F("dB"), RIGHT, 20);
myOLED.update();
}}
//////// ATT RF //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==2){
if(newPosition != oldPosition){oldPosition = newPosition;ch2=ch2-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ch2_conf();audio();}
if(ir.value==DW){ch2--;gr1=1;gr2=0;cl();times=millis();w=1;w2=1;ch2_conf();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){ch2--;gr2=0;cl();times=millis();w=1;w2=1;ch2_conf();audio();}// кнопка >>>>>>
if(ir.value==UP){ch2++;gr1=0;gr2=1;cl();times=millis();w=1;w2=1;ch2_conf();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){ch2++;gr1=0;cl();times=millis();w=1;w2=1;ch2_conf();audio();}// кнопка <<<<<<
if(w==1){w=0;
myOLED.clrScr();
myOLED.setFont(SmallFont);myOLED.print(F("ATT_RF"), LEFT, 0);myOLED.setFont(BigNumbers);myOLED.printNumI(-ch2, 65, 0);
myOLED.setFont(SmallFont);myOLED.print(F("dB"), RIGHT, 20);
myOLED.update();
}}
//////// ATT LR //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==3){
if(newPosition != oldPosition){oldPosition = newPosition;ch3=ch3-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ch3_conf();audio();}
if(ir.value==DW){ch3--;gr1=1;gr2=0;cl();times=millis();w=1;w2=1;ch3_conf();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){ch3--;gr2=0;cl();times=millis();w=1;w2=1;ch3_conf();audio();}// кнопка >>>>>>
if(ir.value==UP){ch3++;gr1=0;gr2=1;cl();times=millis();w=1;w2=1;ch3_conf();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){ch3++;gr1=0;cl();times=millis();w=1;w2=1;ch3_conf();audio();}// кнопка <<<<<<
if(w==1){w=0;
myOLED.clrScr();
myOLED.setFont(SmallFont);myOLED.print(F("ATT_LR"), LEFT, 0);myOLED.setFont(BigNumbers);myOLED.printNumI(-ch3, 65, 0);
myOLED.setFont(SmallFont);myOLED.print(F("dB"), RIGHT, 20);
myOLED.update();
}}
//////// ATT RR //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==4){
if(newPosition != oldPosition){oldPosition = newPosition;ch4=ch4-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ch4_conf();audio();}
if(ir.value==DW){ch4--;gr1=1;gr2=0;cl();times=millis();w=1;w2=1;ch4_conf();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){ch4--;gr2=0;cl();times=millis();w=1;w2=1;ch4_conf();audio();}// кнопка >>>>>>
if(ir.value==UP){ch4++;gr1=0;gr2=1;cl();times=millis();w=1;w2=1;ch4_conf();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){ch4++;gr1=0;cl();times=millis();w=1;w2=1;ch4_conf();audio();}// кнопка <<<<<<
if(w==1){w=0;
myOLED.clrScr();
myOLED.setFont(SmallFont);myOLED.print(F("ATT_RR"), LEFT, 0);myOLED.setFont(BigNumbers);myOLED.printNumI(-ch4, 65, 0);
myOLED.setFont(SmallFont);myOLED.print(F("dB"), RIGHT, 20);
myOLED.update();
}}
//////// ATT CEN//////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==5){
if(newPosition != oldPosition){oldPosition = newPosition;ch5=ch5-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ch5_conf();audio();}
if(ir.value==DW){ch5--;gr1=1;gr2=0;cl();times=millis();w=1;w2=1;ch5_conf();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){ch5--;gr2=0;cl();times=millis();w=1;w2=1;ch5_conf();audio();}// кнопка >>>>>>
if(ir.value==UP){ch5++;gr1=0;gr2=1;cl();times=millis();w=1;w2=1;ch5_conf();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){ch5++;gr1=0;cl();times=millis();w=1;w2=1;ch5_conf();audio();}// кнопка <<<<<<
if(w==1){w=0;
myOLED.clrScr();
myOLED.setFont(SmallFont);myOLED.print(F("ATT_CEN"), LEFT, 0);myOLED.setFont(BigNumbers);myOLED.printNumI(-ch5, 65, 0);
myOLED.setFont(SmallFont);myOLED.print(F("dB"), RIGHT, 20);
myOLED.update();
}}
//////// ATT SUB //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(menu==6){
if(newPosition != oldPosition){oldPosition = newPosition;ch6=ch6-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ch6_conf();audio();}
if(ir.value==DW){ch6--;gr1=1;gr2=0;cl();times=millis();w=1;w2=1;ch6_conf();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){ch6--;gr2=0;cl();times=millis();w=1;w2=1;ch6_conf();audio();}// кнопка >>>>>>
if(ir.value==UP){ch6++;gr1=0;gr2=1;cl();times=millis();w=1;w2=1;ch6_conf();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){ch6++;gr1=0;cl();times=millis();w=1;w2=1;ch6_conf();audio();}// кнопка <<<<<<
if(w==1){w=0;
myOLED.clrScr();
myOLED.setFont(SmallFont);myOLED.print(F("ATT_SUB"), LEFT, 0);myOLED.setFont(BigNumbers);myOLED.printNumI(-ch6, 65, 0);
myOLED.setFont(SmallFont);myOLED.print(F("dB"), RIGHT, 20);
myOLED.update();
}}
////////////////// EEPROM //////////////////////////////////////////////////////////////
if(millis()-times>5000 && w2==1 && mute==0){
EEPROM.update(0,vol);EEPROM.update(1,ch1);EEPROM.update(2,ch2);EEPROM.update(3,ch3);
EEPROM.update(4,ch4);EEPROM.update(5,ch5);EEPROM.update(6,ch6);
cl();menu=0;w2=0;w=1;}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // end loop
void ch1_conf(){if(ch1<0){ch1=0;}if(ch1>15){ch1=15;}}
void ch2_conf(){if(ch2<0){ch2=0;}if(ch2>15){ch2=15;}}
void ch3_conf(){if(ch3<0){ch3=0;}if(ch3>15){ch3=15;}}
void ch4_conf(){if(ch4<0){ch4=0;}if(ch4>15){ch4=15;}}
void ch5_conf(){if(ch5<0){ch5=0;}if(ch5>15){ch5=15;}}
void ch6_conf(){if(ch6<0){ch6=0;}if(ch6>15){ch6=15;}}
void vol_func(){if(vol<0){vol=0;}if(vol>64){vol=64;}}
void to_Timer(){newPosition = myEnc.read()/4;}
void cl(){myOLED.clrScr();ir.value=0;delay(200);}
void audio(){
pt.clear();
// pt.setVolume(75); // int 0...79
pt.setVol1(79-(vol+ch1)); // int 0...79
pt.setVol2(79-(vol+ch2)); // int 0...79
pt.setVol3(79-(vol+ch3)); // int 0...79
pt.setVol4(79-(vol+ch4)); // int 0...79
pt.setVol5(79-(vol+ch5)); // int 0...79
pt.setVol6(79-(vol+ch6)); // int 0...79
pt.setMute(mute); // mute 0 - off / 1 - on
}