Skip to content

Commit d9a0cf0

Browse files
authored
Merge pull request #17 from ladyada/master
turned into a bit of a reworking + linux examples
2 parents c22b490 + eef4d99 commit d9a0cf0

12 files changed

+508
-29
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This driver depends on:
2626

2727
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
2828
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
29+
* `Adafruit framebuf <https://github.com/adafruit/Adafruit_CircuitPython_framebuf>`_
2930

3031
Please ensure all dependencies are available on the CircuitPython filesystem.
3132
This is easily achieved by downloading

adafruit_ssd1306.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,11 @@
6262
#pylint: enable-msg=bad-whitespace
6363

6464

65-
class _SSD1306:
65+
class _SSD1306(framebuf.FrameBuffer):
6666
"""Base class for SSD1306 display driver"""
6767
#pylint: disable-msg=too-many-arguments
68-
#pylint: disable-msg=too-many-instance-attributes
69-
def __init__(self, framebuffer, width, height, *, external_vcc, reset):
70-
self.framebuf = framebuffer
71-
self.fill = self.framebuf.fill
72-
self.pixel = self.framebuf.pixel
73-
self.line = self.framebuf.line
74-
self.text = self.framebuf.text
75-
self.scroll = self.framebuf.scroll
76-
self.blit = self.framebuf.blit
77-
self.vline = self.framebuf.vline
78-
self.hline = self.framebuf.hline
79-
self.fill_rect = self.framebuf.fill_rect
80-
self.rect = self.framebuf.rect
68+
def __init__(self, buffer, width, height, *, external_vcc, reset):
69+
super().__init__(buffer, width, height)
8170
self.width = width
8271
self.height = height
8372
self.external_vcc = external_vcc
@@ -191,8 +180,7 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
191180
# buffer).
192181
self.buffer = bytearray(((height // 8) * width) + 1)
193182
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
194-
framebuffer = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
195-
super().__init__(framebuffer, width, height,
183+
super().__init__(memoryview(self.buffer)[1:], width, height,
196184
external_vcc=external_vcc, reset=reset)
197185

198186
def write_cmd(self, cmd):
@@ -230,8 +218,7 @@ def __init__(self, width, height, spi, dc, reset, cs, *,
230218
polarity=polarity, phase=phase)
231219
self.dc_pin = dc
232220
self.buffer = bytearray((height // 8) * width)
233-
framebuffer = framebuf.FrameBuffer1(self.buffer, width, height)
234-
super().__init__(framebuffer, width, height,
221+
super().__init__(memoryview(self.buffer), width, height,
235222
external_vcc=external_vcc, reset=reset)
236223

237224
def write_cmd(self, cmd):

examples/bonnet_buttons.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright (c) 2017 Adafruit Industries
2+
# Author: James DeVito
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
22+
import board
23+
import busio
24+
from digitalio import DigitalInOut, Direction, Pull
25+
from PIL import Image, ImageDraw
26+
import adafruit_ssd1306
27+
28+
# Create the I2C interface.
29+
i2c = busio.I2C(board.SCL, board.SDA)
30+
# Create the SSD1306 OLED class.
31+
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
32+
33+
34+
35+
# Input pins:
36+
button_A = DigitalInOut(board.D5)
37+
button_A.direction = Direction.INPUT
38+
button_A.pull = Pull.UP
39+
40+
button_B = DigitalInOut(board.D6)
41+
button_B.direction = Direction.INPUT
42+
button_B.pull = Pull.UP
43+
44+
button_L = DigitalInOut(board.D27)
45+
button_L.direction = Direction.INPUT
46+
button_L.pull = Pull.UP
47+
48+
button_R = DigitalInOut(board.D23)
49+
button_R.direction = Direction.INPUT
50+
button_R.pull = Pull.UP
51+
52+
button_U = DigitalInOut(board.D17)
53+
button_U.direction = Direction.INPUT
54+
button_U.pull = Pull.UP
55+
56+
button_D = DigitalInOut(board.D22)
57+
button_D.direction = Direction.INPUT
58+
button_D.pull = Pull.UP
59+
60+
button_C = DigitalInOut(board.D4)
61+
button_C.direction = Direction.INPUT
62+
button_C.pull = Pull.UP
63+
64+
65+
# Clear display.
66+
disp.fill(0)
67+
disp.show()
68+
69+
# Create blank image for drawing.
70+
# Make sure to create image with mode '1' for 1-bit color.
71+
width = disp.width
72+
height = disp.height
73+
image = Image.new('1', (width, height))
74+
75+
# Get drawing object to draw on image.
76+
draw = ImageDraw.Draw(image)
77+
78+
# Draw a black filled box to clear the image.
79+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
80+
81+
82+
while True:
83+
if button_U.value: # button is released
84+
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0) #Up
85+
else: # button is pressed:
86+
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=1) #Up filled
87+
88+
if button_L.value: # button is released
89+
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0) #left
90+
else: # button is pressed:
91+
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=1) #left filled
92+
93+
if button_R.value: # button is released
94+
draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0) #right
95+
else: # button is pressed:
96+
draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=1) #right filled
97+
98+
if button_D.value: # button is released
99+
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0) #down
100+
else: # button is pressed:
101+
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=1) #down filled
102+
103+
if button_C.value: # button is released
104+
draw.rectangle((20, 22, 40, 40), outline=255, fill=0) #center
105+
else: # button is pressed:
106+
draw.rectangle((20, 22, 40, 40), outline=255, fill=1) #center filled
107+
108+
if button_A.value: # button is released
109+
draw.ellipse((70, 40, 90, 60), outline=255, fill=0) #A button
110+
else: # button is pressed:
111+
draw.ellipse((70, 40, 90, 60), outline=255, fill=1) #A button filled
112+
113+
if button_B.value: # button is released
114+
draw.ellipse((100, 20, 120, 40), outline=255, fill=0) #B button
115+
else: # button is pressed:
116+
draw.ellipse((100, 20, 120, 40), outline=255, fill=1) #B button filled
117+
118+
if not button_A.value and not button_B.value and not button_C.value:
119+
catImage = Image.open('happycat_oled_64.ppm').convert('1')
120+
disp.image(catImage)
121+
else:
122+
# Display image.
123+
disp.image(image)
124+
125+
disp.show()

examples/happycat_oled_32.ppm

Whitespace-only changes.

examples/happycat_oled_64.ppm

24.1 KB
Binary file not shown.

examples/pillow_animate.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright (c) 2014 Adafruit Industries
2+
# Author: Tony DiCola
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
22+
import math
23+
import time
24+
from board import SCL, SDA
25+
import busio
26+
from PIL import Image, ImageDraw, ImageFont
27+
import adafruit_ssd1306
28+
29+
# Create the I2C interface.
30+
i2c = busio.I2C(SCL, SDA)
31+
32+
# Create the SSD1306 OLED class.
33+
# The first two parameters are the pixel width and pixel height.
34+
# Change these to the right size for your display!
35+
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
36+
37+
# Note you can change the I2C address, or add a reset pin:
38+
#disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c, reset=reset_pin)
39+
40+
# Get display width and height.
41+
width = disp.width
42+
height = disp.height
43+
44+
# Clear display.
45+
disp.fill(0)
46+
disp.show()
47+
48+
# Create image buffer.
49+
# Make sure to create image with mode '1' for 1-bit color.
50+
image = Image.new('1', (width, height))
51+
52+
# Load default font.
53+
font = ImageFont.load_default()
54+
55+
# Alternatively load a TTF font. Make sure the .ttf font file is in the
56+
# same directory as this python script!
57+
# Some nice fonts to try: http://www.dafont.com/bitmap.php
58+
# font = ImageFont.truetype('Minecraftia.ttf', 8)
59+
60+
# Create drawing object.
61+
draw = ImageDraw.Draw(image)
62+
63+
# Define text and get total width.
64+
text = 'SSD1306 ORGANIC LED DISPLAY. THIS IS AN OLD SCHOOL DEMO SCROLLER!!'+\
65+
'GREETZ TO: LADYADA & THE ADAFRUIT CREW, TRIXTER, FUTURE CREW, AND FARBRAUSCH'
66+
maxwidth, unused = draw.textsize(text, font=font)
67+
68+
# Set animation and sine wave parameters.
69+
amplitude = height/4
70+
offset = height/2 - 4
71+
velocity = -2
72+
startpos = width
73+
74+
# Animate text moving in sine wave.
75+
print('Press Ctrl-C to quit.')
76+
pos = startpos
77+
while True:
78+
# Clear image buffer by drawing a black filled box.
79+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
80+
# Enumerate characters and draw them offset vertically based on a sine wave.
81+
x = pos
82+
for i, c in enumerate(text):
83+
# Stop drawing if off the right side of screen.
84+
if x > width:
85+
break
86+
# Calculate width but skip drawing if off the left side of screen.
87+
if x < -10:
88+
char_width, char_height = draw.textsize(c, font=font)
89+
x += char_width
90+
continue
91+
# Calculate offset from sine wave.
92+
y = offset+math.floor(amplitude*math.sin(x/float(width)*2.0*math.pi))
93+
# Draw text.
94+
draw.text((x, y), c, font=font, fill=255)
95+
# Increment x position based on chacacter width.
96+
char_width, char_height = draw.textsize(c, font=font)
97+
x += char_width
98+
99+
# Draw the image buffer.
100+
disp.image(image)
101+
disp.show()
102+
103+
# Move position for next frame.
104+
pos += velocity
105+
# Start over if text has scrolled completely off left side of screen.
106+
if pos < -maxwidth:
107+
pos = startpos
108+
109+
# Pause briefly before drawing next frame.
110+
time.sleep(0.05)

examples/pillow_images.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (c) 2014 Adafruit Industries
2+
# Author: Tony DiCola
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
22+
from board import SCL, SDA
23+
import busio
24+
from PIL import Image
25+
import adafruit_ssd1306
26+
27+
# Create the I2C interface.
28+
i2c = busio.I2C(SCL, SDA)
29+
30+
# Create the SSD1306 OLED class.
31+
# The first two parameters are the pixel width and pixel height. Change these
32+
# to the right size for your display!
33+
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
34+
35+
# Note you can change the I2C address, or add a reset pin:
36+
#disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c, reset=reset_pin)
37+
38+
# Clear display.
39+
disp.fill(0)
40+
disp.show()
41+
42+
43+
# Load image based on OLED display height. Note that image is converted to 1 bit color.
44+
if disp.height == 64:
45+
image = Image.open('happycat_oled_64.ppm').convert('1')
46+
else:
47+
image = Image.open('happycat_oled_32.ppm').convert('1')
48+
49+
# Alternatively load a different format image, resize it, and convert to 1 bit color.
50+
#image = Image.open('happycat.png').resize((disp.width, disp.height), Image.ANTIALIAS).convert('1')
51+
52+
# Display image.
53+
disp.image(image)
54+
disp.show()

0 commit comments

Comments
 (0)