|
| 1 | +# Basic example of using framebuf capabilities on a SSD1306 OLED display. |
| 2 | +# This example and library is meant to work with Adafruit CircuitPython API. |
| 3 | +# Author: Tony DiCola |
| 4 | +# License: Public Domain |
| 5 | + |
| 6 | +# Import all board pins. |
| 7 | +import time |
| 8 | +import board |
| 9 | +import busio |
| 10 | +from digitalio import DigitalInOut |
| 11 | + |
| 12 | +# Import the SSD1306 module. |
| 13 | +import adafruit_ssd1306 |
| 14 | + |
| 15 | + |
| 16 | +# Create the I2C interface. |
| 17 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 18 | +# A reset line may be required if there is no auto-reset circuitry |
| 19 | +reset_pin = DigitalInOut(board.D5) |
| 20 | + |
| 21 | +# Create the SSD1306 OLED class. |
| 22 | +# The first two parameters are the pixel width and pixel height. Change these |
| 23 | +# to the right size for your display! |
| 24 | +# The I2C address for these displays is 0x3d or 0x3c, change to match |
| 25 | +# A reset line may be required if there is no auto-reset circuitry |
| 26 | +display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3d, reset=reset_pin) |
| 27 | + |
| 28 | +print("Framebuf capability test - these are slow and minimal but don't require" |
| 29 | + "a special graphics management library, only `adafruit_framebuf`") |
| 30 | + |
| 31 | +print("Pixel test") |
| 32 | +# Clear the display. Always call show after changing pixels to make the display |
| 33 | +# update visible! |
| 34 | +display.fill(0) |
| 35 | +display.show() |
| 36 | + |
| 37 | +# Set a pixel in the origin 0,0 position. |
| 38 | +display.pixel(0, 0, 1) |
| 39 | +# Set a pixel in the middle position. |
| 40 | +display.pixel(display.width//2, display.height//2, 1) |
| 41 | +# Set a pixel in the opposite corner position. |
| 42 | +display.pixel(display.width-1, display.height-1, 1) |
| 43 | +display.show() |
| 44 | +time.sleep(0.1) |
| 45 | + |
| 46 | +print("Lines test") |
| 47 | +# we'll draw from corner to corner, lets define all the pair coordinates here |
| 48 | +corners = ((0, 0), (0, display.height-1), (display.width-1, 0), |
| 49 | + (display.width-1, display.height-1)) |
| 50 | + |
| 51 | +display.fill(0) |
| 52 | +for corner_from in corners: |
| 53 | + for corner_to in corners: |
| 54 | + display.line(corner_from[0], corner_from[1], |
| 55 | + corner_to[0], corner_to[1], 1) |
| 56 | +display.show() |
| 57 | +time.sleep(0.1) |
| 58 | + |
| 59 | +print("Rectangle test") |
| 60 | +display.fill(0) |
| 61 | +w_delta = display.width / 10 |
| 62 | +h_delta = display.height / 10 |
| 63 | +for i in range(11): |
| 64 | + display.rect(0, 0, int(w_delta*i), int(h_delta*i), 1) |
| 65 | +display.show() |
| 66 | +time.sleep(0.1) |
| 67 | + |
| 68 | +print("Text test") |
| 69 | +display.fill(0) |
| 70 | +display.text('hello world', 0, 0, 1) |
| 71 | +char_width = 6 |
| 72 | +char_height = 8 |
| 73 | +chars_per_line = display.width//6 |
| 74 | +for i in range(255): |
| 75 | + x = char_width * (i % chars_per_line) |
| 76 | + y = char_height * (i // chars_per_line) |
| 77 | + display.text(chr(i), x, y, 1) |
| 78 | +display.show() |
0 commit comments