#include "U8glib.h"
#include <Wire.h>

double frequency = 88.00; // frequency in mHz
int maxPresets; // maximum number of presets used
double stationsMHZ[7]; // allow for 6 preset stations
String stations[7]; // station names
int presetCounter = 0; //pointer to preset stations
int favourite; // pressing SELECT will select this one
int sl; // signal level 0 - 10
boolean mute = true;
String stringFrequency;
String stringStation;
String stringSignal;
boolean stereo = false;
//String rawData;
int a;
boolean stationName = true; // display the station name

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C

void setup()
{
  Wire.begin();

  stationsMHZ[1] = 88.58;
  stations[1] = "BBC Radio 2";
  stationsMHZ[2] = 96.10;
  stations[2] = "Radio Solent";
  stationsMHZ[3] = 97.50;
  stations[3] = "Heart Solent";
  stationsMHZ[4] = 98.20;
  stations[4] = "BBC Radio 1";
  stationsMHZ[5] = 103.20;
  stations[5] = "Capital FM";
  stationsMHZ[6] = 102.00;
  stations[6] = "Isle of Wight Radio";

  maxPresets = 6; // number of stations in list, change this as required
  favourite = 6; // select your own favourite
  presetCounter = favourite;
  selectStation();

}

void loop() {
  if (favourite > 0 and favourite < maxPresets + 1) {
    presetCounter = favourite;
    frequency = stationsMHZ[presetCounter];
    stationName = true;
    selectStation();
  }
  
  printStereoStatus();
  u8g.firstPage();
  do {
    draw();
  } while ( u8g.nextPage() );
}

void selectStation() {
  frequency = stationsMHZ[presetCounter];
}

void printStereoStatus() {
  sl = 8;
  if (sl) {
    stereo = true;
  } else {
    stereo = false;
  }
}

void draw(void) {
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(40, 8, "FM Radio");
  
  if (mute) { 
    stringFrequency = frequency; // display actual frequency
    stringFrequency = stringFrequency + "mhz";
    a = stringFrequency.length();
    a = 18 - a;
    a = a * 7; // width in pixels of empty space
    a = (a / 2) + 1; // start for string to make it centre
    const char* newFrequency = (const char*) stringFrequency.c_str();
    u8g.setFont(u8g_font_profont15);
    u8g.drawStr(a, 30, newFrequency);
    u8g.setFont(u8g_font_profont12);
    
    if (stationName) {
      stringStation = stations[presetCounter];
    }
    else {
      stringStation = "fine tune";
    }
    a = stringStation.length();
    a = 21 - a;
    a = a * 6; // width in pixels of empty space
    a = (a / 2) + 1; // start for string to make it centre
    const char* newStation = (const char*) stringStation.c_str();
    u8g.drawStr(a, 42, newStation);
    
    if (stereo) {
      u8g.drawStr(5, 17, "Stereo");
    }
    else {
      u8g.drawStr(5, 17, "Mono");
    }
    
    if (sl < 10) {
      stringSignal = "Signal: " + String(sl);
      const char* newSignal = (const char*) stringSignal.c_str();
      u8g.drawStr(70, 17, newSignal);
    }

    u8g.drawLine(4, 58, 124, 58);
    for (int f = 4; f < 128; f = f + 40) {
      u8g.drawLine(f, 52, f, 58);
    }
    for (int f = 4; f < 128; f = f + 8) {
      u8g.drawLine(f, 55, f, 58);
    }
    u8g.drawLine(((frequency - 80) * 4) + 4, 53, ((frequency - 80) * 4) + 4, 63);
    u8g.setFont(u8g_font_profont10);
    u8g.drawStr(0, 52, "80");
    u8g.drawStr(40, 52, "90");
    u8g.drawStr(78, 52, "100");
    u8g.drawStr(114, 52, "110");
  }
  else {
    u8g.drawStr(23, 30, "Radio is muted");
  }
}