private bool OpenConnection() { CloseConnection(); CoolerMasterSDK.SetControlDevice(_data.DeviceType); // Detect if device is connected if (!CoolerMasterSDK.IsDevicePlug()) { return(false); } // Try to enable control of keyboard/mouse if (!CoolerMasterSDK.EnableLedControl(true)) { return(false); } _connected = true; // reset the last values. That means that *any* values that come in will be 'new', and be sent out. _lastValues = new Dictionary <int, byte>(); _nullCommands = new Dictionary <int, int>(); _setupDataBuffers(); _timeoutStopwatch.Reset(); _timeoutStopwatch.Start(); return(true); }
public override void UpdateState(int chainIndex, ICommand[] outputStates) { if (!_connected) { // Try to open the connection if (!OpenConnection()) { return; } } var matrix = COLOR_MATRIX.Create(); var changed = false; for (var i = 0; i < outputStates.Length; i++) { byte newValue = 0; if (outputStates[i] != null) { var command = outputStates[i] as _8BitCommand; if (command == null) { continue; } newValue = command.CommandValue; _nullCommands[i] = 0; } else { // it was a null command. We should turn it off; however, to avoid some potentially nasty flickering, // we will keep track of the null commands for this output, and ignore the first one. Any after that will // actually be sent through. if (_nullCommands[i] == 0) { _nullCommands[i] = 1; newValue = _lastValues[i]; } } if (_lastValues[i] == newValue) { continue; } changed = true; matrix.SetKeyValue(i, _channelCount, newValue); _lastValues[i] = newValue; } // don't bother writing anything if we haven't acutally *changed* any values... // also, send at least a 'null' update command every 10 seconds. if (!changed && _timeoutStopwatch.ElapsedMilliseconds < 10000) { return; } try { _timeoutStopwatch.Restart(); // Write Data if (!CoolerMasterSDK.SetAllLedColor(matrix)) { throw new Exception("SetAllLedColor() Failed"); } CoolerMasterSDK.RefreshLed(); } catch (Exception ex) { //Logging.Warn(LogTag + "failed to write data to device during update", ex); CloseConnection(); } }
private void CloseConnection() { CoolerMasterSDK.EnableLedControl(false); _connected = false; }