Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Displayio unwanted flashing
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Displayio unwanted flashing

by Tonygo on Tue Aug 18, 2020 7:20 am

I've just bought an Edge Badge and am upgrading my knowledge of displays from mono SSD1306 to colour with displayio. I've written a script which works but the last item sent to the screen always flashes at just under a 1 second interval.
I've tried all sorts of things but cannot remove the flashing.

Do we really have to put all this code in the loop?

Code: Select all | TOGGLE FULL SIZE
# Controls Neopixels via buttons and displays RGB values on screen
#  Edge Badge
# Tony Goodhew 17 August 2020
import time
import board
import terminalio
import displayio
import simpleio
import digitalio
import adafruit_imageload
from gamepadshift import GamePadShift
from adafruit_display_text import label
import neopixel

# NeoPixel 0-4 on D8
NUMPIXELS = 5
neopixels = neopixel.NeoPixel(board.D8, NUMPIXELS, brightness=0.1, auto_write=False)
red = 125
green = 125
blue = 125

def up(v):
    v = v + 5
    if v > 255:
        v = 255
    return v
def down(v):
    v = v - 5
    if v < 0:
        v = 0
    return v

#  setup for PyBadge buttons
BUTTON_LEFT = const(128)
BUTTON_UP = const(64)
BUTTON_DOWN = const(32)
BUTTON_RIGHT = const(16)
BUTTON_A = const(2)
BUTTON_B = const(1)

pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
                   digitalio.DigitalInOut(board.BUTTON_OUT),
                   digitalio.DigitalInOut(board.BUTTON_LATCH))

current_buttons = pad.get_pressed()
last_read = time.monotonic()

display = board.DISPLAY
red =   125 # Initial Neopixel values
green = 125
blue =  125

while True:
    #  checks if button has been pressed
    if (last_read + 0.05) < time.monotonic():
        buttons = pad.get_pressed()
        last_read = time.monotonic()
        if buttons & BUTTON_UP:
            red = up(red)
        if buttons & BUTTON_DOWN:
            red = down(red)
        if buttons & BUTTON_LEFT:
            green = down(green)
        if buttons & BUTTON_RIGHT:
            green = up(green)
        if buttons & BUTTON_A:
            blue = up(blue)
        if buttons & BUTTON_B:
            blue = down(blue)
        neopixels[0] = (red, 0, 0)
        neopixels[1] = (0, green, 0)
        neopixels[2] = (0, 0, blue)
        neopixels[3] = (0, 0, 0)
        neopixels[4] = (red, green, blue)
        neopixels.show()
        percent = int(100 * (red + green + blue) / 765)

        # Make the display context
        splash = displayio.Group(max_size=11)
        display.show(splash)

        color_bitmap = displayio.Bitmap(160, 128, 1) # Screen size
        color_palette = displayio.Palette(1)
        color_palette[0] = 0x202020  # Grey
        bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
        splash.append(bg_sprite)

        # Draw a smaller inner rectangle in Darker Grey
        inner_bitmap = displayio.Bitmap(140, 108, 1)
        inner_palette = displayio.Palette(1)
        inner_palette[0] = 0x050505  # Darker grey
        inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=10, y=10)
        splash.append(inner_sprite)

        # Draw graph - rectangle
        graph_bitmap = displayio.Bitmap(percent, 10, 1)
        graph_palette = displayio.Palette(1)
        graph_palette[0] = 0xBBBBBB  # Light grey
        graph_sprite = displayio.TileGrid(graph_bitmap, pixel_shader=graph_palette, x=15, y=80)
        splash.append(graph_sprite)

        # Draw graph baseline - rectangle
        base_bitmap = displayio.Bitmap(1, 20, 1)
        base_palette = displayio.Palette(1)
        base_palette[0] = 0xBBBBBB  # White
        base_sprite = displayio.TileGrid(base_bitmap, pixel_shader=base_palette, x=14, y=75)
        splash.append(base_sprite)

        # Draw a colour values as text
        text_group4 = displayio.Group(max_size=3, scale=1, x=120, y=85)
        textp = str(percent) +" %"
        text_areap = label.Label(terminalio.FONT, text=textp, color=0xFFFFFF)
        text_group4.append(text_areap)  # Subgroup for text scaling
        splash.append(text_group4)

        text_group1 = displayio.Group(max_size=3, scale=2, x=20, y=20)
        textr = str(red)
        text_arear = label.Label(terminalio.FONT, text=textr, color=0xFF0000  )
        text_group1.append(text_arear)  # Subgroup for text scaling
        splash.append(text_group1)

        text_group2 = displayio.Group(max_size=3, scale=2, x=20, y=40)
        textg = str(green)
        text_areag = label.Label(terminalio.FONT, text=textg, color=0x00FF00)
        text_group2.append(text_areag)  # Subgroup for text scaling
        splash.append(text_group2)

        text_group3 = displayio.Group(max_size=3, scale=2, x=20, y=60)
        textb = str(blue)
        text_areab = label.Label(terminalio.FONT, text=textb, color=0x0000FF)
        text_group3.append(text_areab)  # Subgroup for text scaling
        splash.append(text_group3)

        text_group5 = displayio.Group(max_size=3, scale=2, x=100, y=20)
        textt = "DEMO"
        text_areat = label.Label(terminalio.FONT, text=textt, color=0xFF00FF)
        text_group5.append(text_areat)  # Subgroup for text scaling
        splash.append(text_group5)


I would very very grateful if someone with more experience could point me in the right direction.

Tonygo
 
Posts: 79
Joined: Fri Apr 13, 2018 11:09 am

Re: Displayio unwanted flashing

by kevinjwalters on Tue Aug 18, 2020 10:54 am

I have some minor flicker in a program which I left in as a discussion topic. Once you display.show() a Group it will notice any subsequent changes and send them to the screen in the background for you.

If you move your display code out of the loop to just before it and then only change what's needed in the loop that should stop the flickering. In the loop you can just set text_arear.text = str(red) for a new value. A caveat for the Label object is they have a static maximum length set at construction time. You either need something like text=" " in the constuctor or alternatively max_glyphs=3 to allow you to show 0-255.

For bitmaps this will involve a bit more work so there's a choice here about adjusting them vs recreating them. You can assign a new one into the group using array references.

kevinjwalters
 
Posts: 740
Joined: Sun Oct 01, 2017 3:15 pm

Re: Displayio unwanted flashing

by kevinjwalters on Tue Aug 18, 2020 10:59 am

I've not used it but his may be of interest either for direct use or for inspiration on using displayio: https://github.com/adafruit/Adafruit_Ci ... ressbar.py

kevinjwalters
 
Posts: 740
Joined: Sun Oct 01, 2017 3:15 pm

Re: Displayio unwanted flashing

by Tonygo on Tue Aug 18, 2020 11:56 am

Thanks Kevin.
I've tried this

Code: Select all | TOGGLE FULL SIZE
# Controls Neopixels via buttons and displays RGB values on screen
#  Edge Badge
# Tony Goodhew 18 August 2020
import time
import board
import terminalio
import displayio
import simpleio
import digitalio
import adafruit_imageload
from gamepadshift import GamePadShift
from adafruit_display_text import label
import neopixel

# NeoPixel 0-4 on D8
NUMPIXELS = 5
neopixels = neopixel.NeoPixel(board.D8, NUMPIXELS, brightness=0.1, auto_write=False)
red = 125
green = 125
blue = 125
percent = int(100 * (red + green + blue) / 765)
def up(v):
    v = v + 5
    if v > 255:
        v = 255
    return v
def down(v):
    v = v - 5
    if v < 0:
        v = 0
    return v

#  setup for PyBadge buttons
BUTTON_LEFT = const(128)
BUTTON_UP = const(64)
BUTTON_DOWN = const(32)
BUTTON_RIGHT = const(16)
BUTTON_A = const(2)
BUTTON_B = const(1)

pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
                   digitalio.DigitalInOut(board.BUTTON_OUT),
                   digitalio.DigitalInOut(board.BUTTON_LATCH))

current_buttons = pad.get_pressed()
last_read = time.monotonic()

display = board.DISPLAY
red =   125 # Initial Neopixel values
green = 125
blue =  125
# Make the display context
splash = displayio.Group(max_size=11)
display.show(splash)
txtGroup = displayio.Group(max_size=10)
color_bitmap = displayio.Bitmap(160, 128, 1) # Screen size
color_palette = displayio.Palette(1)
color_palette[0] = 0x202020  # Grey
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

# Draw a smaller inner rectangle in Darker Grey
inner_bitmap = displayio.Bitmap(140, 108, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x050505  # Darker grey
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=10, y=10)
splash.append(inner_sprite)

# Draw graph - rectangle
graph_bitmap = displayio.Bitmap(percent, 10, 1)
graph_palette = displayio.Palette(1)
graph_palette[0] = 0xBBBBBB  # Light grey
graph_sprite = displayio.TileGrid(graph_bitmap, pixel_shader=graph_palette, x=15, y=80)
splash.append(graph_sprite)

# Draw graph baseline - rectangle
base_bitmap = displayio.Bitmap(1, 20, 1)
base_palette = displayio.Palette(1)
base_palette[0] = 0xBBBBBB  # White
base_sprite = displayio.TileGrid(base_bitmap, pixel_shader=base_palette, x=14, y=75)
splash.append(base_sprite)

# Draw a colour values as text
text_group4 = displayio.Group(max_size=3, scale=1, x=120, y=85)
textp = str(percent) +" %"
text_areap = label.Label(terminalio.FONT, text=textp, color=0xFFFFFF)
text_group4.append(text_areap)  # Subgroup for text scaling
#splash.append(text_group4)

text_group1 = displayio.Group(max_size=3, scale=2, x=20, y=20)
textr = str(red)
text_arear = label.Label(terminalio.FONT, text=textr, color=0xFF0000  )
text_group1.append(text_arear)  # Subgroup for text scaling
#splash.append(text_group1)

text_group2 = displayio.Group(max_size=3, scale=2, x=20, y=40)
textg = str(green)
text_areag = label.Label(terminalio.FONT, text=textg, color=0x00FF00)
text_group2.append(text_areag)  # Subgroup for text scaling
#splash.append(text_group2)

text_group3 = displayio.Group(max_size=3, scale=2, x=20, y=60)
textb = str(blue)
text_areab = label.Label(terminalio.FONT, text=textb, color=0x0000FF)
text_group3.append(text_areab)  # Subgroup for text scaling
#splash.append(text_group3)

text_group5 = displayio.Group(max_size=3, scale=2, x=100, y=20)
textt = "DEMO"
text_areat = label.Label(terminalio.FONT, text=textt, color=0xFF00FF)
text_group5.append(text_areat)  # Subgroup for text scaling
#splash.append(text_group5)

txtGroup.append(text_group1)
txtGroup.append(text_group2)
txtGroup.append(text_group3)
txtGroup.append(text_group4)
txtGroup.append(text_group5)

splash.append(txtGroup)
while True:
    #  checks if button has been pressed
    if (last_read + 0.05) < time.monotonic():
        buttons = pad.get_pressed()
        last_read = time.monotonic()
        if buttons & BUTTON_UP:
            red = up(red)
        if buttons & BUTTON_DOWN:
            red = down(red)
        if buttons & BUTTON_LEFT:
            green = down(green)
        if buttons & BUTTON_RIGHT:
            green = up(green)
        if buttons & BUTTON_A:
            blue = up(blue)
        if buttons & BUTTON_B:
            blue = down(blue)
    neopixels[0] = (red, 0, 0)
    neopixels[1] = (0, green, 0)
    neopixels[2] = (0, 0, blue)
    neopixels[3] = (0, 0, 0)
    neopixels[4] = (red, green, blue)
    neopixels.show()
   
    percent = int(100 * (red + green + blue) / 765)
    textb = str(blue)
    textg = str(green)
    textr = str(red)
    textp = str(percent) +" %"

    display.show(splash)


It stops the flashing but the graphics do not update at all. They are stuck on initial conditions. The Neopixels update properly

I'm still missing out something vital!

Tonygo
 
Posts: 79
Joined: Fri Apr 13, 2018 11:09 am

Re: Displayio unwanted flashing

by kevinjwalters on Tue Aug 18, 2020 12:17 pm

You need to replace textr = str(red) with text_arear.text = str(red) inside the loop to update the label text.

And the display.show(splash) inside loop is superfluous.

kevinjwalters
 
Posts: 740
Joined: Sun Oct 01, 2017 3:15 pm

Re: Displayio unwanted flashing

by Tonygo on Tue Aug 18, 2020 3:09 pm

Thank you, Kevin.

All working properly. I love this little board - especially the load of ports on the back.

Tonygo
 
Posts: 79
Joined: Fri Apr 13, 2018 11:09 am

Re: Displayio unwanted flashing

by Tonygo on Tue Aug 25, 2020 6:06 am

I like to leave a solution at the end of requests for help so that others can more easily climb out of the holes I got stuck in.

My initial graph, by drawing pixels, was far too slow. This works much better.

Here is my non-flickering solution:

Code: Select all | TOGGLE FULL SIZE
# Controls Neopixels via buttons and displays RGB values on screen
#  Edge Badge  - Steady graph
# Tony Goodhew 25 August 2020
import time
import board
import terminalio
import displayio
import simpleio
import digitalio
import adafruit_imageload
from gamepadshift import GamePadShift
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
import neopixel

# NeoPixels 0-4 on D8
NUMPIXELS = 5
neopixels = neopixel.NeoPixel(board.D8, NUMPIXELS, brightness=0.1, auto_write=False)
red = 125
green = 125
blue = 125
percent = int(100 * (red + green + blue) / 765)
old_p = 0
def up(v):
    v = v + 5
    if v > 255:
        v = 255
    return v
def down(v):
    v = v - 5
    if v < 0:
        v = 0
    return v

#  Setup for PyBadge buttons
BUTTON_LEFT = const(128)
BUTTON_UP = const(64)
BUTTON_DOWN = const(32)
BUTTON_RIGHT = const(16)
BUTTON_A = const(2)
BUTTON_B = const(1)

pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
                   digitalio.DigitalInOut(board.BUTTON_OUT),
                   digitalio.DigitalInOut(board.BUTTON_LATCH))

current_buttons = pad.get_pressed()
last_read = time.monotonic()

display = board.DISPLAY
red =   125 # Initial Neopixel values
green = 125
blue =  125
display = board.DISPLAY

# Make the display context
splash = displayio.Group(max_size=11)
display.show(splash)
txtGroup = displayio.Group(max_size=10)
color_bitmap = displayio.Bitmap(160, 128, 2) # Screen size
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

redBar_group = displayio.Group(max_size=1) # Under bar 100 units long - fixed position
redBar = Rect(0,0,100,10, fill=0xB0B0B0,outline=0xB0B0B0) # Grey looks better
redBar_group.append(redBar)
blackBar_group = displayio.Group(max_size=1) # Upper bar - moves right to expose under bar
blackBar = Rect(0,0,100,10, fill=0x000000,outline=0x000000)
blackBar_group.append(blackBar)
splash.append(redBar_group)       # These bars must be UNDER percent text
splash.append(blackBar_group)

# Draw graph baseline - rectangle
base_bitmap = displayio.Bitmap(1, 20, 1)
base_palette = displayio.Palette(1)
base_palette[0] = 0xBBBBBB  # Grey
base_sprite = displayio.TileGrid(base_bitmap, pixel_shader=base_palette, x=14, y=75)
splash.append(base_sprite)

# Draw colour values as text
text_group4 = displayio.Group(max_size=3, scale=1, x=120, y=85)  # %
textp = str(percent) +" %"
text_areap = label.Label(terminalio.FONT, text=textp, color=0xFFFFFF)
text_group4.append(text_areap)  # Subgroup for text scaling

text_group1 = displayio.Group(max_size=3, scale=2, x=20, y=20)  # Red value
textr = str(red)
text_arear = label.Label(terminalio.FONT, text=textr, color=0xFF0000  )
text_group1.append(text_arear)  # Subgroup for text scaling

text_group2 = displayio.Group(max_size=3, scale=2, x=20, y=40)  # Green value
textg = str(green)
text_areag = label.Label(terminalio.FONT, text=textg, color=0x00FF00)
text_group2.append(text_areag)  # Subgroup for text scaling

text_group3 = displayio.Group(max_size=3, scale=2, x=20, y=60)  # Blue value
textb = str(blue)
text_areab = label.Label(terminalio.FONT, text=textb, color=0x0000FF)
text_group3.append(text_areab)  # Subgroup for text scaling

text_group5 = displayio.Group(max_size=3, scale=2, x=100, y=20) # Title - DEMO
textt = "DEMO"
text_areat = label.Label(terminalio.FONT, text=textt, color=0xFF00FF)
text_group5.append(text_areat)  # Subgroup for text scaling

txtGroup.append(text_group1)
txtGroup.append(text_group2)
txtGroup.append(text_group3)
txtGroup.append(text_group4)
txtGroup.append(text_group5)
splash.append(txtGroup)          # This is ABOVE graph parts

while True:
    #  checks if button has been pressed
    if (last_read + 0.05) < time.monotonic():
        buttons = pad.get_pressed()
        last_read = time.monotonic()
        if buttons & BUTTON_UP:
            red = up(red)
        if buttons & BUTTON_DOWN:
            red = down(red)
        if buttons & BUTTON_LEFT:
            green = down(green)
        if buttons & BUTTON_RIGHT:
            green = up(green)
        if buttons & BUTTON_A:
            blue = up(blue)
        if buttons & BUTTON_B:
            blue = down(blue)
# Update Neopixels
    neopixels[0] = (red, 0, 0)
    neopixels[1] = (0, green, 0)
    neopixels[2] = (0, 0, blue)
    neopixels[3] = (0, 0, 0)
    neopixels[4] = (red, green, blue)
    neopixels.show()
# Update lables
    percent = int(100 * (red + green + blue) / 765)
    text_areab.text = str(blue)
    text_areag.text = str(green)
    text_arear.text = str(red)
    text_areap.text = str(percent) +" %"
# Update graph
    if old_p != percent:
        redBar_group.x = 15
        redBar_group.y = 80
        blackBar_group.y = 80
        blackBar_group.x = percent+15 # Moves right to expose under bar



I've learned a great deal about Displayio while writing this . I hope it helps others.

Tonygo
 
Posts: 79
Joined: Fri Apr 13, 2018 11:09 am

Re: Displayio unwanted flashing

by kevinjwalters on Wed Sep 02, 2020 1:16 pm

The flicker that I mentioned (and created!) is shown in the video on CLUE Rock, Paper, Scissors Game using Bluetooth: Advanced Game.

kevinjwalters
 
Posts: 740
Joined: Sun Oct 01, 2017 3:15 pm

Re: Displayio unwanted flashing

by Tonygo on Thu Sep 03, 2020 5:27 am

Thanks for the link. I'm getting into this now and realise how important is the order of appending the graphic elements.
This version moves 7 blocks of different lengths about and is still flicker free (apart from a tiny bit of pot fluctuation).

Code: Select all | TOGGLE FULL SIZE
# Edge Badge -  Bar Graph - Addition method
# Tony Goodhew 23 August 2020
# Adapted from Adafruit.com Clue examples
# Needs a 10 K potentiometer on A0 to provide input
import board
from adafruit_display_shapes.rect import Rect
from adafruit_display_text import label
import terminalio
import displayio
import busio
import digitalio
import time
from analogio import AnalogIn
pot = AnalogIn(board.A0)   # Red Potentiometer

def map(in_val, in_min, in_max, out_min, out_max): # Change range
    val=(float(in_val)/(in_max - in_min))*(out_max - out_min)
    out_val = out_min+val
    if out_val < 0:
        out_val = 0
    return int(out_val)

block = [64,32,16,8,4,2,1]  # Lengths of the blocks
p = [15,15,15,15,15,15,15]  # Space for blocks' x-positions
percent=0

display = board.DISPLAY     # Built in screen (160 x 128 RGB pixels)
edge_group = displayio.Group(max_size=10)   # Whole display - 10 items (text x2, Graphics x8)

block64_group = displayio.Group(max_size=1) # Groups for the individual blocks
block32_group = displayio.Group(max_size=1) # so that we can reposition them
block16_group = displayio.Group(max_size=1) # separately
block8_group = displayio.Group(max_size=1)
block4_group = displayio.Group(max_size=1)
block2_group = displayio.Group(max_size=1)
block1_group = displayio.Group(max_size=1)

b64 = Rect(0,0,64,10, fill=0xFF0000,outline=0xFF0000) # Draw the 7 blocks
b32 = Rect(0,0,32,10, fill=0xFF0000,outline=0xFF0000)
b16 = Rect(0,0,16,10, fill=0xFF0000,outline=0xFF0000)
b8 = Rect(0,0,8,10, fill=0xFF0000,outline=0xFF0000)
b4 = Rect(0,0,4,10, fill=0xFF0000,outline=0xFF0000)
b2 = Rect(0,0,2,10, fill=0xFF0000,outline=0xFF0000)
b1 = Rect(0,0,1,10, fill=0xFF0000,outline=0xFF0000)

block64_group.append(b64) # Put blocks in their groups
block32_group.append(b32)
block16_group.append(b16)
block8_group.append(b8)
block4_group.append(b4)
block2_group.append(b2)
block1_group.append(b1)

edge_group.append(block64_group) # Append block groups to display
edge_group.append(block32_group)
edge_group.append(block16_group)
edge_group.append(block8_group)
edge_group.append(block4_group)
edge_group.append(block2_group)
edge_group.append(block1_group)

# Draw graph baseline - rectangle - 8th graphic item
base_bitmap = displayio.Bitmap(1, 20, 1)
base_palette = displayio.Palette(1)
base_palette[0] = 0xBBBBBB  # Grey
base_sprite = displayio.TileGrid(base_bitmap, pixel_shader=base_palette, x=14, y=75)
edge_group.append(base_sprite)

# Draw colour values as text - 2 text items
txtGroup = displayio.Group(max_size=2) # There are 2 text items
text_groupP = displayio.Group(max_size=1, scale=1, x=120, y=85)  # %
textP = "    " +str(percent) +" %"
text_areaP = label.Label(terminalio.FONT, text=textP, color=0xFFFFFF)
text_groupP.append(text_areaP)  # Subgroup for text scaling

text_groupT = displayio.Group(max_size=1, scale=2, x=2, y=20) # Title
textT = "Adding Blocks"
text_areaT = label.Label(terminalio.FONT, text=textT, color=0xFF00FF)
text_groupT.append(text_areaT)  # Subgroup for text scaling

txtGroup.append(text_groupP) # The text items are stationary
txtGroup.append(text_groupT)
edge_group.append(txtGroup)
display.show(edge_group)

while True:
    # Read pot
    val = map(pot.value, 224, 65520, 0, 100) # Change range to percentage
    percent = val
    pos = 15              # x-position for next block
    for i in range(7):
        p[i] = 170
        if val & block[i]:
            p[i] = pos
            pos = pos + block[i]
#    print(val,p[0],p[1],p[2],p[3],p[4],p[5],p[6]) # debug

# Update % lable
    pc = str(percent) +" %"
    # Right align pc
    if percent < 100: pc = " " + pc
    if percent < 10: pc = " " + pc
    text_areaP.text = pc      # Update % value on display
    y = 80
    block64_group.x = p[0]    # Update block positions
    block64_group.y = y
    block32_group.x = p[1]
    block32_group.y = y
    block16_group.x = p[2]
    block16_group.y = y
    block8_group.x = p[3]
    block8_group.y = y
    block4_group.x = p[4]
    block4_group.y = y
    block2_group.x = p[5]
    block2_group.y = y
    block1_group.x = p[6]
    block1_group.y = y


I also tried drawing the a single bar graph rectangle last and using 'pop' to remove and replace as necessary. This also works well.

Code: Select all | TOGGLE FULL SIZE
# Controls Neopixels via buttons and displays RGB values on screen
#  Edge Badge  - Steady graph - using the 'POP' methods
# Tony Goodhew 25 August 2020
import time
import board
import terminalio
import displayio
import simpleio
import digitalio
import adafruit_imageload
from gamepadshift import GamePadShift
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
import neopixel

# NeoPixels 0-4 on D8
NUMPIXELS = 5
neopixels = neopixel.NeoPixel(board.D8, NUMPIXELS, brightness=0.1, auto_write=False)
red = 125
green = 125
blue = 125
percent = int(100 * (red + green + blue) / 765)
old_p = 0
def up(v):
    v = v + 5
    if v > 255:
        v = 255
    return v
def down(v):
    v = v - 5
    if v < 0:
        v = 0
    return v

#  Setup for PyBadge buttons
BUTTON_LEFT = const(128)
BUTTON_UP = const(64)
BUTTON_DOWN = const(32)
BUTTON_RIGHT = const(16)
BUTTON_A = const(2)
BUTTON_B = const(1)

pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
                   digitalio.DigitalInOut(board.BUTTON_OUT),
                   digitalio.DigitalInOut(board.BUTTON_LATCH))

current_buttons = pad.get_pressed()
last_read = time.monotonic()

display = board.DISPLAY
red =   125 # Initial Neopixel values
green = 125
blue =  125
display = board.DISPLAY

# Make the display context
splash = displayio.Group(max_size=11)
display.show(splash)
txtGroup = displayio.Group(max_size=10)
color_bitmap = displayio.Bitmap(160, 128, 2) # Screen size
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

# Draw graph baseline - rectangle
base_bitmap = displayio.Bitmap(1, 20, 1)
base_palette = displayio.Palette(1)
base_palette[0] = 0xBBBBBB  # Grey
base_sprite = displayio.TileGrid(base_bitmap, pixel_shader=base_palette, x=14, y=75)
splash.append(base_sprite)

# Draw colour values as text
text_group4 = displayio.Group(max_size=3, scale=1, x=120, y=85)  # %
textp = str(percent) +" %"
text_areap = label.Label(terminalio.FONT, text=textp, color=0xFFFFFF)
text_group4.append(text_areap)  # Subgroup for text scaling

text_group1 = displayio.Group(max_size=3, scale=2, x=20, y=20)  # Red value
textr = str(red)
text_arear = label.Label(terminalio.FONT, text=textr, color=0xFF0000  )
text_group1.append(text_arear)  # Subgroup for text scaling

text_group2 = displayio.Group(max_size=3, scale=2, x=20, y=40)  # Green value
textg = str(green)
text_areag = label.Label(terminalio.FONT, text=textg, color=0x00FF00)
text_group2.append(text_areag)  # Subgroup for text scaling

text_group3 = displayio.Group(max_size=3, scale=2, x=20, y=60)  # Blue value
textb = str(blue)
text_areab = label.Label(terminalio.FONT, text=textb, color=0x0000FF)
text_group3.append(text_areab)  # Subgroup for text scaling

text_group5 = displayio.Group(max_size=3, scale=2, x=100, y=20) # Title - DEMO
textt = "DEMO"
text_areat = label.Label(terminalio.FONT, text=textt, color=0xFF00FF)
text_group5.append(text_areat)  # Subgroup for text scaling

txtGroup.append(text_group1)
txtGroup.append(text_group2)
txtGroup.append(text_group3)
txtGroup.append(text_group4)
txtGroup.append(text_group5)
splash.append(txtGroup)
greybar = Rect(0,0,100,10, fill=0xB0B0B0,outline=0xB0B0B0) # Grey looks better
# Must be last so it can be popped and replaced
splash.append(greybar)
while True:
    #  checks if button has been pressed
    if (last_read + 0.05) < time.monotonic():
        buttons = pad.get_pressed()
        last_read = time.monotonic()
        if buttons & BUTTON_UP:
            red = up(red)
        if buttons & BUTTON_DOWN:
            red = down(red)
        if buttons & BUTTON_LEFT:
            green = down(green)
        if buttons & BUTTON_RIGHT:
            green = up(green)
        if buttons & BUTTON_A:
            blue = up(blue)
        if buttons & BUTTON_B:
            blue = down(blue)
# Update Neopixels
    neopixels[0] = (red, 0, 0)
    neopixels[1] = (0, green, 0)
    neopixels[2] = (0, 0, blue)
    neopixels[3] = (0, 0, 0)
    neopixels[4] = (red, green, blue)
    neopixels.show()
# Update lables
    percent = int(100 * (red + green + blue) / 765)
    text_areab.text = str(blue)
    text_areag.text = str(green)
    text_arear.text = str(red)
    text_areap.text = str(percent) +" %"
# Update graph
    if percent < 1:
        greybar = Rect(15,80,1,10, fill=0x0,outline=0x0) # Background if zero
        splash.pop()
        splash.append(greybar)
    else:
        greybar = Rect(15,80,percent,10, fill=0xB0B0B0,outline=0xB0B0B0)
        splash.pop()
        splash.append(greybar)


I originally expected a rectangle of zero width not to be draw but it appears as 1 pixel wide. Hence the test for percent < 1 and the rectangle drawn in background colour.

There are always so many solutions to a simple problem.

Thanks for your help

Great little board this EDGE. Display is so clear and plenty of IO access on the back.

Tonygo
 
Posts: 79
Joined: Fri Apr 13, 2018 11:09 am

Please be positive and constructive with your questions and comments.