def redraw_6(): """Redraw the screen with up to six nodes. :return: Mode to enter :rtype: int """ num_nodes = (len(G_SAVED_DISPLAY) - 2) / 5 # No items? if num_nodes == 0: return redraw_3() print_neighbors() for index in xrange(6): set_xy(0 if index < 3 else 9, (index % 3) + 1) if index < num_nodes: text = expand_node_entry(G_SAVED_DISPLAY[(index * 5) + 2:(index * 5) + 7]) cache_8x8(text[2:8] + text[-1]) else: cache_8x8(SPACES_7) if index < 3: cache_8x8(SPACES_2) return DMODE_LIST_6
def cycle_batt_msg(): """Cycle through a battery message.""" global G_BATTERY_MSG_STATE # How many messages to cycle around? num_msgs = ord(G_BATTERY_LOW[0]) + (len(G_BATTERY_LOW) / 3) + 1 state = G_BATTERY_MSG_STATE / 2 # Two seconds per state # Wrap around? if state >= num_msgs: G_BATTERY_MSG_STATE = state = 0 # Display the full screen to start if state == 0: display(G_SAVED_DISPLAY) else: set_xy(0, 3) mac_to_show = state - 1 - ord(G_BATTERY_LOW[0]) # Is this node low? if mac_to_show < 0: # Yes, show that cache_8x8(LOW_US) else: # Must be some other node, show them cache_8x8(LOW_NEIGHBOR + mac_to_str(G_BATTERY_LOW[(mac_to_show * 3) + 1:(mac_to_show * 3) + 4])) # Next state G_BATTERY_MSG_STATE += 1
def redraw_4(): """Redraw the screen with up to four nodes. :return: Mode to enter :rtype: int """ num_nodes = (len(G_SAVED_DISPLAY) - 2) / 5 # No items? if num_nodes == 0: return redraw_3() for y in xrange(4): set_xy(0, y) line = expand_node_entry( G_SAVED_DISPLAY[(y * 5) + 2:(y * 5) + 7])[:-1] if num_nodes > y else SPACES_16 cache_8x8(line) return DMODE_LIST_4
def redraw_3(): """Redraw the screen with up to three nodes. :return: Mode to enter :rtype: int """ num_nodes = (len(G_SAVED_DISPLAY) - 2) / 5 print_neighbors() set_xy(0, 1) cache_8x8( expand_node_entry(G_SAVED_DISPLAY[2:7] )[:-1] if num_nodes > 0 else SPACES_16) set_xy(0, 2) if num_nodes > 0: cache_8x8( expand_node_entry(G_SAVED_DISPLAY[7:12] )[:-1] if num_nodes > 1 else SPACES_16) else: cache_8x8(NONE_YET) set_xy(0, 3) cache_8x8( expand_node_entry(G_SAVED_DISPLAY[12:17] )[:-1] if num_nodes > 2 else SPACES_16) return DMODE_LIST_3
def cache_8x8(string): """Write the changed portions of a string out to the display. :param str string: String to write """ # Have any characters changed? offset = G_DRAW_X + (G_DRAW_Y * 16) length = len(string) if string == G_DISPLAY_CACHE[offset:offset + length]: # Nope, unchanged. Advance and return. set_xy(G_DRAW_X + length, G_DRAW_Y) return # At least one character has changed. Loop over all to find changes. x = G_DRAW_X change_starts = None for index in xrange(length): # Has this character changed? if string[index] == G_DISPLAY_CACHE[offset + index:offset + index + 1]: # No, this one was the same, but what about the ones before it? if change_starts is not None: # This is the end of a change, so flush that change out flush_change(string[change_starts:index]) change_starts = None else: # Yes, this character changed, but is it the first? if change_starts is None: # Yes, this is the first character to change. Update write position. if index: set_xy(x + index, G_DRAW_Y) change_starts = index # Loop is finished, but is there an unwritten change to flush? if change_starts is None: # Nothing has changed, so update the position set_xy(x + length, G_DRAW_Y) else: # Yes we have an unwritten change, flush it flush_change(string[change_starts:])
def fsm_go(reason): global G_FSM_STATE, G_GENERAL_COUNTDOWN, G_HEARD_FROM, G_NEED_REBOOT, G_SEQUENCE, G_POSITION if reason == REASON_START: fsm_goto(STATE_WAKE_MESSAGE) elif G_FSM_STATE == STATE_WAKE_MESSAGE: if reason == REASON_GOTO: # Boot-up init_display(USE_SPI) set_xy(0, 0) cache_8x8(" COPS Eval Tool ") set_xy(0, 1) cache_8x8("PUSH/" + RIGHT_ARROW + " for menu ") set_xy(0, 2) cache_8x8("Else controller ") G_GENERAL_COUNTDOWN = INITIAL_TIMEOUT elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_MENU1) set_xy(0, 3) cache_8x8("mode in " + str(G_GENERAL_COUNTDOWN) + " sec ") elif G_FSM_STATE == STATE_CONTROLLER: if reason == REASON_GOTO: low_power() elif reason == REASON_LEFT: init_display(USE_SPI) fsm_goto(STATE_MENU3) elif G_FSM_STATE == STATE_MENU1: # PASSIVE OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8("== MAIN MENU == ") set_xy(0, 1) cache_8x8(RIGHT_ARROW + " Passive mode ") set_xy(0, 2) cache_8x8(" Probe mode ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Controller ") elif reason == REASON_DOWN: fsm_goto(STATE_MENU2) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_PASSIVE) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU2: # PROBE OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8("== MAIN MENU == ") set_xy(0, 1) cache_8x8(UP_ARROW + " Passive mode ") set_xy(0, 2) cache_8x8(RIGHT_ARROW + " Probe mode ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Controller ") elif reason == REASON_UP: fsm_goto(STATE_MENU1) elif reason == REASON_DOWN: fsm_goto(STATE_MENU3) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_PROBE) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU3: # CONTROLLER OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8(UP_ARROW + " Passive mode ") set_xy(0, 1) cache_8x8(" Probe mode ") set_xy(0, 2) cache_8x8(RIGHT_ARROW + " Controller ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Interrogate ") elif reason == REASON_UP: fsm_goto(STATE_MENU7) elif reason == REASON_DOWN: fsm_goto(STATE_MENU4) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_CONTROLLER) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU4: # INTERROGATE OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8(UP_ARROW + " Probe mode ") set_xy(0, 1) cache_8x8(" Controller ") set_xy(0, 2) cache_8x8(RIGHT_ARROW + " Interrogate ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Set NV params ") elif reason == REASON_UP: fsm_goto(STATE_MENU6) elif reason == REASON_DOWN: fsm_goto(STATE_MENU5) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_INTERROGATE) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU5: # SET NV PARAMS OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8(UP_ARROW + " Controller ") set_xy(0, 1) cache_8x8(" Interrogate ") set_xy(0, 2) cache_8x8(RIGHT_ARROW + " Set NV params ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Simon ") elif reason == REASON_UP: fsm_goto(STATE_MENU9) elif reason == REASON_DOWN: fsm_goto(STATE_MENU8) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_SET_NV1) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU6: # CONTROLLER OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8(UP_ARROW + " Probe mode ") set_xy(0, 1) cache_8x8(RIGHT_ARROW + " Controller ") set_xy(0, 2) cache_8x8(" Interrogate ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Set NV params ") elif reason == REASON_UP: fsm_goto(STATE_MENU2) elif reason == REASON_DOWN: fsm_goto(STATE_MENU4) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_CONTROLLER) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU7: # PROBE OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8(UP_ARROW + " Passive mode ") set_xy(0, 1) cache_8x8(RIGHT_ARROW + " Probe mode ") set_xy(0, 2) cache_8x8(" Controller ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Interrogate ") elif reason == REASON_UP: fsm_goto(STATE_MENU2) elif reason == REASON_DOWN: fsm_goto(STATE_MENU3) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_PROBE) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU8: # SIMON OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8(UP_ARROW + " Controller ") set_xy(0, 1) cache_8x8(" Interrogate ") set_xy(0, 2) cache_8x8(" Set NV params ") set_xy(0, 3) cache_8x8(RIGHT_ARROW + " Simon ") elif reason == REASON_UP: fsm_goto(STATE_MENU5) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_SIMON1) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_MENU9: # INTERROGATE OPTION if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(0, 0) cache_8x8(UP_ARROW + " Controller ") set_xy(0, 1) cache_8x8(RIGHT_ARROW + " Interrogate ") set_xy(0, 2) cache_8x8(" Set NV params ") set_xy(0, 3) cache_8x8(DOWN_ARROW + " Simon ") elif reason == REASON_UP: fsm_goto(STATE_MENU6) elif reason == REASON_DOWN: fsm_goto(STATE_MENU5) elif (reason == REASON_RIGHT) or (reason == REASON_PRESS): fsm_goto(STATE_INTERROGATE) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif G_FSM_STATE == STATE_PROBE: if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT display_neighbors(get_battery()) G_HEARD_FROM = "" mcastRpc(1, MAX_TTL, "cops_report_in", PROBE_TIMESLOTS) elif reason == REASON_PRESS: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT display_neighbors(get_battery()) G_HEARD_FROM = "" mcastRpc(1, MAX_TTL, "cops_report_in", PROBE_TIMESLOTS) elif reason == REASON_1S_HOOK: display_on_1s() if G_GENERAL_COUNTDOWN == (CONTROLLER_TIMEOUT - (PROBE_TIMESLOTS / 10)): clear_not_heard_from(G_HEARD_FROM) if general_countdown(): fsm_goto(STATE_CONTROLLER) elif reason == REASON_LEFT: fsm_goto(STATE_MENU2) elif G_FSM_STATE == STATE_PASSIVE: if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT display_neighbors(get_battery()) G_HEARD_FROM = "" mcastRpc(1, MAX_TTL, "cops_report_in", PASSIVE_TIMESLOTS) elif reason == REASON_1S_HOOK: display_on_1s() if general_countdown(): fsm_goto(STATE_CONTROLLER) elif (G_GENERAL_COUNTDOWN % (PASSIVE_TIMESLOTS / 10)) == 0: clear_not_heard_from(G_HEARD_FROM) G_HEARD_FROM = "" mcastRpc(1, MAX_TTL, "cops_report_in", PASSIVE_TIMESLOTS) elif reason == REASON_LEFT: fsm_goto(STATE_MENU1) elif G_FSM_STATE == STATE_INTERROGATE: if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT clear_screen() set_xy(0, 0) cache_8x8("TODO:INTERROGATE") elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) elif reason == REASON_LEFT: fsm_goto(STATE_MENU4) elif G_FSM_STATE == STATE_SET_NV1: set_xy(0, 0) cache_8x8("Channel: " + str(getChannel()) + " ") set_xy(0, 1) cache_8x8("Network ID: " + num_to_hex(getNetId())) set_xy(0, 2) cache_8x8("FeatureBts: " + num_to_hex(loadNvParam(NV_FEATURE_BITS_ID))) set_xy(0, 3) cache_8x8("Encryption: " + ("On " if loadNvParam(NV_AES128_ENABLE_ID) else "Off ")) G_NEED_REBOOT = False fsm_goto(STATE_SET_NV2) elif G_FSM_STATE == STATE_SET_NV2: channel = getChannel() if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(9, 0) cache_8x8(str(channel) + " ") set_xy(9, 0) print_invert(1 if channel > 9 else channel) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(9, 0) if G_GENERAL_COUNTDOWN % 2: print_invert(1 if channel > 9 else channel) else: cache_8x8(str(channel)) elif reason == REASON_UP: setChannel((channel + 1) % 16) fsm_goto(STATE_SET_NV2) elif reason == REASON_DOWN: setChannel((channel + 15) % 16) fsm_goto(STATE_SET_NV2) elif reason == REASON_PRESS: set_xy(9, 0) cache_8x8(str(channel) + " ") saveNvParam(NV_CHANNEL_ID, channel) fsm_goto(STATE_SET_NV3) elif G_FSM_STATE == STATE_SET_NV3: net_id = getNetId() if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(12, 1) cache_8x8(num_to_hex(net_id)) set_xy(12, 1) print_invert(net_id >> 12) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(12, 1) if G_GENERAL_COUNTDOWN % 2: print_invert(net_id >> 12) else: cache_8x8(num_to_hex(net_id)) elif reason == REASON_UP: setNetId(net_id + 0x1000) fsm_goto(STATE_SET_NV3) elif reason == REASON_DOWN: setNetId(net_id + 0xf000) fsm_goto(STATE_SET_NV3) elif reason == REASON_RIGHT: fsm_goto(STATE_SET_NV4) elif reason == REASON_PRESS: set_xy(12, 1) cache_8x8(num_to_hex(net_id)) saveNvParam(NV_NETWORK_ID, net_id) fsm_goto(STATE_SET_NV7) elif G_FSM_STATE == STATE_SET_NV4: net_id = getNetId() if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(12, 1) cache_8x8(num_to_hex(net_id)) set_xy(13, 1) print_invert(net_id >> 8) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: if G_GENERAL_COUNTDOWN % 2: set_xy(13, 1) print_invert(net_id >> 8) else: set_xy(12, 1) cache_8x8(num_to_hex(net_id)) elif reason == REASON_UP: setNetId(((net_id + 0x0100) & 0x0f00) | (net_id & 0xf0ff)) fsm_goto(STATE_SET_NV4) elif reason == REASON_DOWN: setNetId(((net_id + 0x0f00) & 0x0f00) | (net_id & 0xf0ff)) fsm_goto(STATE_SET_NV4) elif reason == REASON_LEFT: fsm_goto(STATE_SET_NV3) elif reason == REASON_RIGHT: fsm_goto(STATE_SET_NV5) elif reason == REASON_PRESS: set_xy(12, 1) cache_8x8(num_to_hex(net_id)) saveNvParam(NV_NETWORK_ID, net_id) fsm_goto(STATE_SET_NV7) elif G_FSM_STATE == STATE_SET_NV5: net_id = getNetId() if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(12, 1) cache_8x8(num_to_hex(net_id)) set_xy(14, 1) print_invert(net_id >> 4) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: if G_GENERAL_COUNTDOWN % 2: set_xy(14, 1) print_invert(net_id >> 4) else: set_xy(12, 1) cache_8x8(num_to_hex(net_id)) elif reason == REASON_UP: setNetId(((net_id + 0x0010) & 0x00f0) | (net_id & 0xff0f)) fsm_goto(STATE_SET_NV5) elif reason == REASON_DOWN: setNetId(((net_id + 0x00f0) & 0x00f0) | (net_id & 0xff0f)) fsm_goto(STATE_SET_NV5) elif reason == REASON_LEFT: fsm_goto(STATE_SET_NV4) elif reason == REASON_RIGHT: fsm_goto(STATE_SET_NV6) elif reason == REASON_PRESS: set_xy(12, 1) cache_8x8(num_to_hex(net_id)) saveNvParam(NV_NETWORK_ID, net_id) fsm_goto(STATE_SET_NV7) elif G_FSM_STATE == STATE_SET_NV6: net_id = getNetId() if reason == REASON_GOTO: G_GENERAL_COUNTDOWN = CONTROLLER_TIMEOUT set_xy(12, 1) cache_8x8(num_to_hex(net_id)) set_xy(15, 1) print_invert(net_id) elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: if G_GENERAL_COUNTDOWN % 2: set_xy(15, 1) print_invert(net_id) else: set_xy(12, 1) cache_8x8(num_to_hex(net_id)) elif reason == REASON_UP: setNetId(((net_id + 0x0001) & 0x000f) | (net_id & 0xfff0)) fsm_goto(STATE_SET_NV6) elif reason == REASON_DOWN: setNetId(((net_id + 0x000f) & 0x000f) | (net_id & 0xfff0)) fsm_goto(STATE_SET_NV6) elif reason == REASON_LEFT: fsm_goto(STATE_SET_NV5) elif reason == REASON_PRESS: set_xy(12, 1) cache_8x8(num_to_hex(net_id)) saveNvParam(NV_NETWORK_ID, net_id) fsm_goto(STATE_SET_NV7) elif G_FSM_STATE == STATE_SET_NV7: feature_bits = loadNvParam(NV_FEATURE_BITS_ID) if feature_bits == 0x011f: fsm_goto(STATE_SET_NV8) elif feature_bits == 0x051f: fsm_goto(STATE_SET_NV9) elif feature_bits == 0x091f: fsm_goto(STATE_SET_NV10) else: fsm_goto(STATE_SET_NV11) elif G_FSM_STATE == STATE_SET_NV8: if reason == REASON_GOTO: set_xy(12, 2) print_invert(0) cache_8x8("1") elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(12, 2) if G_GENERAL_COUNTDOWN % 2: print_invert(0) else: cache_8x8("0") elif reason == REASON_UP: fsm_goto(STATE_SET_NV9) elif reason == REASON_DOWN: fsm_goto(STATE_SET_NV11) elif reason == REASON_PRESS: set_xy(12, 2) cache_8x8("0") if loadNvParam(NV_FEATURE_BITS_ID) != 0x011f: saveNvParam(NV_FEATURE_BITS_ID, 0x011f) G_NEED_REBOOT = True fsm_goto(STATE_SET_NV12) elif G_FSM_STATE == STATE_SET_NV9: if reason == REASON_GOTO: set_xy(12, 2) print_invert(0) cache_8x8("5") elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(12, 2) if G_GENERAL_COUNTDOWN % 2: print_invert(0) else: cache_8x8("0") elif reason == REASON_UP: fsm_goto(STATE_SET_NV10) elif reason == REASON_DOWN: fsm_goto(STATE_SET_NV8) elif reason == REASON_PRESS: set_xy(12, 2) cache_8x8("0") if loadNvParam(NV_FEATURE_BITS_ID) != 0x051f: saveNvParam(NV_FEATURE_BITS_ID, 0x051f) G_NEED_REBOOT = True fsm_goto(STATE_SET_NV12) elif G_FSM_STATE == STATE_SET_NV10: if reason == REASON_GOTO: set_xy(12, 2) print_invert(0) cache_8x8("9") elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(12, 2) if G_GENERAL_COUNTDOWN % 2: print_invert(0) else: cache_8x8("0") elif reason == REASON_UP: fsm_goto(STATE_SET_NV11) elif reason == REASON_DOWN: fsm_goto(STATE_SET_NV9) elif reason == REASON_PRESS: set_xy(12, 2) cache_8x8("0") if loadNvParam(NV_FEATURE_BITS_ID) != 0x091f: saveNvParam(NV_FEATURE_BITS_ID, 0x091f) G_NEED_REBOOT = True fsm_goto(STATE_SET_NV12) elif G_FSM_STATE == STATE_SET_NV11: if reason == REASON_GOTO: set_xy(12, 2) print_invert(0) cache_8x8("d") elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(12, 2) if G_GENERAL_COUNTDOWN % 2: print_invert(0) else: cache_8x8("0") elif reason == REASON_UP: fsm_goto(STATE_SET_NV8) elif reason == REASON_DOWN: fsm_goto(STATE_SET_NV10) elif reason == REASON_PRESS: set_xy(12, 2) cache_8x8("0") if loadNvParam(NV_FEATURE_BITS_ID) != 0x0d1f: saveNvParam(NV_FEATURE_BITS_ID, 0x0d1f) G_NEED_REBOOT = True fsm_goto(STATE_SET_NV12) elif G_FSM_STATE == STATE_SET_NV12: if loadNvParam(NV_AES128_ENABLE_ID): fsm_goto(STATE_SET_NV13) else: fsm_goto(STATE_SET_NV14) elif G_FSM_STATE == STATE_SET_NV13: if reason == REASON_GOTO: set_xy(12, 3) print_invert(0) cache_8x8("n ") elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(12, 3) if G_GENERAL_COUNTDOWN % 2: print_invert(0) else: cache_8x8("O") elif (reason == REASON_UP) or (reason == REASON_DOWN): fsm_goto(STATE_SET_NV14) elif reason == REASON_PRESS: set_xy(12, 3) cache_8x8("O") if loadNvParam(NV_AES128_ENABLE_ID) != 1: saveNvParam(NV_AES128_ENABLE_ID, 1) G_NEED_REBOOT = True fsm_goto(STATE_SET_NV15) elif G_FSM_STATE == STATE_SET_NV14: if reason == REASON_GOTO: set_xy(12, 3) print_invert(0) cache_8x8("ff") elif reason == REASON_1S_HOOK: if general_countdown(): fsm_goto(STATE_CONTROLLER) else: set_xy(12, 3) if G_GENERAL_COUNTDOWN % 2: print_invert(0) else: cache_8x8("O") elif (reason == REASON_UP) or (reason == REASON_DOWN): fsm_goto(STATE_SET_NV13) elif reason == REASON_PRESS: set_xy(12, 3) cache_8x8("O") if loadNvParam(NV_AES128_ENABLE_ID) != 0: saveNvParam(NV_AES128_ENABLE_ID, 0) G_NEED_REBOOT = True fsm_goto(STATE_SET_NV15) if G_FSM_STATE == STATE_SET_NV15: if G_NEED_REBOOT: reboot() else: fsm_goto(STATE_MENU5) elif G_FSM_STATE == STATE_SIMON1: if reason == REASON_GOTO: set_xy(0, 0) cache_8x8("Watch! 3 2 1 ") set_xy(0, 1) cache_8x8(" ") set_xy(0, 2) cache_8x8(" ") set_xy(0, 3) cache_8x8(" ") G_SEQUENCE = "" elif reason == REASON_1S_HOOK: fsm_goto(STATE_SIMON2) elif G_FSM_STATE == STATE_SIMON2: if reason == REASON_GOTO: set_xy(8, 0) cache_8x8(" ") elif reason == REASON_1S_HOOK: fsm_goto(STATE_SIMON3) elif G_FSM_STATE == STATE_SIMON3: if reason == REASON_GOTO: set_xy(10, 0) cache_8x8(" ") elif reason == REASON_1S_HOOK: fsm_goto(STATE_SIMON4) elif G_FSM_STATE == STATE_SIMON4: if reason == REASON_GOTO: set_xy(0, 0) cache_8x8("Watch! ") elif reason == REASON_1S_HOOK: fsm_goto(STATE_SIMON5) elif G_FSM_STATE == STATE_SIMON5: G_SEQUENCE += chr(random() % 4) G_POSITION = 0 fsm_goto(STATE_SIMON6) elif G_FSM_STATE == STATE_SIMON6: if reason == REASON_GOTO: arrow = ord(G_SEQUENCE[G_POSITION]) if arrow == 0: set_xy(7, 1) cache_8x8(UP_ARROW) elif arrow == 1: set_xy(4, 2) cache_8x8(LEFT_ARROW) elif arrow == 2: set_xy(10, 2) cache_8x8(RIGHT_ARROW) else: set_xy(7, 3) cache_8x8(DOWN_ARROW) elif reason == REASON_1S_HOOK: set_xy(7, 1) cache_8x8(" ") set_xy(4, 2) cache_8x8(" ") set_xy(7, 3) cache_8x8(" ") fsm_goto(STATE_SIMON7) elif G_FSM_STATE == STATE_SIMON7: if reason == REASON_1S_HOOK: G_POSITION += 1 fsm_goto( STATE_SIMON6 if G_POSITION < len(G_SEQUENCE) else STATE_SIMON8) elif G_FSM_STATE == STATE_SIMON8: # if reason == REASON_1S_HOOK: set_xy(0, 0) cache_8x8(" ") G_POSITION = 0 fsm_goto(STATE_SIMON9) elif G_FSM_STATE == STATE_SIMON9: if reason == REASON_1S_HOOK: set_xy(0, 0) cache_8x8("Repeat!") G_GENERAL_COUNTDOWN = 2 fsm_goto(STATE_SIMON10) elif G_FSM_STATE == STATE_SIMON10: arrow = None if reason == REASON_UP: arrow = 0 elif reason == REASON_LEFT: arrow = 1 elif reason == REASON_RIGHT: arrow = 2 elif reason == REASON_DOWN: arrow = 3 elif reason == REASON_1S_HOOK: G_GENERAL_COUNTDOWN -= 1 if G_GENERAL_COUNTDOWN == 0: fsm_goto(STATE_SIMON11) if arrow is not None: if arrow == ord(G_SEQUENCE[G_POSITION]): if arrow == 0: set_xy(7, 1) cache_8x8(UP_ARROW) elif arrow == 1: set_xy(4, 2) cache_8x8(LEFT_ARROW) elif arrow == 2: set_xy(10, 2) cache_8x8(RIGHT_ARROW) else: set_xy(7, 3) cache_8x8(DOWN_ARROW) G_POSITION += 1 fsm_goto(STATE_SIMON12 if G_POSITION < len(G_SEQUENCE) else STATE_SIMON13) else: fsm_goto(STATE_SIMON11) elif G_FSM_STATE == STATE_SIMON11: if reason == REASON_GOTO: set_xy(7, 2) cache_8x8("X") G_GENERAL_COUNTDOWN = 2 elif reason == REASON_1S_HOOK: G_GENERAL_COUNTDOWN -= 1 if G_GENERAL_COUNTDOWN == 0: fsm_goto(STATE_MENU8) elif G_FSM_STATE == STATE_SIMON12: if reason == REASON_1S_HOOK: set_xy(7, 1) cache_8x8(" ") set_xy(4, 2) cache_8x8(" ") set_xy(7, 3) cache_8x8(" ") fsm_goto(STATE_SIMON10) elif G_FSM_STATE == STATE_SIMON13: if reason == REASON_1S_HOOK: set_xy(0, 0) cache_8x8(" ") set_xy(7, 1) cache_8x8(" ") set_xy(4, 2) cache_8x8(" ") set_xy(7, 3) cache_8x8("Good job!") G_GENERAL_COUNTDOWN = 2 fsm_goto(STATE_SIMON14) elif G_FSM_STATE == STATE_SIMON14: if reason == REASON_1S_HOOK: G_GENERAL_COUNTDOWN -= 1 if G_GENERAL_COUNTDOWN == 0: set_xy(7, 3) cache_8x8(" ") fsm_goto(STATE_SIMON4)
def clear_screen(): for y in xrange(4): set_xy(0, y) cache_8x8(" ")
def redraw_scroll(): """Redraw the screen with a scrolling display. :return: Mode to enter :rtype: int """ global G_SCROLL_POS, G_DISPLAY_MODE num_nodes = (len(G_SAVED_DISPLAY) - 2) / 5 # No items? if num_nodes == 0: return redraw_3() selected = ord(G_SAVED_DISPLAY[0]) # Beyond the last item? if selected > (num_nodes - 1): selected = num_nodes - 1 # Before the first displayed item? if selected <= G_SCROLL_POS: G_SCROLL_POS = (selected - 1) if selected > 0 else 0 # Selected the fourth row (or beyond) if selected >= (G_SCROLL_POS + 3): # On the last item? if selected == (num_nodes - 1): G_SCROLL_POS = (selected - 3) if selected >= 3 else 0 else: G_SCROLL_POS = (selected - 2) if selected >= 2 else 0 # Would scrolling up show more items? while (G_SCROLL_POS > 0) and ((G_SCROLL_POS + 3) > (num_nodes - 1)): G_SCROLL_POS -= 1 set_xy(0, 0) cache_8x8(UP_ARROW if selected > 0 else RIGHT_ARROW) line = expand_node_entry(G_SAVED_DISPLAY[(G_SCROLL_POS * 5) + 2:(G_SCROLL_POS * 5) + 7]) cache_8x8(line[1:-1]) set_xy(0, 1) if (G_SCROLL_POS + 1) > (num_nodes - 1): cache_8x8(SPACES_16) else: if selected == (G_SCROLL_POS + 1): cache_8x8(RIGHT_ARROW) else: cache_8x8(DOWN_ARROW if (G_SCROLL_POS + 1) == (num_nodes - 1) else " ") line = expand_node_entry(G_SAVED_DISPLAY[(G_SCROLL_POS * 5) + 7:(G_SCROLL_POS * 5) + 12]) cache_8x8(line[1:-1]) set_xy(0, 2) if (G_SCROLL_POS + 2) > (num_nodes - 1): cache_8x8(SPACES_16) else: if selected == (G_SCROLL_POS + 2): cache_8x8(RIGHT_ARROW) else: cache_8x8(DOWN_ARROW if (G_SCROLL_POS + 2) == (num_nodes - 1) else " ") line = expand_node_entry(G_SAVED_DISPLAY[(G_SCROLL_POS * 5) + 12:(G_SCROLL_POS * 5) + 17]) cache_8x8(line[1:-1]) set_xy(0, 3) if (G_SCROLL_POS + 3) > (num_nodes - 1): cache_8x8(SPACES_16) else: cache_8x8(RIGHT_ARROW if selected == (G_SCROLL_POS + 3) else DOWN_ARROW) line = expand_node_entry(G_SAVED_DISPLAY[(G_SCROLL_POS * 5) + 17:(G_SCROLL_POS * 5) + 22]) cache_8x8(line[1:-1]) return DMODE_SCROLL_LIST
def print_neighbors(): """Print the neighbors line at top of screen.""" set_xy(0, 0) cache_8x8("Neighbors: " + BATTERY_CHARS[ord(G_SAVED_DISPLAY[1])])