shapes.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) 2014 Adafruit Industries
  2. # Author: Tony DiCola
  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. from PIL import Image
  22. from PIL import ImageDraw
  23. from PIL import ImageFont
  24. import ST7735 as TFT
  25. import Adafruit_GPIO as GPIO
  26. import Adafruit_GPIO.SPI as SPI
  27. WIDTH = 128
  28. HEIGHT = 160
  29. SPEED_HZ = 4000000
  30. # Raspberry Pi configuration.
  31. DC = 24
  32. RST = 25
  33. SPI_PORT = 0
  34. SPI_DEVICE = 0
  35. # BeagleBone Black configuration.
  36. # DC = 'P9_15'
  37. # RST = 'P9_12'
  38. # SPI_PORT = 1
  39. # SPI_DEVICE = 0
  40. # Create TFT LCD display class.
  41. disp = TFT.ST7735(
  42. DC,
  43. rst=RST,
  44. spi=SPI.SpiDev(
  45. SPI_PORT,
  46. SPI_DEVICE,
  47. max_speed_hz=SPEED_HZ))
  48. # Initialize display.
  49. disp.begin()
  50. # Clear the display to a red background.
  51. # Can pass any tuple of red, green, blue values (from 0 to 255 each).
  52. disp.clear((255, 0, 0))
  53. # Alternatively can clear to a black screen by calling:
  54. # disp.clear()
  55. # Get a PIL Draw object to start drawing on the display buffer.
  56. draw = disp.draw()
  57. # Draw some shapes.
  58. # Draw a blue ellipse with a green outline.
  59. draw.ellipse((10, 10, 110, 80), outline=(0,255,0), fill=(0,0,255))
  60. # Draw a purple rectangle with yellow outline.
  61. draw.rectangle((10, 90, 110, 160), outline=(255,255,0), fill=(255,0,255))
  62. # Draw a white X.
  63. draw.line((10, 170, 110, 230), fill=(255,255,255))
  64. draw.line((10, 230, 110, 170), fill=(255,255,255))
  65. # Draw a cyan triangle with a black outline.
  66. draw.polygon([(10, 275), (110, 240), (110, 310)], outline=(0,0,0), fill=(0,255,255))
  67. # Load default font.
  68. font = ImageFont.load_default()
  69. # Alternatively load a TTF font.
  70. # Some other nice fonts to try: http://www.dafont.com/bitmap.php
  71. #font = ImageFont.truetype('Minecraftia.ttf', 16)
  72. # Define a function to create rotated text. Unfortunately PIL doesn't have good
  73. # native support for rotated fonts, but this function can be used to make a
  74. # text image and rotate it so it's easy to paste in the buffer.
  75. def draw_rotated_text(image, text, position, angle, font, fill=(255,255,255)):
  76. # Get rendered font width and height.
  77. draw = ImageDraw.Draw(image)
  78. width, height = draw.textsize(text, font=font)
  79. # Create a new image with transparent background to store the text.
  80. textimage = Image.new('RGBA', (width, height), (0,0,0,0))
  81. # Render the text.
  82. textdraw = ImageDraw.Draw(textimage)
  83. textdraw.text((0,0), text, font=font, fill=fill)
  84. # Rotate the text image.
  85. rotated = textimage.rotate(angle, expand=1)
  86. # Paste the text into the image, using it as a mask for transparency.
  87. image.paste(rotated, position, rotated)
  88. # Write two lines of white text on the buffer, rotated 90 degrees counter clockwise.
  89. draw_rotated_text(disp.buffer, 'Hello World!', (150, 120), 90, font, fill=(255,255,255))
  90. draw_rotated_text(disp.buffer, 'This is a line of text.', (170, 90), 90, font, fill=(255,255,255))
  91. # Write buffer to display hardware, must be called to make things visible on the
  92. # display!
  93. disp.display()