#include <stdint.h>
#include <stdlib.h>
#include <util/delay.h>
#include <ssd1306xled.h>
#include "power.h"
#include "buttons.h"
#include "screen/state.h"
#include "screen/counter.h"
#include "screen/menu.h"


// Forward declarations
void handleCurrentScreen();



int main()
{
  // Delay is required on power-on for the SSD1306 to initialize,
  _delay_ms(40);
  ssd1306_init();
  ssd1306_clear();

  buttons_init();

  setCurrentScreen(Counter);

  while (1)
  {
    checkPower();
    if (powerState == On)
    {
        handleCurrentScreen();

        if (!buttons_active() && !peekScreenInvalidated())
          sleepUntilButton(_BV(BUTTON_UP) | _BV(BUTTON_DOWN) | _BV(BUTTON_OPTION), 0);
    }
  }

  return 0;
}


void handleCurrentScreen()
{
  switch (currentScreen)
  {
    case Counter: handleCounterScreen(); break;
    case Menu: handleMenuScreen(); break;
  }
}