def test(): button.destroy() welcome_message.destroy() free.destroy() free2.destroy() instruction = Text(app, text="Push the red circle as soon as he appears") instruction.text_color = "white" board = Waffle(app, height=5, width=5, visible=True) board.pixel_size = 75 board.set_all("white") app.update() waitingTime = (random.randint(0, 10)) time.sleep(waitingTime) starttime = time.perf_counter() def ende(x, y): if board[x, y].dotty == True: board[x, y].dotty = False board.set_pixel(x, y, "white") end = time.perf_counter() timeused = round((end - starttime) * 1000) / 1000 timedisplay = Text(app, text="Your reaction-time: " + str(timeused) + "s") timedisplay.text_color = "white" timedisplay.font = "Impact" board.update_command(ende) x, y = random.randint(0, 4), random.randint(0, 4) board[x, y].dotty = True board.set_pixel(x, y, "red")
def test_set_get_pixel(): a = App() w = Waffle(a) w.set_pixel(0,0, "red") assert w.get_pixel(0,0) == "red" a.destroy()
def test_reset(): from itertools import chain a = App() w = Waffle(a) w.set_pixel(0, 1, "red") w.reset() pixels = chain.from_iterable(zip(*w.get_all())) count = 0 for pixel in pixels: assert pixel == w.color count += 1 assert count == w.width * w.height a.destroy()
def setup_gui(): """ Create the GUI for controlling the STS-Pi Rover. """ global app, statusWaffle, roverChoice, connectButton, exitButton global forwardButton, stopButton, backwardButton, spdSlider, durationSlider global spinRightButton, spinLeftButton, photoButton, videoButton global buttonWaffle # Declare the GUI application app = App("STS Controller", layout="grid") # Bluetooth connection status waffle. Single cell, blue=connected, red=not. statusWaffle = Waffle(app, 1, 4, 10, 10, grid=[0, 0]) if connected: statusWaffle.set_pixel(0, 0, "blue") statusWaffle.set_pixel(1, 0, "white") statusWaffle.set_pixel(2, 0, "white") statusWaffle.set_pixel(3, 0, "white") # Choice of STS-PI Rover roverChoice = ButtonGroup(app, options=["STS-Pi 2", "STS-Pi 3"], selected="STS-Pi 2", grid=[1, 0]) # Reconnect button for when the Bluetooth connection has been lost or reset connectButton = PushButton(app, connect, text="Connect", grid=[2, 0]) #Create forwards and backwards buttons forwardButton = PushButton(app, forwards, text="Forwards", grid=[1, 1]) #Create spin left and right buttons spinLeftButton = PushButton(app, spinAntiClockwise, text="Spin Left", grid=[0, 2]) #Button to stop the STS-PI in an emergency stopButton = PushButton(app, stop, text="STOP", grid=[1, 2]) spinRightButton = PushButton(app, spinClockwise, text="Spin Right", grid=[2, 2]) backwardButton = PushButton(app, backwards, text="Backwards", grid=[1, 3]) #Create a slider to set the speed. speedTitle = Text(app, "Speed %", grid=[0, 4]) spdSlider = Slider(app, command=changeSpeed, grid=[1, 4, 3, 1]) spdSlider.value = 15 #Create a slider to set the duration of the next movement durationTitle = Text( app, "Duration (secs)", grid=[0, 5], ) durationSlider = Slider(app, command=changeDuration, start=1, end=10, grid=[1, 5, 3, 1]) durationSlider.value = 1 #Buttons to take videos and photos. photoButton = PushButton(app, takePhoto, text="Photo", grid=[0, 6]) videoButton = PushButton(app, takeVideo, text="Video", grid=[2, 6]) #Waffle to display button presses buttonWaffle = Waffle(app, 1, 8, grid=[0, 7, 3, 2]) # Ids for the buttons on the STS-PI rover buttonIds = Text(app, "1 2 3 4 5 6 7 8 ", grid=[0, 9, 3, 1]) # This spacer text increases the gap below the controls and the exit button. spacer = Text(app, "", grid=[0, 10]) # Button to quit the application exitButton = PushButton(app, closeDown, text="Exit", grid=[1, 11])
#main body app = App(title="countdown conundrum", width=300, height=600) text = Text(app, grid=[50, 50]) waffle = Waffle(app, color="blue", align="bottom", command=scramble, height=5, pad=1, width=5, grid=[10, 10]) waffle.bg = "green" waffle.set_pixel(2, 2, "red") #button = PushButton(app, command=scramble, text="Start") combo = Combo(app, options=[ " ", "raspberry", "picademy", "codeclub", "coderdojo", "educator", "debugging", "process", "belfast", "boston", "foundation" ], command=compare, grid=[15, 20]) text2 = Text(app, text="1", grid=[25, 0]) text2.repeat(1000, counter) # Schedule call to counter() every 1000ms my_cat = Picture(app, image="download.jfif", grid=[5, 5]) app.display() # score
def balancetest(): m = 0 r = 255, m, 0 x_values = [] z_values = [] global sb, txt, qth, score sb.visible = False txt.visible = False stillx = 0.068 stillz = 0.064 rockx = 0.296 rockz = 0.228 bigrockx = 0.608 bigrockz = 0.849 panacake = Waffle(qth, height=1, width=10, dim=70) qth.update() for i in range(100): raw = s.get_accelerometer_raw() x_values.append(raw['x']) z_values.append(raw['z']) sleep(0.2) if i // 10 == i / 10: t = i / 10 print(t) panacake.set_pixel(int(t), 0, r) qth.update() m = m + 25 r = 255, m, 0 for i in range(4): panacake.set_all('green') qth.update() sleep(0.3) panacake.set_all('white') qth.update() sleep(0.3) panacake.set_all('green') qth.update() sleep(0.3) panacake.set_all('white') qth.update() sleep(0.3) panacake.set_all('green') qth.update() sleep(0.3) panacake.set_all('white') qth.update() sleep(0.3) panacake.visible = False xvalue = max(x_values) - min(x_values) zvalue = max(z_values) - min(z_values) if xvalue < 0.1 or xvalue < stillx and zvalue < 0.1 or zvalue < stillz: print('standing still : passed') score = score + 1 if xvalue > 0.1 and xvalue < 0.4 and zvalue > 0.1 and zvalue < 0.4: print('rocking slightly : failed') if xvalue > 0.4 and zvalue > 0.4: print('rocking alot : failed') if xvalue > 1 and zvalue > 1: print('u fell over') print('score is ' + str(score)) finalcheck()
# This almost works, amazingly (given that I'm just throwing stuff # at the screen), but the click positions are all wrong. Scaling is off. # ...which isn't a great surprise, since the numbers are all made up. from guizero import App, Waffle from tkinter import Frame app = App() def callback(event): print("Clicked at: ", event.x, event.y) # Set the waffle to have a memory my_waffle = Waffle(App, remember=True) my_waffle.set_pixel(2, 1, "red") # Now your waffle will remember what colour each pixel is! print(my_waffle.get_pixel(2, 1)) # Even the ones aut0-set at the start (which are white by default) print(my_waffle.get_pixel(1, 1)) frame = Frame(my_waffle, width=100, height=100) frame.bind("<Button-1>", callback) frame.pack() app.display()
from guizero import App, Waffle, PushButton def clicked(x, y): waffle.set_pixel(x, y, "red") print("{}.{}".format(x, y)) print(waffle.get_all()) waffle.width = 5 waffle.height = 5 waffle.pixel_size = 50 waffle.dotty = True waffle.pad = 25 waffle.color = "green" app = App() waffle = Waffle(app, command=clicked, height=15, width=15) waffle.bg = (255, 255, 0) waffle.set_all("red") waffle.set_pixel(0, 0, "blue") waffle.set_pixel(8, 1, "#00ff00") waffle.set_pixel(3, 4, "blue") waffle.pixel(1, 2).dotty = True print(waffle[8, 1].color) button = PushButton(app, command=waffle.reset) app.display()
def startup(): HOME = str(Path.home()) HERE = os.path.dirname(os.path.realpath(__file__)) global NOHAT global ONEMU global tstamp tstamp = "" NOHAT = False ONEMU = False global framerate framerate = 1 def no_hat_check(): global NOHAT global ONEMU NOHAT = yesno( "No SenseHAT detected", "No SenseHat detected - Do you want to carry on anyway?") if NOHAT: if "arm" in os.uname().machine: ONEMU = yesno( "Looks like a Pi", "Do you want to try to run in the SenseHat emulator?") if ONEMU: if not os.path.isfile("/usr/bin/sense_emu_gui"): warn( "Sorry", "It doesn't look like the SenseHAT emulator is installed" ) sys.exit() else: warn( "No pi!", "It doesn't look like the you're on a Raspberry Pi. You can still save animations and images." ) else: sys.exit() global col col = (255, 255, 255) global blank_frame blank_frame = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)] global frames frames = {1: blank_frame.copy()} global current_frame_number current_frame_number = 1 framerate = 1 # frames per second global looping looping = False def illum_pixel(x, y): global col #global NOHAT matrix.set_pixel(x, y, col) if not NOHAT: sh.set_pixel(x, y, col) def col_select(x, y): global col if y == 0: col = (255, 0, 0) button_clear.text_color = "black" elif y == 1: col = (0, 255, 0) button_clear.text_color = "black" elif y == 2: col = (0, 0, 255) button_clear.text_color = "white" elif y == 3: col = (255, 255, 0) button_clear.text_color = "black" elif y == 4: col = (80, 80, 80) button_clear.text_color = "white" elif y == 5: col = (255, 255, 255) button_clear.text_color = "black" elif y == 6: col = (255, 0, 255) button_clear.text_color = "black" elif y == 7: col = (255, 150, 0) button_clear.text_color = "black" button_clear.text_color = "black" elif y == 8: col = (0, 0, 0) button_clear.text_color = "white" elif y == 9: col = (66, 220, 240) button_clear.text_color = "black" box.bg = col button_clear.bg = col def hex_to_rgb(hex): return (tuple(int(hex[i:i + 2], 16) for i in (0, 2, 4))) def p_clicked(x, y): if (x <= 7) and (y <= 7): if matrix.get_pixel(x, y) == "black": illum_pixel(x, y) frames[current_frame_number][(y * 8) + x] = col elif hex_to_rgb(str(matrix.get_pixel(x, y).strip('#'))) == col: matrix.set_pixel(x, y, "black") sh.set_pixel(x, y, (0, 0, 0)) frames[current_frame_number][(y * 8) + x] = (0, 0, 0) else: illum_pixel(x, y) frames[current_frame_number][(y * 8) + x] = col def clear_matrix(): global NOHAT if not NOHAT: sh.clear(col) for x in range(8): for y in range(8): matrix.set_pixel(x, y, col) frames[current_frame_number][(y * 8) + x] = col def new_frame(): global current_frame_number global frames global blank_frame if current_frame_number != len(frames): # not last frame for f in range(len(frames), current_frame_number, -1): frames[f + 1] = frames[f].copy() frames[current_frame_number + 1] = blank_frame.copy() current_frame_number += 1 load_frame() def copy_frame(): global current_frame_number global frames if current_frame_number != len(frames): # not last frame for f in range(len(frames), current_frame_number, -1): frames[f + 1] = frames[f].copy() frames[current_frame_number + 1] = frames[current_frame_number].copy() current_frame_number += 1 load_frame() def delete_frame(): global current_frame_number global frames global blank_frame if current_frame_number != 1: if current_frame_number != len(frames): # not last frame for f in range(current_frame_number, len(frames)): frames[f] = frames[f + 1].copy() current_frame_number -= 1 del frames[len(frames)] load_frame() else: warn("Heads up", "Only one frame exits - you can't delete it") def load_frame(): global NOHAT frame_status_text.value = ("Frame " + str(current_frame_number).zfill(3) + " of " + str(len(frames)).zfill(3)) if not NOHAT: sh.set_pixels(frames[current_frame_number]) for x in range(8): for y in range(8): matrix.set_pixel(x, y, frames[current_frame_number][(y * 8) + x]) load_other_frames() def load_other_frames(): prev_matrix.color = "black" next_matrix.color = "black" if len(frames) == 2: if current_frame_number == 1: for x in range(8): for y in range(8): next_matrix.set_pixel( x, y, frames[current_frame_number + 1][(y * 8) + x]) for x in range(8): for y in range(8): prev_matrix.set_pixel(x, y, "grey") else: for x in range(8): for y in range(8): prev_matrix.set_pixel( x, y, frames[current_frame_number - 1][(y * 8) + x]) for x in range(8): for y in range(8): next_matrix.set_pixel(x, y, "grey") if len(frames) >= 3: if current_frame_number == 1: for x in range(8): for y in range(8): next_matrix.set_pixel( x, y, frames[current_frame_number + 1][(y * 8) + x]) for x in range(8): for y in range(8): prev_matrix.set_pixel(x, y, "grey") elif current_frame_number == len(frames): for x in range(8): for y in range(8): prev_matrix.set_pixel( x, y, frames[current_frame_number - 1][(y * 8) + x]) for x in range(8): for y in range(8): next_matrix.set_pixel(x, y, "grey") else: for x in range(8): for y in range(8): next_matrix.set_pixel( x, y, frames[current_frame_number + 1][(y * 8) + x]) for x in range(8): for y in range(8): prev_matrix.set_pixel( x, y, frames[current_frame_number - 1][(y * 8) + x]) def left(): global current_frame_number if current_frame_number > 1: current_frame_number -= 1 load_frame() def right(): global current_frame_number if current_frame_number < len(frames): current_frame_number += 1 load_frame() def right_play(tin): global tstamp global current_frame_number global stopped global looping if tstamp == tin: if (current_frame_number < len(frames)) and not stopped: current_frame_number += 1 load_frame() if (current_frame_number == len(frames)) and not stopped: button_play.enable() button_stop.disable() slider_framerate.enable() button_go_end.enable() button_go_start.enable() button_left.enable() button_right.enable() if checkbox_repeat.value == 1: looping = True current_frame_number = 0 play() else: stopped = True def go_end(): global current_frame_number global frames current_frame_number = len(frames) load_frame() def go_start(): global current_frame_number current_frame_number = 1 load_frame() def play(): global framerate global stopped global current_frame_number global looping global tstamp if len(frames) > 1: button_play.disable() button_stop.enable() button_go_end.disable() button_go_start.disable() button_go_end.disable() button_left.disable() button_right.disable() slider_framerate.disable() stopped = False tstamp = str(time.time()) t = int(1000 / framerate) if looping: for i in range( 1, len(frames) + 1 ): # because we set current_frame_number = 0 when looping we need an extra iteration frame_status_text.after(t * i, right_play, args=[tstamp]) else: for i in range(1, len(frames)): frame_status_text.after(t * i, right_play, args=[tstamp]) def stop(): global stopped stopped = True button_play.enable() button_stop.disable() slider_framerate.enable() button_go_end.enable() button_go_start.enable() button_left.enable() button_right.enable() #for i in range(len(frames)): # frame_status_text.cancel(right_play) def export_as_python(): global framerate filename = filedialog.asksaveasfilename( initialdir=HOME, title="Export file", filetypes=(("python files", "*.py"), ("all files", "*.*"))) if len(filename) != 0: with open(filename, "w") as export_file: export_file.write("# m8tricks output file \n") export_file.write("from sense_hat import SenseHat\n") export_file.write("from time import sleep\n") export_file.write("sh = SenseHat()\n") export_file.write("sh.clear(0,0,0)\n") for e in range(1, len(frames) + 1): export_file.write("sh.set_pixels(" + str(frames[e]) + ")\n") export_file.write("sleep(1/" + str(framerate) + ")\n") def import_python(): global framerate global current_frame_number current_frame_number = 1 filename = filedialog.askopenfilename(initialdir=HOME, title="Select file", filetypes=(("python files", "*.py"), ("all files", "*.*"))) if len(filename) != 0: with open(filename, "r") as import_file: line1 = import_file.readline() if line1 == "# m8tricks output file \n": #print("This looks like an 8x8 Grid Editor file") try: for line in import_file: if line.startswith("sh.set_pixels"): grid = line[14:-2] frames[ current_frame_number] = ast.literal_eval( grid) current_frame_number += 1 current_frame_number -= 1 load_frame() except: error("Import failed", "Sorry, that file could not be imported") else: not_our_file = yesno( "Uh-oh", "This doesn't look like a m8tricks file. Carry on trying to import it?" ) if not_our_file == True: try: for line in import_file: if line.startswith("sh.set_pixels"): grid = line[14:-2] frames[ current_frame_number] = ast.literal_eval( grid) current_frame_number += 1 current_frame_number -= 1 load_frame() except: error("Import failed", "Sorry, that file could not be imported") def set_framerate(): global framerate framerate = slider_framerate.value def sh_rotation(): sh.set_rotation(int(combo_rotation.value)) app = App(title="m8tricks", layout="grid", height=540, width=500) box_top = Box(app, layout="grid", grid=[0, 0, 5, 1]) button_go_start = PushButton(box_top, command=go_start, grid=[0, 0, 2, 1], text="<<", image=HERE + "/images/endl.png") button_left = PushButton(box_top, command=left, grid=[2, 0, 2, 1], text="<", image=HERE + "/images/left.png") button_play = PushButton(box_top, command=play, grid=[4, 0, 2, 1], text="PLAY", image=HERE + "/images/play.png") button_stop = PushButton(box_top, command=stop, grid=[6, 0, 2, 1], text="STOP", enabled=False, image=HERE + "/images/stop.png") button_right = PushButton(box_top, command=right, grid=[8, 0, 2, 1], text=">", image=HERE + "/images/right.png") button_go_end = PushButton(box_top, command=go_end, grid=[10, 0, 2, 1], text=">>", image=HERE + "/images/endr.png") checkbox_repeat = CheckBox(app, text=" Repeat", grid=[6, 0, 1, 1]) box_framerate = Box(app, layout="grid", grid=[7, 0, 2, 1]) box_framerate.set_border(0, "#ff0000") slider_framerate = Slider(box_framerate, command=set_framerate, grid=[0, 0], start=1, end=25) text_slider = Text(box_framerate, text="Framerate (fps)", grid=[0, 1], size=10) matrix = Waffle(app, height=8, width=8, dim=30, command=p_clicked, color="black", grid=[0, 1, 7, 7]) palette = Waffle(app, height=10, width=1, dim=20, command=col_select, grid=[7, 1, 1, 7]) palette.set_pixel(0, 0, "red") palette.set_pixel(0, 1, (0, 255, 0)) palette.set_pixel(0, 2, "blue") palette.set_pixel(0, 3, "yellow") palette.set_pixel(0, 4, (100, 100, 100)) palette.set_pixel(0, 5, "white") palette.set_pixel(0, 6, (255, 0, 255)) palette.set_pixel(0, 7, "orange") palette.set_pixel(0, 8, "black") palette.set_pixel(0, 9, (66, 220, 240)) box = Box(app, width=30, height=30, grid=[2, 10, 2, 1]) box.bg = col text_current_col = Text(app, text="Selected Colour:", grid=[0, 10, 3, 1]) text_rotation = Text(app, text="LED Rotation:", grid=[5, 10, 3, 1]) combo_rotation = Combo(app, options=["0", "90", "180", "270"], grid=[8, 10, 2, 1], command=sh_rotation) button_clear = PushButton(app, command=clear_matrix, grid=[8, 1], text="Clear") button_clear.bg = col frame_status_text = Text(app, text="Frame " + str(current_frame_number).zfill(3) + " of " + str(len(frames)).zfill(3), grid=[0, 11, 3, 1]) button_new_frame = PushButton(app, command=new_frame, grid=[3, 11, 2, 1], text="New", padx=28) button_new_frame = PushButton(app, command=copy_frame, grid=[5, 11, 2, 1], text="Duplicate") button_new_frame = PushButton(app, command=delete_frame, grid=[7, 11, 2, 1], text="Delete", padx=20) prev_matrix = Waffle(app, height=8, width=8, dim=8, color="grey", grid=[0, 13, 3, 3]) next_matrix = Waffle(app, height=8, width=8, dim=8, color="grey", grid=[7, 13, 3, 3]) menubar = MenuBar(app, toplevel=["File"], options=[[["Import file", import_python], ["Export as", export_as_python]]]) if os.path.isfile("/proc/device-tree/hat/product"): file = open("/proc/device-tree/hat/product", "r") hat = file.readline() if hat == "Sense HAT\x00": #print('Sense HAT detected') file.close() else: #print("No SenseHAT detected") no_hat_check() else: #print("No SenseHAT detected") no_hat_check() if not NOHAT: # SenseHat detected to run normally from sense_hat import SenseHat sh = SenseHat() sh.clear(0, 0, 0) else: if ONEMU: # No SenseHat - and we're on a Pi so try the emulator from sense_emu import SenseHat sh = SenseHat() sh.clear(0, 0, 0) NOHAT = False else: #Disable SenseHat functions text_rotation.disable() combo_rotation.disable() app.display()