A Mode State Machine

from enum import Enum, auto

class UM(Enum):
    UM_POWERUP = 0
    UM_IDLE = auto()
    UM_DO_SOMETHING = auto()

current_mode = UM.UM_POWERUP
current_mode_last = -1
    
#***************************************
#***************************************
#********** OPERATION PROCESS **********
#***************************************
#***************************************
def operation_process():
    
    global current_mode
    global current_mode_last

    #------------------------------
    #----- CHECK FOR NEW MODE -----
    #------------------------------
    just_entered_new_mode = False;
    if current_mode != current_mode_last:
        #Flag that we've just entered a new mode
        just_entered_new_mode = True;
        current_mode_last = current_mode;
	


    #------------------------------------
    #------------------------------------
    #----- PROCESS THE CURRENT MODE -----
    #------------------------------------
    #------------------------------------
    try:
        match (current_mode):
            
            case UM.UM_POWERUP:
                #-------------------
                #-------------------
                #----- POWERUP -----
                #-------------------
                #-------------------
                if just_entered_new_mode:
                    #----- JUST ENTERED THIS MODE -----
                    pass

                
                #------------------------
                #----- PROCESS MODE -----
                #------------------------

                #Exit to mode idle
                current_mode = UM.UM_IDLE


            case UM.UM_IDLE:
                #-------------------------------
                #-------------------------------
                #----- IDLE - DEFAULT MODE -----
                #-------------------------------
                #-------------------------------
                if just_entered_new_mode:
                    #----- JUST ENTERED THIS MODE -----
                    pass

                #------------------------
                #----- PROCESS MODE -----
                #------------------------





            #<<<<< ADD OTHER MODES HERE


        #END OF match (current_mode):
    except GeneratorExit:
        #match break equivalent exit point (use: raise GeneratorExit("") )
        pass
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *