Skip to content

Ported examples from Raspberry Pi Guy Learn guide #32

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 1 commit into from
Sep 26, 2019
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
53 changes: 53 additions & 0 deletions examples/ssd1306_pillow_clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This example is for use on (Linux) computers that are using CPython with
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)!
#
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display

# Imports the necessary libraries...
import time
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

# Setting some variables for our reset pin etc.
RESET_PIN = digitalio.DigitalInOut(board.D4)

i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)

# Load a font in 2 different sizes.
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 16)
font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24)

offset = 0 # flips between 0 and 32 for double buffering

while True:
# write the current time to the display after each scroll
draw.rectangle((0, 0, oled.width, oled.height * 2), outline=0, fill=0)
text = time.strftime("%A")
draw.text((0, 0), text, font=font, fill=255)
text = time.strftime("%e %b %Y")
draw.text((0, 14), text, font=font, fill=255)
text = time.strftime("%X")
draw.text((0, 36), text, font=font2, fill=255)
oled.image(image)
oled.show()

time.sleep(1)

for i in range(0, oled.height//2):
offset = (offset + 1) % oled.height
oled.write_cmd(adafruit_ssd1306.SET_DISP_START_LINE | offset)
oled.show()
time.sleep(0.001)
4 changes: 4 additions & 0 deletions examples/ssd1306_pillow_demo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""
This demo will fill the screen with white, draw a black box on top
and then print Hello World! in the center of the display

This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
"""

import board
Expand Down
30 changes: 30 additions & 0 deletions examples/ssd1306_pillow_image_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This example is for use on (Linux) computers that are using CPython with
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)!
#
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display

# Imports the necessary libraries...
import sys
import board
import digitalio
from PIL import Image
import adafruit_ssd1306

# Setting some variables for our reset pin etc.
RESET_PIN = digitalio.DigitalInOut(board.D4)

i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)

# Clear display.
oled.fill(0)
oled.show()

# Open, resize, and convert image to Black and White
image = Image.open(sys.argv[1]).resize((oled.width, oled.height), Image.BICUBIC).convert('1')

# Display the converted image
oled.image(image)
oled.show()
64 changes: 64 additions & 0 deletions examples/ssd1306_pillow_ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This example is for use on (Linux) computers that are using CPython with
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)!
#
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display

# Imports the necessary libraries...
import socket
import fcntl
import struct
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

# This function allows us to grab any of our IP addresses
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', str.encode(ifname[:15]))
)[20:24])

# Setting some variables for our reset pin etc.
RESET_PIN = digitalio.DigitalInOut(board.D4)
TEXT = ''

# Very important... This lets py-gaugette 'know' what pins to use in order to reset the display
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)

# This sets TEXT equal to whatever your IP address is, or isn't
try:
TEXT = get_ip_address('wlan0') # WiFi address of WiFi adapter. NOT ETHERNET
except IOError:
try:
TEXT = get_ip_address('eth0') # WiFi address of Ethernet cable. NOT ADAPTER
except IOError:
TEXT = ('NO INTERNET!')

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)

# Load a font in 2 different sizes.
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28)
font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 14)

# Draw the text
intro = 'Hello!'
ip = 'Your IP Address is:'
draw.text((0, 46), TEXT, font=font2, fill=255)
draw.text((0, 0), intro, font=font, fill=255)
draw.text((0, 30), ip, font=font2, fill=255)

# Display image
oled.image(image)
oled.show()
40 changes: 40 additions & 0 deletions examples/ssd1306_pillow_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This example is for use on (Linux) computers that are using CPython with
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)!
#
# Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
# https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display

# Imports the necessary libraries...
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

# Setting some variables for our reset pin etc.
RESET_PIN = digitalio.DigitalInOut(board.D4)

# Very important... This lets py-gaugette 'know' what pins to use in order to reset the display
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d, reset=RESET_PIN)

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)

# Load a font in 2 different sizes.
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28)
font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 14)

# Draw the text
draw.text((0, 0), 'Hello!', font=font, fill=255)
draw.text((0, 30), 'Hello!', font=font2, fill=255)
draw.text((34, 46), 'Hello!', font=font2, fill=255)

# Display image
oled.image(image)
oled.show()