#include <Wire.h>
#include <Adafruit_Si4713.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <EEPROM.h>
Adafruit_SSD1306 display(128, 64, &Wire,1);

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//#define OLED_RESET   -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3d ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, SEEK_SET);

#define RESETPIN 12
#define stereo_RDSoff 3
#define stereo 7
#define mono 0

//void update( int idx, uint8_t val )  { EERef( idx ).update( val ); }
const int up = 2; // the number of the pushbutton pin
const int set = 3;
const int dow = 4; // the number of the pushbutton pin
const int KEYPin = 5;
const int ledPin = 11;
uint16_t  frec = 10070;
uint16_t  Audio;
int  po, fset = 0;
char ind = 17;
char cl = 219;
int  mod;
//Adafruit_Si4713 radio = Adafruit_Si4713(RESETPIN);


void Oled() {
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 0);            // Start at top-left corner
  display.println(F("RFW FM TX"));

  // display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  // display.println(3.141592);

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10, 20);
  display.print(F("F:")); display.print(frec / 100); display.print(F(".")); display.print(frec % 100);

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10, 40);
  display.print(F("M:"));

  if (mod == 4) {
    display.setTextSize(2);             // Draw 2X-scale text
    display.setTextColor(SSD1306_WHITE, BLACK);
    display.setCursor(35, 40);
    display.print(F("MONO  "));
  }
  else {
    display.setTextSize(2);             // Draw 2X-scale text
    display.setTextColor(SSD1306_WHITE, BLACK);
    display.setCursor(35, 40);
    display.print(F("STEREO"));
  }
  display.display();
  delay(200);
}

void MODO(int h) {
  //  radio.setProperty(SI4713_PROP_TX_COMPONENT_ENABLE,h);
}

void Audio_DV(uint16_t h) {
  // radio.setProperty(SI4713_PROP_TX_AUDIO_DEVIATION,h);
}


void setup() {
  frec =  10070;
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  //if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  if (!display.begin(1)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.display();
  delay(50); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);


  //EEPROM.put(60,10070);// int16
  //EEPROM.write(0, 23);//int

  //frec = EEPROM.read(0);//int
  //EEPROM.get(60,frec);//int16


  pinMode(up, INPUT_PULLUP);
  pinMode(set, INPUT_PULLUP);
  pinMode(dow, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(KEYPin, OUTPUT);
  EEPROM.get( 80, frec);
  mod = EEPROM.read(0);
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(48, 0);            // Start at top-left corner
  display.println(F("RFW"));
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(26, 20);            // Start at top-left corner
  display.println(F("TRANSMISORES"));
  display.display();
  delay(3000);

  if (frec > 10790) {
    frec = 9850;
    Audio = 6550;
    po = 115;
    mod = 4;  //  mod=7; --> ST  mod=4; --> MONO
  }
  else{
    frec= 10070;
    Audio = 6550;
    po = 115;
    mod = 7;  //  stereo
  }
  Oled();
  /* radio.reset();
    delay(100);
    radio.begin();
    delay(500);

    radio.setTXpower(100);  // dBuV, 88-115 max
    radio.tuneFM(frec); // 100.7 mhz
    radio.setRDSstation("JM RADIO");
    radio.setRDSbuffer( "Que Buena Radio!");
    digitalWrite(KEYPin,LOW);
  */
  // stereo_RDSoff
  // stereo
  // mono
  MODO(mod);
  // Audio_DV(Audio);
}

void loop() {
  if (digitalRead(set) == LOW) {
    fset = 2;
    delay(30);
    fmsetup();
  }
}

void fmsetup() {
  while (fset == 2) {
    display.setTextSize(2);             // Draw 2X-scale text
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(110, 20);            // Start at top-left corner
    display.print(ind);
    display.display();

    if (digitalRead(up) == LOW) {
      frec = frec + 20;
      // radio.tuneFM(frec); // 100.7 mhz
      Oled();
    }

    if (digitalRead(dow) == LOW) {
      frec = frec - 20;
      //radio.tuneFM(frec); // 100.7 mhz
      Oled();
    }
    if (digitalRead(set) == LOW) {
      EEPROM.put(80, frec);
      delay(300);
      display.setTextSize(2);             // Draw 2X-scale text
      display.setTextColor(SSD1306_WHITE, BLACK);
      display.setCursor(110, 20);            // Start at top-left corner
      display.print(F(" "));
      display.display();
      fset = 4;
    }
    while (fset == 4) {
      display.setTextSize(2);             // Draw 2X-scale text
      display.setTextColor(SSD1306_WHITE);
      display.setCursor(110, 40);            // Start at top-left corner
      display.print(ind);
      display.display();

      if (digitalRead(up) == LOW) {
        mod = 7;
        MODO(mod);
        Oled();
      }

      if (digitalRead(dow) == LOW) {
        mod = 4;
        MODO(mod);
        Oled();
      }

      if (digitalRead(set) == LOW) {
        EEPROM.write(0, mod);
        display.clearDisplay();
        Oled();
        fset = 6;
      }
    }
    while (fset == 6) {
      if (digitalRead(set) == LOW) {
        delay(300);
        fset = 2;
      }
    }
  }
}