Two hosts power switch ###################### General principle ================= This project detects up to two host power state through the their usb port and switch on their associated devices' mains outlets (screen, sound amplifier, printer, etc). Devices might be shared (the same printer is used by both computers), in this case, the device is switched on by whatever computer is detected on. Flowchart ========= Outlet table update:: .------------------. .-------------------. | Host state | | Skip filter input | | change detection | | (usb control) | '------------------' '-------------------' .--------------. | .-------------. | .------>| Switch relay | '-->| Skip filter |<--' | '--------------' '-------------' | | .---------------------. v | Start on/off timers | .---------------. '---------------------' | Update target | ^ | table |-------------' '---------------' Skip table: serial skip update -> update skip table computer state table update -> reset skip lines of updated computers Computer state table: Host state change detection -> lookup skip table -> update computer state table computer button toggle -> update computer state table serial logical computer state toggle -> update computer state table Outlet table: outlet toggle button -> update table computer state table update -> update table serial toggle command -> update table Outlet table update can start timer (doing eventually a relay switch on or off action), switch on relay directly, switch off relay directly. Feature list ============ * Detect when one of the host is up (not in standby), switching on its associated devices (mains outlet). * Each computer has its own set of associated outlets to switch on. * An outlet may be shared between computers, if at least one of the computers is on the outlet is on (logical OR). * Each outlet have an optional delay before it switch on or off. Delay for on and off may be different. * Configuration for outlet associations and for the switch delay is saved in non-volatile memory. * Computer standby detection is isolated and done by a mini-usb plug (calibrated for standard 5 volts supply, but you might change the input tension according to the optical coupler and input resistor, see schematic for more details). Molex to usb cable might be done if all usb ports of the computer are always on and its standby is never detected. * There is one button for each computer to toggle its state (thus overriding its current state). The override is reset at the next computer state change. If the override reset is the same as the current state, you will see no change: if an override set the state to on, and the computer is switched on, the state change will not be seen until computer is in standby or off again or if another override is done. * There is one button for each outlet so it can be toggled off or on. This toggle bypass the configured delay. The outlet can be toggled again normally according to the associated computer(s) state change. * If at least one of the computers is on, global led is on. An individual led indicate the state of each outlet. * Very low power when no outlet nor computer is on: the MCU will sleep and consume near nothing and all led will be off. * Instead of pressing button, the dedicated control usb connection can be used to toggle state of things and to set a detection skip for the next off to on detection of a computer. * A low consumption power converter (near 0 watt in full standby) is used to power the MCU. Advantages compared to being powered by control port include being autonomous, not using control port to switch relay (might induce little current spikes), and keeping current state when unplugging controlling computer. Serial commands =============== - `CNY`: Change computer N (0/1) state to Y (0/1) - `TN`: Toggle computer N (0/1) state. - `cNY`: Change outlet N (0-4) state to Y (0/1) - `tN`: Toggle outlet N (0-4) state. - `SNY`: Update computer skip line to Y (0/1, 1 means skipping the next on detection change). Rationale ========= The power switch could have triggered computer wakeup instead of doing computer standby detection, but it is more complicated and it prevents using the keyboard or another source of wakeup to switch on its associated devices. The disadvantage is to need a way to skip the switch-on of the associated devices when it is not needed (for example when doing wakeonlan to read data in another location, one does not need the associated devices to be on). In my case, the wake-on-lan was an exception to the normal usage of my computer, so I made the skip choice to be in the workflow. State machines ============== - debouncing buttons state tables:: #0 button released [pin change interrupt] -> debounce start [timeout] -> timeout [button pressed] -> #1 + event button_down -> timeout [button released] -> #0 #1 button pressed [timeout] -> first up [timeout] -> debounce [timeout] -> timeout [button pressed] -> #1 -> timeout [button released] -> #0 + event button_up Ex of a transition table: State Event Next state Transition function stopped play_event playing do_play playing stop_event stopped do_stop playing pause_event paused do_pause paused play_event playing do_resume paused stop_event stopped do_stop ex of fsm implem for another language: https://github.com/hyperandroid/Automata ex of fsm implem in c: https://github.com/gitrealname/ofsm formal description in yaml ? fsm0: initialstate: state0 state0: enter: action_enter exit: action_exit events: - event: ok nextstate: state1 - event: ... state1: ...