"""
Temperature and Humidity
Written by: Anykey

"""

import leds
import htmlcolor
import buttons
import display
import utime
import bme680

tempColor = [htmlcolor.LIGHTBLUE, htmlcolor.LIGHTBLUE ,htmlcolor.LIGHTBLUE, htmlcolor.LIGHTYELLOW, htmlcolor.LIGHTYELLOW, htmlcolor.LIGHTYELLOW,  htmlcolor.ORANGE, htmlcolor.ORANGE, htmlcolor.ORANGERED, htmlcolor.ORANGERED, htmlcolor.RED]

def TempToLED(temperatur):
    useTemp = temperatur
    if (temperatur < 10):
        useTemp = 10
    if (temperatur > 32):
        useTemp = 32
    LedTemp = int(useTemp/2)-5
    # print("Temp: {}  Leds: {}".format(temperatur,LedTemp))
    for L in range(0, 11):
        if L <= LedTemp:
            leds.prep(10-L, tempColor[L])
        else:
            leds.prep(10-L, htmlcolor.BLACK)
    leds.update()


bme680.init()
cont = True

leds.prep(1, htmlcolor.RED)
leds.update()

while cont:
    temperature, humidity, pressure, resistance = bme680.get_data()
    disp = display.open()
    disp.clear()
    disp.print("{:2.2f} °C".format(temperature), fg=[0,255,255], bg=[0,0,0], posx=0, posy=20)
    disp.print("{:2.2f} %r.h.".format(humidity), fg=[0,255,255], bg=[0,0,0], posx=0, posy=40)
    disp.update()
    TempToLED(int(temperature))
    t = 0
    while t < 60:
        pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT)
        if pressed & buttons.BOTTOM_LEFT != 0:
            cont=False
            bme680.deinit()
            break
        utime.sleep(1)
        t = t + 1





