Because the script is expected to wait for the button press I need it to in the background. The script is called from rc.local (see below) . push_button2.py below:
Code: Select all
#!/usr/bin/env python
import daemon
import time
import os
import I2C_LCD_driver
mylcd = I2C_LCD_driver.lcd()
mylcd.lcd_display_string("READY", 1)
print(time.ctime() + " Push Button Script Loaded")
import RPi.GPIO as GPIO
printer_busy = False
def button_callback(channel):
while True:
print(time.ctime() + " Print Button Pressed")
global printer_busy
if printer_busy:
# Already printing, do nothing
return
printer_busy = True
# mylcd.lcd_clear()
mylcd.lcd_display_string("PRINTING", 1)
os.chdir("/media/pi/BB2B-791B/sandwich")
# os.system("sh print")
time.sleep(1)
mylcd.lcd_clear()
mylcd.lcd_display_string("READY", 1)
printer_busy = False
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
def run():
with daemon.DaemonContext():
button_callback(channel)
if __name__ == "__main__":
run()
GPIO.cleanup() # Clean up
Running the code in Thorny produces I am unconcerned about those errors because the test script taken from here: https://stackoverflow.com/a/8375012/5651732 does the same thing in Thorny but run fine in the console and gives the expected behavior of populating the log file with the current time.