#displayio
1 messages · Page 1 of 1 (latest)
Forgot the top half
also is there a reason you're not using elif
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.
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
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
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?
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"
...
The picolcd lib is actually https://github.com/foamyguy/Foamyguy_CircuitPython_Waveshare_Pico_LCD_1_14
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
ah!
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
Would there be a good explainer(video and/or tutorial) for displayio?
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)
oop
but overall it should be good
Thanks so much for the help
there's some extensive info on labels: https://learn.adafruit.com/circuitpython-display_text-library
displayio
Was this section of the group page the only spot you noticed with references to max_size? I can remove them. This is the one I found but if there are others I can grab them at the same time.
I didn't go through the whole guide, is it fully up to date to CP7 ?
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.
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.
Uh oh, I think maybe I didn't actually finish making the button functionality
If you are using my Waveshare library and havn't modified it... The code here https://github.com/FoamyGuy/Foamyguy_CircuitPython_Waveshare_Pico_LCD_1_14/blob/44e75e29b6b8cd25932e5035af77f1808a1d85f7/foamyguy_waveshare_pico_lcd_1_14.py#L97-L104 isn't returning anything, it's only printing.
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.
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
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
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']
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.
#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.
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
Would there be much of a performance difference between the two methods for a pi pico?
Or mostly space usage in the storage?
wouldn't take much space no
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
>>>
I think this line:
display = lcd.display()
should not have the parens at the end. Like this instead: