Skip to content

Commit 5afb8b6

Browse files
committed
Ported examples from Raspberry Pi Guy Learn guide
1 parent d6f9d0d commit 5afb8b6

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

examples/ssd1306_pillow_clock.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# This example is for use on (Linux) computers that are using CPython with
2+
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
3+
# not support PIL/pillow (python imaging library)!
4+
#
5+
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
6+
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display
7+
8+
# Imports the necessary libraries...
9+
import time
10+
import board
11+
import digitalio
12+
from PIL import Image, ImageDraw, ImageFont
13+
import adafruit_ssd1306
14+
15+
# Setting some variables for our reset pin etc.
16+
RESET_PIN = digitalio.DigitalInOut(board.D4)
17+
18+
i2c = board.I2C()
19+
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)
20+
21+
# Clear display.
22+
oled.fill(0)
23+
oled.show()
24+
25+
# Create blank image for drawing.
26+
image = Image.new('1', (oled.width, oled.height))
27+
draw = ImageDraw.Draw(image)
28+
29+
# Load a font in 2 different sizes.
30+
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 16)
31+
font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24)
32+
33+
offset = 0 # flips between 0 and 32 for double buffering
34+
35+
while True:
36+
# write the current time to the display after each scroll
37+
draw.rectangle((0, 0, oled.width, oled.height * 2), outline=0, fill=0)
38+
text = time.strftime("%A")
39+
draw.text((0, 0), text, font=font, fill=255)
40+
text = time.strftime("%e %b %Y")
41+
draw.text((0, 14), text, font=font, fill=255)
42+
text = time.strftime("%X")
43+
draw.text((0, 36), text, font=font2, fill=255)
44+
oled.image(image)
45+
oled.show()
46+
47+
time.sleep(1)
48+
49+
for i in range(0, oled.height//2):
50+
offset = (offset + 1) % oled.height
51+
oled.write_cmd(adafruit_ssd1306.SET_DISP_START_LINE | offset)
52+
oled.show()
53+
time.sleep(0.001)

examples/ssd1306_pillow_demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""
22
This demo will fill the screen with white, draw a black box on top
33
and then print Hello World! in the center of the display
4+
5+
This example is for use on (Linux) computers that are using CPython with
6+
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
7+
not support PIL/pillow (python imaging library)!
48
"""
59

610
import board
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This example is for use on (Linux) computers that are using CPython with
2+
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
3+
# not support PIL/pillow (python imaging library)!
4+
#
5+
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
6+
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display
7+
8+
# Imports the necessary libraries...
9+
import sys
10+
import board
11+
import digitalio
12+
from PIL import Image
13+
import adafruit_ssd1306
14+
15+
# Setting some variables for our reset pin etc.
16+
RESET_PIN = digitalio.DigitalInOut(board.D4)
17+
18+
i2c = board.I2C()
19+
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)
20+
21+
# Clear display.
22+
oled.fill(0)
23+
oled.show()
24+
25+
# Open, resize, and convert image to Black and White
26+
image = Image.open(sys.argv[1]).resize((oled.width, oled.height), Image.BICUBIC).convert('1')
27+
28+
# Display the converted image
29+
oled.image(image)
30+
oled.show()

examples/ssd1306_pillow_ip.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This example is for use on (Linux) computers that are using CPython with
2+
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
3+
# not support PIL/pillow (python imaging library)!
4+
#
5+
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
6+
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display
7+
8+
# Imports the necessary libraries...
9+
import socket
10+
import fcntl
11+
import struct
12+
import board
13+
import digitalio
14+
from PIL import Image, ImageDraw, ImageFont
15+
import adafruit_ssd1306
16+
17+
# This function allows us to grab any of our IP addresses
18+
def get_ip_address(ifname):
19+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
20+
return socket.inet_ntoa(fcntl.ioctl(
21+
s.fileno(),
22+
0x8915, # SIOCGIFADDR
23+
struct.pack('256s', str.encode(ifname[:15]))
24+
)[20:24])
25+
26+
# Setting some variables for our reset pin etc.
27+
RESET_PIN = digitalio.DigitalInOut(board.D4)
28+
TEXT = ''
29+
30+
# Very important... This lets py-gaugette 'know' what pins to use in order to reset the display
31+
i2c = board.I2C()
32+
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)
33+
34+
# This sets TEXT equal to whatever your IP address is, or isn't
35+
try:
36+
TEXT = get_ip_address('wlan0') # WiFi address of WiFi adapter. NOT ETHERNET
37+
except IOError:
38+
try:
39+
TEXT = get_ip_address('eth0') # WiFi address of Ethernet cable. NOT ADAPTER
40+
except IOError:
41+
TEXT = ('NO INTERNET!')
42+
43+
# Clear display.
44+
oled.fill(0)
45+
oled.show()
46+
47+
# Create blank image for drawing.
48+
image = Image.new('1', (oled.width, oled.height))
49+
draw = ImageDraw.Draw(image)
50+
51+
# Load a font in 2 different sizes.
52+
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28)
53+
font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 14)
54+
55+
# Draw the text
56+
intro = 'Hello!'
57+
ip = 'Your IP Address is:'
58+
draw.text((0, 46), TEXT, font=font2, fill=255)
59+
draw.text((0, 0), intro, font=font, fill=255)
60+
draw.text((0, 30), ip, font=font2, fill=255)
61+
62+
# Display image
63+
oled.image(image)
64+
oled.show()

examples/ssd1306_pillow_text.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This example is for use on (Linux) computers that are using CPython with
2+
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
3+
# not support PIL/pillow (python imaging library)!
4+
#
5+
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
6+
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display
7+
8+
# Imports the necessary libraries...
9+
import board
10+
import digitalio
11+
from PIL import Image, ImageDraw, ImageFont
12+
import adafruit_ssd1306
13+
14+
# Setting some variables for our reset pin etc.
15+
RESET_PIN = digitalio.DigitalInOut(board.D4)
16+
17+
# Very important... This lets py-gaugette 'know' what pins to use in order to reset the display
18+
i2c = board.I2C()
19+
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)
20+
21+
# Clear display.
22+
oled.fill(0)
23+
oled.show()
24+
25+
# Create blank image for drawing.
26+
image = Image.new('1', (oled.width, oled.height))
27+
draw = ImageDraw.Draw(image)
28+
29+
# Load a font in 2 different sizes.
30+
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28)
31+
font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 14)
32+
33+
# Draw the text
34+
draw.text((0, 0), 'Hello!', font=font, fill=255)
35+
draw.text((0, 30), 'Hello!', font=font2, fill=255)
36+
draw.text((34, 46), 'Hello!', font=font2, fill=255)
37+
38+
# Display image
39+
oled.image(image)
40+
oled.show()

0 commit comments

Comments
 (0)