Andreea Diana CrutIoana Anda Hancu
Published

Press To Switch

The design consists of displaying different screens by pressing the attached buttons, both forward and backwards.

BeginnerFull instructions provided406
Press To Switch

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
0.96" OLED 64x128 Display Module
ElectroPeak 0.96" OLED 64x128 Display Module
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×2
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic

Code

Code

Python
import RPi.GPIO as GPIO
from PIL import Image,ImageFont
from datetime import datetime
from luma.core.render import canvas
from luma.core.interface.serial import i2c
from luma.core.virtual import terminal
from luma.oled.device import sh1106
import re,os,time

GPIO.setmode(GPIO.BCM)
GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(16,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

display=-1
def display_time():
    today_last_time = "Unknown"
    now = datetime.now().time()
    today_date = now.strftime("%d %b %Y")
    today_time = now.strftime("%H:%M:%S")
    if today_time != today_last_time:
        today_last_time = today_time
        with canvas(device) as draw:
            now = datetime.now()
            today_date = now.strftime("%d %b %Y")
            font=ImageFont.truetype('DejaVuSerif-Bold.ttf',24)
            x_pos=(device.width/2)-58
            y_pos=2+(device.height-12)/2-(35/2)
            draw.text((x_pos,y_pos),today_time,font=font,fill=255)
            x_pos=(device.width/2)-33
            y_pos=device.height-10
            draw.text((x_pos,y_pos),today_date,fill=255)
    time.sleep(0.1)


def welcome():
    img_path = os.path.abspath(os.path.join(os.path.dirname(__file__),'pi_logo.png'))
    logo = Image.open(img_path).convert("RGBA")
    fff = Image.new(logo.mode, logo.size, (255,) * 4)

    background = Image.new("RGBA", device.size, "white")
    posn = ((device.width - logo.width) // 2, 0)
    
    background.paste(logo, posn)
    device.display(background.convert(device.mode))
 

def prezentare():
    term=terminal(device)
    term.println("Proiect SM")
    term.println("----------")
    term.println("Hancu Anda-Ioana")
    term.println("Crut Andreea-Diana")
    
    
def display_network():
    ipaddress=os.popen("ifconfig wlan0 | grep 'inet ' | awk -F: '{print $1}'").read()
    ipaddress=re.sub(' +',' ',ipaddress)
    ipaddress=ipaddress.split(' ')
    inet="IP:"+" "+ipaddress[2]
    subnet="NM:"+" "+ipaddress[4]
    broadcast="BC:"+" "+ipaddress[6]

    with canvas(device) as draw:
        draw.text((0,0),inet,fill=255)
        draw.text((0,20),subnet,fill=255)
        draw.text((0,40),broadcast,fill=255)
        
serial=i2c(port=1,address=0x3C)
device=sh1106(serial,rotate=0)
device.clear()

while True:
    if(GPIO.input(12)):
        display+=1
        if(display>4):
            display=0
    elif(GPIO.input(16)):
        display-=1
    if(display==0):
        display+=1
        prezentare()
    elif(display ==1):
        welcome()
    elif(display==2):
        display_time()
    elif(display==3):
        display_network()
    time.sleep(0.1)

Credits

Andreea Diana Crut

Andreea Diana Crut

1 project • 0 followers
Ioana Anda Hancu

Ioana Anda Hancu

1 project • 0 followers

Comments

Add projectSign up / Login