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!