#include "power.h"
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/atomic.h>
#include <util/delay.h>
#include <ssd1306xled.h>


// I'm not really keen on referencing the buttons directly from here,
// feels like a coding red flag. It'll do for now until I come up with
// a better design. Or forget about it most likely.
#include "buttons.h"




#define VccOffTreshold 3000
#define VccOnTreshold 3100

#define SSD1306CommandOff 0xAE
#define SSD1306CommandOn 0xAF

#define SleepInterval8s (_BV(WDP3) | _BV(WDP0))


PowerState powerState = BatteryLow;
uint16_t vcc = 0;


// Forward declarations
uint16_t readVCC();



void checkPower()
{
  vcc = readVCC();

  switch (powerState)
  {
    case On:
      // Turn display off below 3v. It holds up surprisingly well, but at around
      // 1.5v it does corrupt the screen and requires reinitialization when the
      // voltage is turned back up.
      //
      // ...although by then the battery would be damaged, but still, turning off at
      // 3v means we're still in the safe range when we go into battery saving mode
      // and the reinitialization afterwards prevents any issues.
      if (vcc < VccOffTreshold)
      {
        setPowerState(BatteryLow);
        sleepUntilTimer(SleepInterval8s);
      }

      break;

    case BatteryLow:
      if (vcc > VccOnTreshold)
        setPowerState(On);
      else
        sleepUntilTimer(SleepInterval8s);

      break;

    case ManualOff:
      sleepUntilButton(_BV(BUTTON_OPTION));
      setPowerState(On);
      break;
  }
}


void setPowerState(PowerState newState)
{
  if (newState == powerState) return;

  switch (newState)
  {
    case On:
      buttons_reset();
      ssd1306_send_command(SSD1306CommandOn);
      break;

    case BatteryLow:
    case ManualOff:
      if (powerState == On)
        ssd1306_send_command(SSD1306CommandOff);


      break;
  }

  powerState = newState;
}





// Source: http://21stdigitalhome.blogspot.nl/2014/10/trinket-attiny85-internal-temperature.html
//
// I've tried many versions and none seemed to work with my ATTiny85-20SU's.
// For example:
//   https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
//   https://github.com/cano64/ArduinoSystemStatus/blob/master/SystemStatus.cpp
//   http://www.avrfreaks.net/forum/attiny-adc-using-internal-ref-measure-vcc-problem
//
// The key for me was in: ADMUX  = 0x0c | _BV(REFS2);
uint16_t readVCC() {
  ADCSRA |= _BV(ADEN);

  // Read 1.1V reference against AVcc
  // set the reference to Vcc and the measurement to the internal 1.1V reference
  /*
  #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
    ADMUX = _BV(MUX5) | _BV(MUX0);
  #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
    ADMUX = _BV(MUX3) | _BV(MUX2);
  #else
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #endif
  */
  ADMUX  = 0x0c | _BV(REFS2);

  _delay_ms(100);

  ADCSRA |= _BV(ADSC);
  while (bit_is_set(ADCSRA,ADSC));

  uint16_t result = ADC;
  ADCSRA &= ~(_BV(ADEN));

  return result == 0 ? 0 : 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
}


static inline void sleepStart()
{
  // Turn ADC off
  ADCSRA &= ~_BV(ADEN);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  // Set sleep bit and halt the CPU
  sleep_enable();
  sei();
  sleep_cpu();

  // ...goooood morning!
  cli();
}


static inline void sleepEnd()
{
  sleep_disable();
  ADCSRA |= _BV(ADEN);

  sei();
}


void sleepUntilButton(uint8_t pinMask)
{
  // Enable pin change interrupts
  GIMSK |= _BV(PCIE);

  // Set up pin change mask
  PCMSK = pinMask;

  sleepStart();
  PCMSK = 0;
  sleepEnd();
}


void sleepUntilTimer(uint8_t prescaler)
{
  // Disable watchdog reset flag
  MCUSR &= ~_BV(WDRF);

  // Enable watchdog
  WDTCR |= _BV(WDCE) | _BV(WDE);

  // Set timeout value and enable interrupt mode
  WDTCR = prescaler | _BV(WDIE);

  sleepStart();

  // Disable watchdog (need to write a logic one first, see datasheet)
  WDTCR |= _BV(WDCE) | _BV(WDE);
  WDTCR = 0;

  sleepEnd();
}


// Interrupt for pin changes
ISR(PCINT0_vect)
{
}

// Interrupt for watchdog timer
ISR(WDT_vect)
{
}