From f998c59bc97fdb9743529cb36bb3bab9f62f9ce2 Mon Sep 17 00:00:00 2001 From: Alan Johnston Date: Tue, 28 Jul 2026 09:31:52 -0400 Subject: [PATCH] Create pushbutton.py from pi-power-button --- pushbutton.py | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 pushbutton.py diff --git a/pushbutton.py b/pushbutton.py new file mode 100644 index 00000000..3999dc3b --- /dev/null +++ b/pushbutton.py @@ -0,0 +1,156 @@ +#!/usr/bin/env python + +# originally based on listen-for-shutdown.py from pi-power-button + +import RPi.GPIO as GPIO +import subprocess +import time +import os +from time import sleep + +def blink(times): + blink_time = 0.1 + powerPin = 16 + for i in range(times): # blink times + GPIO.output(powerPin, 0) + sleep(blink_time) + GPIO.output(powerPin, 1) + sleep(blink_time) + sleep(0.65) + +def change_mode(): + push_button = 26 + powerPin = 16 + txPin = 27 + sleep(0.75) + if GPIO.input(push_button): + print("sudo reboot -h now") + os.system("echo 'reboot due to push button!' | wall") + GPIO.setwarnings(False) + GPIO.setup(powerPin, GPIO.OUT) + GPIO.output(powerPin, 0); + subprocess.call(['reboot', '-h', 'now'], shell=False) + return + blink(1) + if GPIO.input(push_button): + print("switch to AFSK") + os.system("echo 'switch to AFSK due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -a") + return + blink(2) + if GPIO.input(push_button): + print("switch to FSK") + os.system("echo 'switch to FSK due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -f") + return + blink(3) + if GPIO.input(push_button): + print("switch to BPSK") + os.system("echo 'switch to BPSK due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -b") + return + blink(4) + if GPIO.input(push_button): + print("switch to SSTV") + os.system("echo 'switch to SSTV due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -s") + return + blink(5) + if GPIO.input(push_button): + print("switch to CW") + os.system("echo 'switch to CW due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -m") + return + blink(6) + if GPIO.input(push_button): + print("switch to Repeater") + os.system("echo 'switch to Repeater due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -e") + return + blink(7) + if GPIO.input(push_button): + print("switch to FunCube") + os.system("echo 'switch to FunCube due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -j") + return + blink(8) + if GPIO.input(push_button): + print("switch to PacSat") + os.system("echo 'switch to PacSat due to push button!' | wall") + os.system("/home/pi/CubeSatSim/config -G") + return + for i in range(3): # blink 3 times slowly + GPIO.output(powerPin, 0) + sleep(0.35) + GPIO.output(powerPin, 1) + sleep(0.35) + sleep(0.65) + if GPIO.input(push_button): + print("sudo nmcli connection up preconfigured") + os.system("sudo nmcli connection up preconfigured") + print("sudo shutdown -h now") + os.system("echo 'shutdown due to push button!' | wall") + GPIO.setwarnings(False) + GPIO.setup(powerPin, GPIO.OUT) + subprocess.call(['shutdown', '-h', 'now'], shell=False) + return + for i in range(3): # blink two times even more slowly + GPIO.output(powerPin, 0) + sleep(0.7) + GPIO.output(powerPin, 1) + sleep(0.7) + sleep(0.7) + print("toggle command and control mode") + try: + f = open("/home/pi/CubeSatSim/command_control", "r") + f.close() + print("command and control will be deactivated") + os.system('sudo rm /home/pi/CubeSatSim/command_control') + os.system("echo 'command and control deactivated by push button!' | wall") + os.system('sudo systemctl restart command') + except: + print("command and control will be activated") + os.system('touch /home/pi/CubeSatSim/command_control') + os.system("echo 'command and control activated by push button!' | wall") + os.system('sudo systemctl restart command') + sleep(1) + +powerPin = 16 +txPin = 27 +push_button = 26 +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) +GPIO.setup(txPin, GPIO.OUT) +GPIO.output(txPin, 0) +GPIO.setup(powerPin, GPIO.OUT) +GPIO.output(powerPin, 0) +# GPIO.output(powerPin, 1) +# sleep(0.5) +# GPIO.output(powerPin, 0) +# sleep(0.5) + +GPIO.setup(push_button, GPIO.IN, pull_up_down=GPIO.PUD_UP) +# sleep(1) +if not GPIO.input(push_button): # if pushbutton is held down during boot + print("Activating WiFi access point!") + GPIO.output(powerPin, 1) + sleep(0.1) + GPIO.output(powerPin, 0) + sleep(0.1) + GPIO.output(powerPin, 1) + sleep(0.3) + GPIO.output(powerPin, 0) + sleep(0.1) + GPIO.output(powerPin, 1) + sleep(0.3) + GPIO.output(powerPin, 0) + print("sudo nmcli device wifi hotspot con-name HotSpot ifname wlan0 ssid CubeSat password amsatao7") + os.system("sudo nmcli device wifi hotspot con-name HotSpot ifname wlan0 ssid CubeSat password amsatao7") + sleep(2) +GPIO.setup(powerPin, GPIO.OUT) +GPIO.output(powerPin, 1) +while (True): + sleep(1) + GPIO.wait_for_edge(push_button, GPIO.FALLING) + change_mode() + sleep(5)