Skip to content

framebuf, text + test #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions adafruit_ssd1306.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

from micropython import const
from adafruit_bus_device import i2c_device, spi_device
import framebuf
try:
import framebuf
except ImportError:
import adafruit_framebuf as framebuf

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
Expand Down Expand Up @@ -63,7 +66,7 @@ class _SSD1306:
"""Base class for SSD1306 display driver"""
#pylint: disable-msg=too-many-arguments
#pylint: disable-msg=too-many-instance-attributes
def __init__(self, framebuffer, width, height, external_vcc, reset):
def __init__(self, framebuffer, width, height, *, external_vcc, reset):
self.framebuf = framebuffer
self.fill = self.framebuf.fill
self.pixel = self.framebuf.pixel
Expand All @@ -74,6 +77,7 @@ def __init__(self, framebuffer, width, height, external_vcc, reset):
self.vline = self.framebuf.vline
self.hline = self.framebuf.hline
self.fill_rect = self.framebuf.fill_rect
self.rect = self.framebuf.rect
self.width = width
self.height = height
self.external_vcc = external_vcc
Expand Down Expand Up @@ -188,7 +192,8 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
self.buffer = bytearray(((height // 8) * width) + 1)
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
framebuffer = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
super().__init__(framebuffer, width, height, external_vcc, reset)
super().__init__(framebuffer, width, height,
external_vcc=external_vcc, reset=reset)

def write_cmd(self, cmd):
"""Send a command to the SPI device"""
Expand Down Expand Up @@ -226,7 +231,8 @@ def __init__(self, width, height, spi, dc, reset, cs, *,
self.dc_pin = dc
self.buffer = bytearray((height // 8) * width)
framebuffer = framebuf.FrameBuffer1(self.buffer, width, height)
super().__init__(framebuffer, width, height, external_vcc, reset)
super().__init__(framebuffer, width, height,
external_vcc=external_vcc, reset=reset)

def write_cmd(self, cmd):
"""Send a command to the SPI device"""
Expand Down
78 changes: 78 additions & 0 deletions examples/ssd1306_framebuftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Basic example of using framebuf capabilities on a SSD1306 OLED display.
# This example and library is meant to work with Adafruit CircuitPython API.
# Author: Tony DiCola
# License: Public Domain

# Import all board pins.
import time
import board
import busio
from digitalio import DigitalInOut

# Import the SSD1306 module.
import adafruit_ssd1306


# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# A reset line may be required if there is no auto-reset circuitry
reset_pin = DigitalInOut(board.D5)

# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height. Change these
# to the right size for your display!
# The I2C address for these displays is 0x3d or 0x3c, change to match
# A reset line may be required if there is no auto-reset circuitry
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3d, reset=reset_pin)

print("Framebuf capability test - these are slow and minimal but don't require"
"a special graphics management library, only `adafruit_framebuf`")

print("Pixel test")
# Clear the display. Always call show after changing pixels to make the display
# update visible!
display.fill(0)
display.show()

# Set a pixel in the origin 0,0 position.
display.pixel(0, 0, 1)
# Set a pixel in the middle position.
display.pixel(display.width//2, display.height//2, 1)
# Set a pixel in the opposite corner position.
display.pixel(display.width-1, display.height-1, 1)
display.show()
time.sleep(0.1)

print("Lines test")
# we'll draw from corner to corner, lets define all the pair coordinates here
corners = ((0, 0), (0, display.height-1), (display.width-1, 0),
(display.width-1, display.height-1))

display.fill(0)
for corner_from in corners:
for corner_to in corners:
display.line(corner_from[0], corner_from[1],
corner_to[0], corner_to[1], 1)
display.show()
time.sleep(0.1)

print("Rectangle test")
display.fill(0)
w_delta = display.width / 10
h_delta = display.height / 10
for i in range(11):
display.rect(0, 0, int(w_delta*i), int(h_delta*i), 1)
display.show()
time.sleep(0.1)

print("Text test")
display.fill(0)
display.text('hello world', 0, 0, 1)
char_width = 6
char_height = 8
chars_per_line = display.width//6
for i in range(255):
x = char_width * (i % chars_per_line)
y = char_height * (i // chars_per_line)
display.text(chr(i), x, y, 1)
display.show()