Skip to content

Instantly share code, notes, and snippets.

@jminor
Created February 14, 2016 19:23
Show Gist options
  • Save jminor/a9f24b0c73ed7e71f49e to your computer and use it in GitHub Desktop.
Save jminor/a9f24b0c73ed7e71f49e to your computer and use it in GitHub Desktop.
# Import the modulo python library
import modulo
# Create a new Port object using the first Modulo Controller it finds.
port = modulo.Port()
# Create a Display object attached to the port
display = modulo.Display(port)
eyes=[1,1]
mouth=.25
eye_closed = 0.2
eye_open = 0.9
mouth_closed = 0.15
mouth_open = 0.65
rate = 0.75
def lerp(a,b,t):
return a*(1.0-t)+b*t
# Run this loop until the program is terminated
while True :
# Call port.loop() on each iteration to process events
port.loop()
# Clear the display
display.clear()
# Draw text to the display. The color and text depend on which
# button is pressed.
if display.getButton(0) :
eyes[0] = lerp(eyes[0],eye_closed,rate)
else:
eyes[0] = lerp(eyes[0],eye_open,rate)
if display.getButton(1) :
mouth = lerp(mouth,mouth_open,rate)
else:
mouth = lerp(mouth,mouth_closed,rate)
if display.getButton(2) :
eyes[1] = lerp(eyes[1],eye_closed,rate)
else:
eyes[1] = lerp(eyes[1],eye_open,rate)
numeyes = len(eyes)
for e in range(numeyes):
size = eyes[e]
x = (1.0+e)/(numeyes+1.0) * display.width
y = display.height/3
w = display.width/3
h = w * size
display.setFillColor(1,1,1)
display.drawRect(x-w/2,y-h/2,w,h,h/2)
display.setFillColor(0,0,0)
pupilsize=10
if h>pupilsize:
display.drawRect(x-pupilsize/2.0,y-pupilsize/2.0,pupilsize,pupilsize,pupilsize/2)
size = mouth
x = display.width/2
y = 2*display.height/3
w = display.width/2
h = w * size
display.setFillColor(.5,.5,.5)
display.setLineColor(1,1,1)
display.drawRect(x-w/2,y-h/2,w,h,h/2)
# display.setTextColor(1,0,0)
# print >>display, "x=%d" % x
# Call refresh to update the display
display.refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment