|
| 1 | +# Copyright (c) 2017 Adafruit Industries |
| 2 | +# Author: James DeVito |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +# THE SOFTWARE. |
| 21 | + |
| 22 | +import board |
| 23 | +import busio |
| 24 | +from digitalio import DigitalInOut, Direction, Pull |
| 25 | +from PIL import Image, ImageDraw |
| 26 | +import adafruit_ssd1306 |
| 27 | + |
| 28 | +# Create the I2C interface. |
| 29 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 30 | +# Create the SSD1306 OLED class. |
| 31 | +disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +# Input pins: |
| 36 | +button_A = DigitalInOut(board.D5) |
| 37 | +button_A.direction = Direction.INPUT |
| 38 | +button_A.pull = Pull.UP |
| 39 | + |
| 40 | +button_B = DigitalInOut(board.D6) |
| 41 | +button_B.direction = Direction.INPUT |
| 42 | +button_B.pull = Pull.UP |
| 43 | + |
| 44 | +button_L = DigitalInOut(board.D27) |
| 45 | +button_L.direction = Direction.INPUT |
| 46 | +button_L.pull = Pull.UP |
| 47 | + |
| 48 | +button_R = DigitalInOut(board.D23) |
| 49 | +button_R.direction = Direction.INPUT |
| 50 | +button_R.pull = Pull.UP |
| 51 | + |
| 52 | +button_U = DigitalInOut(board.D17) |
| 53 | +button_U.direction = Direction.INPUT |
| 54 | +button_U.pull = Pull.UP |
| 55 | + |
| 56 | +button_D = DigitalInOut(board.D22) |
| 57 | +button_D.direction = Direction.INPUT |
| 58 | +button_D.pull = Pull.UP |
| 59 | + |
| 60 | +button_C = DigitalInOut(board.D4) |
| 61 | +button_C.direction = Direction.INPUT |
| 62 | +button_C.pull = Pull.UP |
| 63 | + |
| 64 | + |
| 65 | +# Clear display. |
| 66 | +disp.fill(0) |
| 67 | +disp.show() |
| 68 | + |
| 69 | +# Create blank image for drawing. |
| 70 | +# Make sure to create image with mode '1' for 1-bit color. |
| 71 | +width = disp.width |
| 72 | +height = disp.height |
| 73 | +image = Image.new('1', (width, height)) |
| 74 | + |
| 75 | +# Get drawing object to draw on image. |
| 76 | +draw = ImageDraw.Draw(image) |
| 77 | + |
| 78 | +# Draw a black filled box to clear the image. |
| 79 | +draw.rectangle((0, 0, width, height), outline=0, fill=0) |
| 80 | + |
| 81 | + |
| 82 | +while True: |
| 83 | + if button_U.value: # button is released |
| 84 | + draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0) #Up |
| 85 | + else: # button is pressed: |
| 86 | + draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=1) #Up filled |
| 87 | + |
| 88 | + if button_L.value: # button is released |
| 89 | + draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0) #left |
| 90 | + else: # button is pressed: |
| 91 | + draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=1) #left filled |
| 92 | + |
| 93 | + if button_R.value: # button is released |
| 94 | + draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0) #right |
| 95 | + else: # button is pressed: |
| 96 | + draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=1) #right filled |
| 97 | + |
| 98 | + if button_D.value: # button is released |
| 99 | + draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0) #down |
| 100 | + else: # button is pressed: |
| 101 | + draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=1) #down filled |
| 102 | + |
| 103 | + if button_C.value: # button is released |
| 104 | + draw.rectangle((20, 22, 40, 40), outline=255, fill=0) #center |
| 105 | + else: # button is pressed: |
| 106 | + draw.rectangle((20, 22, 40, 40), outline=255, fill=1) #center filled |
| 107 | + |
| 108 | + if button_A.value: # button is released |
| 109 | + draw.ellipse((70, 40, 90, 60), outline=255, fill=0) #A button |
| 110 | + else: # button is pressed: |
| 111 | + draw.ellipse((70, 40, 90, 60), outline=255, fill=1) #A button filled |
| 112 | + |
| 113 | + if button_B.value: # button is released |
| 114 | + draw.ellipse((100, 20, 120, 40), outline=255, fill=0) #B button |
| 115 | + else: # button is pressed: |
| 116 | + draw.ellipse((100, 20, 120, 40), outline=255, fill=1) #B button filled |
| 117 | + |
| 118 | + if not button_A.value and not button_B.value and not button_C.value: |
| 119 | + catImage = Image.open('happycat_oled_64.ppm').convert('1') |
| 120 | + disp.image(catImage) |
| 121 | + else: |
| 122 | + # Display image. |
| 123 | + disp.image(image) |
| 124 | + |
| 125 | + disp.show() |
0 commit comments