import bhi160
import display
import utime
import math
import buttons
import power
import bme680
import color
import display
import vibra
import urandom
import ledfx
import leds, color


backlight = 20

def set_backlight(x):
    global backlight
    if x != backlight:
        try:
            display.backlight(x)
        except AttributeError:
            import sys_display
            sys_display.backlight(x)
        backlight = x




disp = display.open()
lives = 3
flag = 0
iteration = 0
idle = False
idle_time = 0

set_backlight(20)

def sqrt(x):
    if x <= 0.0:
        return 0.0
    r = float(x)/3.0
    for i in range(32):
        r = (r + (float(x) / r) / 2.0)
    return r

bme680.init()

RED = [255, 0, 0]
YELLOW = [255, 255, 0]
ORANGE = [255, 128, 0]
GREEN = [0, 255, 0]
BLUE = [0, 0, 255]
WHITE = [255, 255, 255]

def activity(iteration):
    global idle, idle_time
    idle = False
    idle_time = iteration


with bhi160.BHI160Accelerometer() as sensor:

    voltage = 0
    temperature = 0
    while True:
        if iteration % 60 == 0:
            voltage = power.read_battery_voltage()
            #temperature = bme680.get_data()[0]

            #if iteration - idle_time > 30:
            #    idle = True


        #---- colorified life!
        if lives == 3:
            for led in range(0,15):
                leds.set(led, GREEN)
        if lives == 2:
            for led in range(0,15):
                leds.set(led, YELLOW)
        if lives == 1:
            for led in range(0,15):
                leds.set(led, RED)
        if lives == 0:
            for led in range(0,15):
                leds.set(led, WHITE)
            vibra.vibrate(60)
            utime.sleep(0.1)
            vibra.vibrate(60)
            utime.sleep(0.1)
            vibra.vibrate(60)
            utime.sleep(0.1)
            idle = True
            activity(iteration)
            lives -= 1
            


        #---- random colors
        # u=urandom.randint(0,31)
        # leds.set_rocket(0, u)
        # u=urandom.randint(0,31)
        # leds.set_rocket(1, u)
        # u=urandom.randint(0,31)
        # leds.set_rocket(2, u)

        #----------lectura de sensores
        samples = sensor.read()
        if len(samples) > 0:
            sample = samples[0]

            s = sqrt(sample.x*sample.x + sample.y*sample.y + sample.z*sample.z)

            if s > 9:
                color = RED
                if flag == 0:
                    vibra.vibrate(60)
                    lives -= 1
                    flag = 1

            elif s > 7:
                color = ORANGE
                flag = 0
                activity(iteration)
            elif s > 6:
                color = YELLOW
                flag = 0
                activity(iteration)
            else:
                color = GREEN
                flag = 0

            if not idle:
                battery_color = GREEN
                if voltage < 3.5:
                    battery_color = RED
                elif voltage < 3.6:
                    battery_color = ORANGE
                elif voltage < 3.8:
                    battery_color = YELLOW

                # temperature_color = GREEN
                # if temperature > 35:
                #     temperature_color = RED
                # elif temperature > 30:
                #     temperature_color = ORANGE
                # elif temperature > 25:
                #     temperature_color = ORANGE
                # elif temperature < 18:
                #     temperature_color = BLUE

                set_backlight(20)

                disp.clear()
                disp.print("Lives: %d" % (lives), posy=0, fg=color)
                #disp.print("Battery:", posy=20, fg=battery_color)
                disp.print("%f V" % (voltage), posy=40, fg=battery_color)
                #disp.print("%.2f C" % (temperature), posy=60, fg=temperature_color)

                disp.update()
            else:
                set_backlight(0)


        #-------LEctura de botones
        v = buttons.read(buttons.TOP_RIGHT)
        if v == 0:
            button_pressed = False

        if not button_pressed and v & buttons.TOP_RIGHT != 0:
            button_pressed = True
            lives = 3
            flag = 0
            idle_time = iteration
        utime.sleep(0.1)
        iteration += 1