Skip to content

Commit ef8de2d

Browse files
Add Matrix_Portal_Eyes project
1 parent 7a3caa5 commit ef8de2d

16 files changed

+232
-0
lines changed

Matrix_Portal_Eyes/code.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
"""
2+
RASTER EYES for Adafruit Matrix Portal: animated spooky eyes.
3+
"""
4+
5+
# pylint: disable=import-error
6+
import math
7+
import random
8+
import time
9+
#import board
10+
import displayio
11+
import adafruit_imageload
12+
from adafruit_matrixportal.matrix import Matrix
13+
14+
# TO LOAD DIFFERENT EYE DESIGNS: change the middle word here (between
15+
# 'eyes.' and '.data') to one of the folder names inside the 'eyes' folder:
16+
from eyes.werewolf.data import EYE_DATA
17+
#from eyes.cyclops.data import EYE_DATA
18+
#from eyes.kobold.data import EYE_DATA
19+
20+
# UTILITY FUNCTIONS AND CLASSES --------------------------------------------
21+
22+
# pylint: disable=too-few-public-methods
23+
class Sprite(displayio.TileGrid):
24+
"""Single-tile-with-bitmap TileGrid subclass, adds a height element
25+
because TileGrid doesn't appear to have a way to poll that later,
26+
object still functions in a displayio.Group.
27+
"""
28+
def __init__(self, filename, transparent=None):
29+
"""Create Sprite object from color-paletted BMP file, optionally
30+
set one color to transparent (pass as RGB tuple or list to locate
31+
nearest color, or integer to use a known specific color index).
32+
"""
33+
bitmap, palette = adafruit_imageload.load(
34+
filename, bitmap=displayio.Bitmap, palette=displayio.Palette)
35+
if isinstance(transparent, (tuple, list)): # Find closest RGB match
36+
closest_distance = 0x1000000 # Force first match
37+
for color_index, color in enumerate(palette): # Compare each...
38+
delta = (transparent[0] - ((color >> 16) & 0xFF),
39+
transparent[1] - ((color >> 8) & 0xFF),
40+
transparent[2] - (color & 0xFF))
41+
rgb_distance = (delta[0] * delta[0] +
42+
delta[1] * delta[1] +
43+
delta[2] * delta[2]) # Actually dist^2
44+
if rgb_distance < closest_distance: # but adequate for
45+
closest_distance = rgb_distance # compare purposes,
46+
closest_index = color_index # no sqrt needed
47+
palette.make_transparent(closest_index)
48+
elif isinstance(transparent, int):
49+
palette.make_transparent(transparent)
50+
super(Sprite, self).__init__(bitmap, pixel_shader=palette)
51+
self.height = bitmap.height
52+
53+
54+
# ONE-TIME INITIALIZATION --------------------------------------------------
55+
56+
MATRIX = Matrix(bit_depth=6)
57+
DISPLAY = MATRIX.display
58+
59+
# Order in which sprites are added determines the 'stacking order' and
60+
# visual priority. Lower lid is added before the upper lid so that if they
61+
# overlap, the upper lid is 'on top' (e.g. if it has eyelashes or such).
62+
SPRITES = displayio.Group()
63+
SPRITES.append(Sprite(EYE_DATA['eye_image'])) # Base image is opaque
64+
SPRITES.append(Sprite(EYE_DATA['lower_lid_image'], EYE_DATA['transparent']))
65+
SPRITES.append(Sprite(EYE_DATA['upper_lid_image'], EYE_DATA['transparent']))
66+
SPRITES.append(Sprite(EYE_DATA['stencil_image'], EYE_DATA['transparent']))
67+
DISPLAY.show(SPRITES)
68+
69+
EYE_CENTER = ((EYE_DATA['eye_move_min'][0] + # Pixel coords of eye
70+
EYE_DATA['eye_move_max'][0]) / 2, # image when centered
71+
(EYE_DATA['eye_move_min'][1] + # ('neutral' position)
72+
EYE_DATA['eye_move_max'][1]) / 2)
73+
EYE_RANGE = (abs(EYE_DATA['eye_move_max'][0] - # Max eye image motion
74+
EYE_DATA['eye_move_min'][0]) / 2, # delta from center
75+
abs(EYE_DATA['eye_move_max'][1] -
76+
EYE_DATA['eye_move_min'][1]) / 2)
77+
UPPER_LID_MIN = (min(EYE_DATA['upper_lid_open'][0], # Motion bounds of
78+
EYE_DATA['upper_lid_closed'][0]), # upper and lower
79+
min(EYE_DATA['upper_lid_open'][1], # eyelids
80+
EYE_DATA['upper_lid_closed'][1]))
81+
UPPER_LID_MAX = (max(EYE_DATA['upper_lid_open'][0],
82+
EYE_DATA['upper_lid_closed'][0]),
83+
max(EYE_DATA['upper_lid_open'][1],
84+
EYE_DATA['upper_lid_closed'][1]))
85+
LOWER_LID_MIN = (min(EYE_DATA['lower_lid_open'][0],
86+
EYE_DATA['lower_lid_closed'][0]),
87+
min(EYE_DATA['lower_lid_open'][1],
88+
EYE_DATA['lower_lid_closed'][1]))
89+
LOWER_LID_MAX = (max(EYE_DATA['lower_lid_open'][0],
90+
EYE_DATA['lower_lid_closed'][0]),
91+
max(EYE_DATA['lower_lid_open'][1],
92+
EYE_DATA['lower_lid_closed'][1]))
93+
EYE_PREV = EYE_CENTER
94+
EYE_NEXT = EYE_CENTER
95+
MOVE_STATE = False # Initially stationary
96+
MOVE_EVENT_DURATION = random.uniform(0.1, 3) # Time to first move
97+
BLINK_STATE = 2 # Start eyes closed
98+
BLINK_EVENT_DURATION = random.uniform(0.25, 0.5) # Time for eyes to open
99+
TIME_OF_LAST_MOVE_EVENT = TIME_OF_LAST_BLINK_EVENT = time.monotonic()
100+
101+
102+
# MAIN LOOP ----------------------------------------------------------------
103+
104+
while True:
105+
NOW = time.monotonic()
106+
107+
# Eye movement ---------------------------------------------------------
108+
109+
if NOW - TIME_OF_LAST_MOVE_EVENT > MOVE_EVENT_DURATION:
110+
TIME_OF_LAST_MOVE_EVENT = NOW # Start new move or pause
111+
MOVE_STATE = not MOVE_STATE # Toggle between moving & stationary
112+
if MOVE_STATE: # Starting a new move?
113+
MOVE_EVENT_DURATION = random.uniform(0.08, 0.17) # Move time
114+
ANGLE = random.uniform(0, math.pi * 2)
115+
EYE_NEXT = (math.cos(ANGLE) * EYE_RANGE[0], # (0,0) in center,
116+
math.sin(ANGLE) * EYE_RANGE[1]) # NOT pixel coords
117+
else: # Starting a new pause
118+
MOVE_EVENT_DURATION = random.uniform(0.04, 3) # Hold time
119+
EYE_PREV = EYE_NEXT
120+
121+
# Fraction of move elapsed (0.0 to 1.0), then ease in/out 3*e^2-2*e^3
122+
RATIO = (NOW - TIME_OF_LAST_MOVE_EVENT) / MOVE_EVENT_DURATION
123+
RATIO = 3 * RATIO * RATIO - 2 * RATIO * RATIO * RATIO
124+
EYE_POS = (EYE_PREV[0] + RATIO * (EYE_NEXT[0] - EYE_PREV[0]),
125+
EYE_PREV[1] + RATIO * (EYE_NEXT[1] - EYE_PREV[1]))
126+
127+
# Blinking -------------------------------------------------------------
128+
129+
if NOW - TIME_OF_LAST_BLINK_EVENT > BLINK_EVENT_DURATION:
130+
TIME_OF_LAST_BLINK_EVENT = NOW # Start change in blink
131+
BLINK_STATE += 1 # Cycle paused/closing/opening
132+
if BLINK_STATE == 1: # Starting a new blink (closing)
133+
BLINK_EVENT_DURATION = random.uniform(0.03, 0.07)
134+
elif BLINK_STATE == 2: # Starting de-blink (opening)
135+
BLINK_EVENT_DURATION *= 2
136+
else: # Blink ended,
137+
BLINK_STATE = 0 # paused
138+
BLINK_EVENT_DURATION = random.uniform(BLINK_EVENT_DURATION * 3, 4)
139+
140+
if BLINK_STATE: # Currently in a blink?
141+
# Fraction of closing or opening elapsed (0.0 to 1.0)
142+
RATIO = (NOW - TIME_OF_LAST_BLINK_EVENT) / BLINK_EVENT_DURATION
143+
if BLINK_STATE == 2: # Opening
144+
RATIO = 1.0 - RATIO # Flip ratio so eye opens instead of closes
145+
else: # Not blinking
146+
RATIO = 0
147+
148+
# Eyelid tracking ------------------------------------------------------
149+
150+
# Initial estimate of 'tracked' eyelid positions
151+
UPPER_LID_POS = (EYE_DATA['upper_lid_center'][0] + EYE_POS[0],
152+
EYE_DATA['upper_lid_center'][1] + EYE_POS[1])
153+
LOWER_LID_POS = (EYE_DATA['lower_lid_center'][0] + EYE_POS[0],
154+
EYE_DATA['lower_lid_center'][1] + EYE_POS[1])
155+
# Then constrain these to the upper/lower lid motion bounds
156+
UPPER_LID_POS = (min(max(UPPER_LID_POS[0],
157+
UPPER_LID_MIN[0]), UPPER_LID_MAX[0]),
158+
min(max(UPPER_LID_POS[1],
159+
UPPER_LID_MIN[1]), UPPER_LID_MAX[1]))
160+
LOWER_LID_POS = (min(max(LOWER_LID_POS[0],
161+
LOWER_LID_MIN[0]), LOWER_LID_MAX[0]),
162+
min(max(LOWER_LID_POS[1],
163+
LOWER_LID_MIN[1]), LOWER_LID_MAX[1]))
164+
# Then interpolate between bounded tracked position to closed position
165+
UPPER_LID_POS = (UPPER_LID_POS[0] + RATIO *
166+
(EYE_DATA['upper_lid_closed'][0] - UPPER_LID_POS[0]),
167+
UPPER_LID_POS[1] + RATIO *
168+
(EYE_DATA['upper_lid_closed'][1] - UPPER_LID_POS[1]))
169+
LOWER_LID_POS = (LOWER_LID_POS[0] + RATIO *
170+
(EYE_DATA['lower_lid_closed'][0] - LOWER_LID_POS[0]),
171+
LOWER_LID_POS[1] + RATIO *
172+
(EYE_DATA['lower_lid_closed'][1] - LOWER_LID_POS[1]))
173+
174+
# Move eye sprites -----------------------------------------------------
175+
176+
SPRITES[0].x, SPRITES[0].y = (int(EYE_CENTER[0] + EYE_POS[0] + 0.5),
177+
int(EYE_CENTER[1] + EYE_POS[1] + 0.5))
178+
SPRITES[2].x, SPRITES[2].y = (int(UPPER_LID_POS[0] + 0.5),
179+
int(UPPER_LID_POS[1] + 0.5))
180+
SPRITES[1].x, SPRITES[1].y = (int(LOWER_LID_POS[0] + 0.5),
181+
int(LOWER_LID_POS[1] + 0.5))
3.79 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
""" Configuration data for the cyclops eye """
2+
EYE_PATH = __file__[:__file__.rfind('/') + 1]
3+
EYE_DATA = {
4+
'eye_image' : EYE_PATH + 'cyclops-eye.bmp',
5+
'upper_lid_image' : EYE_PATH + 'cyclops-upper-lid.bmp',
6+
'lower_lid_image' : EYE_PATH + 'cyclops-lower-lid.bmp',
7+
'stencil_image' : EYE_PATH + 'cyclops-stencil.bmp',
8+
'transparent' : (255, 0, 0), # Transparent color in above images
9+
'eye_move_min' : (-4, -15), # eye_image (left, top) move limit
10+
'eye_move_max' : (14, -2), # eye_image (right, bottom) move limit
11+
'upper_lid_open' : (15, -23), # upper_lid_image pos when open
12+
'upper_lid_center' : (15, -18), # " when eye centered
13+
'upper_lid_closed' : (15, 0), # " when closed
14+
'lower_lid_open' : (15, 24), # lower_lid_image pos when open
15+
'lower_lid_center' : (15, 23), # " when eye centered
16+
'lower_lid_closed' : (15, 17), # " when closed
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
""" Configuration data for the kobold eyes """
2+
EYE_PATH = __file__[:__file__.rfind('/') + 1]
3+
EYE_DATA = {
4+
'eye_image' : EYE_PATH + 'kobold-eyes.bmp',
5+
'upper_lid_image' : EYE_PATH + 'kobold-upper-lids.bmp',
6+
'lower_lid_image' : EYE_PATH + 'kobold-lower-lids.bmp',
7+
'stencil_image' : EYE_PATH + 'kobold-stencil.bmp',
8+
'transparent' : (255, 0, 0), # Transparent color in above images
9+
'eye_move_min' : (-10, -9), # eye_image (left, top) move limit
10+
'eye_move_max' : (6, 6), # eye_image (right, bottom) move limit
11+
'upper_lid_open' : (6, -7), # upper_lid_image pos when open
12+
'upper_lid_center' : (6, -4), # " when eye centered
13+
'upper_lid_closed' : (6, 6), # " when closed
14+
'lower_lid_open' : (6, 25), # lower_lid_image pos when open
15+
'lower_lid_center' : (6, 23), # " when eye centered
16+
'lower_lid_closed' : (6, 15), # " when closed
17+
}
3.44 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)