|
| 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 | +import time |
| 28 | +import random |
| 29 | +from colorsys import hsv_to_rgb |
| 30 | +import board |
| 31 | +from digitalio import DigitalInOut, Direction |
| 32 | +from PIL import Image, ImageDraw, ImageFont |
| 33 | +import adafruit_rgb_display.st7789 as st7789 |
| 34 | + |
| 35 | +# Create the display |
| 36 | +cs_pin = DigitalInOut(board.CE0) |
| 37 | +dc_pin = DigitalInOut(board.D25) |
| 38 | +reset_pin = DigitalInOut(board.D24) |
| 39 | +BAUDRATE = 24000000 |
| 40 | + |
| 41 | +spi = board.SPI() |
| 42 | +disp = st7789.ST7789( |
| 43 | + spi, |
| 44 | + height=240, |
| 45 | + y_offset=80, |
| 46 | + rotation=180, |
| 47 | + cs=cs_pin, |
| 48 | + dc=dc_pin, |
| 49 | + rst=reset_pin, |
| 50 | + baudrate=BAUDRATE, |
| 51 | +) |
| 52 | + |
| 53 | +# Input pins: |
| 54 | +button_A = DigitalInOut(board.D5) |
| 55 | +button_A.direction = Direction.INPUT |
| 56 | + |
| 57 | +button_B = DigitalInOut(board.D6) |
| 58 | +button_B.direction = Direction.INPUT |
| 59 | + |
| 60 | +button_L = DigitalInOut(board.D27) |
| 61 | +button_L.direction = Direction.INPUT |
| 62 | + |
| 63 | +button_R = DigitalInOut(board.D23) |
| 64 | +button_R.direction = Direction.INPUT |
| 65 | + |
| 66 | +button_U = DigitalInOut(board.D17) |
| 67 | +button_U.direction = Direction.INPUT |
| 68 | + |
| 69 | +button_D = DigitalInOut(board.D22) |
| 70 | +button_D.direction = Direction.INPUT |
| 71 | + |
| 72 | +button_C = DigitalInOut(board.D4) |
| 73 | +button_C.direction = Direction.INPUT |
| 74 | + |
| 75 | +# Turn on the Backlight |
| 76 | +backlight = DigitalInOut(board.D26) |
| 77 | +backlight.switch_to_output() |
| 78 | +backlight.value = True |
| 79 | + |
| 80 | +# Create blank image for drawing. |
| 81 | +# Make sure to create image with mode 'RGB' for color. |
| 82 | +width = disp.width |
| 83 | +height = disp.height |
| 84 | +image = Image.new("RGB", (width, height)) |
| 85 | + |
| 86 | +# Get drawing object to draw on image. |
| 87 | +draw = ImageDraw.Draw(image) |
| 88 | + |
| 89 | +# Clear display. |
| 90 | +draw.rectangle((0, 0, width, height), outline=0, fill=(255, 0, 0)) |
| 91 | +disp.image(image) |
| 92 | + |
| 93 | +# Get drawing object to draw on image. |
| 94 | +draw = ImageDraw.Draw(image) |
| 95 | + |
| 96 | +# Draw a black filled box to clear the image. |
| 97 | +draw.rectangle((0, 0, width, height), outline=0, fill=0) |
| 98 | + |
| 99 | +udlr_fill = "#00FF00" |
| 100 | +udlr_outline = "#00FFFF" |
| 101 | +button_fill = "#FF00FF" |
| 102 | +button_outline = "#FFFFFF" |
| 103 | + |
| 104 | +fnt = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 30) |
| 105 | + |
| 106 | +while True: |
| 107 | + up_fill = 0 |
| 108 | + if not button_U.value: # up pressed |
| 109 | + up_fill = udlr_fill |
| 110 | + draw.polygon( |
| 111 | + [(40, 40), (60, 4), (80, 40)], outline=udlr_outline, fill=up_fill |
| 112 | + ) # Up |
| 113 | + |
| 114 | + down_fill = 0 |
| 115 | + if not button_D.value: # down pressed |
| 116 | + down_fill = udlr_fill |
| 117 | + draw.polygon( |
| 118 | + [(60, 120), (80, 84), (40, 84)], outline=udlr_outline, fill=down_fill |
| 119 | + ) # down |
| 120 | + |
| 121 | + left_fill = 0 |
| 122 | + if not button_L.value: # left pressed |
| 123 | + left_fill = udlr_fill |
| 124 | + draw.polygon( |
| 125 | + [(0, 60), (36, 42), (36, 81)], outline=udlr_outline, fill=left_fill |
| 126 | + ) # left |
| 127 | + |
| 128 | + right_fill = 0 |
| 129 | + if not button_R.value: # right pressed |
| 130 | + right_fill = udlr_fill |
| 131 | + draw.polygon( |
| 132 | + [(120, 60), (84, 42), (84, 82)], outline=udlr_outline, fill=right_fill |
| 133 | + ) # right |
| 134 | + |
| 135 | + center_fill = 0 |
| 136 | + if not button_C.value: # center pressed |
| 137 | + center_fill = button_fill |
| 138 | + draw.rectangle((40, 44, 80, 80), outline=button_outline, fill=center_fill) # center |
| 139 | + |
| 140 | + A_fill = 0 |
| 141 | + if not button_A.value: # left pressed |
| 142 | + A_fill = button_fill |
| 143 | + draw.ellipse((140, 80, 180, 120), outline=button_outline, fill=A_fill) # A button |
| 144 | + |
| 145 | + B_fill = 0 |
| 146 | + if not button_B.value: # left pressed |
| 147 | + B_fill = button_fill |
| 148 | + draw.ellipse((190, 40, 230, 80), outline=button_outline, fill=B_fill) # B button |
| 149 | + |
| 150 | + # make a random color and print text |
| 151 | + rcolor = tuple([int(x * 255) for x in hsv_to_rgb(random.random(), 1, 1)]) |
| 152 | + draw.text((20, 150), "Hello World", font=fnt, fill=rcolor) |
| 153 | + rcolor = tuple([int(x * 255) for x in hsv_to_rgb(random.random(), 1, 1)]) |
| 154 | + draw.text((20, 180), "Hello World", font=fnt, fill=rcolor) |
| 155 | + rcolor = tuple([int(x * 255) for x in hsv_to_rgb(random.random(), 1, 1)]) |
| 156 | + draw.text((20, 210), "Hello World", font=fnt, fill=rcolor) |
| 157 | + |
| 158 | + # Display the Image |
| 159 | + disp.image(image) |
| 160 | + |
| 161 | + time.sleep(0.01) |
0 commit comments