#include <Arduino.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
#include <LowPower.h>

#include "timer.h"
#include "accel.h"
#include "pindef.h"
#include "dice_decoder.h"
#include "dice.h"


void int_func() {
  //Serial.begin(9600);
  //Serial.println("bla"); 
  //Serial.flush();

   //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);

  Wire.begin();

  pinMode(INT_ACCEL, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(INT_ACCEL), int_func, RISING);

  init_low_power_mpu();
  init_timer();

  randomSeed(analogRead(0) + analogRead(1) + analogRead(3) + analogRead(4));

  roll_the_dice();
  dice_roll_ready = false;

}




void loop() {
  //mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  /*Serial.print("a/g:\t");
  Serial.print(ax); Serial.print("\t");
  Serial.print(ay); Serial.print("\t");
  Serial.print(az); Serial.print("\t");
  Serial.print(gx); Serial.print("\t");
  Serial.print(gy); Serial.print("\t");
  Serial.println(gz);    */    

  //LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

  roll_the_dice();
  if(dice_roll_ready) {
    delay(5000);
    dice_set_value(0, 0, 0, 0);
    delay(1000);
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
    roll_the_dice();
    dice_roll_ready = false;
  }
}

