/// <summary> /// Switch power state of the given device to the requested power state /// </summary> /// <param name="device"></param> /// <param name="requestedPowerState"></param> /// <returns cref="PowerState">New PowerSate. Power state is unknown if the switch fails</returns> /// <exception cref="CecException">Can't connect to CecBus</exception> public PowerState SwitchDevicePowerState(CecLogicalAddress device, PowerState requestedPowerState) { PowerState state = PowerState.PowerUnknown; switch (requestedPowerState) { case PowerState.PowerOff: if (IsDeviceReady(device) && connection.StandbyDevices(device)) { state = PowerState.PowerOff; } break; case PowerState.PowerOn: // Close and reopen the connection as we lose it when we go to sleep connection.Close(); if (!connection.Open(controller.ComPort, timeout)) { throw new CecException(Resources.ErrorNoCecControllerConnection); } // Register active source of the connection connection.SetActiveSource(CecDeviceType.PlaybackDevice); if (IsDeviceReady(device) && connection.PowerOnDevices(device)) { state = PowerState.PowerOn; } break; default: break; } return(state); }
public bool SetSource(bool active) { if (active) { WriteLog("Sent active source."); return(_lib.SetActiveSource(CecDeviceType.Reserved)); } WriteLog("Sent inactive source."); return(_lib.SetInactiveView()); }
public void Transmit(CecCommand cmd, bool activateSource = false) { if (cmd.Opcode == CecOpcode.Standby && Lib.GetDevicePowerStatus(CecLogicalAddress.Tv) != CecPowerStatus.On) { return; } if (_settingActiveSource) { return; } if (activateSource) { _settingActiveSource = true; Lib.SetActiveSource(CecDeviceType.PlaybackDevice); /* * Since we dont know when its fully connected as a source, * we have to take a guess on when its done, if we dont * our queued cmd wont trigger. */ Thread.Sleep(CMD_DELAY); _settingActiveSource = false; Transmit(cmd); return; } //First try and transmit command, if that fails try and reconnect the send again. if (!Lib.Transmit(cmd)) { Transmit(cmd, true); return; } //We need some delay if we suceeded with the transmission, //else the tv will spazz out if overloaded with commands. Thread.Sleep(CMD_DELAY); }
/// <summary> /// Initialize the Cec bus without callback methods (no need) /// </summary> /// <param name="timeout">Timeout for the connections to the bus. Must be positive</param> /// <exception cref="CecException">Unable to initialize Cec Bus or can't find controller</exception> /// <exception cref="ArgumentOutOfRangeException">timeout not strictly positive</exception> public CecBus(int timeout) { if (timeout <= 0) { throw new ArgumentOutOfRangeException(nameof(timeout)); } this.timeout = timeout; configuration = new LibCECConfiguration(); configuration.DeviceTypes.Types[0] = CecDeviceType.PlaybackDevice; configuration.DeviceName = Resources.StrCecDeviceName; configuration.ClientVersion = LibCECConfiguration.CurrentVersion; configuration.SetCallbacks(this); connection = new LibCecSharp(configuration) ?? throw new CecException(Resources.ErrorNoCecBus); connection.InitVideoStandalone(); // Get the controller on the bus controller = null; CecAdapter[] adapters = connection.FindAdapters(string.Empty); if (adapters.Length > 0) { controller = adapters[controllerId]; } else { throw new CecException(Resources.ErrorNoCecController); } // Connection must be openned before going to suspend mode as the SCM stop the thread if we try to open // the connection during power event because it's an async operation if (!connection.Open(controller.ComPort, timeout)) { throw new CecException(Resources.ErrorNoCecControllerConnection); } // Register active source of the connection connection.SetActiveSource(CecDeviceType.PlaybackDevice); }
public void SetActiveSource() { lib.SetActiveSource(CecDeviceType.PlaybackDevice); }