#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <si4703.h>
si4703 radio(3,2,4,5); // SDIO, SCLK, SEN, RST
LiquidCrystal_I2C lcd(0x27,20,4);
byte v1[8] = {0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07};
byte v2[8] = {0x07,0x07,0x00,0x00,0x00,0x00,0x00,0x00};
byte v3[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v4[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v5[8] = {0x1C,0x1C,0x00,0x00,0x00,0x00,0x1C,0x1C};
byte v6[8] = {0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C};
byte v7[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07};
byte v8[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00};
byte d1,d2,d3,d4,d5,d6,e1,e2,e3;
int a[6],x;
bool w=1,save_ch,w1;
int canel,vol;
unsigned long times,times1;
byte t;
char old[8];
char * print_rds;
void setup() {
Wire.begin();lcd.init();lcd.backlight();
lcd.createChar(1, v1);lcd.createChar(2, v2);lcd.createChar(3, v3);lcd.createChar(4, v4);
lcd.createChar(5, v5);lcd.createChar(6, v6);lcd.createChar(7, v7);lcd.createChar(8, v8);
//Serial.begin(9600);
radio.init();
pinMode(7,INPUT_PULLUP);// button up
pinMode(8,INPUT_PULLUP);// button down
pinMode(9,INPUT_PULLUP);// button volume UP
pinMode(10,INPUT_PULLUP);// button volume DOWN
canel = ((EEPROM.read(400)<<8))|EEPROM.read(401);vol=EEPROM.read(402);
if(canel>320){canel=0;}if(vol>15){vol=10;}
Serial.print("Device ID: ");Serial.println(radio.read_device_id(),HEX);
Serial.print("Chip ID: ");Serial.println(radio.read_chip_id(),HEX);
radio.si_volume(vol);
}
void loop() {
//////// BUTTON /////////////////////////////////////////////////
if(digitalRead(7)==LOW){radio.seek_up();save_ch=1;times=millis();delay(100);}
if(digitalRead(8)==LOW){radio.seek_down();save_ch=1;times=millis();delay(100);}
if(digitalRead(9)==LOW){vol++;save_ch=1;times=millis();if(vol>15){vol=15;}radio.si_volume(vol);delay(100);}
if(digitalRead(10)==LOW){vol--;save_ch=1;times=millis();if(vol<0){vol=0;}radio.si_volume(vol);delay(100);}
/////////////////////////////////////////////////////////////////
/////// EEPROM //////////////////////////////////////////////////
if(millis()-times>60000&&save_ch==1){save_ch=0;canel = radio.nom_ch();
EEPROM.update(400,(canel>>8) & 0b11);EEPROM.update(401,canel & 0xFF);EEPROM.update(402,vol);}
/////// LCD STEREO/MONO SIGNAL VOLUME /////////////////////////////////////////////////
if(w==1){w=0;radio.fm_start(canel);}
if(millis()-times1>1000){times1=millis();lcd.setCursor(13,2);lcd.print("SIG ");lcd.print(radio.rss());lcd.print(" ");
if(radio.st_mn()==0){lcd.setCursor(13,1);lcd.print("MONO ");}
else{lcd.setCursor(13,1);lcd.print("STEREO");}}
lcd.setCursor(13,0);lcd.print("VOL ");lcd.print(vol);lcd.print(" ");
lcd.setCursor(2,2);lcd.print("FM RADIO");
////// LCD PRINT RDS ST_NAME ///////////////////////////////////////////////
if(radio.seach()==1 && radio.rss()>30){print_rds = radio.readRDS();}
if(t==0){for(int i=0;i<8;i++){old[i]=print_rds[i];}}t++;
if(radio.seach()==0){
for(int i=0;i<8;i++){print_rds[i]=61;}t=0;lcd.setCursor(6,3);lcd.print(" No RDS ");}
byte d=0;for(int i=0;i<8;i++){if(old[i]==print_rds[i]){}else{d++;}}
if(d==0 && t==5){t=0;
for(int i=0;i<8;i++){lcd.setCursor(6+i,3);lcd.print(print_rds[i]);}}
if(t>8){t=0;}
lcd.setCursor(0,3);lcd.print("======");
lcd.setCursor(14,3);lcd.print("======");
///////// LCD BIG ///////////////////////////////////////////////
int f_ch = radio.nom_ch() + 760;
a[0]=f_ch/1000;
a[1]=f_ch/100%10;
a[2]=f_ch/10%10;
a[3]=f_ch%10;
if(f_ch<1000){a[0]=10;}
for(x=0;x<4;x++){
switch(x){
case 0: e1=0;e2=0,e3=1;break;
case 1: e1=2,e2=3,e3=4;break;
case 2: e1=5,e2=6,e3=7;break;
case 3: e1=9,e2=10,e3=11;break;
}digit();}
lcd.setCursor(8,1);lcd.print(".");
//////////////////////////////////////////////////////////////////
delay(1);
}// loop
void digit(){switch(a[x]){
case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;
case 10:d1=150,d2=150,d3=150,d4=150,d5=150,d6=150;break;}
if(x>0){lcd.setCursor(e1,0);lcd.write((uint8_t)d1);}
lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);}