Skip to content

Commit 427c934

Browse files
authored
Merge pull request #14 from ladyada/master
framebuf, text + test
2 parents 40d648c + 8ffedb1 commit 427c934

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

adafruit_ssd1306.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232

3333
from micropython import const
3434
from adafruit_bus_device import i2c_device, spi_device
35-
import framebuf
35+
try:
36+
import framebuf
37+
except ImportError:
38+
import adafruit_framebuf as framebuf
3639

3740
__version__ = "0.0.0-auto.0"
3841
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
@@ -63,7 +66,7 @@ class _SSD1306:
6366
"""Base class for SSD1306 display driver"""
6467
#pylint: disable-msg=too-many-arguments
6568
#pylint: disable-msg=too-many-instance-attributes
66-
def __init__(self, framebuffer, width, height, external_vcc, reset):
69+
def __init__(self, framebuffer, width, height, *, external_vcc, reset):
6770
self.framebuf = framebuffer
6871
self.fill = self.framebuf.fill
6972
self.pixel = self.framebuf.pixel
@@ -74,6 +77,7 @@ def __init__(self, framebuffer, width, height, external_vcc, reset):
7477
self.vline = self.framebuf.vline
7578
self.hline = self.framebuf.hline
7679
self.fill_rect = self.framebuf.fill_rect
80+
self.rect = self.framebuf.rect
7781
self.width = width
7882
self.height = height
7983
self.external_vcc = external_vcc
@@ -188,7 +192,8 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
188192
self.buffer = bytearray(((height // 8) * width) + 1)
189193
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
190194
framebuffer = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
191-
super().__init__(framebuffer, width, height, external_vcc, reset)
195+
super().__init__(framebuffer, width, height,
196+
external_vcc=external_vcc, reset=reset)
192197

193198
def write_cmd(self, cmd):
194199
"""Send a command to the SPI device"""
@@ -226,7 +231,8 @@ def __init__(self, width, height, spi, dc, reset, cs, *,
226231
self.dc_pin = dc
227232
self.buffer = bytearray((height // 8) * width)
228233
framebuffer = framebuf.FrameBuffer1(self.buffer, width, height)
229-
super().__init__(framebuffer, width, height, external_vcc, reset)
234+
super().__init__(framebuffer, width, height,
235+
external_vcc=external_vcc, reset=reset)
230236

231237
def write_cmd(self, cmd):
232238
"""Send a command to the SPI device"""

examples/ssd1306_framebuftest.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)