## No comment yet
from machine import ADC,Pin
import system, utime, gc, rgb
battavg = []
# get voltage from ADC pin 35
def get_vcc():
    _get_vcc_bat = None
    try:
        _get_vcc_bat = system.get_vcc_bat
        if callable(_get_vcc_bat):
            vcc = system.get_vcc_bat()
            return vcc
    except:
        print("Using backup ADC method")
        vcc_bat = ADC(Pin(35))
        vcc_bat.width(ADC.WIDTH_12BIT)
        vcc_bat.atten(ADC.ATTN_11DB)
        vcc = int(vcc_bat.read()  / (4095 / 4034) * 2) 
        vcc_bat.deinit()
        return vcc

def get_usb():
    vcc = None
    try:
        vcc_usb = ADC(Pin(34))
        vcc_usb.width(ADC.WIDTH_12BIT)
        vcc_usb.atten(ADC.ATTN_11DB)
        vcc = int(vcc_usb.read()  / (4095 / 4034) * 2) 
        vcc_usb.deinit()
    finally:
        return vcc

def drawPlug():
    global framebuf
    plug = [0,0,1,0,0,1,0,
            0,0,1,0,0,1,0,
            0,0,1,0,0,1,0,
            0,1,1,1,1,1,1,
            0,1,1,1,1,1,1,
            0,1,1,1,1,1,1,
            0,0,1,1,1,1,0,
            0,0,0,1,1,0,0]

    row = 0
    col = 0
    for i in plug:
        if i == 1:
            drawPixel((255,255,255),(col,row))
        col += 1
        if (col > 6):
            col = 0
            row+=1
number =  ((1,1,1,1,0,1,1,0,1,1,0,1,1,1,1),
            (0,1,0,0,1,0,0,1,0,0,1,0,0,1,0),
            (0,1,1,0,0,1,0,1,1,0,1,0,0,1,1),
            (0,1,1,0,0,1,0,1,1,0,0,1,0,1,1),
            (0,1,0,0,1,1,0,1,1,0,0,1,0,0,1),
            (0,1,1,0,1,0,0,1,1,0,0,1,0,1,1),
            (0,1,0,1,0,0,1,1,1,1,0,1,1,1,1),
            (0,1,1,0,0,1,0,1,0,0,1,0,0,1,0),
            (1,1,1,1,0,1,1,1,1,1,0,1,1,1,1),
            (1,1,1,1,0,1,1,1,1,0,0,1,1,1,0))

def drawNumber(draw_num,point,color):
    global number
    if draw_num < 10 and draw_num >= 0:
        cols = 0
        rows = 0
        for i in number[draw_num]:
            if i == 1:
                drawPixel(color,(point[0]+cols,point[1]+rows))
            cols += 1
            if cols > 2:
                cols = 0
                rows += 1



bat_empty = 3300
bat_full = 4200

rgb.disablecomp()
framebuf = []

def clearScreen():
    global framebuf
    framebuf = []
    for i in range(0,256):
        framebuf.append(0)
 

clearScreen()
rgb.frame(framebuf)
def drawPixel(color,pixel):
    global framebuf
    index = int( pixel[0] + (pixel[1]*32) )
    #print("New pixel draw @ "+str(index)+ " "+str(len(framebuf)))
    if index < len(framebuf):
        if len(color) != 3:
            print("ERR")
        newcolor = ( (color[0] * (256*256*256)) + (color[1] *(256* 256)) + (color[2]*256))
        #print("New color: "+str(newcolor))
        framebuf[index] = newcolor

def rect(x1,y1,x2,y2, color):
    global framebuf
    # Line for X axis (first)
    for i in range(x1,x2+1):
        drawPixel(color,(i,y1))
        drawPixel(color,(i,y2))
    for i in range(y1+1,y2):
        drawPixel(color,(x1,i))
        drawPixel(color,(x2,i))
    #print(framebuf)    

while True:
    gc.collect()

    voltage_bat = get_vcc() #mVolt
   
    bat_percentage = int((100 * (voltage_bat - bat_empty)) / (bat_full - bat_empty))
    if bat_percentage > 100:
        bat_percentage = 100
    if bat_percentage < 0:
        bat_percentage = 0
    newavg = 0
    battavg.append(bat_percentage)
    for i in battavg:
        newavg += i
    bat_percentage = newavg // len(battavg)
    if len(battavg) == 10:
        temp = battavg.pop(0)

    bars = bat_percentage // 5
    #bat_percentage = ( bars * 5)

    clearScreen()
    #rgb.setfont(rgb.FONT_6x3)
    rect(30,2,31,5,(0,255,192))
    rect(8,0,29,7,(0,255,192))
    rect(9,1,bars+9,6,(0,255,0))
    rect(9,2,bars+9,5,(0,255,0))
    
    if bars > 1:
        rect(10,3,bars+8,4,(0,255,0))
    if get_usb() > 4500:
        drawPlug()

    # Draw percentage
    lastdigit = bat_percentage % 10
    secdigit = bat_percentage // 10

    if secdigit > 0:
        if secdigit == 10:
            secdigit = 0
        drawNumber(secdigit,(17,2),(255,0,0))
    if(bat_percentage == 100):
        drawNumber(1,(13,2),(255,0,0))
    
    drawNumber(lastdigit,(21,2),(255,0,0))
    rgb.frame(framebuf) 

    utime.sleep(1)