intro.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import math
  2. import time
  3. import Adafruit_GPIO.SPI as SPI
  4. import Adafruit_SSD1306
  5. from PIL import Image
  6. from PIL import ImageFont
  7. from PIL import ImageDraw
  8. RST = 24
  9. # 128x64 display with hardware I2C:
  10. disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
  11. # Initialize library.
  12. disp.begin()
  13. # Get display width and height.
  14. width = disp.width
  15. height = disp.height
  16. # Clear display.
  17. disp.clear()
  18. disp.display()
  19. # Create image buffer.
  20. # Make sure to create image with mode '1' for 1-bit color.
  21. image = Image.new('1', (width, height))
  22. # Load default font.
  23. font = ImageFont.load_default()
  24. # Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as this python script!
  25. # Some nice fonts to try: http://www.dafont.com/bitmap.php
  26. # font = ImageFont.truetype('Minecraftia.ttf', 8)
  27. # Create drawing object.
  28. draw = ImageDraw.Draw(image)
  29. # Define text and get total width.
  30. text = 'Floppy Player'
  31. maxwidth, unused = draw.textsize(text, font=font)
  32. # Set animation and sine wave parameters.
  33. amplitude = height/4-10
  34. offset = height/2 - 4
  35. velocity = -2
  36. startpos = width
  37. # Animate text moving in sine wave.
  38. # print('Press Ctrl-C to quit.')
  39. pos = startpos
  40. while True:
  41. # Clear image buffer by drawing a black filled box.
  42. draw.rectangle((0,0,width,height), outline=0, fill=0)
  43. # Enumerate characters and draw them offset vertically based on a sine wave.
  44. x = pos
  45. for i, c in enumerate(text):
  46. # Stop drawing if off the right side of screen.
  47. if x > width:
  48. break
  49. # Calculate width but skip drawing if off the left side of screen.
  50. if x < -10:
  51. char_width, char_height = draw.textsize(c, font=font)
  52. x += char_width
  53. continue
  54. # Calculate offset from sine wave.
  55. y = offset+math.floor(amplitude*math.sin(x/float(width)*2.0*math.pi))
  56. # Draw text.
  57. draw.text((x, y), c, font=font, fill=255)
  58. # Increment x position based on chacacter width.
  59. char_width, char_height = draw.textsize(c, font=font)
  60. x += char_width
  61. # Draw the image buffer.
  62. disp.image(image)
  63. disp.display()
  64. # Move position for next frame.
  65. pos += velocity
  66. # Start over if text has scrolled completely off left side of screen.
  67. if pos < -maxwidth:
  68. break
  69. # Pause briefly before drawing next frame.
  70. time.sleep(0.1)