{"id":4526,"date":"2023-12-15T11:41:53","date_gmt":"2023-12-15T11:41:53","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4526"},"modified":"2023-12-15T12:18:57","modified_gmt":"2023-12-15T12:18:57","slug":"state-machine","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/state-machine\/state-machine","title":{"rendered":"State Machine"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">A Mode State Machine<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from enum import Enum, auto\n\nclass UM(Enum):\n    UM_POWERUP = 0\n    UM_IDLE = auto()\n    UM_DO_SOMETHING = auto()\n\ncurrent_mode = UM.UM_POWERUP\ncurrent_mode_last = -1\n    \n#***************************************\n#***************************************\n#********** OPERATION PROCESS **********\n#***************************************\n#***************************************\ndef operation_process():\n    \n    global current_mode\n    global current_mode_last\n\n    #------------------------------\n    #----- CHECK FOR NEW MODE -----\n    #------------------------------\n    just_entered_new_mode = False;\n    if current_mode != current_mode_last:\n        #Flag that we've just entered a new mode\n        just_entered_new_mode = True;\n        current_mode_last = current_mode;\n\t\n\n\n    #------------------------------------\n    #------------------------------------\n    #----- PROCESS THE CURRENT MODE -----\n    #------------------------------------\n    #------------------------------------\n    try:\n        match (current_mode):\n            \n            case UM.UM_POWERUP:\n                #-------------------\n                #-------------------\n                #----- POWERUP -----\n                #-------------------\n                #-------------------\n                if just_entered_new_mode:\n                    #----- JUST ENTERED THIS MODE -----\n                    pass\n\n                \n                #------------------------\n                #----- PROCESS MODE -----\n                #------------------------\n\n                #Exit to mode idle\n                current_mode = UM.UM_IDLE\n\n\n            case UM.UM_IDLE:\n                #-------------------------------\n                #-------------------------------\n                #----- IDLE - DEFAULT MODE -----\n                #-------------------------------\n                #-------------------------------\n                if just_entered_new_mode:\n                    #----- JUST ENTERED THIS MODE -----\n                    pass\n\n                #------------------------\n                #----- PROCESS MODE -----\n                #------------------------\n\n\n\n\n\n            #&lt;&lt;&lt;&lt;&lt; ADD OTHER MODES HERE\n\n\n        #END OF match (current_mode):\n    except GeneratorExit:\n        #match break equivalent exit point (use: raise GeneratorExit(\"\") )\n        pass<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Mode State Machine<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[],"class_list":["post-4526","post","type-post","status-publish","format-standard","hentry","category-state-machine"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4526"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4526\/revisions"}],"predecessor-version":[{"id":4538,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4526\/revisions\/4538"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}