Pimo pico watch

Hey,
I try to make a simple watch…


Again, I have problems with buttons X and Y…???
Maybe it’s due to uasyncio…
So I use only 2 buttons… But I want manpulate the 3 rgb canal of colors…
So more than 2 buttons could be much more better…
If someone can help me…
My code:

from machine import I2C, SPI, Pin
import uasyncio
import st7789
from pimoroni import RGBLED
from time import localtime
import vga1_16x16 as font

##buttons
A = Pin(12,Pin.IN,Pin.PULL_UP)
B = Pin(13,Pin.IN, Pin.PULL_UP)
X = Pin(14,Pin.IN,Pin.PULL_UP)
Y = Pin(15,Pin.IN,Pin.PULL_UP)
button = Pin(23, Pin.IN, Pin.PULL_DOWN)

#rgb led
led = RGBLED(6, 7, 8)
ledi = 1 ## rgb led state on/off 
ledu = (0,5,7) ##rgb led color
r,g,b = ledu[0],ledu[1],ledu[2]
led.set_rgb(r,g,b)
colori = st7789.color565(0,255,255) ###for have the number 0 in the same color of the rgb led that you could choice
placi = 0

async def color_choice(r,g,b):
    global colori, ledu
    colori = st7789.color565(r,g,b)
    ledu = (r,g,b)
    led.set_rgb(r,g,b)
    
##lcd display
spi = SPI(0, baudrate=60000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(19),miso=Pin(16))
lcd = st7789.ST7789(spi,135,240,reset=Pin(29, Pin.OUT),cs=Pin(17, Pin.OUT),dc=Pin(16, Pin.OUT),backlight=Pin(20, Pin.OUT),rotation=0)
stato = 1  ##lcd state on/off 
backi = 0.5  ##lcd backlight intensity(float number between 0 and 1)
tempo = 1

async def disp():
    global stato, colori, tempo
    if stato == 1:
        t = localtime()
        heure, minute = t[3], t[4]
        lcd.fill(0)
        uasyncio.create_task(affichage(heure,20, 25, 5))
        if tempo == 1:
            for i in range(10):
                lcd.vline(20+i*10, 115, 5, colori)
                tempo = 0
        else:
            tempo = 1
        uasyncio.create_task(affichage(minute,20, 150, 5))
        await uasyncio.sleep_ms(500)
        if X.value() == 0:
            if stato == 1:
                stato = 0
                lcd.backlight(0)
                backi = 0
        if B.value() == 0:
            if stato == 1:
                stato = 2
            else:
                stato = 1
        await uasyncio.sleep_ms(500)
        
    if stato == 2:
        global ledu, r, g, b, placi
        r,g,b = ledu[0],ledu[1],ledu[2]
        lcd.fill(0)
        if placi == 0:
            lcd.fill_rect(10,2,5,20,st7789.WHITE)
        lcd.text(font, "R: {}".format(r), 20, 2, st7789.WHITE, st7789.BLACK)
        if placi == 1:
            lcd.fill_rect(10,40,5,20,st7789.WHITE)
        lcd.text(font, "G: {}".format(g), 20, 40, st7789.WHITE, st7789.BLACK)
        if placi == 2:
            lcd.fill_rect(10,80,5,20,st7789.WHITE)
        lcd.text(font, "B: {}".format(b), 20, 80, st7789.WHITE, st7789.BLACK)
        
        lcd.text(font, "Color565", 2, 120, st7789.WHITE, st7789.BLACK)
        global colori
        lcd.text(font, str(colori), 2, 150, st7789.WHITE, st7789.BLACK)
        hexi = str(hex(colori))
        lcd.text(font, "ColorHex",2, 180, st7789.WHITE, st7789.BLACK)
        lcd.text(font, str(hexi), 2, 210, st7789.WHITE, st7789.BLACK)
        lcd.text(font, "boot for save", 2, 230, st7789.WHITE, st7789.BLACK)
        await uasyncio.sleep_ms(500)

        if button.value() == 0:
            stato = 1       
        if Y.value() == 0:
            stato = 1
        if X.value() == 0:
            r += 10
            if r > 255:
                r = 0
        if A.value() == 0:
            g += 10
            if g > 255:
                g = 0
        if B.value() == 0:
            placi += 1
            if placi == 3:
                placi = 0
        uasyncio.create_task(color_choice(r,g,b))
        await uasyncio.sleep_ms(500)
        
async def main():
    while 1:
        uasyncio.create_task(ledron(r,g,b))
        uasyncio.create_task(disp())         
        await uasyncio.sleep_ms(5000)

async def ledron(r,g,b):
    if A.value() == 0:
        global ledi  
        if ledi == 1:
            ledi = 0
            led.set_rgb(0,0,0)
        else:
            ledi = 1
    if ledi == 1:
        led.set_rgb(r,g,b)            
    await uasyncio.sleep_ms(500)
    
##font design         
async def space(a,b,scale):
    pass

async def number_0(a,b,scale):
    global colori
    for i in range(3):
        lcd.hline(a,b+i,9*scale,colori)
    for i in range(3):
        lcd.hline(a,11*scale+b+i,9*scale,colori)
    for i in range(3):
        lcd.vline(a+i,b,12*scale,colori)
    for i in range(3):
        lcd.vline(8*scale+a+i,b,12*scale,colori)

async def number_1(a,b,scale):
    for i in range(3):
        lcd.vline(4*scale+a+i,b,12*scale,st7789.color565(255,255,255))

async def number_2(a,b,scale):
    for i in range(3):
        lcd.hline(a,i+b,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,6*scale+i+b,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,11*scale+i+b,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(a+i,7*scale+b,5*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(8*scale+a+i,b,7*scale,st7789.color565(255,255,255))

async def number_3(a,b,scale):
    for i in range(3):
        lcd.hline(a,b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a+3*scale,6*scale+b+i,6*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,11*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(8*scale+a+i,b,12*scale,st7789.color565(255,255,255))

async def number_4(a,b,scale):
    for i in range(3):
          lcd.vline(a+i,b,9*scale,st7789.color565(255,255,255))
    for i in range(3):
          lcd.vline(5*scale+a+i,b+5*scale,6*scale,st7789.color565(255,255,255))
    for i in range(3):
          lcd.hline(a,8*scale+b+i,9*scale,st7789.color565(255,255,255))


async def number_5(a,b,scale):
    for i in range(3):
        lcd.hline(a,b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,6*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,11*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(a+i,b,7*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(8*scale+a+i,6*scale+b,6*scale,st7789.color565(255,255,255))

async def number_6(a,b,scale):
    for i in range(3):
        lcd.hline(a,b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,6*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,11*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(a+i,b,12*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(8*scale+a+i,6*scale+b,6*scale,st7789.color565(255,255,255))

async def number_7(a,b,scale):
    for i in range(3):
        lcd.hline(a+2*scale,b+i,5*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a+3*scale,5*scale+b+i,6*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(6*scale+a+i,b,12*scale,st7789.color565(255,255,255))

async def number_8(a,b,scale):
    for i in range(3):
        lcd.hline(a,b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,6*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,11*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(a+i,b,12*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(8*scale+a+i,b,12*scale,st7789.color565(255,255,255))

async def number_9(a,b,scale):
    for i in range(3):
        lcd.hline(a,b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,6*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.hline(a,11*scale+b+i,9*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(a+i,b,7*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(8*scale+a+i,b,12*scale,st7789.color565(255,255,255))

async def point(a,b,scale):
    for i in range(3):
        lcd.vline(3+a+i,b+9*scale,3*scale,st7789.color565(255,255,255))

async def points2(a,b,scale):
    for i in range(3):
        lcd.vline(3+a+i,b+9*scale,3*scale,st7789.color565(255,255,255))
    for i in range(3):
        lcd.vline(3+a+i,b+5*scale,3*scale,st7789.color565(255,255,255))

async def conversion(symbol,a,b,scale=1):
    symbol = str(symbol)
    a = str(a)
    b = str(b)
    scale = str(scale)
    liste_noms_symbols = ["space", "point","points2","number_0","number_1","number_2","number_3","number_4","number_5","number_6","number_7","number_8","number_9"]
    liste_symbols = [" ", ".",":", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    for i in liste_symbols:
        if i == symbol:
            phrasi = "uasyncio.create_task(" + liste_noms_symbols[liste_symbols.index(i)] + "(" + a + "," + b + "," + "scale=" + scale + "))"
    convert = eval(phrasi)
    return convert

async def affichage(numbers,a,b,scale):
    numbers = str(numbers)
    sautx_8 = [" ", ".",":"]
    sautx_11 = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    for i in numbers:
        i = str(i)
        if i in sautx_8:
            uasyncio.create_task(conversion(i,a,b,scale))
            a += 6*scale
        if i in sautx_11:
            uasyncio.create_task(conversion(i,a,b,scale))
            a += 11*scale       
uasyncio.run(main())

Thanks

Have you seen this button example? It’s what I use with my Display Packs.
pimoroni-pico/micropython/examples/pico_display/button_test.py at main · pimoroni/pimoroni-pico (github.com)

Hi Alpha,
I think that I tried this example and it has worked fine. But, when it comes harder situations…
I will try exactly this example, just adapted with a time display and a possibility to change the 3 rgb colors…
Thanks.

Hey!
Look at that… An addon to my picmo watch…

It works perfectly, but without display, JUST my android phone with the repl application…
my code:

from machine import Pin, SPI
from micropython_rfm9x import *
from time import sleep

RADIO_FREQ_MHZ = 433.0
CS = Pin(17, Pin.OUT)
RESET = Pin(20, Pin.OUT)
spi = SPI(0, baudrate=2000000, polarity=0, phase=0)
rfm9x = RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
rfm9x.hight_power = 20

def rfm_check():
    packet9x = rfm9x.receive()
    print(packet9x)
    packet_text9x = ""
    if packet9x is None:
        pass
    else:
        try:
            packet_text9x = str(packet9x, "utf-8")
            print(packet_text9x)
        except:
            pass
    
def rfm_send():    
    try:
        rfm9x.send(bytes("DM?", "utf-8"))
        print(text)      
    except:
        pass

while 1:
    rfm_send()
    rfm_check()
    sleep(1)

I’m thinking yet to the next pimo board!