+ vstatus.cur_pin = ''
+ vstatus.mk.set_message('PIN: ')
+ logging.info('need pin for user %s'%vstatus.cur_user)
+ vstatus.state = STATE_GETTING_PIN
+ return
+
+
+def handle_idle_key(state, event, params, v, vstatus):
+ #print "handle_idle_key (s,e,p)", state, " ", event, " ", params
+
+ key = params
+
+ if key == 11:
+ vstatus.cur_user = ''
+ vstatus.mk.set_message(GREETING)
+ idle_in(vstatus,5)
+ choose_idler()
+ return
+
+ vstatus.state = STATE_GETTING_UID
+ run_handler(event, key, v, vstatus)
+
+
+def handle_idle_tick(state, event, params, v, vstatus):
+ ### State logged out ... initiate idler in 5 (first start?)
+ if vstatus.time_to_idle == None and vstatus.cur_user == '':
+ vstatus.time_to_idle = time() + 5
+ choose_idler()
+
+ ### State idling
+
+ if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle:
+ idle_step()
+
+ if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle + 30:
+ vstatus.time_to_idle = time()
+ choose_idler()
+
+ ###
+
+ vstatus.mk.update_display()
+
+
+def handle_door_idle(state, event, params, v, vstatus):
+ # don't care right now.
+ pass
+
+def handle_door_event(state, event, params, v, vstatus):
+ vstatus.time_to_idle = None
+
+ if params == 1: #door open
+ vstatus.state = STATE_DOOR_OPENING
+ logging.warning("Entering open door mode")
+ v.display("-FEED ME-")
+ #door_open_mode(v);
+ vstatus.cur_user = ''
+ vstatus.cur_pin = ''
+ elif params == 0: #door closed
+ vstatus.state = STATE_DOOR_CLOSING
+ idle_in(vstatus, 5)
+
+ logging.warning('Leaving open door mode')
+ v.display("-YUM YUM!-")
+
+def idle_in(vstatus,seconds):
+ vstatus.time_to_idle = time() + seconds
+
+def return_to_idle(state,event,params,v,vstatus):
+ if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle:
+ vstatus.mk.set_message(GREETING)
+ vstatus.state = STATE_IDLE
+ return
+ if not vstatus.time_to_idle:
+ vstatus.mk.set_message(GREETING)
+ vstatus.state = STATE_IDLE
+ return
+
+def create_state_table(vstatus):
+ vstatus.state_table[(STATE_IDLE,TICK,1)] = handle_idle_tick
+ vstatus.state_table[(STATE_IDLE,KEY,1)] = handle_idle_key
+ vstatus.state_table[(STATE_IDLE,DOOR,1)] = handle_door_event
+
+ vstatus.state_table[(STATE_DOOR_OPENING,TICK,1)] = handle_door_idle
+ vstatus.state_table[(STATE_DOOR_OPENING,DOOR,1)] = handle_door_event
+ vstatus.state_table[(STATE_DOOR_OPENING,KEY,1)] = do_nothing
+
+ vstatus.state_table[(STATE_DOOR_CLOSING,TICK,1)] = return_to_idle
+ vstatus.state_table[(STATE_DOOR_CLOSING,DOOR,1)] = handle_door_event
+ vstatus.state_table[(STATE_DOOR_CLOSING,KEY,1)] = do_nothing
+
+ vstatus.state_table[(STATE_GETTING_UID,TICK,1)] = handle_getting_uid_idle
+ vstatus.state_table[(STATE_GETTING_UID,DOOR,1)] = do_nothing
+ vstatus.state_table[(STATE_GETTING_UID,KEY,1)] = handle_getting_uid_key
+
+ vstatus.state_table[(STATE_GETTING_PIN,TICK,1)] = handle_getting_pin_idle
+ vstatus.state_table[(STATE_GETTING_PIN,DOOR,1)] = do_nothing
+ vstatus.state_table[(STATE_GETTING_PIN,KEY,1)] = handle_getting_pin_key
+
+ vstatus.state_table[(STATE_GET_SELECTION,TICK,1)] = handle_get_selection_idle
+ vstatus.state_table[(STATE_GET_SELECTION,DOOR,1)] = do_nothing
+ vstatus.state_table[(STATE_GET_SELECTION,KEY,1)] = handle_get_selection_key
+
+def get_state_table_handler(vstatus, state, event, counter):
+ return vstatus.state_table[(state,event,counter)]
+