#include <Arduino.h>
#include "ColorConverterLib.h"

#include "dice_decoder.h"

bool dice_roll_ready = true;

int value = 1;
double hue = 0.0;

unsigned long delay_change = 0; //us
unsigned long count = 0;
unsigned long timer0 = micros();
void roll_the_dice() {
    if(!dice_roll_ready) {
        if(micros()-timer0>delay_change) {
            uint8_t red=255, green=255, blue=255;
            double saturation=1.0, val=1.0;
            ColorConverter::HsvToRgb(hue, saturation, val, red, green, blue);
            hue += 0.1;
            if(hue>1.0) hue -= 1.0;

            dice_set_value(value, red>>5, green>>5, blue>>5);

            value++;
            if(value>6) value = 1;

            delay_change = pow(1.15, count);
            count++;

            if(count>95) dice_roll_ready = true;

            timer0 = micros();
        }
    } else {
        count = 0;
        value = random(1, 6 + 1);
        hue = random(0, 10 + 1) / 10.0;
    }
}