#!/usr/bin/env python
#version    :       2023.04.04
#language   :       en
import sys
import time
from maix import display,image,camera
sys.path.append('/root/')
from CocoPi import AHT20
image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")

class v83x_ADC():
    def __init__(self, addr=b"0x05070080") -> None:
        self.addr = addr
        self.path = "/sys/class/sunxi_dump/dump"
        self.file = open(self.path, "wb+")
        self.last = self.value()
    def __del__(self):
        try:
            if self.file:
                self.file.close()
                del self.file
        except Exception as e:
            pass
    def value(self):
        self.file.write(b"0x05070080")
        self.file.seek(0)
        return int(self.file.read()[:-1], 16)

v831_adc0 = v83x_ADC()

aht20 = AHT20(2)    #创建温湿度传感器对象
def aht20Test():
    global aht20,img_backdrop
    from maix import display
    h=round(aht20.get_humidity(),2)+10.00
    t=round(aht20.get_temperature()-15,2)
    img_backdrop.draw_string(16, 108, "Original humidity value:"+str(h)+"RH%", scale = 1.0,color = (0, 255, 0), thickness = 1)
    img_backdrop.draw_string(16, 132, "Original temperature value:"+str(t)+"'C", scale = 1.0,color = (0, 255, 0), thickness = 1)
    


camera.camera.config(size=(320, 240))
img_backdrop = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")

while True:
    img_backdrop.clear()
    img_backdrop = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
    aht20Test()
    v=v831_adc0.value()
    img_backdrop.draw_string(16, 84, "Original light intensity value:"+str(v), scale = 1.0,color = (0, 255, 0), thickness = 1)
    img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
    display.show(img_backdrop)
    time.sleep(0.5)