prjbrook

OLED2

Jun 23rd, 2022
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1.  
  2. # Basic example of clearing and drawing pixels on a SSD1306 OLED display.
  3. # This example and library is meant to work with Adafruit CircuitPython API.
  4.  
  5. # SPDX-FileCopyrightText: Tony DiCola
  6. # SPDX-License-Identifier: CC0-1.0
  7.  
  8. # Basic example of using framebuf capabilities on a SSD1306 OLED display.
  9. # This example and library is meant to work with Adafruit CircuitPython API.
  10.  
  11. # Import all board pins.
  12. import time
  13. import board
  14. import busio
  15. from digitalio import DigitalInOut
  16.  
  17. # Import the SSD1306 module.
  18. import adafruit_ssd1306
  19.  
  20.  
  21. # Create the I2C interface.
  22. #i2c = busio.I2C(board.SCL, board.SDA)
  23. i2c = busio.I2C(board.GP17, board.GP16)
  24. # A reset line may be required if there is no auto-reset circuitry
  25. #reset_pin = DigitalInOut(board.D5)
  26.  
  27. # Create the SSD1306 OLED class.
  28. # The first two parameters are the pixel width and pixel height . Change these
  29. # to the right size for your display!
  30. # The I2C address for these displays is 0x3d or 0x3c, change to match
  31. # A reset line may be required if there is no auto-reset circuitry
  32. #display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3C, reset=reset_pin)
  33. display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
  34. print(
  35. "Framebuf capability test - these are slow and minimal but don't require "
  36. "a special graphics management library, only `adafruit_framebuf`"
  37. )
  38.  
  39. print("Pixel test")
  40. # Clear the display. Always call show after changing pixels to make the display
  41. # update visible!
  42. display.fill(0)
  43. display.show()
  44.  
  45. # Set a pixel in the origin 0,0 position.
  46. display.pixel(0, 0, 1)
  47. # Set a pixel in the middle position.
  48. display.pixel(display.width // 2, display.height // 2, 1)
  49. # Set a pixel in the opposite corner position.
  50. display.pixel(display.width - 1, display.height - 1, 1)
  51. display.show()
  52. time.sleep(0.1)
  53.  
  54. print("Lines test")
  55. # we'll draw from corner to corner, lets define all the pair coordinates here
  56. corners = (
  57. (0, 0),
  58. (0, display.height - 1),
  59. (display.width - 1, 0),
  60. (display.width - 1, display.height - 1),
  61. )
  62.  
  63. display.fill(0)
  64. for corner_from in corners:
  65. for corner_to in corners:
  66. display.line(corner_from[0], corner_from[1], corner_to[0], corner_to[1], 1)
  67. display.show()
  68. time.sleep(0.1)
  69.  
  70. print("Rectangle test")
  71. display.fill(0)
  72. w_delta = display.width / 10
  73. h_delta = display.height / 10
  74. for i in range(11):
  75. display.rect(0, 0, int(w_delta * i), int(h_delta * i), 1)
  76. display.show()
  77. time.sleep(0.1)
  78.  
  79. print("Text test")
  80. print("Text test")
  81. display.fill(0)
  82. try:
  83. display.text("hello000 world", 0, 0, 1)
  84. display.show()
  85. time.sleep(1)
  86. display.fill(0)
  87. char_width = 6
  88. char_height = 8
  89. chars_per_line = display.width // 6
  90. for i in range(255):
  91. x = char_width * (i % chars_per_line)
  92. y = char_height * (i // chars_per_line)
  93. display.text(chr(i), x, y, 1)
  94. display.show()
  95. except FileNotFoundError:
  96. print(
  97. "To test the framebuf font setup, you'll need the font5x8.bin file from "
  98. + "https://github.com/adafruit/Adafruit_CircuitPython_framebuf/blob/main/examples/"
  99. + " in the same directory as this script"
  100. )
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment