### ST7735 TFT 128x160 Menu 4 without library ###


from ST7735 import TFT
from sysfont import sysfont
from machine import SPI,Pin
from machine import ADC
import time, utime
import math

# *** SW. menu
B_up = Pin(13, Pin.IN, Pin.PULL_UP)   # press UP.
B_ok = Pin(14, Pin.IN, Pin.PULL_UP)   # press OK.
B_dn = Pin(15, Pin.IN, Pin.PULL_UP)   # press DN.

led_out1 = Pin(2, Pin.OUT) # led_1 toggle
led_out2 = Pin(3, Pin.OUT) # led_2 toggle
led_out3 = Pin(4, Pin.OUT) # led_1 toggle

press = 0               # push SW.
delay_pressMenu = 2000  # delay goto meno
delay_press = 100       # delay SW.
CountTime_ms = 1000     # without delay
#***************************************

#spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
spi = SPI(1, baudrate=20000000, polarity=0, phase=0,
              sck=Pin(10), mosi=Pin(11), miso=None)
tft=TFT(spi,16,17,18)
tft.initr()
tft.rgb(True)

tft.initr()
tft.rgb(True)
tft.fill(TFT.BLACK)

adcPin = 26   # *** ADC pin GPIO 26,27,28,29 = A0-A3 (pi pico, pi zero)
adc_1 = ADC(Pin(adcPin))

tab_r = 100   # *** tab row up-dn box displsy 0-100
teb_manu1 = 0
DELAY_MS = 1500   # anlog Read

def box1(): # *** box , text
    # GRAY,WHITE,PURPLE,YELLOW,CYAN,NAVY,FOREST,GREEN,MAROON,RED,BLACK
    tft.fillrect((0, tab_r), (128, 50), TFT.BLACK)  # size box 128x50
    tft.rect((0, tab_r), (128, 50), TFT.CYAN)

    # ***def text( self, aPos, aString, aColor, aFont, aSize = 1, nowrap = False ) :
    tft.text((23, 10+tab_r), str('Sensor Voltage'), TFT.CYAN, sysfont, 1)
    tft.text((85, 25+tab_r), "VOLT.", TFT.YELLOW, sysfont, 1, nowrap=True)
    tft.text((85, 35+tab_r), "<<<<<", TFT.RED, sysfont, 1, nowrap=True)
    
def acd1Read(): #**********************************
    value_last = adc_1.read_u16()
    utime.sleep_ms(10)
    value = adc_1.read_u16()  # adc_1.read_u16() , 16 bit 0-65535 3.3v
    volt  = (value/65535)*3.3
    
    tft.fillrect((20, 25+tab_r), (55, 15), TFT.BLACK) 
    if (value == value_last ):
        tft.text((20, 25+tab_r), str("%.3f" % (volt)), TFT.WHITE, sysfont, 2, nowrap=True)
       
    if (value != value_last ):  
        tft.text((20, 25+tab_r), str("%.3f" % (volt)), TFT.WHITE, sysfont, 2, nowrap=True)
    #print(value, value_last)
#--------------------------------------------------------------------------
def set_menu1(): 
    time.sleep(0.5)
    select_menu1()
    while (1):
        print("begin B_ok 1"); time.sleep(1)
        if (B_ok.value() == press): 
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_ok sw.on-off led_1"); time.sleep(1)
            led_out1.toggle()

        if (B_up.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_up ^^^^^^^^^^^^ 1"); time.sleep(1)
            set_menuExit()
            break

        if (B_dn.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_dn ............. 1"); time.sleep(1)
            set_menu2() 
            break

def set_menu2(): 
    time.sleep(0.5)
    select_menu2()
    while (1):
        print("begin B_ok 2"); time.sleep(1)
        if (B_ok.value() == press): 
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_ok sw.on-off led_2"); time.sleep(1)
            led_out2.toggle()

        if (B_up.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_up ^^^^^^^^^^^^ 2"); time.sleep(1)
            set_menu1()
            break

        if (B_dn.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_dn ............. 2"); time.sleep(1)
            set_menu3() 
            break

def set_menu3(): 
    time.sleep(0.5)
    select_menu3()
    while (1):
        print("begin B_ok 3"); time.sleep(1)
        if (B_ok.value() == press): 
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_ok sw.on-off led_3"); time.sleep(1)
            led_out3.toggle()

        if (B_up.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_up ^^^^^^^^^^^^ 3"); time.sleep(1)
            set_menu2()
            break

        if (B_dn.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_dn ............. 3"); time.sleep(1)
            set_menuExit() 
            break
def set_menuExit(): 
    time.sleep(0.5)
    select_menuExit()
    while (1):
        print("begin B_ok Exit"); time.sleep(1)
        if (B_ok.value() == press): 
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_ok Exit"); time.sleep(1)

            tft.text((25, 10+teb_manu1), "Menu 1", TFT.BLACK, sysfont, 2)
            tft.text((25, 30+teb_manu1), "Menu 2", TFT.BLACK, sysfont, 2)
            tft.text((25, 50+teb_manu1), "Menu 3", TFT.BLACK, sysfont, 2)
            tft.text((25, 70+teb_manu1), "EXIT",   TFT.BLACK, sysfont, 2)   
            tft.text((10,  10+teb_manu1), ">",     TFT.BLACK, sysfont, 2)
            tft.text((10,  30+teb_manu1), ">",     TFT.BLACK, sysfont, 2)
            tft.text((10,  50+teb_manu1), ">",     TFT.BLACK, sysfont, 2)
            tft.text((10,  70+teb_manu1), ">",     TFT.BLACK, sysfont, 2) 
            break

        if (B_up.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_up ^^^^^^^^^^^^ Exit"); time.sleep(1)
            set_menu3()
            break

        if (B_dn.value() == press):
            time.sleep_ms(delay_press)
            # add commend
            print("pass B_dn ............. Exit"); time.sleep(1)
            set_menu1() 
            break

# *** menu view ******
def menu():
    #teb_manu1 = 0
    tft.text((25, 10+teb_manu1), "Menu 1", TFT.WHITE, sysfont, 2)
    tft.text((25, 30+teb_manu1), "Menu 2", TFT.WHITE, sysfont, 2)
    tft.text((25, 50+teb_manu1), "Menu 3", TFT.WHITE, sysfont, 2)
    tft.text((25, 70+teb_manu1), "EXIT",   TFT.WHITE, sysfont, 2)

def select_menu0():    
    tft.text((10,  10+teb_manu1), ">", TFT.WHITE, sysfont, 2)
    tft.text((10,  30+teb_manu1), ">", TFT.WHITE, sysfont, 2)
    tft.text((10,  50+teb_manu1), ">", TFT.WHITE, sysfont, 2)
    tft.text((10,  70+teb_manu1), ">", TFT.WHITE, sysfont, 2)

def select_menu1():    
    tft.text((10,  10+teb_manu1), ">", TFT.YELLOW, sysfont, 2)
    tft.text((10,  30+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  50+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  70+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    
def select_menu2():    
    tft.text((10,  10+teb_manu1), ">", TFT.BLACK,  sysfont, 2)
    tft.text((10,  30+teb_manu1), ">", TFT.YELLOW, sysfont, 2)
    tft.text((10,  50+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  70+teb_manu1), ">", TFT.BLACK, sysfont, 2)

def select_menu3():    
    tft.text((10,  10+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  30+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  50+teb_manu1), ">", TFT.YELLOW, sysfont, 2)
    tft.text((10,  70+teb_manu1), ">", TFT.BLACK, sysfont, 2)

def select_menuExit():    
    tft.text((10,  10+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  30+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  50+teb_manu1), ">", TFT.BLACK, sysfont, 2)
    tft.text((10,  70+teb_manu1), ">", TFT.YELLOW, sysfont, 2)
 
#--------------------------------------------------------------------------   
def drivePicBMP():
    if f.read(2) == b'BM':  #header (2)
        dummy   = f.read(8) #file size(4), creator bytes(4) #*** f.read(8)
        offset  = int.from_bytes(f.read(4), 'little')
        hdrsize = int.from_bytes(f.read(4), 'little')
        width   = int.from_bytes(f.read(4), 'little')
        height  = int.from_bytes(f.read(4), 'little')
        
        if int.from_bytes(f.read(2), 'little') == 1: #planes must be 1
            depth = int.from_bytes(f.read(2), 'little')
            if depth == 24 and int.from_bytes(f.read(4), 'little') == 0: #compress method == uncompressed
                #print("Image size:", width,"x",height) #***
                rowsize = (width * 3 + 3) & ~ 3   
                if height < 0:          # < 0
                    height = -height    # -
                    flip = False
                else:
                    flip = True
                w, h = width, height   #  ***w, h = width, height
                if w > 128: w = 128
                if h > 160: h = 160
                tft._setwindowloc((0, 0),(w - 1,h - 1))
                for row in range(h):
                    if flip:
                        pos = offset + (height - 1 - row) * rowsize
                    else:
                        pos = offset + row * rowsize
                    if f.tell() != pos:
                        dummy = f.seek(pos)
                    for col in range(w):
                        bgr = f.read(3)
                        tft._pushcolor(TFTColor(bgr[2], bgr[1], bgr[0]))
    spi.deinit() #******
#-------------------------------------------------------------------------    

# *** loop program ***********
while True: # *************
    print("### ST7735 TFT 128x160 Menu 4 without library ###")

    #f = open('/PicBMP/circuit_P.bmp', 'rb') #**** / .BMP in directory  *****
    #drivePicBMP()

    box1()

    # *** timer acdRead *******
    t_saved_start = time.ticks_ms()  # *** time without delay

    # *** Loop ***
    while (1):
        value = adc_1.read_u16()  # adc_1.read_u16() , 16 bit 0-65535 3.3v
        volt  = (value/65535)*3.3
        print("%.2f" %(volt)); time.sleep(1)
        time_now = time.ticks_ms()  # ****
        time_delta = time.ticks_diff(time_now, t_saved_start) 
        if time_delta >= CountTime_ms:   # *** int =DELAY_MS= 
            t_saved_start = time_now
            acd1Read() # *****************
        #*** set menu**************************
        if (B_ok.value() == press):
            time.sleep_ms(delay_pressMenu) # goto menu prass RED "OK" 2 sec.
            # add command
            menu()
            select_menu0()
            time.sleep_ms(1000)
            set_menu1() #*****
            break
#////////////////////////////////////////////////////////////////////////////////

#### #https://wokwi.com/projects/379906405679232001
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT