/ examples / rgb_display_pillow_bonnet_buttons.py
rgb_display_pillow_bonnet_buttons.py
  1  # Copyright (c) 2017 Adafruit Industries
  2  # Author: James DeVito
  3  # Ported to RGB Display by Melissa LeBlanc-Williams
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  
 23  # This example is for use on (Linux) computers that are using CPython with
 24  # Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 25  # not support PIL/pillow (python imaging library)!
 26  """
 27  This example is for use on (Linux) computers that are using CPython with
 28  Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 29  not support PIL/pillow (python imaging library)!
 30  """
 31  
 32  import time
 33  import random
 34  from colorsys import hsv_to_rgb
 35  import board
 36  from digitalio import DigitalInOut, Direction
 37  from PIL import Image, ImageDraw, ImageFont
 38  import adafruit_rgb_display.st7789 as st7789
 39  
 40  # Create the display
 41  cs_pin = DigitalInOut(board.CE0)
 42  dc_pin = DigitalInOut(board.D25)
 43  reset_pin = DigitalInOut(board.D24)
 44  BAUDRATE = 24000000
 45  
 46  spi = board.SPI()
 47  disp = st7789.ST7789(
 48      spi,
 49      height=240,
 50      y_offset=80,
 51      rotation=180,
 52      cs=cs_pin,
 53      dc=dc_pin,
 54      rst=reset_pin,
 55      baudrate=BAUDRATE,
 56  )
 57  
 58  # Input pins:
 59  button_A = DigitalInOut(board.D5)
 60  button_A.direction = Direction.INPUT
 61  
 62  button_B = DigitalInOut(board.D6)
 63  button_B.direction = Direction.INPUT
 64  
 65  button_L = DigitalInOut(board.D27)
 66  button_L.direction = Direction.INPUT
 67  
 68  button_R = DigitalInOut(board.D23)
 69  button_R.direction = Direction.INPUT
 70  
 71  button_U = DigitalInOut(board.D17)
 72  button_U.direction = Direction.INPUT
 73  
 74  button_D = DigitalInOut(board.D22)
 75  button_D.direction = Direction.INPUT
 76  
 77  button_C = DigitalInOut(board.D4)
 78  button_C.direction = Direction.INPUT
 79  
 80  # Turn on the Backlight
 81  backlight = DigitalInOut(board.D26)
 82  backlight.switch_to_output()
 83  backlight.value = True
 84  
 85  # Create blank image for drawing.
 86  # Make sure to create image with mode 'RGB' for color.
 87  width = disp.width
 88  height = disp.height
 89  image = Image.new("RGB", (width, height))
 90  
 91  # Get drawing object to draw on image.
 92  draw = ImageDraw.Draw(image)
 93  
 94  # Clear display.
 95  draw.rectangle((0, 0, width, height), outline=0, fill=(255, 0, 0))
 96  disp.image(image)
 97  
 98  # Get drawing object to draw on image.
 99  draw = ImageDraw.Draw(image)
100  
101  # Draw a black filled box to clear the image.
102  draw.rectangle((0, 0, width, height), outline=0, fill=0)
103  
104  udlr_fill = "#00FF00"
105  udlr_outline = "#00FFFF"
106  button_fill = "#FF00FF"
107  button_outline = "#FFFFFF"
108  
109  fnt = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 30)
110  
111  while True:
112      up_fill = 0
113      if not button_U.value:  # up pressed
114          up_fill = udlr_fill
115      draw.polygon(
116          [(40, 40), (60, 4), (80, 40)], outline=udlr_outline, fill=up_fill
117      )  # Up
118  
119      down_fill = 0
120      if not button_D.value:  # down pressed
121          down_fill = udlr_fill
122      draw.polygon(
123          [(60, 120), (80, 84), (40, 84)], outline=udlr_outline, fill=down_fill
124      )  # down
125  
126      left_fill = 0
127      if not button_L.value:  # left pressed
128          left_fill = udlr_fill
129      draw.polygon(
130          [(0, 60), (36, 42), (36, 81)], outline=udlr_outline, fill=left_fill
131      )  # left
132  
133      right_fill = 0
134      if not button_R.value:  # right pressed
135          right_fill = udlr_fill
136      draw.polygon(
137          [(120, 60), (84, 42), (84, 82)], outline=udlr_outline, fill=right_fill
138      )  # right
139  
140      center_fill = 0
141      if not button_C.value:  # center pressed
142          center_fill = button_fill
143      draw.rectangle((40, 44, 80, 80), outline=button_outline, fill=center_fill)  # center
144  
145      A_fill = 0
146      if not button_A.value:  # left pressed
147          A_fill = button_fill
148      draw.ellipse((140, 80, 180, 120), outline=button_outline, fill=A_fill)  # A button
149  
150      B_fill = 0
151      if not button_B.value:  # left pressed
152          B_fill = button_fill
153      draw.ellipse((190, 40, 230, 80), outline=button_outline, fill=B_fill)  # B button
154  
155      # make a random color and print text
156      rcolor = tuple([int(x * 255) for x in hsv_to_rgb(random.random(), 1, 1)])
157      draw.text((20, 150), "Hello World", font=fnt, fill=rcolor)
158      rcolor = tuple([int(x * 255) for x in hsv_to_rgb(random.random(), 1, 1)])
159      draw.text((20, 180), "Hello World", font=fnt, fill=rcolor)
160      rcolor = tuple([int(x * 255) for x in hsv_to_rgb(random.random(), 1, 1)])
161      draw.text((20, 210), "Hello World", font=fnt, fill=rcolor)
162  
163      # Display the Image
164      disp.image(image)
165  
166      time.sleep(0.01)