Python 如何在超时的情况下从GPIO读取输入
我目前正在构建一个分支菜单系统,它使用3个按钮来选择左、右、选择。使用python,我如何读取这些按钮的输入,以便在用户不按按钮15秒的情况下返回主菜单?我想我能够使用以下代码包含超时,该代码使用树数据结构来实现菜单Python 如何在超时的情况下从GPIO读取输入,python,user-interface,menu,Python,User Interface,Menu,我目前正在构建一个分支菜单系统,它使用3个按钮来选择左、右、选择。使用python,我如何读取这些按钮的输入,以便在用户不按按钮15秒的情况下返回主菜单?我想我能够使用以下代码包含超时,该代码使用树数据结构来实现菜单 ##Create a class for storing menu items within a tree class menu_tree: 'This class stores menu options' #Create an initalization fun
##Create a class for storing menu items within a tree
class menu_tree:
'This class stores menu options'
#Create an initalization function for root node
def __init__(self, option):
self.option = option #initialize option value
self.children = [] #intialize empty child list
self.parent = None #root has no parents (Aww D: )
#Create a function for adding child nodes
def add_child(self, child):
child.parent = self #parent of new instance becomes old instance
self.children.append(child) #add child instance to list of children
#Begin creation of menu menu tree
root = menu_tree("Main Menu") #Create root node for main menu
Light = menu_tree("Light") #Create child node for light
Light.add_child(menu_tree("Hours")) #Add child node for amount of hours to light
Light.add_child(menu_tree("Time")) #Add child node for time of day to start lighting
root.add_child(Light) #Add child node for light
root.add_child(menu_tree("Water")) #Add a child node for water
root.add_child(menu_tree("Soil")) #Add a child node for soil
root.add_child(menu_tree("Temp")) #Add a child node for temp
root.add_child(menu_tree("Humidity")) #Add a child node for humidity
#Create a function for choosing between menu options
def menu():
current_option = root.option #Set initial option to the root option
position = 0 #set left/right postion
timer = 0 #start timer
while timer <= 80: #infinite loop while user is actively choosing
oled.write_center(current_option.children[position]) #print the current option to the screen
if pi.read(BUTTON_ONE) == True :
if postion != 0: #can't have negative postion
position -= 1 #move one spot to the left
else:
position = len(current_option.children)
timer = 0 #reset timer
elif pi.read(BUTTON_TWO) == True :
if len(current_option.children) != 0: #if current option has children
current_option = current_option.children[position] #set the current option to the first child of the chosen node
timer = 0 #reset timer
else: #if the option has no children, it is the final option
return current_option.option #and it's string is returned
elif pi.read(BUTTON_THREE) == True:
if postion < len(current_option.children): #if position is not at the end of the list
position += 1 #move one spot to the right
else:
position = 0 #move position back to other end
timer = 0 #reset timer
else:
timer += 1 #count the timer up
time.sleep(0.25) #1/4 second delay
我想我能够使用下面的代码包含超时,它使用树数据结构来实现菜单
##Create a class for storing menu items within a tree
class menu_tree:
'This class stores menu options'
#Create an initalization function for root node
def __init__(self, option):
self.option = option #initialize option value
self.children = [] #intialize empty child list
self.parent = None #root has no parents (Aww D: )
#Create a function for adding child nodes
def add_child(self, child):
child.parent = self #parent of new instance becomes old instance
self.children.append(child) #add child instance to list of children
#Begin creation of menu menu tree
root = menu_tree("Main Menu") #Create root node for main menu
Light = menu_tree("Light") #Create child node for light
Light.add_child(menu_tree("Hours")) #Add child node for amount of hours to light
Light.add_child(menu_tree("Time")) #Add child node for time of day to start lighting
root.add_child(Light) #Add child node for light
root.add_child(menu_tree("Water")) #Add a child node for water
root.add_child(menu_tree("Soil")) #Add a child node for soil
root.add_child(menu_tree("Temp")) #Add a child node for temp
root.add_child(menu_tree("Humidity")) #Add a child node for humidity
#Create a function for choosing between menu options
def menu():
current_option = root.option #Set initial option to the root option
position = 0 #set left/right postion
timer = 0 #start timer
while timer <= 80: #infinite loop while user is actively choosing
oled.write_center(current_option.children[position]) #print the current option to the screen
if pi.read(BUTTON_ONE) == True :
if postion != 0: #can't have negative postion
position -= 1 #move one spot to the left
else:
position = len(current_option.children)
timer = 0 #reset timer
elif pi.read(BUTTON_TWO) == True :
if len(current_option.children) != 0: #if current option has children
current_option = current_option.children[position] #set the current option to the first child of the chosen node
timer = 0 #reset timer
else: #if the option has no children, it is the final option
return current_option.option #and it's string is returned
elif pi.read(BUTTON_THREE) == True:
if postion < len(current_option.children): #if position is not at the end of the list
position += 1 #move one spot to the right
else:
position = 0 #move position back to other end
timer = 0 #reset timer
else:
timer += 1 #count the timer up
time.sleep(0.25) #1/4 second delay