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!

8
  • 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. Commented Dec 28, 2017 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. Commented Dec 28, 2017 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. Commented Dec 28, 2017 at 15:27
  • That would be great. Would love to see some other approaches to this type of communication. Thank you. Commented Dec 28, 2017 at 15:30
  • well check this out stackoverflow.com/questions/47986858/… , 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 Commented Dec 28, 2017 at 15:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.