#displayio

1 messages · Page 1 of 1 (latest)

rose dust
#

where are your imports

scenic narwhal
#

Forgot the top half

rose dust
#

also is there a reason you're not using elif

scenic narwhal
#
from displayio import Group, TileGrid, Bitmap, Palette
from adafruit_display_text import bitmap_label as label
import terminalio
import colors
from time import sleep

BORDER = 20
FONTSCALE = 5
BACKGROUND_COLOR = colors.black
FOREGROUND_COLOR = colors.black
TEXT_COLOR = 0xFFFFFF

lcd = WavesharePicoLCD114()
display = lcd.display```
#

I was basically using Foamyguy's pacman game as reference.

rose dust
#

so couple of things: a) every one of those sleep(1) instances can be removed; just put one of them at the end of the while True: block

#

b) a dict would probably be useful instead of all those ifs

plucky storm
#

also, sleep(1) is a long amount of time during which you are not listening to button presses

#

now, maybe the buttons are listened to in the background since it looks like it uses the keypad module, still you don't need your code to wait that long

#

oh and now that I read the rest of the code, it's really not how displayio works

#

classic mistake, what you should do is setup the UI once, and the only modify the contents

rose dust
#

w.r.t. b) you can just do something like keymap = { lcd.A = "A", etc.} and then centered_text(keymap[event.key_number]) I think?

plucky storm
#

that how you do it: setup once, then change

main_group = Group()

display.show(main_group)

# Draw background
color_bitmap = Bitmap(display.width, display.height, 1)
color_palette = Palette(1)
color_palette[0] = BACKGROUND_COLOR

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

# Draw text/label
txt = text
text_area = label.Label(terminalio.FONT, text="", color=TEXT_COLOR)
text_width = text_area.bounding_box[2] * font_size
text_group = Group(
    scale=font_size,
    x=display.width // 2 - text_width // 2,
    y=display.height // 2,
)
text_group.append(text_area)
main_group.append(text_group)
 
while True:
    event = lcd.key_events()
    if event:
        if event.pressed:
            #print("{} pressed".format(lcd.KEY_DICT[event.key_number]))
            if event.key_number == lcd.A:
                text_area.text = "A"
    ...
scenic narwhal
plucky storm
#

if not, every call to centered_text is just creating more UI elements stepping over each other, and you'll end up running out of memory

scenic narwhal
#

ah!

plucky storm
#

although since you replace main_group with a new one every time, the old one would be garbage collected, but it's still not the right way to use displayio

scenic narwhal
#

Would there be a good explainer(video and/or tutorial) for displayio?

rose dust
#

seems like a good place to start

plucky storm
#

there are many guides that use it that can serve as examples

#

yeah that guide might need some minor updates, I just saw a reference to max_size (which is no longer used)

rose dust
#

oop

plucky storm
#

but overall it should be good

scenic narwhal
#

Thanks so much for the help

plucky storm
rose dust
#

displayio

dawn dome
plucky storm
dawn dome
#

I doubt it based on the max_size being in there still. I know that I've not gone over it since then to update anything.

I took a quick look through this morning to find the max_size references and that was the one that jumped out. I'll plan on going over it more thoroughly soon to update anything else that needs it. I did change that max_size section when I peeked this morning though.

scenic narwhal
# plucky storm that how you do it: setup once, then change ```py main_group = Group() display....
text = "Halo"
text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR)
text_width = text_area.bounding_box[2] * FONTSCALE
text_group = Group(
    scale=FONTSCALE,
    x=display.width // 2 - text_width // 2,
    y=display.height // 2,
)
text_group.append(text_area)
main_group.append(text_group)

while True:
    event = lcd.key_events()
    if event:
        if event.pressed:
            # print("{} pressed".format(lcd.KEY_DICT[event.key_number]))
            if event.key_number == lcd.A:
                text_area.text = "A"
            elif event.key_number == lcd.B:
                text_area.text = "B"
            elif event.key_number == lcd.UP:
                text_area.text = "UP"
            elif event.key_number == lcd.DOWN:
                text_area.text = "DOWN"
            elif event.key_number == lcd.LEFT:
                text_area.text = "LEFT"
            elif event.key_number == lcd.RIGHT:
                text_area.text = "RIGHT"
            elif event.key_number == lcd.ENTER:
                text_area.text = "ENTER"
                
        if event.released:
            print("Released")
            pass               
    pass``` I see the presses in the console but the screen does not update.
dawn dome
#

I've updated the repo (and the chart chompers repo) with a newer version I found on my pico. This one is returning the key event to you to process in your own code.

scenic narwhal
#
from picolcd import WavesharePicoLCD114
from displayio import Group, TileGrid, Bitmap, Palette
from adafruit_display_text import bitmap_label as label
import terminalio
import colors
from code import text, text_area, text_width, text_group
from time import sleep


#class Menu:
#
#    def __init__(self):
#        pass
#
#    def action_a(self):
#        text_area.text = "TEST"

def action_a():
    print("TEst2?")
#

@plucky storm Here

plucky storm
#

hmmm that's weird, it's basic python, check that you are not overwriting menu somewhere in the code, check that there is no menu file or directory in lib, try changing the name of the file to something longer and unique

scenic narwhal
#

%Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "menu.py", line 6, in <module>
File "code.py", line 48, in <module>
AttributeError: 'module' object has no attribute 'action_a'

#

Don't know if that changes anything but if something jumps out at you

#

Changed the file to m_menu.py >>> dir(m_menu) ['__class__', '__name__', '__file__', 'Bitmap', 'Group', 'Palette', 'TileGrid', 'label', 'terminalio', 'WavesharePicoLCD114', 'colors']

dawn dome
#

Does code.py still have code that is importing from menu?

#

I don't think you can import menu inside of code, and then also import things from code inside of menu. This isn't the exact error I would have expected, but I think it's "circular imports" or kind of like "chicken and egg" problem. One imports the other, and the other imports the first so neither can ever really complete.

#

I can't tell the exact type of menu or UI that you're aiming for, but generally speaking I would recomend try to keep all of your UI stuff in one file if possible. So anything that needs to access a Label for instance should all be in the same file if possible. Or if it must be in different files I think it may need to be passed as an argument rather than imported.

scenic narwhal
#
#from picolcd import WavesharePicoLCD114
#from displayio import Group, TileGrid, Bitmap, Palette
#from adafruit_display_text import bitmap_label as label
#import terminalio
#import colors
#from code import text, text_area, text_width, text_group
#from time import sleep


#class Menu:
#
#    def __init__(self):
#        pass
#
#    def action_a(self):
#        text_area.text = "TEST"

def action_a():
    print("yup")
``` Works but I cant change the text_area from menu.
plucky storm
#

oh yeah I missed the imports from code, reminds me of the issue we had on your stream

#

so, what you could do is make it use that Menu class, and parametrize with text_area (you pass it in init and keep it as self.text_area for example) (and welcome to the world of dependency injection)

#

or as foamyguy says, setup the menu UI in the menu file, and add it to the main group from code.py, via a setup function or an object

scenic narwhal
#

Would there be much of a performance difference between the two methods for a pi pico?

#

Or mostly space usage in the storage?

plucky storm
#

wouldn't take much space no

scenic narwhal
#
from picolcd import WavesharePicoLCD114
from displayio import Group, TileGrid, Bitmap, Palette
from adafruit_display_text import bitmap_label as label
import terminalio


class Menu:
    
    def __init__(
            self,
            fontscale,
            background_color,
            foreground_color,
            text_color,
            # color_bitmap,
            # color_palette,
            text_area,
            display
    ):

        self.fontscale = fontscale
        self.background_color = background_color
        self.foreground_color = foreground_color
        self.text_color = text_color
       #  self.color_bitmap = color_bitmap
       #  self.color_palette = color_palette
        self.text_area = text_area
        self.display = display
    
    # Setup Screen object
    def screen_init():
        lcd = WavesharePicoLCD114()
        display = lcd.display()
        return self.display
``` ```
>>> import menu
>>> menu = menu.Menu
>>> menu.screen_init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/menu.py", line 33, in screen_init
TypeError: 'ST7789' object is not callable
>>> 
dawn dome
#
display = lcd.display()

should not have the parens at the end. Like this instead: