public void AddButtonLightMapping(ButtonEnum button, ControllerLEDEnum LED, bool lightOnHold, int intensity) { /*if (!ButtonLights.ContainsKey(button)) * ButtonLights.Add((int)button, new LightProperties(LED, lightOnHold, intensity)); * else*/ if (ButtonLights.Contains((int)button)) { ButtonLights.Remove((int)button);//to save on later garbage collection } ButtonLights[(int)button] = new LightProperties(LED, lightOnHold, intensity); }
/// <summary> /// Checks the button state based on the raw data returned, and if it has, the ButtonStateChanged event is raised /// </summary> /// <param name="buf">Raw data buffer retrieved from the controller</param> private void CheckStateChanged(byte[] buf) { ButtonEnum[] values = (ButtonEnum[])Enum.GetValues(typeof(ButtonEnum)); stateChangedArray = new ButtonState[values.Length]; bool updateLights = false; for (int i = 0; i < values.Length; i++) { ButtonMasks.ButtonMask mask = ButtonMasks.MaskList[(int)values[i]]; ButtonState state = new ButtonState(); state.button = (ButtonEnum)values[i]; state.currentState = ((buf[mask.bytePos] & mask.maskValue) > 0); state.changed = isStateChanged(buf, mask.bytePos, mask.maskValue); ButtonEnum currentButton = (ButtonEnum)(i); //only do something if button changed, and button was pressed and button is in hashtable if (state.changed) { if (updateGearLights && state.button == ButtonEnum.GearLeverStateChange) { GearLightsRefresh((int)unchecked ((sbyte)buf[25]));//copied this code from GearLever accessor, changed it since we need ot updateLights = true; } //check button - light mapping if (ButtonLights.ContainsKey(i)) { updateLights = true; LightProperties currentLightProperties = (LightProperties)(ButtonLights[i]); if (currentLightProperties.lightOnHold) { if (state.currentState) { SetLEDState(currentLightProperties.LED, currentLightProperties.intensity, false); } else { SetLEDState(currentLightProperties.LED, 0, false); } } else if (state.currentState) //only switch when button is pressed { int result = GetLEDState(currentLightProperties.LED); if (result > 0) //light is on { SetLEDState(currentLightProperties.LED, 0); //turn off } else { SetLEDState(currentLightProperties.LED, currentLightProperties.intensity); } } } if (ButtonKeys.ContainsKey(i)) { KeyProperties currentKeyProperties = (KeyProperties)(ButtonKeys[i]); if (currentKeyProperties.useModifier) { if (state.currentState)//button is pressed, then press key { InputSimulator.SimulateModifiedKeyStroke(currentKeyProperties.modifier, currentKeyProperties.keyCode1); } } else { if (currentKeyProperties.holdDown) //send separate down and up keypresses { if (state.currentState) { InputSimulator.SimulateKeyDown(currentKeyProperties.keyCode1); } else { InputSimulator.SimulateKeyUp(currentKeyProperties.keyCode1); } } else { if (state.currentState) { InputSimulator.SimulateKeyPress(currentKeyProperties.keyCode1); } } } } } stateChangedArray[(int)values[i]] = state; } if (updateLights) { RefreshLEDState(); } }