|
| 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() |
0 commit comments