From 304e7fcb79c3ac42085d7b2fb0ecde499a55fe75 Mon Sep 17 00:00:00 2001 From: ladyada Date: Fri, 21 Dec 2018 22:20:40 -0500 Subject: [PATCH 1/2] oled test with framebuf, its really slow with soft-framebuf, but does work --- examples/ssd1306_framebuftest.py | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 examples/ssd1306_framebuftest.py diff --git a/examples/ssd1306_framebuftest.py b/examples/ssd1306_framebuftest.py new file mode 100644 index 0000000..a78f8c5 --- /dev/null +++ b/examples/ssd1306_framebuftest.py @@ -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() From 8ffedb1a341f51f66abfe768c197363b4d861cf5 Mon Sep 17 00:00:00 2001 From: ladyada Date: Fri, 21 Dec 2018 22:24:29 -0500 Subject: [PATCH 2/2] add missing rectangle drawer, allow use of adafruit_framebuf --- adafruit_ssd1306.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/adafruit_ssd1306.py b/adafruit_ssd1306.py index 7f635b8..ed55026 100644 --- a/adafruit_ssd1306.py +++ b/adafruit_ssd1306.py @@ -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" @@ -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 @@ -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 @@ -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""" @@ -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"""