jtloper1
Posts: 10
Joined: Tue Jan 20, 2015 11:37 pm

Loop Trouble(s)

Fri Jun 21, 2019 8:13 am

The primary purpose of this script is to call the sh file "print" which resides in another directory when a GPIO button is pressed. The secondary function is to display instructions and feedback on a 16x2 LCD. When the script loads the LCD displays "READY" as expected. When the button is pressed the print script runs (sometimes more that once, stupid button bounce) as expected however, the LCD displays "PRINTING" indifferently. What I need to for the LCD to display "READY" after printing has completed.

Code: Select all

#!/usr/bin/env python
import daemon
import RPi.GPIO as GPIO
import time
import subprocess
import os
import I2C_LCD_driver
mylcd = I2C_LCD_driver.lcd()
mylcd.lcd_display_string("READY", 1)
def button_handler(pin):
    print("pin %s's value is %s" % (pin, GPIO.input(pin)))
#    mylcd.lcd_clear()
    mylcd.lcd_display_string("PRINTING", 1)
    os.chdir("/media/pi/BB2B-791B/sandwich")
   os.system("sh print")
    time.sleep(10)
    mylcd.lcd_clear()
if __name__ == '__main__':
    button_pin = 10

    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(button_pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

    # events can be GPIO.RISING, GPIO.FALLING, or GPIO.BOTH
    GPIO.add_event_detect(button_pin, GPIO.BOTH,
                          callback=button_handler,
                          bouncetime=300)

    try:
        time.sleep(1000)
    except KeyboardInterrupt:
        GPIO.cleanup()
        
        
20190621_010728.jpg
20190621_010728.jpg (173.68 KiB) Viewed 630 times

User avatar
PeterO
Posts: 5028
Joined: Sun Jul 22, 2012 4:14 pm

Re: Loop Trouble(s)

Fri Jun 21, 2019 8:27 am

jtloper1 wrote:
Fri Jun 21, 2019 8:13 am
, the LCD displays "PRINTING" indifferently.
Did you mean "indefinitely" ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ghp
Posts: 1413
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Loop Trouble(s)

Fri Jun 21, 2019 9:06 am

Hello, instead of using os.system("sh print"), try to switch to subprocess.Popen:

Code: Select all

import subprocess
p = subprocess.Popen("sh print.sh", shell=True)
p.wait()
print("ok")
My print.sh is:

Code: Select all

echo "start "
sleep 10
echo "stop"

User avatar
Paeryn
Posts: 2680
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Loop Trouble(s)

Fri Jun 21, 2019 10:16 am

jtloper1 wrote:
Fri Jun 21, 2019 8:13 am
The primary purpose of this script is to call the sh file "print" which resides in another directory when a GPIO button is pressed. The secondary function is to display instructions and feedback on a 16x2 LCD. When the script loads the LCD displays "READY" as expected. When the button is pressed the print script runs (sometimes more that once, stupid button bounce) as expected however, the LCD displays "PRINTING" indifferently. What I need to for the LCD to display "READY" after printing has completed.

Code: Select all

#!/usr/bin/env python
import daemon
import RPi.GPIO as GPIO
import time
import subprocess
import os
import I2C_LCD_driver
mylcd = I2C_LCD_driver.lcd()
mylcd.lcd_display_string("READY", 1)
def button_handler(pin):
    print("pin %s's value is %s" % (pin, GPIO.input(pin)))
#    mylcd.lcd_clear()
    mylcd.lcd_display_string("PRINTING", 1)
    os.chdir("/media/pi/BB2B-791B/sandwich")
   os.system("sh print")
    time.sleep(10)
    mylcd.lcd_clear()
if __name__ == '__main__':
    button_pin = 10

    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(button_pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

    # events can be GPIO.RISING, GPIO.FALLING, or GPIO.BOTH
    GPIO.add_event_detect(button_pin, GPIO.BOTH,
                          callback=button_handler,
                          bouncetime=300)

    try:
        time.sleep(1000)
    except KeyboardInterrupt:
        GPIO.cleanup()
        
        
20190621_010728.jpg
Your code, as given, doesn't print anything to the LCD after the printing is done, rather it just clears the display (if the lcd_clear() is doing what it says). What you need is to print the READY message when it is done. You really need to put some code in there so that multiple presses don't cause the function to run multiple times at once, e.g. a global variable that you set when the function starts and clears when it finishes, if it's already set when the function is called you can ignore it.

I just noticed, you have the callback set to occur on both rising and falling edges of the gpio, that will call the function when the button is pressed and again when it is released, is this intended?

Code: Select all

printer_busy = False
def button_handler(pin):
    global printer_busy
    if printer_busy:
        # Already printing, do nothing
        return

    printer_busy = True
    print("pin %s's value is %s" % (pin, GPIO.input(pin)))
#    mylcd.lcd_clear()
    mylcd.lcd_display_string("PRINTING", 1)
    os.chdir("/media/pi/BB2B-791B/sandwich")
    os.system("sh print")
    time.sleep(10)
    mylcd.lcd_clear()
    mylcd.lcd_display_string("READY", 1)
    printer_busy = False
She who travels light — forgot something.

jtloper1
Posts: 10
Joined: Tue Jan 20, 2015 11:37 pm

Re: Loop Trouble(s)

Wed Jun 26, 2019 1:08 am

Thanks much it works! :)

Return to “Python”