PRESTO Touch Screen Demo

Delighted with my Presto. Here is another demo to help get you started.
Merry Christmas to you all!

# Touch screen demo on PRESTO
# Tony Goodhew 25 Dec 2024

import time
from random import randint
from presto import Presto

# Setup for the Presto display
presto = Presto(ambient_light=True)
display = presto.display
WIDTH, HEIGHT = display.get_bounds()

# Couple of colours for use later
BLUE = display.create_pen(28, 181, 202)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(230, 60, 45)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(9, 185, 120)
PINK = display.create_pen(250, 125, 180)
PURPLE = display.create_pen(118, 95, 210)
BLACK = display.create_pen(0, 0, 0)

COLOURS = [BLUE, RED, ORANGE, GREEN, PINK, PURPLE]

display.clear()
touch = presto.touch

# (xx, yy) point of arrowhead sz is size = point to centre of base
xx= 185
yy= 185
sz = 25
sz2 = int(sz*0.8)
s2 = int(sz*0.5773) # tan30
d = 2 * sz
def wipe():
    display.set_pen(BLACK)
    display.rectangle(0,0,100,50)
    
display.set_pen(RED)
display.triangle(xx,yy-d,xx-s2,yy-d+sz, xx+s2,yy-d+sz)    
display.triangle(xx,yy+d,xx-s2,yy+d-sz,xx+s2,yy+d-sz)
display.triangle(xx+d,yy,xx+sz,yy-s2,xx+sz,yy+s2)
display.triangle(xx-d,yy,xx-sz,yy+s2,xx-sz,yy-s2)
display.set_pen(GREEN)
display.circle(xx,yy,s2)
# Grid lines
display.set_pen(BLUE)
display.line(xx-d,yy-d,xx+d,yy-d)
display.line(xx-d,yy-sz2,xx+d,yy-sz2)
display.line(xx-d,yy+d,xx+d,yy+d)
display.line(xx-d,yy+sz2,xx+d,yy+sz2)
display.line(xx-d,yy-d,xx-d,yy+d)
display.line(xx-sz2,yy-d,xx-sz2,yy+d)
display.line(xx+sz2,yy-d,xx+sz2,yy+d)
display.line(xx+d,yy-d,xx+d,yy+d)
presto.update()

while True:
    txt=""
    touch.poll()
    if touch.state:
        x = touch.x
        y = touch.y
        print(x,y)


        display.set_pen(BLUE)
        if (x > (xx-d)) and (x <= (xx - sz2)) and (y > (yy -d)) and (y <= (yy -sz2)): # top left
            txt = "NW"
            
        if (x > (xx-d)) and (x <= (xx - sz2)) and (y > (yy -sz2)) and (y <= (yy +sz2)): # centre left
            txt = "W"
        if (x > (xx-d)) and (x <= (xx - sz2)) and (y > (yy +sz2)) and (y <= (yy +d)): # bottom left
            txt = "SW"   
        if (x > (xx-sz2)) and (x <= (xx + sz2)) and (y > (yy -d)) and (y <= (yy -sz2)): # top centre
            txt = "N"
        if (x > (xx-sz2)) and (x <= (xx + sz2)) and (y > (yy -sz2)) and (y <= (yy +sz2)): #centre,centre
            txt = "C"
        if (x > (xx-sz2)) and (x <= (xx + sz2)) and (y > (yy +sz2)) and (y <= (yy +d)): # bottom centre
            txt ="S"
        if (x > (xx+sz2)) and (x <= (xx + d)) and (y > (yy -d)) and (y <= (yy -sz2)): # top right
            txt = "NE"
        if (x > (xx+sz2)) and (x <= (xx + d)) and (y > (yy -sz2)) and (y <= (yy +sz2)): # centre right
            txt ="E"
        if (x > (xx+sz2)) and (x < (xx + d)) and (y > (yy +sz2)) and (y < (yy +d)): # bottom right
            txt = "SE"
    wipe()
    display.set_pen(BLUE)
    display.text(txt,0,0,200,5)
    presto.update()
    time.sleep(0.1)


Try changing the value of sz, xx and yy. The grid can be moved and resized.
Just a thought: Why is it presto.update() and not display.update() ?

I wrote this because I get an error when trying to run the touch_buttons program.

Iโ€™m really impressed with the accuracy of the touch co-ordinates.

Comments welcome.

1 Like

If you download the latest UF2 from here:
Release Version 0.0.5 - Touch Buttons! ยท pimoroni/presto ยท GitHub

it fixes the Button problem

1 Like

This program demonstrates how to input small integer values by touching the screen of the PRESTO.

โ€˜Dโ€™ is delete last digit, โ€˜Rโ€™ is RETURN/ENTER and โ€˜-โ€™ is toggle +ve/-ve.

# Demo for Pimoroni PRESTO
# Enter value from the touch screen
# Tony Goodhew 28th Dec 2024
# '0 - 9' input digits
# '-' toggle +ve/-ve
# 'D' delete last digit = rubout
# 'R' = RETURN/ENTER finish entering the number

import time
from presto import Presto

# Setup for the Presto display
presto = Presto(full_res=True,ambient_light=True)
display = presto.display

WIDTH, HEIGHT = display.get_bounds()

BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(255,0,0)
GREEN = display.create_pen(0,255,0)
BLUE = display.create_pen(0,0,255)
ORANGE = display.create_pen(245,165,4)

display.set_pen(BLACK)
display.clear()
touch = presto.touch

display.set_font("bitmap8") 

yt = 430
xh = 0
s = "-0123456789DR"

# Touch character buttons
for p in range(len(s)):
    display.set_pen(RED)
    if p > 10:
        display.set_pen(BLUE)
    if p == 12:
        display.set_pen(GREEN)
    display.text(s[p],p*36,yt,20,6)
presto.update()

v = 0
running = True
while running:
    touch.poll()
    if touch.state:
        x = touch.x
        y = touch.y
        if y >= 430:
            p = int(x/36) -1 # Calculate character position
            if p == 10: # DELETE
                p = -2
#        print(p) # For debug checking
        if p == 11: # R = finished number entry
            result = v
            running = False # STOP looping = RETURN HIT!
            break
        if p > -1:
            v = v *10 + p
        elif p == -1:
            v = v * -1
        elif p == -2:
            v = int(v/10)
        display.set_pen(BLACK)    
        display.rectangle(0,220,479,70)
        display.set_pen(GREEN)
        display.text(str(v),20,220,300,5)        
    presto.update()
    time.sleep(0.1)
    
display.set_pen(BLACK)
display.clear()
display.set_pen(GREEN)
display.text("Value entered:",20,170,400,5)
display.set_pen(ORANGE)
display.text(str(v),20,220,400,5)
presto.update()
time.sleep(4)

# Tidy up
display.set_pen(BLACK)
display.clear()
presto.update()

A very impressive new board.

1 Like