0

To simplify a complicated project, I have one switch and 1 pushbutton. What I want is to have code do separate functions when the button is pressed for both positions of the switch.

So I have two files. The main file sets up some gpio and event detects. Another file has a callback function defined for the pushbutton. Here is the main file:

import Adafruit_GPIO as GPIO
gpio = GPIO.get_platform_gpio()

global staff_mode    
staff_mode = 0
Bg1 = 24
global audio_latch
audio_latch = 13
global image_latch
image_latch = 26

def staff_mode_audio():
    staff_mode_write(0)
    global staff_mode
    if  not gpio.input(audio_latch) and gpio.input(image_latch):
        staff_mode=0;
        print('You are in audio setup')
        print(staff_mode)



def staff_mode_image():
    staff_mode_write(1)
    global staff_mode
    if not gpio.input(image_latch) and gpio.input(audio_latch):
        staff_mode=1;
        print('You are in image setup')

gpio.add_event_detect(Bg1, GPIO.FALLING, callback = lambda x:grid_input(1,page_select,current_mode,staff_mode), bouncetime=debounce_time)
gpio.add_event_detect(image_latch, GPIO.FALLING, callback = lambda x: staff_mode_image(), bouncetime=300)
gpio.add_event_detect(audio_latch, GPIO.FALLING, callback = lambda x: staff_mode_audio(), bouncetime=300)
try:
    while True:
        signal.pause()

except KeyboardInterrupt:
    gpio.cleanup()

The key being that the the lines image_select_write('num') is defined in another file but basically just writes a 0 or 1 to a text file. The next file is:

def staff_mode_write(num):
    file=open('image_select.txt', 'w')
    file.write(str(num))
    file.close()

def staff_mode_read():
    file=open('image_select.txt', 'rt')
    image_select=int(file.read())
    file.close()
    return image_select

def grid_input(grid_select,page_select,current_mode,staff_mode):
    staff_mode = staff_mode_read()
    if (staff_mode == 0):
        #Do stuff
        staff_mode_write(1)
    else:
        #Do other stuff
        staff_mode_write(0)

So the callback function grid_input reads the text file to determine what function the pushbutton should perform. I tried unsucessfully to communicate the value of staff_mode using global variables.

This solution worked but it was clunky. How can I communicate the status of staff_mode without using a text file? Thank you!

martineau
  • 119,623
  • 25
  • 170
  • 301
Joe Lyons
  • 13
  • 5
  • You can use the pipe and use stdin and stdout to read the data from each other files, But one file have to execute the completely. – babygame0ver Dec 28 '17 at 15:20
  • I'm not exactly sure what you mean by this. Isn't this essentially the same as writing thru a file? I was thinking of more a "pointer" in python type solution. – Joe Lyons Dec 28 '17 at 15:25
  • There is no such concept of pointer in Python. But If you want I can show you the example to exchange the data b/w two scripts based on the pipes. Maybe that will work for you. – babygame0ver Dec 28 '17 at 15:27
  • That would be great. Would love to see some other approaches to this type of communication. Thank you. – Joe Lyons Dec 28 '17 at 15:30
  • well check this out https://stackoverflow.com/questions/47986858/give-inputs-to-a-running-program-using-python/47990014#47990014 , but one thing to remember while using pipes that if one file has done executing then another file will automatically get the output through the pipe – babygame0ver Dec 28 '17 at 15:31
  • Are you saying you can't pass `staff_mode` to `grid_input(grid_select,page_select,current_mode,staff_mode)` from event detect? – zerox1212 Dec 28 '17 at 15:45
  • I tried to simplify larger code to ask my question but I realize now that I made a mistake in my posting. I will try your solution. Thanks. – Joe Lyons Dec 28 '17 at 15:56
  • I think a cleaner way to do this would be to make a class `FuncSelect` then keep track of the mode and button press inside that. Then you can store `staff_mode` as `self.staff_mode` and avoid using globals. I would look for a more object oriented approach if possible. – zerox1212 Dec 28 '17 at 16:02

1 Answers1

0

You should indeed be able to use a global for this. However you have to declare the global wherever it is used. You can't just declare it at the top of your module. In other words you have to change grid_input to this to access the global.

def grid_input(grid_select,page_select,current_mode):
    global staff_mode
    if (staff_mode == 0):
        #Do stuff
    else:
        #Do other stuff

However the goal of your code is not that clear to me so I think you could probably come up with a better approach.

zerox1212
  • 166
  • 1
  • 7