ssd1306_pillow_ip.py
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( 21 fcntl.ioctl( 22 s.fileno(), 23 0x8915, # SIOCGIFADDR 24 struct.pack("256s", str.encode(ifname[:15])), 25 )[20:24] 26 ) 27 28 29 # Setting some variables for our reset pin etc. 30 RESET_PIN = digitalio.DigitalInOut(board.D4) 31 TEXT = "" 32 33 # Very important... This lets py-gaugette 'know' what pins to use in order to reset the display 34 i2c = board.I2C() 35 oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3D, reset=RESET_PIN) 36 37 # This sets TEXT equal to whatever your IP address is, or isn't 38 try: 39 TEXT = get_ip_address("wlan0") # WiFi address of WiFi adapter. NOT ETHERNET 40 except IOError: 41 try: 42 TEXT = get_ip_address("eth0") # WiFi address of Ethernet cable. NOT ADAPTER 43 except IOError: 44 TEXT = "NO INTERNET!" 45 46 # Clear display. 47 oled.fill(0) 48 oled.show() 49 50 # Create blank image for drawing. 51 image = Image.new("1", (oled.width, oled.height)) 52 draw = ImageDraw.Draw(image) 53 54 # Load a font in 2 different sizes. 55 font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 28) 56 font2 = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 14) 57 58 # Draw the text 59 intro = "Hello!" 60 ip = "Your IP Address is:" 61 draw.text((0, 46), TEXT, font=font2, fill=255) 62 draw.text((0, 0), intro, font=font, fill=255) 63 draw.text((0, 30), ip, font=font2, fill=255) 64 65 # Display image 66 oled.image(image) 67 oled.show()