-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproto_eye_joystick.py
58 lines (43 loc) · 1.58 KB
/
proto_eye_joystick.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
import numpy as np
import libraries.graphics.unicorn_graphics as ug
import pygame
import time
# Initialize pygame
pygame.init()
pygame.joystick.init()
# Initialize the joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
# Initialize display
d = ug.Display()
# Draw a circle
sclera = ug.Circle(8, 8, 6, rgb=(255, 192, 203))
# Draw a quad for the eyebrow
eyebrow = ug.Polygon([(0, 0), (0, 15), (6, 15), (12, 0)], filled=True, rgb=(255, 0, 0))
# Set the initial display
d.set(sclera.pixels)
d.draw_on_top(eyebrow.pixels)
def map_range(value, low1, high1, low2, high2):
return low2 + (high2 - low2) * (value - low1) / (high1 - low1)
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.JOYAXISMOTION:
tilt = joystick.get_axis(0) # Left and right motion
open_close = joystick.get_axis(1) # Up and down motion
print("tilt: " + str(tilt))
print("open_close: " + str(open_close))
vertical_delta = map_range(open_close, -1, 1, -15, 15)
eyerow_left = map_range(tilt, -1, 1, 0, 15 + vertical_delta)
eyerow_right = map_range(tilt, -1, 1, 15 + vertical_delta, 0)
sclera = ug.Circle(7, 7, 7, rgb=(255, 192, 203))
eyebrow = ug.Polygon([(0, 0), (0, 15), (eyerow_left, 15), (eyerow_right, 0)], filled=True, rgb=(0, 0, 0))
d.clear()
d.set(sclera.pixels)
d.draw_on_top(eyebrow.pixels)
d.update()
# Quit pygame
pygame.quit()