Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new example for Unicorn Hat Mini #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions examples/target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env python3

import time
from gpiozero import Button
from signal import pause
from unicornhatmini import UnicornHATMini

print("""Unicorn HAT Mini: target.py

Demonstrates the use of Unicorn HAT Mini's buttons to control c>

Press Ctrl+C to exit!

""")

uh = UnicornHATMini()
x = 0
y = 0

def display():
uh.clear()
for i in range(17):
for j in range(7):
if (i == x) or (j == y):
if (i == x) and (j == y):
uh.set_pixel(i, j, 255, 0, 0)
else:
uh.set_pixel(i, j, 0, 255, 0)
uh.show()

def up():
global y
if y == 0:
y = 6
else:
y = y - 1

def down():
global y
if y == 6:
y = 0
else:
y = y + 1

def left():
global x
if x == 0:
x = 16
else:
x = x - 1

def right():
global x
if x == 16:
x = 0
else:
x = x + 1

def pressed(button):
button_name = button_map[button.pin.number]
if button == button_a:
up()
if button == button_b:
down()
if button == button_x:
left()
if button == button_y:
right()
display()

button_map = {5: "A",
6: "B",
16: "X",
24: "Y"}

button_a = Button(5)
button_b = Button(6)
button_x = Button(16)
button_y = Button(24)

display()

try:
button_a.when_pressed = pressed
button_b.when_pressed = pressed
button_x.when_pressed = pressed
button_y.when_pressed = pressed

pause()

except KeyboardInterrupt:
button_a.close()
button_b.close()
button_x.close()
button_y.close()