I have a while statement running and checking if my button (on a gpio) is pressed and once it is I print something. I would like to add some shell commands like exit, etc... How can i have the while statement running while also accepting raw input and run different things depending on what's typed? (ex. check for button press in background but have raw input accept the word exit and close the program)
--Thanks
#--------------------------
#SORRY FOR HOW SLOPPY IT IS
#--------------------------
import RPi.GPIO as GPIO
import time
import os
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
#Library that allows me to print to my printer
from escpos import *
#To get the weather
import pywapi
import string
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
button = 26
green = 4
yellow = 18
GPIO.setup(green, GPIO.OUT)
GPIO.setup(yellow, GPIO.OUT)
GPIO.setup(button, GPIO.IN, GPIO.PUD_UP)
GPIO.output(green, 1)
GPIO.output(yellow, 0)
print("Waiting for button press...")
#When the button is pressed begin the weather fetching and printing
def buttonPress(button):
GPIO.output(green, 0)
GPIO.output(yellow, 1)
print("Printing...")
weather_com_result = pywapi.get_weather_from_weather_com('CAXX2088')
temp = (weather_com_result['current_conditions']['temperature'])
text = string.lower(weather_com_result['current_conditions']['text'])
precip0 = (weather_com_result['forecasts'][0]['day']['chance_precip'])
precip1 = (weather_com_result['forecasts'][1]['day']['chance_precip'])
precip_date1 = (weather_com_result['forecasts'][1]['date'])
high = (weather_com_result['forecasts'][1]['high'])
low = (weather_com_result['forecasts'][1]['low'])
Epson = printer.Usb(0x04b8,0x0202)
Epson.set(align="CENTER")
Epson.set(bold=True)
Epson.set(size="2x")
Epson.text("Current Weather\n\n")
Epson.set(size='normal')
Epson.set(bold=False)
Epson.text(string.capwords(text))
Epson.text(" and "+temp)
Epson.text("C\n")
Epson.text("POP: " + precip0 + "%\n\n")
Epson.set(bold=True)
Epson.set(size="2x")
Epson.text("Forecast For " + precip_date1 + "\n\n")
Epson.set(size='normal')
Epson.set(bold=False)
Epson.text("POP: " + precip1 + "\n")
Epson.text("High: " + high + "C Low: "+ low +"C\n")
Epson.cut()
time.sleep(3)
print("Printed.")
#A loop that flashes a green light until the printer is ready for another print
waittime=0
while waittime<11:
GPIO.output(green, 1)
GPIO.output(yellow, 0)
time.sleep(1)
GPIO.output(green, 0)
GPIO.output(yellow, 0)
time.sleep(1)
waittime += 2
GPIO.output(green, 1)
print("Waiting for button press...")
#Where I want to be able to input the command
#Also i know that this isnt a loop and will only happen once (i think) but i think i can manage to turn it into a loop later on
command = raw_input(": ")
GPIO.add_event_detect(button, GPIO.RISING,callback=buttonPress)
if command == "exit":
GPIO.cleanup()
sys.exit()
SIGTERM
that typing<CTRL>-C
sends when running python code interactively (in the foreground, in Raspian and other *nix OSes). Look up the pythontry
,except
(used withKeyboardInterrupt:
) andfinally
keywords... – SlySven Jan 29 '16 at 20:26