# Copyright (c) 2014 Adafruit Industries # Author: Tony DiCola # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. from PIL import Image from PIL import ImageDraw from PIL import ImageFont import ST7735 as TFT import Adafruit_GPIO as GPIO import Adafruit_GPIO.SPI as SPI WIDTH = 128 HEIGHT = 160 SPEED_HZ = 4000000 # Raspberry Pi configuration. DC = 24 RST = 25 SPI_PORT = 0 SPI_DEVICE = 0 # BeagleBone Black configuration. # DC = 'P9_15' # RST = 'P9_12' # SPI_PORT = 1 # SPI_DEVICE = 0 # Create TFT LCD display class. disp = TFT.ST7735( DC, rst=RST, spi=SPI.SpiDev( SPI_PORT, SPI_DEVICE, max_speed_hz=SPEED_HZ)) # Initialize display. disp.begin() # Clear the display to a red background. # Can pass any tuple of red, green, blue values (from 0 to 255 each). disp.clear((255, 0, 0)) # Alternatively can clear to a black screen by calling: # disp.clear() # Get a PIL Draw object to start drawing on the display buffer. draw = disp.draw() # Draw some shapes. # Draw a blue ellipse with a green outline. draw.ellipse((10, 10, 110, 80), outline=(0,255,0), fill=(0,0,255)) # Draw a purple rectangle with yellow outline. draw.rectangle((10, 90, 110, 160), outline=(255,255,0), fill=(255,0,255)) # Draw a white X. draw.line((10, 170, 110, 230), fill=(255,255,255)) draw.line((10, 230, 110, 170), fill=(255,255,255)) # Draw a cyan triangle with a black outline. draw.polygon([(10, 275), (110, 240), (110, 310)], outline=(0,0,0), fill=(0,255,255)) # Load default font. font = ImageFont.load_default() # Alternatively load a TTF font. # Some other nice fonts to try: http://www.dafont.com/bitmap.php #font = ImageFont.truetype('Minecraftia.ttf', 16) # Define a function to create rotated text. Unfortunately PIL doesn't have good # native support for rotated fonts, but this function can be used to make a # text image and rotate it so it's easy to paste in the buffer. def draw_rotated_text(image, text, position, angle, font, fill=(255,255,255)): # Get rendered font width and height. draw = ImageDraw.Draw(image) width, height = draw.textsize(text, font=font) # Create a new image with transparent background to store the text. textimage = Image.new('RGBA', (width, height), (0,0,0,0)) # Render the text. textdraw = ImageDraw.Draw(textimage) textdraw.text((0,0), text, font=font, fill=fill) # Rotate the text image. rotated = textimage.rotate(angle, expand=1) # Paste the text into the image, using it as a mask for transparency. image.paste(rotated, position, rotated) # Write two lines of white text on the buffer, rotated 90 degrees counter clockwise. draw_rotated_text(disp.buffer, 'Hello World!', (150, 120), 90, font, fill=(255,255,255)) draw_rotated_text(disp.buffer, 'This is a line of text.', (170, 90), 90, font, fill=(255,255,255)) # Write buffer to display hardware, must be called to make things visible on the # display! disp.display()