Skip to content

Commit 2c3b18d

Browse files
authored
Merge pull request #39 from pimoroni/examples/buzzer
Examples: adding buzzer example
2 parents c41278c + 7f66b58 commit 2c3b18d

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

examples/buzzer.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from presto import Presto, Buzzer
2+
from touch import Button
3+
from picovector import ANTIALIAS_FAST, PicoVector, Polygon, Transform
4+
5+
presto = Presto()
6+
display = presto.display
7+
WIDTH, HEIGHT = display.get_bounds()
8+
9+
# CONSTANTS
10+
CX = WIDTH // 2
11+
CY = HEIGHT // 2
12+
13+
BUTTON_WIDTH = 110
14+
BUTTON_HEIGHT = 110
15+
16+
# Pico Vector
17+
vector = PicoVector(display)
18+
vector.set_antialiasing(ANTIALIAS_FAST)
19+
t = Transform()
20+
21+
vector.set_font("Roboto-Medium.af", 54)
22+
vector.set_font_letter_spacing(100)
23+
vector.set_font_word_spacing(100)
24+
vector.set_transform(t)
25+
26+
# Couple of colours for use later
27+
WHITE = display.create_pen(255, 255, 255)
28+
RED = display.create_pen(230, 60, 45)
29+
BLACK = display.create_pen(0, 0, 0)
30+
31+
# We'll need this for the touch element of the screen
32+
touch = presto.touch
33+
34+
# Define our button vectors and touch button
35+
button = Button(CX - (BUTTON_WIDTH // 2), CY - (BUTTON_HEIGHT // 2), BUTTON_WIDTH, BUTTON_HEIGHT)
36+
button_vector = Polygon()
37+
button_vector.circle(CX, CY, 50)
38+
button_outline = Polygon()
39+
button_outline.circle(CX, CY, 54, 5)
40+
41+
# Calculate our text positions now rather than in the main loop
42+
vector.set_font_size(28)
43+
x, y, w, h = vector.measure_text("TOUCH", x=0, y=0, angle=None)
44+
text_x = int(CX - (w // 2))
45+
text_y = int(CY + (h // 2))
46+
text_x_offset = text_x + 2
47+
text_y_offset = text_y + 2
48+
49+
# Setup the buzzer. The Presto piezo is on pin 43.
50+
buzzer = Buzzer(43)
51+
52+
while True:
53+
54+
# Check for touch changes
55+
touch.poll()
56+
57+
# Clear the screen and set the background colour
58+
display.set_pen(WHITE)
59+
display.clear()
60+
61+
# Draw the touch button outline and inner section
62+
display.set_pen(BLACK)
63+
vector.draw(button_outline)
64+
display.set_pen(RED)
65+
vector.draw(button_vector)
66+
67+
# Draw vector text with drop shadow
68+
display.set_pen(BLACK)
69+
vector.text("TOUCH", text_x_offset, text_y_offset)
70+
display.set_pen(WHITE)
71+
vector.text("TOUCH", text_x, text_y)
72+
73+
# If we're pressing the onscreen button, play a tone!
74+
# otherwise play nothing :)
75+
if button.is_pressed():
76+
buzzer.set_tone(150)
77+
else:
78+
buzzer.set_tone(-1)
79+
80+
# Finally, we update the screen so we can see our changes!
81+
presto.update()

0 commit comments

Comments
 (0)