Refactor GPIO handling with output and input functions

Refactor GPIO handling by creating output and input functions. Replace direct GPIO calls with these functions for better abstraction and error handling.
master-b-gpio
Alan Johnston 1 month ago committed by GitHub
parent 0597f66c16
commit ad3a12e4a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import RPi.GPIO as GPIO #import RPi.GPIO as GPIO
from RPi.GPIO import output #from RPi.GPIO import output
#import subprocess #import subprocess
import time import time
from time import sleep from time import sleep
@ -14,6 +14,41 @@ import serial
import random import random
import subprocess import subprocess
def output(pin, value):
command = "gpio -g write " + str(pin) + " " + str(value)
system(command)
def input(pin):
# command = "gpio -g read " + str(pin)
query = ["gpio", "-g", "read", str(pin)] # Read GPIO pin
try:
result = subprocess.run(query, capture_output=True, text=True, check=True)
print(f"Command run was: {query}")
print("Sucess!")
print(f"Output of the command (stdout): {result}")
return result
except subprocess.CalledProcessError as e:
print(f"Command failed with return code: {e.returncode}")
print(f"Command run was: {e.cmd}")
print(f"Output of the command (stdout): {e.stdout}")
print(f"Error output of the command (stderr): {e.stderr}")
return 0
def setup(pin, config):
if config == "in" or config == "out" or config == "up" or config == "down":
command = "gpio -g mode " + str(powerPin) + " " + config
system(command)
else
print(f"Unknown GPIO setup configuration: {config}")
def blink(times):
powerPin = 16
for i in range(times):
system("gpio -g write " + str(powerPin) + " 0") # blink two times
sleep(0.1)
system("gpio -g write " + str(powerPin) + " 1")
sleep(0.1)
def sim_failure_check(): def sim_failure_check():
try: try:
global card global card
@ -46,7 +81,6 @@ def sim_failure_check():
except: except:
print("No failure mode") print("No failure mode")
card = "Headphones" card = "Headphones"
# card = "Device"
if sim_config: if sim_config:
sim_mode = True sim_mode = True
@ -65,9 +99,9 @@ def battery_saver_check():
def blink(times): def blink(times):
powerPin = 16 powerPin = 16
for i in range(times): for i in range(times):
system("gpio -g write " + str(powerPin) + " 0") # blink two times output(powerPin, 0) # blink
sleep(0.1) sleep(0.1)
system("gpio -g write " + str(powerPin) + " 1") output(powerPin, 1)
sleep(0.1) sleep(0.1)
def increment_mode(): def increment_mode():
@ -114,13 +148,13 @@ def increment_mode():
file.close() file.close()
print(".mode file written") print(".mode file written")
GPIO.setwarnings(False) # GPIO.setwarnings(False)
system("gpio -g write " + str(txLed) + " 0") output(txLed, 0)
system("gpio -g write " + str(powerPin) + " 0") output(powerPin, 0)
print("sudo reboot -h now") print("sudo reboot -h now")
GPIO.setwarnings(False) # GPIO.setwarnings(False)
system("gpio -g mode " + str(powerPin) + " out") setup(powerPin, "out")
system("gpio -g write " + str(powerPin) + " 0"); output(powerPin, 0);
# system("reboot -h now") # system("reboot -h now")
# release = True; # release = True;
@ -193,70 +227,54 @@ powerPin = 16
command_tx = True command_tx = True
try: GPIO.setmode(GPIO.BCM)
system("gpio -g write " + str(powerPin) + " 1") GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) setup(13, "up")
GPIO.setwarnings(False) setup(12, "up")
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) setup(27, "up")
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) setup(txc_pin, "up")
GPIO.setup(txc_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) setup(green, "out")
GPIO.setup(green, GPIO.OUT) output(powerPin, 1)
GPIO.setup(27, GPIO.IN)
except:
print("gpio setup problem")
system("gpio -g mode 27 IN")
try:
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
except:
print("Can't set txLed")
transmit = False transmit = False
txLed = 27 txLed = 27
# txLed = 17 1 = 1
txLedOn = 1 0 = 0
txLedOff = 0 if input(12) == False:
if GPIO.input(12) == False:
print("LPF present") print("LPF present")
transmit = True transmit = True
else: else:
print("No LPF") print("No LPF")
# system("gpio -g mode " + str(txLed) + " out") # GPIO.setup(txLed, GPIO.OUT)
# system("gpio -g write " + str(txLed) + " 0") # output(txLed, 0)
GPIO.setmode(GPIO.BCM) # Repeat to make LED work on Pi 4 # GPIO.setmode(GPIO.BCM) # Repeat to make LED work on Pi 4
GPIO.setwarnings(False) # GPIO.setwarnings(False)
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out")
except:
print("Can't set txLed")
GPIO.setup(pd, GPIO.OUT) setup(pd, "out")
#output(pd, 1) #output(pd, 1)
output(pd, 0) output(pd, 0)
GPIO.setup(ptt, GPIO.OUT) setup(ptt, "out")
output (ptt, 1) output (ptt, 1)
txc = False txc = False
if GPIO.input(txc_pin) == False: if input(txc_pin) == False:
print("TXC is present") print("TXC is present")
txc = True; txc = True;
else: else:
print("TXC not present") print("TXC not present")
# txc = False # forcing it off # txc = False # forcing it off
output(txLed, 1)
try: sleep(1)
system("gpio -g write " + str(txLed) + " 1") output(txLed, 0)
sleep(1)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
battery_saver_check() battery_saver_check()
# print(txLedOn) # print(1)
print(txLed) print(txLed)
# GPIO.setup(27, GPIO.OUT) # GPIO.setup(27, GPIO.OUT)
# GPIO.output(27, 0) # GPIO.output(27, 0)
@ -440,10 +458,10 @@ if __name__ == "__main__":
try: try:
f = open("/home/pi/CubeSatSim/command_control", "r") f = open("/home/pi/CubeSatSim/command_control", "r")
f.close() f.close()
GPIO.setmode(GPIO.BCM) # GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) # GPIO.setwarnings(False)
GPIO.setup(squelch, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## pull up in case pin is not connected setup(squelch, "up") ## pull up in case pin is not connected
if GPIO.input(squelch) == False: if input(squelch) == False:
print("squelch not set correctly, no command input!") print("squelch not set correctly, no command input!")
else: else:
if (mode != 'n') and (mode != 'x'): if (mode != 'n') and (mode != 'x'):
@ -458,34 +476,13 @@ if __name__ == "__main__":
print(callsign) print(callsign)
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
try: # print(txLed)
print(txLed) # print(1)
print(txLedOn) setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out")
except:
print("Can't set txLed")
# card = "Headphones" # default using pcm audio output of Pi Zero card = "Headphones" # default using pcm audio output of Pi Zero
card = "Device" # using USB sound card for audio output # card = "Device" # using USB sound card for audio output
query = ["grep", "VERSION_CODENAME=bullseye", "/etc/os-release"]
try:
result = subprocess.run(query, capture_output=True, text=True, check=True)
print(f"Command run was: {query}")
os_status = result.stdout.strip()
print(f"Output of the command (stdout): {os_status}")
except subprocess.CalledProcessError as e:
# print(f"Command failed with return code: {e.returncode}")
print(f"Command run was: {e.cmd}")
os_status = e.stdout.strip()
print(f"Output of the command (stdout): {e.stdout}")
# print(f"Error output of the command (stderr): {e.stderr}")
if os_status != "VERSION_CODENAME=bullseye":
os_status = "bookworm"
else:
os_status = "bullseye"
print (os_status)
query = ["sudo", "systemctl", "is-active", "gpsd.socket"] query = ["sudo", "systemctl", "is-active", "gpsd.socket"]
try: try:
@ -532,12 +529,9 @@ if __name__ == "__main__":
print("Don't transmit CW ID since APRS HAB mode is active") print("Don't transmit CW ID since APRS HAB mode is active")
else: else:
if (((mode == 'a') or (mode == 'b') or (mode == 'f') or (mode == 's') or (mode == 'j')) and (command_tx == True) and (skip == False)) or ((mode == 'e') and (command_tx == True)): # battery_saver_mode if (((mode == 'a') or (mode == 'b') or (mode == 'f') or (mode == 's') or (mode == 'j')) and (command_tx == True) and (skip == False)) or ((mode == 'e') and (command_tx == True)): # battery_saver_mode
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
print("Transmit CW ID") print("Transmit CW ID")
status = "" status = ""
if not no_command: if not no_command:
@ -548,10 +542,7 @@ if __name__ == "__main__":
system("echo 'hi hi de " + callsign + status + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3") system("echo 'hi hi de " + callsign + status + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3")
else: else:
system("echo 'hi hi de " + callsign + status + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1") system("echo 'hi hi de " + callsign + status + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
try: output(txLed, 0)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
sleep(1) sleep(1)
else: else:
@ -570,7 +561,7 @@ if __name__ == "__main__":
if (mode == 'a'): if (mode == 'a'):
print("AFSK") print("AFSK")
else: else:
# system("gpio -g write " + str(powerPin) + " 0") # GPIO.output(powerPin, 0)
print("Transmit APRS Commands") print("Transmit APRS Commands")
system("sudo systemctl stop command") system("sudo systemctl stop command")
# while True: # while True:
@ -587,12 +578,9 @@ if __name__ == "__main__":
system("gen_packets -o /home/pi/CubeSatSim/telem.wav /home/pi/CubeSatSim/t.txt -r 48000 > /dev/null 2>&1") system("gen_packets -o /home/pi/CubeSatSim/telem.wav /home/pi/CubeSatSim/t.txt -r 48000 > /dev/null 2>&1")
system("cat /home/pi/CubeSatSim/t.txt") system("cat /home/pi/CubeSatSim/t.txt")
if (command_tx == True): if (command_tx == True):
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# output(pd, 1) # output(pd, 1)
# output (ptt, 0) # output (ptt, 0)
# sleep(.1) # sleep(.1)
@ -618,10 +606,7 @@ if __name__ == "__main__":
sleep(0.1) sleep(0.1)
# output (ptt, 1) # output (ptt, 1)
# output(pd, 0) # output(pd, 0)
try: output(txLed, 0)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
system("sudo rm /home/pi/CubeSatSim/ready") system("sudo rm /home/pi/CubeSatSim/ready")
f.close() f.close()
@ -651,12 +636,9 @@ if __name__ == "__main__":
system(command) system(command)
## chan = chan + 1 ## chan = chan + 1
if (command_tx == True): if (command_tx == True):
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
system("gpio -g mode " + str(txLed) + " out") setup(txLed, "out")
try: output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
if (txc): if (txc):
sim_failure_check() sim_failure_check()
@ -672,10 +654,7 @@ if __name__ == "__main__":
system("cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3") system("cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3")
else: else:
system("cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1") system("cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
try: output(txLed, 0)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
# command_control_check() # command_control_check()
sleep(2) sleep(2)
@ -689,20 +668,27 @@ if __name__ == "__main__":
# command_control_check() # command_control_check()
output (ptt, 1) output (ptt, 1)
output(pd, 1) output(pd, 1)
try:
print("Testing for camera") # from picamera import PiCamera
if camera_photo(): # from pysstv.sstv import SSTV
# camera = PiCamera()
print("Testing for camera")
if os_status == "bookworm":
system("rpicam-still -o /home/pi/CubeSatSim/camera_out.jpg --width 320 --height 256") # > /dev/null 2>&1")
else:
system("raspistill -o /home/pi/CubeSatSim/camera_out.jpg -w 320 -h 256")
f = open("/home/pi/CubeSatSim/camera_out.jpg")
f.close()
print("Camera present")
camera_present = 1 camera_present = 1
print("camera present") # camera.close()
else: except:
camera_present = 0
print("No camera available") print("No camera available")
print(" -> if camera plugged in, is software enabled?") print(" -> if camera plugged in, is software enabled?")
camera_present = 0
try: # while 1:
system("gpio -g write " + str(txLed) + " 0") output(txLed, 0)
except:
print("Can't set txLed")
# output (ptt, 1) # output (ptt, 1)
# output(pd, 0) # output(pd, 0)
if (camera_present == 1): if (camera_present == 1):
@ -714,12 +700,9 @@ if __name__ == "__main__":
if (command_tx == True): if (command_tx == True):
print ("Sending SSTV image") print ("Sending SSTV image")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# battery_saver_check() # battery_saver_check()
if (txc): if (txc):
@ -735,10 +718,7 @@ if __name__ == "__main__":
else: else:
system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1") system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
try: output(txLed, 0)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
# sleep(1) # sleep(1)
except: except:
@ -753,12 +733,9 @@ if __name__ == "__main__":
if (command_tx == True): if (command_tx == True):
print ("Sending SSTV image") print ("Sending SSTV image")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# battery_saver_check() # battery_saver_check()
if (txc): if (txc):
@ -774,10 +751,8 @@ if __name__ == "__main__":
system("cat /home/pi/CubeSatSim/camera_out.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3") system("cat /home/pi/CubeSatSim/camera_out.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3")
else: else:
system("cat /home/pi/CubeSatSim/camera_out.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1") system("cat /home/pi/CubeSatSim/camera_out.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
try:
system("gpio -g write " + str(txLed) + " 0") output(txLed, 0)
except:
print("Can't set txLed")
# output (ptt, 1) # output (ptt, 1)
# output(pd, 0) # output(pd, 0)
@ -796,12 +771,9 @@ if __name__ == "__main__":
if (command_tx == True): if (command_tx == True):
print ("Sending SSTV image") print ("Sending SSTV image")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# battery_saver_check() # battery_saver_check()
@ -817,10 +789,8 @@ if __name__ == "__main__":
system("cat /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3") system("cat /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3")
else: else:
system("cat /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1") system("cat /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
try:
system("gpio -g write " + str(txLed) + " 0") output(txLed, 0)
except:
print("Can't set txLed")
# output (ptt, 1) # output (ptt, 1)
# output(pd, 0) # output(pd, 0)
sleep(1) sleep(1)
@ -839,12 +809,9 @@ if __name__ == "__main__":
if (command_tx == True): if (command_tx == True):
print ("Sending SSTV image") print ("Sending SSTV image")
try: # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# battery_saver_check() # battery_saver_check()
if (txc): if (txc):
@ -859,10 +826,8 @@ if __name__ == "__main__":
system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3") system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3")
else: else:
system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1") system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
try:
system("gpio -g write " + str(txLed) + " 0") output(txLed, 0)
except:
print("Can't set txLed")
# output (ptt, 1) # output (ptt, 1)
# output(pd, 0) # output(pd, 0)
sleep(10) sleep(10)
@ -875,12 +840,9 @@ if __name__ == "__main__":
if (command_tx == True): if (command_tx == True):
# command_control_check() # command_control_check()
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# battery_saver_check() # battery_saver_check()
@ -893,10 +855,8 @@ if __name__ == "__main__":
# output (pd, 0) # output (pd, 0)
else: else:
sleep(60) sleep(60)
try:
system("gpio -g write " + str(txLed) + " 0") output(txLed, 0)
except:
print("Can't set txLed")
# output (ptt, 1) # output (ptt, 1)
# output(pd, 0) # output(pd, 0)
sleep(10) sleep(10)
@ -910,11 +870,9 @@ if __name__ == "__main__":
print("turn on FM rx") print("turn on FM rx")
output(pd, 1) output(pd, 1)
output(ptt, 1) output(ptt, 1)
try:
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
system("gpio -g mode " + str(txLed) + " out") setup(txLed, "out")
except:
print("Can't set txLed")
if (command_tx == True): if (command_tx == True):
# system("sudo nc -l 8080 | csdr convert_i16_f | csdr fir_interpolate_cc 2 | csdr dsb_fc | csdr bandpass_fir_fft_cc 0.002 0.06 0.01 | csdr fastagc_ff | sudo /home/pi/rpitx/sendiq -i /dev/stdin -s 96000 -f 434.9e6 -t float &") # system("sudo nc -l 8080 | csdr convert_i16_f | csdr fir_interpolate_cc 2 | csdr dsb_fc | csdr bandpass_fir_fft_cc 0.002 0.06 0.01 | csdr fastagc_ff | sudo /home/pi/rpitx/sendiq -i /dev/stdin -s 96000 -f 434.9e6 -t float &")
@ -924,26 +882,20 @@ if __name__ == "__main__":
print("Initial image_id: " + str(image_id) + "\n") print("Initial image_id: " + str(image_id) + "\n")
while 1: while 1:
# print ("LED on") # print ("LED on")
try: output(txLed, 0)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
sleep(0.4) sleep(0.4)
# if (command_tx == False): # if (command_tx == False):
# system("gpio -g write " + str(txLed) + " 1") # output(txLed, 1)
# sleep(0.03) # sleep(0.03)
# system("gpio -g write " + str(txLed) + " 0") # output(txLed, 0)
# command_control_check() # command_control_check()
if (command_tx == True): if (command_tx == True):
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# print(txLed) # print(txLed)
# print(txLedOn) # print(1)
if (mode == 'b'): if (mode == 'b'):
sleep(4.2) sleep(4.2)
@ -979,14 +931,11 @@ if __name__ == "__main__":
print("turn on FM rx") print("turn on FM rx")
output(pd, 1) output(pd, 1)
output(ptt, 1) output(ptt, 1)
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") # GPIO.setup(powerPin, GPIO.OUT)
except: setup(squelch, "up") ## pull up in case pin is not connected
print("Can't set txLed") # GPIO.output(powerPin, 1) # was 0
# system("gpio -g mode " + str(powerPin) + " out")
GPIO.setup(squelch, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## pull up in case pin is not connected
# system("gpio -g write " + str(powerPin) + " 1") # was 0
# txf = float(tx) - 288.9 # txf = float(tx) - 288.9
# print("Transmit frequency: ",txf) # print("Transmit frequency: ",txf)
if (command_tx != True): if (command_tx != True):
@ -994,24 +943,18 @@ if __name__ == "__main__":
print("Ready to detect carrier") print("Ready to detect carrier")
while True: while True:
if (GPIO.input(squelch) == False) and (command_tx == True): if (input(squelch) == False) and (command_tx == True):
print("Carrier detected, starting repeater") print("Carrier detected, starting repeater")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
try: setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
system("sudo nc -l 8011 | csdr convert_i16_f | csdr gain_ff 16000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1 &") system("sudo nc -l 8011 | csdr convert_i16_f | csdr gain_ff 16000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1 &")
sleep(0.5) sleep(0.5)
system("sudo arecord -D shared_mic -r48000 -fS16_LE -c1 | nc localhost 8011 &") system("sudo arecord -D shared_mic -r48000 -fS16_LE -c1 | nc localhost 8011 &")
while (GPIO.input(squelch) == False): while (input(squelch) == False):
sleep(1) sleep(1)
print("No carrier detected, stopping repeater") print("No carrier detected, stopping repeater")
try: output(txLed, 0)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
system("sudo rpitx -i null > /dev/null 2>&1") system("sudo rpitx -i null > /dev/null 2>&1")
system("sudo killall -9 arecord > /dev/null 2>&1") system("sudo killall -9 arecord > /dev/null 2>&1")
system("sudo killall -9 nc > /dev/null 2>&1") system("sudo killall -9 nc > /dev/null 2>&1")
@ -1027,35 +970,26 @@ if __name__ == "__main__":
output(pd, 1) output(pd, 1)
output(ptt, 1) output(ptt, 1)
try: # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4 setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out")
except:
print("Can't set txLed")
if (command_tx == True): if (command_tx == True):
system("sudo nc -l 8080 | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 &") system("sudo nc -l 8080 | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 &")
print("Turning LED on/off and listening for carrier") print("Turning LED on/off and listening for carrier")
while 1: while 1:
try: output(txLed, 0)
system("gpio -g write " + str(txLed) + " 0")
except:
print("Can't set txLed")
sleep(0.4) sleep(0.4)
# if (command_tx == False): # if (command_tx == False):
# system("gpio -g write " + str(txLed) + " 1") # output(txLed, 1)
# sleep(0.03) # sleep(0.03)
# system("gpio -g write " + str(txLed) + " 0") # output(txLed, 0)
# command_control_check() # command_control_check()
if (command_tx == True): if (command_tx == True):
try: # GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4 setup(txLed, "out")
system("gpio -g mode " + str(txLed) + " out") output(txLed, 1)
system("gpio -g write " + str(txLed) + " 1")
except:
print("Can't set txLed")
# print(txLed) # print(txLed)
# print(txLedOn) # print(1)
sleep(4.2) sleep(4.2)
else: else:
print("No Low Pass Filter so no telemetry transmit. See http://cubesatsim.org/wiki for instructions on how to build the LPF.") print("No Low Pass Filter so no telemetry transmit. See http://cubesatsim.org/wiki for instructions on how to build the LPF.")

Loading…
Cancel
Save

Powered by TurnKey Linux.