def evaluate_lightsaber(hardware: Hardware, state: State) -> State: new_state = state.__copy__() seconds_button_was_held_for = seconds_button_was_pressed(hardware) if seconds_button_was_held_for >= 4: actions.mode_select(hardware, state) new_state = new_state.__copy__(initial_mode=mode.MODE_SELECT) elif seconds_button_was_held_for > 0 and state.state == mode.OFF: actions.power_on(hardware, state) new_state.state = mode.ON elif seconds_button_was_held_for > 0 and state.state == mode.ON: actions.power_off(hardware, state) new_state.state = mode.OFF elif acceleration_total(hardware) >= CLASH_THRESHOLD: actions.clash(hardware, state) elif acceleration_total(hardware) >= SWING_THRESHOLD: actions.swing(hardware, state) elif state.state == mode.ON and time.monotonic( ) - state.time_since_wav_was_looped >= 4: actions.cycle_idle_loop(hardware, state) new_state = state.__copy__() new_state.time_since_wav_was_looped = time.monotonic() return new_state
def evaluate_mode_select(hardware: Hardware, state: State) -> State: new_state = state.__copy__() seconds_button_was_held_for = seconds_button_was_pressed(hardware) if seconds_button_was_held_for >= 4 and state.mode_selector.current( ).name == 'LIGHTSABER': actions.power_on(hardware, state) new_state.mode = mode.LIGHTSABER new_state.state = mode.ON new_state.sounds = sound.Lightsaber() elif seconds_button_was_held_for >= 4 and state.mode_selector.current( ).name == 'COLOR_SELECT': actions.activate_color_change_mode(hardware) new_state = State(initial_mode=mode.COLOR_CHANGE, initial_color=colors.ALL_COLORS[0]) elif seconds_button_was_held_for >= 4 and state.mode_selector.current( ).name == 'WOWSABER': actions.power_on(hardware, state) new_state.mode = mode.LIGHTSABER new_state.state = mode.ON new_state.sounds = sound.Wowsaber() elif seconds_button_was_held_for > 0 and state.mode == mode.MODE_SELECT: new_state = state.__copy__() new_state.mode_selector.next() actions.mode_select(hardware, state) return new_state
def evaluate_color_change(hardware: Hardware, state: State) -> State: new_state = state.__copy__() seconds_button_was_held_for = seconds_button_was_pressed(hardware) if seconds_button_was_held_for >= 4: actions.power_on(hardware, state) new_state.mode = mode.LIGHTSABER new_state.state = mode.ON elif seconds_button_was_held_for > 0: actions.next_color(hardware, state) new_state = state.__copy__( initial_color=colors.next_color(state.color)) return new_state
def run(state: State, hardware: Hardware) -> State: if state.mode == mode.LIGHTSABER: return state_manager.evaluate_lightsaber(hardware, state) elif state.mode == mode.MODE_SELECT: return state_manager.evaluate_mode_select(hardware, state) elif state.mode == mode.COLOR_CHANGE: return state_manager.evaluate_color_change(hardware, state) else: return state.__copy__()
def get_action(state: State, hardware: Hardware): return state.__copy__()