Always More to Learn! Always More to Do and Share!
The Home Page of Amateur Radio Operator W1AN

Raspberry Pi GPIO Selection of TTY Streams and Files and Art Pix

Up to 27 Choices using three 3 Position Toggle Switches

#!/usr/bin/python3
# /home/user/gpiotty27.py

# Works on Pi 3B,5
# Free to use and enjoy and modify, but at your own risk. 
# By W1AN
# Used with 'bookworm' 64bit with desktop on RazPi 5 and also works on Pi 3B. May work on others.
# Recommend using USB sound adapter cord.
# sudo apt install python3 but likely already installed
# sudo apt install mpg123 (audio player), sudo apt install minimodem for files.
# sudo apt remove python3-rpi-gpio (RazPi 5 only with new chipset), then:
# sudo apt install python3-rpi-lgpio for RazPi 5 with new chipset

# add ~/start-tty.sh to .bashrc in your user folder for start on boot without login
# add ./gpiotty27.sh to new start-tty.sh in user folder
# in your user folder create executable start-tty.sh with ./gpiotty27.py after sleep 5 

# A and C on SW1, D and F on SW2, G and I on SW3, etc., with ~300-470 ohm resistor on Common to Gnd. 
# Wire optically coupled relay to OutputA if needed.

import RPi.GPIO as GPIO
import time
import os
# set I/O IDs to use BCM
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# adjust BCM #s for where your switch inputs are connected and future relay output. 
# These are GPIO #s.that may be available. You can select your own.
PinA = 17
PinC = 27
PinD = 22
PinF = 10
PinG = 9
PinI = 11
# PinJ = 14
# PinL = 15

OutA = 19

# These are the choices based on physical switch positions.
# Middle of switch B, E, H is determined from != on same switch.

ADG = 0 # Selection 1
ADH = 0 # Selection 2
ADI = 0 # Selection 3
AEG = 0 # Selection 4
AEH = 0 # Selection 5
AEI = 0 # Selection 6
AFG = 0 # Selection 7
AFH = 0 # Selection 8
AFI = 0 # Selection 9
BDG = 0 # Selection 10
BDH = 0 # Selection 11
BDI = 0 # Selection 12
BEG = 0 # Selection 13
BEH = 0 # Selection 14
BEI = 0 # Selection 15
BFG = 0 # Selection 16
BFH = 0 # Selection 17
BFI = 0 # Selection 18
CDG = 0 # Selection 19
CDH = 0 # Selection 20
CDI = 0 # Selection 21
CEG = 0 # Selection 22
CEH = 0 # Selection 23
CEI = 0 # Selection 24
CFG = 0 # Selection 25
CFH = 0 # Selection 26
CFI = 0 # Selection 27
NR = 0 # For future use

# set as inputs _UP or_DOWN. Wire thru 470 ohm resistor to GND for each.

GPIO.setup(PinA,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(PinC,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(PinD,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(PinF,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(PinG,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(PinI,GPIO.IN,pull_up_down=GPIO.PUD_UP)
# GPIO.setup(PinJ,GPIO.IN,pull_up_down=GPIO.PUD_UP)
# GPIO.setup(PinL,GPIO.IN,pull_up_down=GPIO.PUD_UP)

A = GPIO.input(PinA)
C = GPIO.input(PinC)
D = GPIO.input(PinD)
F = GPIO.input(PinF)
G = GPIO.input(PinG)
I = GPIO.input(PinI)
# J = GPIO.input(PinJ)
# L = GPIO.input(PinL)

# set as outputs. You can use = 0 or 1
GPIO.setup(OutA,GPIO.OUT,initial=1)

while True:
# Assume the script to call is long enough to not require input debouncing
# There are up to 9 choices of using two 3 ON-OFF-ON switches. A,B,C and D,E,F
# There are up to 27 choices using three 3 ON-OFF-ON switches. A,B,C and D,E,F and G,H,I

# Made sure to switch off outputs/relays not needed.
# This prints switch positions:

    if A == 0 and D == 0 and G == 0: ADG = 1; print("***ADG Selection 1***"); NR = 1
    if A == 0 and D == 0 and G != 0 and I != 0: ADH = 1; print("***ADH Selection 2***"); NR = 2
    if A == 0 and D == 0 and I == 0: ADI = 1; print("***ADI Selection 3***"); NR = 3

    if A == 0 and D != 0 and F != 0 and G == 0: AEG = 1; print("***AEG Selection 4***"); NR = 4
    if A == 0 and D != 0 and F != 0 and G != 0 and I != 0: AEH = 1; print("***AEH Selection 5***"); NR = 5
    if A == 0 and D != 0 and F != 0 and I == 0: AEI = 1; print("***AEI Selection 6***"); NR = 6

    if A == 0 and F == 0 and G == 0: AFG = 1; print("***AFG Selection 7***"); NR = 7
    if A == 0 and F == 0 and G != 0 and I != 0: AFH = 1; print("***AFH Selection 8***"); NR = 8
    if A == 0 and F == 0 and I == 0: AFI = 1; print("***AFI Selection 9***"); NR = 9

    if A != 0 and C != 0 and D == 0 and G == 0: BDG = 1; print("***BDG Selection 10***"); NR = 10
    if A != 0 and C != 0 and D == 0 and G != 0 and I != 0: BDH = 1; print("***BDH Selection 11***"); NR = 11
    if A != 0 and C != 0 and D == 0 and I == 0: BDI = 1; print("***BDI Selection 12***"); NR = 12

    if A != 0 and C != 0 and D != 0 and F != 0 and G == 0: BEG = 1; print("***BEG Selection 13***"); NR = 13
    if A != 0 and C != 0 and D != 0 and F != 0 and G != 0 and I != 0: BEH = 1; print("***BEH Selection 14***"); NR = 14
    if A != 0 and C != 0 and D != 0 and F != 0 and I == 0: BEI = 1; print("***BEI Selection 15***"); NR = 15

    if A != 0 and C != 0 and F == 0 and G == 0: BFG = 1; print("***BFG Selection 16***"); NR = 16
    if A != 0 and C != 0 and F == 0 and G != 0 and I != 0: BFH = 1; print("***BFH Selection 17***"); NR = 17
    if A != 0 and C != 0 and F == 0 and I == 0: BFI = 1; print("***BFI Selection 18***"); NR = 18

    if C == 0 and D == 0 and G == 0: CDG = 1; print("***CDG Selection 19***"); NR = 19
    if C == 0 and D == 0 and G != 0 and I != 0: CDH = 1; print("***CDH Selection 20***"); NR = 20
    if C == 0 and D == 0 and I == 0: CDI = 1; print("***CDI Selection 21***"); NR = 21

    if C == 0 and D != 0 and F != 0 and G == 0: CEG = 1; print("***CEG Selection 22***"); NR = 22
    if C == 0 and D != 0 and F != 0 and G != 0 and I != 0: CEH = 1; print("***CEH Selection 23***"); NR = 23
    if C == 0 and D != 0 and F != 0 and I == 0: CEI = 1; print("***CEI Selection 24***"); NR = 24

    if C == 0 and F == 0 and G == 0: CFG = 1; print("***CFG Selection 25***"); NR = 25
    if C == 0 and F == 0 and G != 0 and I != 0: CFH = 1; print("***CFH Selection 26***"); NR = 26
    if C == 0 and F == 0 and I == 0: CFI = 1; print("***CFI Selection 27***"); NR = 27

# Relay A will activate relay on opening file. Add a relay if needed.

    if (ADG == 1):
        print("**** ITTY-EUROPE enabled **A-D-G******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Start-ITTY-EU.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (ADH == 1):
        print("**** TTY AUTOSTART enabled **A-D-H****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Autostart.sh");
        GPIO.output(OutA,GPIO.HIGH)

    if (ADI == 1):
        print("******* ITTY USA enabled **A-D-I******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Start-ITTY-US.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (AEG == 1):
        print("********* NEWS enabled **A-E-G********")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Start-News.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (AEH == 1):
        print("****** NEWS 850 enabled **A-E-H*******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Start-News850.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (AEI == 1):
        GPIO.output(OutA,GPIO.LOW)
        print("******** OLD NEWS enabled **A-E-I*****")
        os.system("/home/w1an/Start-OldNews.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (AFG == 1):
        print("******* Art 0 **A-F-G******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art0.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (AFH == 1):
        print("******* Art 1 **A-F-H******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art1.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (AFI == 1):
        print("******* ART 2 **A-F-I******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art2.sh")
        GPIO.output(OutA,GPIO.HIGH)  

    if (BDG == 1):
        print("******* ART 3 **B-D-G*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art3.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BDH == 1):
        print("******* ART 4 **B-D-H******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art4.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BDI == 1):
        print("******* ART 5 **B-D-I******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art5.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BEG == 1):
        print("******* ART 6 **B-E-G******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art6.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BEH == 1):
        print("******* ART 7 **B-E-H******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art7.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BEI == 1):
        print("******* ART 8 **B-E-I******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art8.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BFG == 1):
        print("******* ART 9 **B-F-G******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art9.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BFH == 1):
        print("******* ART 10 **B-F-H******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Art10.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (BFI == 1):
        print("******* ART 11 **C-F-I******")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/Ar11.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CDG == 1):
        print("**** Battleship NJ **C-D-G***")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/battleshipnj.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CDH == 1):
        print("***** Battleships **C-D-H****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/battleships.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CDI == 1):
        print("****** US Navy 1 **C-D-I*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/usnavy1.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CEG == 1):
        print("****** US Navy 2 **C-E-G*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/usnavy2.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CEH == 1):
        print("****** US Navy 3 **C-E-H*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/usnavy3.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CEI == 1):
        print("****** US Navy 4 **C-E-I*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/usnavy4.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CFG == 1):
        print("****** US Navy 4 **C-F-G*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/usnavy5.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CFH == 1):
        print("****** US Navy 5 **C-F-H*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/usnavy5.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CFI == 1):
        print("****** US Navy 6 **C-F-I*****")
        GPIO.output(OutA,GPIO.LOW)
        os.system("/home/w1an/usnavy6.sh")
        GPIO.output(OutA,GPIO.HIGH)

    if (CFH == 1 or CFI == 1):
        print("**** Shells not setup Yet ***")

    if (1 == 1):
        time.sleep (1)
        print("** No file or choice made ***")
        print("************ Exiting TTY  ************")
        GPIO.output(OutA,GPIO.HIGH)

        GPIO.cleanup()
# Cleanup only resets outputs back to inputs that are set in this code.
        print("********* GPIO Cleanup done **********")

        exit(0)

GPIO.cleanup()

os.exit(0)