PRESTO - Touch control of the Backlight Neopixels

I’ve been ‘playing’ with my PRESTO, using the touch screen as input and the backlight Neopixels as output - basic colour mixing.

Working pretty well. Just need to report that the screen displays a little bit of jitter. Probably because all the cores are working flat out.

Here is the code if you would like to give it a go.

# PRESTO - Touch control of the Backlight Neopixels
# Tony Goodhew 4th Jan 2025
# Touch the + and - characters on the screen, or lower down, 
# to change the current RGB values of the back lights

# Touch the Halt at top right to end execution of program
import time
from random import randint
from presto import Presto
import plasma   # Needed for Neopixels

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

WIDTH, HEIGHT = display.get_bounds()

touch = presto.touch # Activate touch

# WS2812 / NeoPixel™ LEDs used for the backlight
bl = plasma.WS2812(7, 0, 0, 33)
bl.start()
for i in range(7):
    bl.set_rgb(i, 255, 0, 0)
time.sleep(0.02)

# Create some colours
BLUE = display.create_pen(20,0,255)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(255,0,0)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(0,255,0)
PINK = display.create_pen(250, 125, 180)
CYAN = display.create_pen(0,255,255)
MAGENTA = display.create_pen(255,0,255)
BLACK = display.create_pen(0, 0, 0)
YELLOW = display.create_pen(255, 255, 0)
GREY = display.create_pen(75, 75, 75)

display.set_font("bitmap8") # Change the font

# A few procedures to be used later    
def clean(): # Clear the screen to Black
    display.set_pen(BLACK)
    display.clear()
    
def adjust(vt,dif,tmin,tmax): # Adjust RGB values
    vt = vt + dif
    if vt > tmax:
        vt = tmax
    elif vt < tmin:
        vt = tmin
    return vt

def sp(vv):  # Simple right justification
    vs = "   " + str(vv)
    vr =(vs[-3:])
    return vr

def draw():
    ss = [" -",sp(red)," +"," -",sp(green)," +"," -",sp(blue)," +"]
    for p in range(9):
        if p < 3:
            display.set_pen(RED)
        elif p < 6:
            display.set_pen(GREEN)
        else:
            display.set_pen(BLUE)
        pos = 15 + p * 50
        display.text(ss[p],pos, 400,80,5)
        
# Title page
clean()
display.set_pen(RED)
display.text("Touch Inputs",40,120,460,8)
display.set_pen(YELLOW)
display.text("Colour control",90,280,480,4)
display.set_pen(BLUE)
display.text("Tony Goodhew, Leicester UK",120,450,480,2)
presto.update()
time.sleep(1)

# Neopixels off
for i in range(7):
    bl.set_rgb(i,0, 0, 0)
time.sleep(0.02)

# Values: initial colour settings
red = 125
green = 125
blue = 125
d = 5   # Difference

    
running = True    
while running:
    clean()
    draw()
    display.set_pen(GREY)
    display.text("Halt",400,20,100,3)
    presto.update()
    
    touch.poll()
    if touch.state:
        x = touch.x
        y = touch.y
        if y < 100 and x > 400:
            running = False
        if x <= 90:
            red = adjust(red,-d,0,255)
        elif x <= 165:
            red = adjust(red, +d,0,255)
        elif x <= 240:
            green  = adjust(green,-d,0,255)
        elif x <= 315:
            green  = adjust(green,+d,0,255)
        elif x <= 390:
            blue  = adjust(blue,-d,0,255)   
        elif x > 375:
            blue  = adjust(blue,+d,0,255) 
        for i in range(7):
            bl.set_rgb(i,red, green,blue)
        time.sleep(0.02)

# Tidy up
for i in range(7):
    bl.set_rgb(i,0,0,0)
clean()
presto.update()

I’m not sure about the significance of the 33 in this statement. Does anyone know?
bl = plasma.WS2812(7, 0, 0, 33)

Comments/suggestions welcome.

Hello,
The 33 is likely the brightness level often ranging from 0 (off) to 255 (full brightness). A value of 33 would correspond to a relatively dim level.

Have a look here:

Thank you. 33 looks to be bright enough for me. I will try some other values.

With other values the Neopixels do not light up. ???

do you need to call bl.show()? Or does presto.update() handle that automatically?

I’ve not used bl.show() and it appears to update at once without it - Not seen it in the examples, which is all we have to work with until documentation appears in final version.

There are not many of us with the beta version and I’m the only one posting here.

I’ve added screen colours to the new version:

# PRESTO - Touch control of the Backlight Neopixels
# Version 2: screen colours added
# Tony Goodhew 5th Jan 2025
# Touch the + and - characters on the screen to change
# the current RGB values of the back lights

# Touch the Halt at top right to end execution of program
import time
from random import randint
from presto import Presto
import plasma   # Needed for Neopixels

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

WIDTH, HEIGHT = display.get_bounds()

touch = presto.touch # Activate touch

# WS2812 / NeoPixel™ LEDs used for the backlight
bl = plasma.WS2812(7, 0, 0, 33)
bl.start()
for i in range(7):
    bl.set_rgb(i, 255, 0, 0)
time.sleep(0.02)

# Create some colours
BLUE = display.create_pen(20,0,255)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(255,0,0)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(0,255,0)
PINK = display.create_pen(250, 125, 180)
CYAN = display.create_pen(0,255,255)
MAGENTA = display.create_pen(255,0,255)
BLACK = display.create_pen(0, 0, 0)
YELLOW = display.create_pen(255, 255, 0)
GREY = display.create_pen(75, 75, 75)

display.set_font("bitmap8") # Change the font

# A few procedures to be used later    
def clean(): # Clear the screen to Black
    display.set_pen(BLACK)
    display.clear()
    
def adjust(vt,dif,tmin,tmax): # Adjust RGB values
    vt = vt + dif
    if vt > tmax:
        vt = tmax
    elif vt < tmin:
        vt = tmin
    return vt

def sp(vv):  # Simple right justification
    vs = "   " + str(vv)
    vr =(vs[-3:])
    return vr

def draw():
    ss = [" -",sp(red)," +"," -",sp(green)," +"," -",sp(blue)," +"]
    for p in range(9):
        if p < 3:
            display.set_pen(RED)
        elif p < 6:
            display.set_pen(GREEN)
        else:
            display.set_pen(BLUE)
        pos = 15 + p * 50
        # Screen colours
        display.text(ss[p],pos, 400,80,5)
        display.set_pen(display.create_pen(red,0,0))
        display.rectangle(60,300,75,75)
        display.set_pen(display.create_pen(0,green,0))
        display.rectangle(215,300,75,75)
        display.set_pen(display.create_pen(0,0,blue))
        display.rectangle(360,300,75,75)
        TEMP =display.create_pen(red,green,blue)
        display.set_pen(TEMP)
        display.rectangle(20,100,440,50)

# Title page
clean()
display.set_pen(RED)
display.text("Touch Inputs",40,120,460,8)
display.set_pen(YELLOW)
display.text("Colour control",90,280,480,4)
display.set_pen(BLUE)
display.text("Tony Goodhew, Leicester UK",120,450,480,2)
presto.update()
time.sleep(2)

# Neopixels off
for i in range(7):
    bl.set_rgb(i,0, 0, 0)
time.sleep(0.02)

# Values: initial colour settings
red = 125
green = 125
blue = 125

# Colour differences on screen r & b 5 bits and green 6 bits
# Neopixels are 8 bits for each colour
drb = 8   # Difference
dg = 4   
running = True
for i in range(7):
    bl.set_rgb(i,red, green,blue) # Neopixels ON
time.sleep(0.02)
while running:
    clean()
    draw()    
    display.set_pen(GREY)
    display.text("Halt",400,20,100,3)
    presto.update()
    
    touch.poll()
    if touch.state:
        x = touch.x
        y = touch.y
        if y < 100 and x > 400:
            running = False
        if x <= 90:
            red = adjust(red,-drb,0,255)
        elif x <= 165:
            red = adjust(red, +drb,0,255)
        elif x <= 240:
            green  = adjust(green,-dg,0,255)
        elif x <= 315:
            green  = adjust(green,+dg,0,255)
        elif x <= 390:
            blue  = adjust(blue,-drb,0,255)   
        elif x > 375:
            blue  = adjust(blue,+drb,0,255) 
        for i in range(7):
            bl.set_rgb(i,red, green,blue)
        time.sleep(0.02)

# Tidy up
for i in range(7):
    bl.set_rgb(i,0,0,0)
clean()
presto.update()

A very simple example controlling the backlight Neopixels.

# PRESTO - Simple backlight manipulation
# Tony Goodhew - 7 Jan 2024

import time
from presto import Presto
import plasma

import time
from random import randint

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

WIDTH, HEIGHT = display.get_bounds()

# Create some screen colours
BLUE = display.create_pen(20,0,255)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(255,0,0)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(0,255,0)
PINK = display.create_pen(250, 125, 180)
CYAN = display.create_pen(0,255,255)
MAGENTA = display.create_pen(255,0,255)
BLACK = display.create_pen(0, 0, 0)
YELLOW = display.create_pen(255, 255, 0)
GREY = display.create_pen(70,70,70)

colours = [RED,YELLOW,ORANGE,GREEN,CYAN,BLUE,MAGENTA]
NP_colours = [[200,0,0],[200,200,0],[0,200,0],[0,200,200],[0,0,200],[200,0,200],[245,165,4]]
def clean(): # Clear the screen to Black
    display.set_pen(BLACK)
    display.clear()
    presto.update()

clean()
display.set_pen(BLUE)
display.text("Backlight Neopixels",20,120,480,6)
presto.update()
time.sleep(2)
clean()
# WS2812 / NeoPixel™ LEDs used for the backlight
bl = plasma.WS2812(7, 0, 0, 33)
bl.start()    


def NP_clear():
    for z in range(7):
        bl.set_rgb(z, 0, 0, 0)
   
clean()
display.set_pen(RED)
display.text("Red - slow fill and clear",20,120,480,6)
presto.update()
for n in range(2):
    for i in range(7):
        bl.set_rgb(i, 200, 0, 0)
        time.sleep(0.3)
    time.sleep(1)
    for i in range(7):
        bl.set_rgb(i, 0, 0, 0)
        time.sleep(0.2)
    time.sleep(1)

clean()
display.set_pen(RED)
display.text("Mixed - singles",20,120,480,6)
presto.update()
for n in range(2):
    for i in range(7):
        r,g,b = NP_colours[i]
        bl.set_rgb(i, r,g,b)
        time.sleep(0.5)
        bl.set_rgb(i, 0,0,0)
    time.sleep(1)
    NP_clear()

clean()
display.set_pen(GREY)
display.text("20 single random colours",20,300,480,7)
presto.update()
display.set_pen(ORANGE)
for n in range(20):    
    i = randint(0,6)
    r,g,b = (randint(0,255), randint(0,255), randint(0,255))
    display.set_pen(BLACK)
    display.rectangle(30,120,360,50)    
    display.set_pen(ORANGE)
    display.text(str(r)+" "+str(b)+" "+str(b),30,120,400,5)
    presto.update()
    bl.set_rgb(i, randint(0,255), randint(0,255), randint(0,255))
    time.sleep(0.7)
    bl.set_rgb(i, 0,0,0)
    time.sleep(0.2)
    NP_clear()

clean()
display.set_pen(GREY)
display.text("20 random colours",20,300,480,7)
presto.update()
display.set_pen(ORANGE)
for n in range(20):    
    r,g,b = (randint(0,255), randint(0,255), randint(0,255))
    display.set_pen(BLACK)
    display.rectangle(30,120,360,50)    
    display.set_pen(ORANGE)
    display.text(str(r)+" "+str(b)+" "+str(b),30,120,400,5)
    presto.update()
    for i in range(7):
        bl.set_rgb(i, r,b,g)
    time.sleep(0.7)
    for i in range(7):
        bl.set_rgb(i, 0,0,0)
    time.sleep(0.2)
    NP_clear()

clean()
presto.update()
1 Like

Colour mixing, with touch control.

I find the touch screen very accurate and find it easier to use the touch position rather than build loads of separate buttons. Here we mix colours for the screen and the backlight Neopixels by just touching the screen on or below the “+” or “-” characters on the display.

# PRESTO - Touch control of the Backlight Neopixels
# Tony Goodhew 21st Jan 2025
# Touch the + and - characters on the screen to change
# the current RGB values of the back lights and
# the rectangles on the display

# Touch the Halt at top right to end execution of program
import time
from presto import Presto
import plasma   # Needed for Neopixels

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

WIDTH, HEIGHT = display.get_bounds()

touch = presto.touch # Activate touch

# WS2812 / NeoPixel™ LEDs used for the backlight
bl = plasma.WS2812(7, 0, 0, 33)
bl.start()
for i in range(7):
    bl.set_rgb(i, 255, 0, 0)
time.sleep(0.02)

# Create some colours
BLUE = display.create_pen(20,0,255)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(255,0,0)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(0,255,0)
PINK = display.create_pen(250, 125, 180)
CYAN = display.create_pen(0,255,255)
MAGENTA = display.create_pen(255,0,255)
BLACK = display.create_pen(0, 0, 0)
YELLOW = display.create_pen(255, 255, 0)
GREY = display.create_pen(75, 75, 75)

display.set_font("bitmap8") # Change the font

# A few procedures to be used later    
def clean(): # Clear the screen to Black
    display.set_pen(BLACK)
    display.clear()
    
def adjust(vt,dif,tmin,tmax): # Adjust RGB values
    vt = vt + dif
    if vt > tmax:
        vt = tmax
    elif vt < tmin:
        vt = tmin
    return vt

def sp(vv):  # Simple right justification
    vs = "   " + str(vv)
    vr =(vs[-3:])
    return vr

def draw():
    ss = [" -",sp(red)," +"," -",sp(green)," +"," -",sp(blue)," +"]
    for p in range(9):
        if p < 3:
            display.set_pen(RED)
        elif p < 6:
            display.set_pen(GREEN)
        else:
            display.set_pen(BLUE)
        pos = 15 + p * 50
        display.text(ss[p],pos, 400,80,5)
        
# Title page
clean()
display.set_pen(RED)
display.text("Touch Inputs",40,120,460,8)
display.set_pen(YELLOW)
display.text("Colour control",90,280,480,4)
display.set_pen(BLUE)
display.text("Tony Goodhew, Leicester UK",120,450,480,2)
presto.update()
time.sleep(1)

# Neopixels off
for i in range(7):
    bl.set_rgb(i,0, 0, 0)
time.sleep(0.02)

# Values: initial colour settings
red = 125
green = 125
blue = 125
d = 5   # Difference
    
running = True    
while running:
    clean()
    draw()
    TEMP = display.create_pen(red,green,blue)
    display.set_pen(TEMP)
    display.rectangle(20,100,440,50)
    display.set_pen(GREY)
    display.text("Halt",400,20,100,3)
    rtemp = display.create_pen(red,0,0)
    display.set_pen(rtemp)
    display.rectangle(75,300,50,50)
    gtemp = display.create_pen(0,green,0)
    display.set_pen(gtemp)
    display.rectangle(225,300,50,50)
    btemp = display.create_pen(0,0,blue)
    display.set_pen(btemp)
    display.rectangle(375,300,50,50)
    presto.update()
    
    touch.poll()
    if touch.state:
        x = touch.x
        y = touch.y
        if y < 100 and x > 400:
            running = False
        if x <= 90:
            red = adjust(red,-d,0,255)
        elif x <= 165:
            red = adjust(red, +d,0,255)
        elif x <= 240:
            green  = adjust(green,-d,0,255)
        elif x <= 315:
            green  = adjust(green,+d,0,255)
        elif x <= 390:
            blue  = adjust(blue,-d,0,255)   
        elif x > 375:
            blue  = adjust(blue,+d,0,255) 
        for i in range(7):
            bl.set_rgb(i,red, green,blue)
        time.sleep(0.02)

# Tidy up
for i in range(7):
    bl.set_rgb(i,0,0,0)
clean()
presto.update()

We are still awaiting an answer to the original question. What is the 33 for?
bl = plasma.WS2812(7, 0, 0, 33)