-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (95 loc) · 3.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import time
import ntptime
import urequests
import random
import os
from cosmic import CosmicUnicorn
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN
from wave_player import WavePlayer
from pngdec import PNG
from machine import Pin
graphics = PicoGraphics(display=DISPLAY_COSMIC_UNICORN)
gu = CosmicUnicorn()
#Setup PIR Sensor
#PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
pir = machine.Pin(5)
gu.set_brightness(1)
wp = WavePlayer(gu)
png = PNG(graphics)
#png.open_file("/s4m_ur4i-pirate-characters.png")
png.open_file("/faces.png")
gu.set_volume(0.1)
LAUGH_SOUND = [ 'laugh/%s'%f for f in os.listdir('laugh') ]
print (LAUGH_SOUND)
frame_width = 32
frame_height = 32
black = graphics.create_pen(0,0,0)
anims = [
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,3,2,1,0,0,1,2,3,3,2,1,0],
[0,1,2,3,3,2,1,0,0,1,2,3,3,2,1,0],
[0,1,2,3,3,2,1,0,0,1,2,3,3,2,1,0],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3],
[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3]
]
busy = False
def JumpScare():
global busy
#gu.set_brightness(max(.15,min(1.,gu.light()/500)))
busy = True
#gu.set_volume(0.1)
#play random sound
####wp.play('laugh/LAUGH-1.wav', loop=1)
#wp.play(random.choice(LAUGH_SOUND), loop=1)
#pick random anim
foo = len(anims) - 1
selected_anim_number = random.randint(0, (len(anims) - 1))
selected_anim = anims[selected_anim_number]
frame_count = len(selected_anim)
#print(f"foo: {foo}")
print(f"Selected frame: {selected_anim_number}")
# set initial brightness to 0
brightness = 0
gu.set_brightness(brightness)
gu.update(graphics)
for index in range(frame_count):
# Clear the display before drawing the new frame
graphics.set_pen(black)
graphics.clear()
frame = selected_anim[index]
# Decode and display the current frame
png.decode(0, 0, source=(frame * frame_width, selected_anim_number * frame_height, frame_width, frame_height), scale=(1, 1), rotate=0)
gu.update(graphics)
if index < 2:
for _ in range(10):
brightness += 0.1
gu.set_brightness(brightness)
gu.update(graphics)
time.sleep(0.01)
elif index > frame_count - 3:
for _ in range(10):
brightness -= 0.1
gu.set_brightness(brightness)
gu.update(graphics)
time.sleep(0.01)
else:
time.sleep(0.1) # Adjust speed of anim
# Clear display
graphics.set_pen(black)
graphics.clear()
gu.update(graphics)
time.sleep(5) # Time out so it does not trigger again immediately
busy = False
while True:
#if gu.is_pressed(CosmicUnicorn.Sbauble_A):
if pir.value() == 1:
if not busy:
JumpScare()