from m5stack import *
from m5ui import *
from uiflow import *
from time import sleep
import imu
import network
import _thread
# 颜色字典colorMap
COLOR = {
            'black':0x130c0e,
            'white':0xffffff,
            'red':0xd71345,
            'pink':0xef5b9c,
            'orange':0xf26522,
            'yellow':0xfcf16e,
            'blue':0x009ad6,
            'miku':0x50b7c1,
            'green':0x65c294,
            'purple':0x6950a1,
        }


# 类object
class InfoBar(object):
    def __init__(self):
        self.list = ['M5']
        self.title0 = M5Title(title="0x00", x=3 , fgcolor=COLOR['white'], bgcolor=COLOR['blue'])  # THIS METHOD IS OK

    def add(self, slogan='new info'):
        self.list.append(slogan)

    def refresh(self):
        if len(self.list) > 0:
            self.title0.setTitle(str(self.list.pop(0)))  # BUG IS HERE, IT WILL CAUSE BACKGROUND COLOR DISAPPEARING
        else:
            self.title0.setTitle('M5')  # ALSO WRONG


class Battery(object):
    def __init__(self):
        self.MAX_OCV = 4.144
        self.MIN_OCV = 3.6
        self.voltage = 0
        self.batLevel = 0
        _thread.start_new_thread(self.refresh, ())

    def refresh(self):
        while 1:
            self.getVoltage()
            self.getBatLevel()
            time.sleep(0.5)

    def getVoltage(self):
        self.voltage = axp.getBatVoltage()

    def getBatLevel(self):
        self.batLevel = (self.voltage - self.MIN_OCV) / (self.MAX_OCV - self.MIN_OCV)


class Wifi(object):
    def __init__(self):
        self.SSID = 'SSID'  #  CONNECT TO YOUR OWN WIFI
        self.PSWD = '@@@@@@'  #  CONNECT TO YOUR OWN WIFI
        self.wlan0 = network.WLAN(network.STA_IF)  # 在network中初始化一个sta模式的wlan实例
        self.wifiswitch(True)

    def wifiswitch(self, sw=True):  # 开关wifi
        if sw == True:
            self.connect()
        self.wlan0.active(sw)

    def connect(self):  # 根据init中的设定连接wifi
        self.wlan0.connect(self.SSID, self.PSWD)
        info.add(str(self.SSID))
    def isconnected(self):  # 返回是否已经连接
        return self.wlan0.isconnected()

    def scan(self):
        return self.wlan0.scan()




# 实例化instance
info = InfoBar()
bat0 = Battery()
# wifi0 = Wifi()
imu0 = imu.IMU()
# clock0 = Clock()

# 初始设定init
# wifi0.connect()

setScreenColor(COLOR['black'])
label0 = M5TextBox(0, 20, "Text", lcd.FONT_Default,COLOR['white'], rotate=0)
label1 = M5TextBox(0, 30, "Text", lcd.FONT_Default,COLOR['white'], rotate=0)
label2 = M5TextBox(0, 40, "Text", lcd.FONT_Default,COLOR['white'], rotate=0)
label3 = M5TextBox(0, 50, "Text", lcd.FONT_Default,COLOR['white'], rotate=0)
# axp.setLcdBrightness(30)


# 运行一次doOnce


# 永久运行loop

while 1:
    info.refresh()
    label0.setText('Bat{:.1%}'.format(bat0.batLevel))
    label1.setText('x' + str(imu0.acceleration[0]))
    label2.setText('y' + str(imu0.acceleration[1]))
    label3.setText('z' + str(imu0.acceleration[2]))
    time.sleep(0.5)
