Skip to content

Commit 99d0979

Browse files
committed
cleanup qteye_blink_esp32s3, put config variables at top
1 parent 4ab368b commit 99d0979

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

larger-tricks/eyeballs/qteye_blink_esp32s3.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
import adafruit_imageload
1010
import gc9a01
1111

12-
# wiring for QT Py, should work on any QT Py or XIAO board, but ESP32-S3 is fastest
12+
# config: behaviors
13+
eye_twitch_time = 2 # bigger is less twitchy
14+
eye_twitch_amount = 20 # allowable deviation from center for iris
15+
eye_blink_time = 1.2 # bigger is slower
16+
eye_rotation = 0 # 0 or 180, don't do 90 or 270 because too slow
17+
18+
# config: wiring for QT Py, should work on any QT Py or XIAO board, but ESP32-S3 is fastest
1319
tft0_clk = board.SCK
1420
tft0_mosi = board.MOSI
1521

@@ -22,26 +28,32 @@
2228
dw, dh = 240, 240 # display dimensions
2329

2430
# load our eye and iris bitmaps
25-
eyeball_bitmap, eyeball_pal = adafruit_imageload.load("imgs/eye0_ball2.bmp")
31+
## static so load from disk
32+
eyeball_bitmap = displayio.OnDiskBitmap(open("/imgs/eye0_ball2.bmp", "rb"))
33+
eyeball_pal = eyeball_bitmap.pixel_shader
34+
## moves aorund, so load into RAM
2635
iris_bitmap, iris_pal = adafruit_imageload.load("imgs/eye0_iris0.bmp")
2736
iris_pal.make_transparent(0)
28-
eyelid_bitmap = displayio.OnDiskBitmap(open("/imgs/eyelid_spritesheet2.bmp", "rb"))
29-
eyelid_pal = eyelid_bitmap.pixel_shader
30-
#eyelid_bitmap, eyelid_pal = adafruit_imageload.load("/imgs/eyelid_spritesheet.bmp")
37+
## also moves, so load into RAM (hopefully)
38+
try:
39+
eyelid_bitmap, eyelid_pal = adafruit_imageload.load("/imgs/eyelid_spritesheet2.bmp")
40+
except Exception as e:
41+
print("couldn't load",e)
42+
eyelid_bitmap = displayio.OnDiskBitmap(open("/imgs/eyelid_spritesheet2.bmp", "rb"))
43+
eyelid_pal = eyelid_bitmap.pixel_shader
3144
eyelid_sprite_cnt = eyelid_bitmap.width // dw # should be 16
3245
eyelid_pal.make_transparent(1)
3346

3447
# compute or declare some useful info about the eyes
3548
iris_w, iris_h = iris_bitmap.width, iris_bitmap.height # iris is normally 110x110
3649
iris_cx, iris_cy = dw//2 - iris_w//2, dh//2 - iris_h//2
37-
r = 20 # allowable deviation from center for iris
3850

3951

4052
spi0 = busio.SPI(clock=tft0_clk, MOSI=tft0_mosi)
4153

4254
# class to help us track eye info (not needed for this use exactly, but I find it interesting)
4355
class Eye:
44-
def __init__(self, spi, dc, cs, rst, rot=0, eye_speed=0.25, twitch=2):
56+
def __init__(self, spi, dc, cs, rst, rot=0, eye_speed=0.25):
4557
display_bus = displayio.FourWire(spi, command=dc, chip_select=cs, reset=rst)
4658
display = gc9a01.GC9A01(display_bus, width=dw, height=dh, rotation=rot)
4759
display.auto_refresh = False
@@ -56,29 +68,35 @@ def __init__(self, spi, dc, cs, rst, rot=0, eye_speed=0.25, twitch=2):
5668
main.append(self.lids)
5769
self.x, self.y = iris_cx, iris_cy
5870
self.tx, self.ty = self.x, self.y
59-
self.next_time = time.monotonic()
71+
self.next_time = 0
6072
self.eye_speed = eye_speed
61-
self.twitch = twitch
6273
self.lidpos = 0
6374
self.lidpos_inc = 1
6475
self.lid_next_time = 0
6576

6677
def update(self):
78+
"""
79+
global variables used:
80+
- eye_twitch_amount
81+
- eye_twitch_time
82+
- eye_blink_time
83+
-
84+
"""
6785
# make the eye twitch around
6886
self.x = self.x * (1-self.eye_speed) + self.tx * self.eye_speed # "easing"
6987
self.y = self.y * (1-self.eye_speed) + self.ty * self.eye_speed
7088
self.iris.x = int( self.x )
71-
self.iris.y = int( self.y ) + 10
89+
self.iris.y = int( self.y ) + 10 # have it look down a bit FIXME
7290
if time.monotonic() > self.next_time:
7391
# pick a new "target" for the eye to look at
74-
t = random.uniform(0.25,self.twitch)
92+
t = random.uniform(0.25, eye_twitch_time)
7593
self.next_time = time.monotonic() + t
76-
self.tx = iris_cx + random.uniform(-r,r)
77-
self.ty = iris_cy + random.uniform(-r,r)
78-
79-
if time.monotonic() > self.lid_next_time:
94+
self.tx = iris_cx + random.uniform(-eye_twitch_amount,eye_twitch_amount)
95+
self.ty = iris_cy + random.uniform(-eye_twitch_amount,eye_twitch_amount)
96+
# elif to minimize display changes per update
97+
elif time.monotonic() > self.lid_next_time:
8098
# make the eye blink its eyelids
81-
self.lid_next_time = time.monotonic() + random.uniform(1,3)
99+
self.lid_next_time = time.monotonic() + random.uniform( eye_blink_time*0.5, eye_blink_time*1.5)
82100
self.lidpos = self.lidpos + self.lidpos_inc
83101
self.lids[0] = self.lidpos
84102
if self.lidpos == 0 or self.lidpos == eyelid_sprite_cnt-1:
@@ -88,10 +106,9 @@ def update(self):
88106

89107
# a list of all the eyes, in this case, only one
90108
the_eyes = [
91-
Eye( spi0, tft_L0_dc, tft_L0_cs, tft_L0_rst, rot=0),
109+
Eye( spi0, tft_L0_dc, tft_L0_cs, tft_L0_rst, rot=eye_rotation),
92110
]
93111

94112
while True:
95113
for eye in the_eyes:
96114
eye.update()
97-

0 commit comments

Comments
 (0)