Datasheet
This is a basic polling example - we'll check each GPIO.input in order, and draw a different shape - a directional arrow
or a round circle) depending on whether the button is pressed. If the button is pressed we have the shape filled in. If
the button is not pressed, we draw an outline only
Then we run disp.image(image) and disp.display() to actually push the updated image to the OLED. This is required
to actually make the changes appear!
A small time.sleep() delay just keeps the OLED from getting flickery and 'de-bounces' the button inputs.
More Demos & Examples
You can check out our other examples in the example, just make sure to edit each one with nano animate.py for
example, and find the line that says:
try:
while 1:
if GPIO.input(U_pin): # button is released
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0) #Up
else: # button is pressed:
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=1) #Up filled
if GPIO.input(L_pin): # button is released
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0) #left
else: # button is pressed:
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=1) #left filled
if GPIO.input(R_pin): # button is released
draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0) #right
else: # button is pressed:
draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=1) #right filled
if GPIO.input(D_pin): # button is released
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0) #down
else: # button is pressed:
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=1) #down filled
if GPIO.input(C_pin): # button is released
draw.rectangle((20, 22,40,40), outline=255, fill=0) #center
else: # button is pressed:
draw.rectangle((20, 22,40,40), outline=255, fill=1) #center filled
if GPIO.input(A_pin): # button is released
draw.ellipse((70,40,90,60), outline=255, fill=0) #A button
else: # button is pressed:
draw.ellipse((70,40,90,60), outline=255, fill=1) #A button filled
if GPIO.input(B_pin): # button is released
draw.ellipse((100,20,120,40), outline=255, fill=0) #B button
else: # button is pressed:
draw.ellipse((100,20,120,40), outline=255, fill=1) #B button filled
# Display image.
disp.image(image)
disp.display()
time.sleep(.01)
© Adafruit Industries https://learn.adafruit.com/adafruit-128x64-oled-bonnet-for-raspberry-pi Page 11 of 15