import bhi160
import display
import utime
import math
import buttons
import power
import bme680
import color
import display


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()
steps = 0
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]

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

        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 > 10:
                color = RED
                if flag == 0:
                    steps += 1
                    flag = 1
                activity(iteration)
            elif s > 8:
                color = ORANGE
                flag = 0
                activity(iteration)
            elif s > 7:
                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("Steps: %d" % (steps), 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)


        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
            steps = 0
            flag = 0
            idle_time = iteration
        utime.sleep(0.1)
        iteration += 1

