From 5afb8b6ec1c3e9b40ccae403aa2428cef5c93bf3 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Thu, 26 Sep 2019 12:41:13 -0700 Subject: [PATCH] Ported examples from Raspberry Pi Guy Learn guide --- examples/ssd1306_pillow_clock.py | 53 ++++++++++++++++++++ examples/ssd1306_pillow_demo.py | 4 ++ examples/ssd1306_pillow_image_display.py | 30 +++++++++++ examples/ssd1306_pillow_ip.py | 64 ++++++++++++++++++++++++ examples/ssd1306_pillow_text.py | 40 +++++++++++++++ 5 files changed, 191 insertions(+) create mode 100644 examples/ssd1306_pillow_clock.py create mode 100644 examples/ssd1306_pillow_image_display.py create mode 100644 examples/ssd1306_pillow_ip.py create mode 100644 examples/ssd1306_pillow_text.py diff --git a/examples/ssd1306_pillow_clock.py b/examples/ssd1306_pillow_clock.py new file mode 100644 index 0000000..af0def2 --- /dev/null +++ b/examples/ssd1306_pillow_clock.py @@ -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) diff --git a/examples/ssd1306_pillow_demo.py b/examples/ssd1306_pillow_demo.py index c0df7d2..302fc59 100644 --- a/examples/ssd1306_pillow_demo.py +++ b/examples/ssd1306_pillow_demo.py @@ -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 diff --git a/examples/ssd1306_pillow_image_display.py b/examples/ssd1306_pillow_image_display.py new file mode 100644 index 0000000..a613f95 --- /dev/null +++ b/examples/ssd1306_pillow_image_display.py @@ -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() diff --git a/examples/ssd1306_pillow_ip.py b/examples/ssd1306_pillow_ip.py new file mode 100644 index 0000000..be6ce85 --- /dev/null +++ b/examples/ssd1306_pillow_ip.py @@ -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() diff --git a/examples/ssd1306_pillow_text.py b/examples/ssd1306_pillow_text.py new file mode 100644 index 0000000..7e455a4 --- /dev/null +++ b/examples/ssd1306_pillow_text.py @@ -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()