Advertisement
Guest User

Untitled

a guest
Jan 20th, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  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. import time
  22.  
  23. import Adafruit_GPIO.SPI as SPI
  24. import Adafruit_SSD1306
  25.  
  26. from PIL import Image
  27. from PIL import ImageDraw
  28. from PIL import ImageFont
  29.  
  30.  
  31. # Raspberry Pi pin configuration:
  32. RST = 24
  33. # Note the following are only used with SPI:
  34. DC = 23
  35. SPI_PORT = 0
  36. SPI_DEVICE = 0
  37.  
  38. # Beaglebone Black pin configuration:
  39. # RST = 'P9_12'
  40. # Note the following are only used with SPI:
  41. # DC = 'P9_15'
  42. # SPI_PORT = 1
  43. # SPI_DEVICE = 0
  44.  
  45. # 128x32 display with hardware I2C:
  46. disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
  47.  
  48. # 128x64 display with hardware I2C:
  49. # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
  50.  
  51. # Note you can change the I2C address by passing an i2c_address parameter like:
  52. # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C)
  53.  
  54. # Alternatively you can specify an explicit I2C bus number, for example
  55. # with the 128x32 display you would use:
  56. # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=2)
  57.  
  58. # 128x32 display with hardware SPI:
  59. # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
  60.  
  61. # 128x64 display with hardware SPI:
  62. # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
  63.  
  64. # Alternatively you can specify a software SPI implementation by providing
  65. # digital GPIO pin numbers for all the required display pins. For example
  66. # on a Raspberry Pi with the 128x32 display you might use:
  67. # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, sclk=18, din=25, cs=22)
  68.  
  69. # Initialize library.
  70. disp.begin()
  71.  
  72. # Clear display.
  73. disp.clear()
  74. disp.display()
  75.  
  76. # Create blank image for drawing.
  77. # Make sure to create image with mode '1' for 1-bit color.
  78. width = disp.width
  79. height = disp.height
  80. image = Image.new('1', (width, height))
  81.  
  82. # Get drawing object to draw on image.
  83. draw = ImageDraw.Draw(image)
  84.  
  85. # Draw a black filled box to clear the image.
  86. draw.rectangle((0,0,width,height), outline=0, fill=0)
  87.  
  88. # Draw some shapes.
  89. # First define some constants to allow easy resizing of shapes.
  90. padding = 2
  91. shape_width = 20
  92. top = padding
  93. bottom = height-padding
  94. # Move left to right keeping track of the current x position for drawing shapes.
  95. x = padding
  96. # Draw an ellipse.
  97. draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0)
  98. x += shape_width+padding
  99. # Draw a rectangle.
  100. draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
  101. x += shape_width+padding
  102. # Draw a triangle.
  103. draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=255, fill=0)
  104. x += shape_width+padding
  105. # Draw an X.
  106. draw.line((x, bottom, x+shape_width, top), fill=255)
  107. draw.line((x, top, x+shape_width, bottom), fill=255)
  108. x += shape_width+padding
  109.  
  110. # Load default font.
  111. font = ImageFont.load_default()
  112.  
  113. # Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script!
  114. # Some other nice fonts to try: http://www.dafont.com/bitmap.php
  115. #font = ImageFont.truetype('Minecraftia.ttf', 8)
  116.  
  117. # Write two lines of text.
  118. draw.text((x, top), 'Hello', font=font, fill=255)
  119. draw.text((x, top+20), 'World!', font=font, fill=255)
  120.  
  121. # Display image.
  122. disp.image(image)
  123. disp.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement