/*
  Name:    SinglePlotMode.ino
  Created:  04/01/2020 11:59:52 AM
  Author: José Gabriel Companioni Benítez (https://github.com/elC0mpa)
  Description: Example to demonstrate how to draw cartesian charts in Adafruit SSD1306 oleds in single plot mode 
*/

#include <Arduino.h>
#include <OLED_SSD1306_Chart.h>
#include <Adafruit_I2CDevice.h> //Include this to avoid compile errors in Platformio
#include <Wire.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

#define BAUDRATE 9600

#define SDA_PIN D1
#define SCL_PIN D2

OLED_SSD1306_Chart display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

unsigned long rpmtime;
float rpmfloat;
unsigned int rpm;
bool tooslow = 1;
unsigned int value;
unsigned int maximo = 0;
char cara[8];
String str;
String strm;
unsigned int i=0;

int numReadings = 23;
int premean;
int mean;
//int numReadings = 23;
int average = 0;

char actualThickness;

void setup()
{
// put your setup code here, to run once:
#if defined ESP8266
  Wire.begin(SDA_PIN, SCL_PIN);
#else
  Wire.begin();
#endif

  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
  display.clearDisplay();
  display.setChartCoordinates(0, 60);      //Chart lower left coordinates (X, Y)
  display.setChartWidthAndHeight(123, 44); //Chart width = 123 and height = 60
  display.setXIncrement(5);                //Distance between Y points will be 5px
  display.setYLimits(50, 2000);             //Ymin = 0 and Ymax = 100
  display.setYLimitLabels("50", "2k");    //Setting Y axis labels
  display.setYLabelsVisible(true);
  display.setAxisDivisionsInc(12, 5);    //Each 12 px a division will be painted in X axis and each 6px in Y axis
  display.setPlotMode(SINGLE_PLOT_MODE); //Set single plot mode
  // display.setPointGeometry(POINT_GEOMETRY_CIRCLE);
  actualThickness = NORMAL_LINE;
  display.setLineThickness(actualThickness);
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.drawChart(); //Update the buffer to draw the cartesian chart
  display.display();

  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1B |= (1 << CS12); //Prescaler 256
  TIMSK1 |= (1 << TOIE1); //enable timer overflow
  pinMode(2, INPUT);
  attachInterrupt(0, RPM, FALLING);


}

ISR(TIMER1_OVF_vect) {
  tooslow = 1;
}


void loop()
{
  // put your main code here, to run repeatedly:
  if (tooslow == 0)
  {
    rpmfloat = 120 / (rpmtime/ 31250.00);
    rpm = round(rpmfloat);
    value = rpm;
    maximo = max(maximo,rpm);
    str = String(maximo);
    maximo = round(maximo/100+1)*100;
    //str.toCharArray(cara,8);
  }
  else
  {value = 0;}
  

  if (!display.updateChart(value)) //Value between Ymin and Ymax will be added to chart
  {
    display.setYLimits(0, maximo);             //Ymin = 0 and Ymax = 100
    strm = String(maximo/100);
    strm.toCharArray(cara,4);
    display.setYLimitLabels("0", cara);    //Setting Y axis labels
    display.clearDisplay(); //If chart is full, it is drawn again
    display.setCursor(0,0);
    mean = premean/numReadings;
    strm = String(mean);
    // int16_t i = 64;
    display.println("Max: "+str+ " Mean: "+strm);
    //i = 0;
    maximo = 60;
    premean = 0;

    //if (actualThickness == NORMAL_LINE)
    //{
    //  actualThickness = LIGHT_LINE;
    //}
    //else if (actualThickness == LIGHT_LINE)
    //{
    //  actualThickness = NORMAL_LINE;
    //}
    //display.setLineThickness(actualThickness);
    display.drawChart();
  }
  premean+= rpm;
  delay(100);
  // i++;
}


void RPM () {
  rpmtime = TCNT1;
  TCNT1 = 0;
  tooslow = 0;
}
