/ examples / ssd1306_bonnet_buttons.py
ssd1306_bonnet_buttons.py
  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  # This example is for use on (Linux) computers that are using CPython with
 23  # Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 24  # not support PIL/pillow (python imaging library)!
 25  
 26  import board
 27  import busio
 28  from digitalio import DigitalInOut, Direction, Pull
 29  from PIL import Image, ImageDraw
 30  import adafruit_ssd1306
 31  
 32  # Create the I2C interface.
 33  i2c = busio.I2C(board.SCL, board.SDA)
 34  # Create the SSD1306 OLED class.
 35  disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
 36  
 37  
 38  # Input pins:
 39  button_A = DigitalInOut(board.D5)
 40  button_A.direction = Direction.INPUT
 41  button_A.pull = Pull.UP
 42  
 43  button_B = DigitalInOut(board.D6)
 44  button_B.direction = Direction.INPUT
 45  button_B.pull = Pull.UP
 46  
 47  button_L = DigitalInOut(board.D27)
 48  button_L.direction = Direction.INPUT
 49  button_L.pull = Pull.UP
 50  
 51  button_R = DigitalInOut(board.D23)
 52  button_R.direction = Direction.INPUT
 53  button_R.pull = Pull.UP
 54  
 55  button_U = DigitalInOut(board.D17)
 56  button_U.direction = Direction.INPUT
 57  button_U.pull = Pull.UP
 58  
 59  button_D = DigitalInOut(board.D22)
 60  button_D.direction = Direction.INPUT
 61  button_D.pull = Pull.UP
 62  
 63  button_C = DigitalInOut(board.D4)
 64  button_C.direction = Direction.INPUT
 65  button_C.pull = Pull.UP
 66  
 67  
 68  # Clear display.
 69  disp.fill(0)
 70  disp.show()
 71  
 72  # Create blank image for drawing.
 73  # Make sure to create image with mode '1' for 1-bit color.
 74  width = disp.width
 75  height = disp.height
 76  image = Image.new("1", (width, height))
 77  
 78  # Get drawing object to draw on image.
 79  draw = ImageDraw.Draw(image)
 80  
 81  # Draw a black filled box to clear the image.
 82  draw.rectangle((0, 0, width, height), outline=0, fill=0)
 83  
 84  
 85  while True:
 86      if button_U.value:  # button is released
 87          draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0)  # Up
 88      else:  # button is pressed:
 89          draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=1)  # Up filled
 90  
 91      if button_L.value:  # button is released
 92          draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0)  # left
 93      else:  # button is pressed:
 94          draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=1)  # left filled
 95  
 96      if button_R.value:  # button is released
 97          draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0)  # right
 98      else:  # button is pressed:
 99          draw.polygon(
100              [(60, 30), (42, 21), (42, 41)], outline=255, fill=1
101          )  # right filled
102  
103      if button_D.value:  # button is released
104          draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0)  # down
105      else:  # button is pressed:
106          draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=1)  # down filled
107  
108      if button_C.value:  # button is released
109          draw.rectangle((20, 22, 40, 40), outline=255, fill=0)  # center
110      else:  # button is pressed:
111          draw.rectangle((20, 22, 40, 40), outline=255, fill=1)  # center filled
112  
113      if button_A.value:  # button is released
114          draw.ellipse((70, 40, 90, 60), outline=255, fill=0)  # A button
115      else:  # button is pressed:
116          draw.ellipse((70, 40, 90, 60), outline=255, fill=1)  # A button filled
117  
118      if button_B.value:  # button is released
119          draw.ellipse((100, 20, 120, 40), outline=255, fill=0)  # B button
120      else:  # button is pressed:
121          draw.ellipse((100, 20, 120, 40), outline=255, fill=1)  # B button filled
122  
123      if not button_A.value and not button_B.value and not button_C.value:
124          catImage = Image.open("happycat_oled_64.ppm").convert("1")
125          disp.image(catImage)
126      else:
127          # Display image.
128          disp.image(image)
129  
130      disp.show()