#include <TinyWireM.h>
//#include <Arduino.h>
//#include <U8g2lib.h>
#include "ssd1306.h"
#include "ina219.h"

// variables
SSD1306 oled;
//U8G2_SH1107_SEEED_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//INA219 power_mon;

#define CEILING_BUFFER 0.1F
#define CHART_LEFT 0
#define CHART_RIGHT 75

static uint8_t i = CHART_LEFT;
static float voltage = 0;
static float current = 0;
static float power = 0;
static float mWh = 0;
static float last_second = 0;

static float max_v = 5.0F;
static float max_ma = 10.0F;
static float max_mw = 5.0F;



void setup() {
TinyWireM.begin();
  oled.begin();
  //power_mon.begin();

  oled.fill(0x00); // clear in black
  oled.set_pos(64 - 5, 0);
  oled.print("V");
  oled.set_pos(64 - 10, 2);
  oled.print("mA");
  oled.set_pos(64 - 15, 4);
  oled.print("mWh");
  oled.set_pos(CHART_RIGHT, 6);
  oled.print("Time:");

  
}

void loop() {
 // collect figures
 
  //voltage = power_mon.read_bus_voltage();
  voltage = 5.0;
  //current = power_mon.read_current();
  current =100.0;
  //power = power_mon.read_power();
  power = 15.0;
  float curr_second = millis() / 1000.0;
  mWh += power / (curr_second - last_second) / 3600;
  last_second = curr_second;
  
  // print figures
  oled.set_pos(CHART_RIGHT, 0);
  oled.print_float(voltage, 9);
  oled.set_pos(CHART_RIGHT, 2);
  oled.print_float(current, 8);
  oled.set_pos(CHART_RIGHT, 4);
  oled.print_float(mWh, 7);
  oled.set_pos(CHART_RIGHT, 7);
  oled.print(curr_second);
   
  /*
  // auto adjust graph ceiling
  if (voltage >= max_v)
  {
    max_v = voltage + CEILING_BUFFER;
  }
  if (current >= max_ma)
  {
    max_ma = current + CEILING_BUFFER;
  }
  if (power > max_mw)
  {
    max_mw = power + CEILING_BUFFER;
  }
  // plot graph
  oled.plot_value(i, 0, 2, voltage, 0.0F, max_v);
  oled.plot_value(i, 2, 2, current, 0.0F, max_ma);
  oled.plot_area(i, 4, 4, power, 0.0F, max_mw);

  // loop graph position
  i++;
  if (i >= CHART_RIGHT)
  {
    i = CHART_LEFT;
  }

  // draw current position line
  oled.draw_pattern(i, 0, 1, 8, 0xFF);
  */
}
ATTINY8520PU