bensimmo
Posts: 2068
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

GuiZero & the SenseHAT

Sat Jan 13, 2018 12:12 pm

I had a play with GuiZero again since it's v0.4 was released last month and 'clickable waffles' now allows me to do something I wanted to do from the start when guizero came and I notice the waffle had been advanced, notably it is clickable.

This is so simple yet soooo satisfying. Not mega coding, not even something that is that taxing to think of

I adapted the example https://github.com/lawsie/guizero/blob/ ... _waffle.py

to an 8x8 grid,
and added the sense_hat (or sense_emu, which makes it even more flexible for teaching)
then just get it to colour the grid when clicked

Code: Select all

from guizero import App, Waffle, PushButton
from sense_hat import SenseHat

sense=SenseHat()
sense.clear(255,255,255)

def change_pixel(x,y):
    if big_waffle.get_pixel(x,y) == "white":
        big_waffle.set_pixel(x, y, "red")
        sense.set_pixel( x,y, [255,0,0] )
    else:
        big_waffle.set_pixel(x, y, "white")
        sense.set_pixel( x,y, [255,255,255] )
        

app = App("Waffle!", height=20*20, width=20*20)

big_waffle = Waffle(app, height=8, width=8, dim=25, pad=2, dotty=True, command=change_pixel)
enable_button = PushButton(app, big_waffle.enable, text="Enable")
disable_button = PushButton(app, big_waffle.disable, text="Disable")

app.display()

While guizero doesn't do colour in RGB, but predetermined word strings and hex, I did suggest RGB be added, I forget where there led too, anyway this is the reason I suggested it. That and we teach the easy scale of 0 of 255 but not the hex confloption in maths and science ;-)

They both work well together.
GuiZero can control the display.
SenseHAT can give GuiZero it's sensor to create a display for.
Sense_emu can also be used, though does lack the bright light 'wow' factor

Extensions (off the top of my head)
-Add a clear(0,0,0) on windows close ;-)
-Change the colour for the click, e.g. RGB sliders
-Create an image in the waffle and see it on screen/hat, then figure out a way of exporting the colours (get_pixels?) in to a cut and paste list for use in SenseHat code.
-Sensor display, since sense-emu is built in to Raspberry Pi Desktop Raspbian/x86
-Text in a text box and display it on the SenseHat.


SenseHAT LED https://pythonhosted.org/sense-hat/api/#led-matrix
GuiZero Waffle https://lawsie.github.io/guizero/waffle/

Note to self/ check code as it's from memory so technically untested, mainly the waffle grid and size.
/Code checked and added the missed off set_pixel, oops

bensimmo
Posts: 2068
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GuiZero & the SenseHAT

Wed Jan 17, 2018 12:32 pm

Added the much needed clear LED on close, save the eyes
Added a RGBtoHEX function thanks to https://github.com/lawsie/guizero/issues/31
and the Sliders to go with it RGB (top to bottom, just add labels), maybe even a single waffle to display the colour
and a reset display.
- possible changes alter if big_waffle.get_pixel(x,y) == "white": to clear on same colour not just any colour

Code: Select all

from guizero import App, Waffle, PushButton, Slider
from sense_hat import SenseHat

def RGBtoHEX():
    return "#%02x%02x%02x" % (Red_slider.value, Green_slider.value, Blue_slider.value)

def change_pixel(x,y):    
    if big_waffle.get_pixel(x,y) == "white":
        big_waffle.set_pixel(x, y, RGBtoHEX())
        sense.set_pixel(x,y, [Red_slider.value, Green_slider.value, Blue_slider.value])
    else:
        big_waffle.set_pixel(x, y, "white")
        sense.set_pixel(x,y, [255,255,255])

def reset_display():
    sense.clear(255,255,255)
    big_waffle.set_all("white")

def close_app():
    sense.clear()
    app.destroy()

#--Start of code, setup SenseHAT, set to all white display
sense=SenseHat()
sense.clear(255,255,255)

#---
app = App("SenseHat Waffle", height=20*20, width=20*20)

big_waffle = Waffle(app, height=8, width=8, dim=20, pad=2, dotty=True, color="white", command=change_pixel)
reset_button = PushButton(app, reset_display, text="Reset display")

Red_slider = Slider(app, end=255)
Green_slider = Slider(app, end=255)
Blue_slider = Slider(app, end=255)
#---

app.on_close(close_app)    #Close app and clear SenseHat display to blank. 

app.display()

Next task figure out that sensor display I can't get to work properly.

scotty101
Posts: 2456
Joined: Fri Jun 08, 2012 6:03 pm

Re: GuiZero & the SenseHAT

Wed Jan 17, 2018 1:13 pm

Did you see my sense_draw application?
viewtopic.php?f=32&t=202010
It has an example of converting from Hex to RGB and allows you to directly draw sprites on to the sense hat.

BTW I added the clickable waffle functionality to guizero so I'll take look to see if I can change it to accept RGB (and hex) and raise a pull request.
EDIT: Spotted your issue on guizero github. I've responded
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

bensimmo
Posts: 2068
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GuiZero & the SenseHAT

Wed Jan 17, 2018 3:35 pm

Didn't see that.
Whatever the RGB->HEX HEX->RGB solution is, it needs to be guizero wide, not just in a waffle, but i'll leave that to the github chat, keep this topic clear. The quick function I used works well.

My solution for exporting the values would be a simple, since I've based it around letting the kids play and is designed to use the sense_hat (or sense_emu which all Raspberry Desktops should have) in Teaching, CodeClubs, Jam's etc.

So I would create a textbox (though it seems we cannot increase the height like a true textbox :(
then just either with a button or at the end of the change_pixel code (for constant update on change)
add
textbox.value=sense.get_pixels()
(or just as you print(sense.get_pixels()) though that kind of defeats the point of the GUI ;-) :lol: )

no extra code needed and returns the uniquely odd numbers the Sensehat converts too.
For example my white (set as 255,255,255) is stored and returned as (248,252,248). I know why they are not what they are set as, but not why R and B is different to G.
Nothing to worry about, but might be interesting explaining it to older kids.

scotty101
Posts: 2456
Joined: Fri Jun 08, 2012 6:03 pm

Re: GuiZero & the SenseHAT

Thu Jan 18, 2018 1:19 pm

bensimmo wrote:
Wed Jan 17, 2018 3:35 pm
So I would create a textbox (though it seems we cannot increase the height like a true textbox :(
Its a missing feature from guizero. There are two text entry widgets in Tkinter, Entry and Text. Entry is single line, Text is Multiline.
GuiZero only implements Entry limiting us to single line only.

You could save the array of pixel colours to a file rather than writing them to a widget.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 2456
Joined: Fri Jun 08, 2012 6:03 pm

Re: GuiZero & the SenseHAT

Thu Jan 18, 2018 1:56 pm

If you are able to modify your version of Guizero, I've created a branch that supports a textbox with multiple lines.

Example of usage shown below.

Code: Select all

from guizero import App, TextBox

def new_text():
    textbox.set("Hello Setting\nText")
    print(textbox.get())

app = App()
textbox = TextBox(app,multiline=True,height=5,text="Hello\nWorld")
textbox.clear()
textbox.value = "Hello \n World2"
textbox.append("New Line")
app.after(1000,new_text)

app.display()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

bensimmo
Posts: 2068
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GuiZero & the SenseHAT

Thu Jan 18, 2018 2:31 pm

I can do what I like to mine, these are just T&L (section we are in) ideas and resources for anyone that happens to be looking.

scotty101
Posts: 2456
Joined: Fri Jun 08, 2012 6:03 pm

Re: GuiZero & the SenseHAT

Thu Jan 18, 2018 3:42 pm

bensimmo wrote:
Thu Jan 18, 2018 2:31 pm
I can do what I like to mine, these are just T&L (section we are in) ideas and resources for anyone that happens to be looking.
Yep and I'm trying to help :D
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Teaching and learning resources”

Who is online

Users browsing this forum: No registered users and 4 guests