public static bool DelcomLEDAction(uint hUSB, LightColors color, LightStates action) { bool success = false; //lock here so multiple threads don't try to toggle LEDs at once //(though presently only one instance of the program works with the light at a time anyway) lock (DelcomLightWrapper.CurrentLEDStates) { LightStates dictVal; if (DelcomLightWrapper.CurrentLEDStates.TryGetValue(color, out dictVal) && dictVal == action) { //that LED is already in appropriate state success = true; } else { for (int i = 0; i < LEDMaxFailures; i++) { if (Delcom.DelcomLEDControl(hUSB, (byte)color, (byte)action) == 0) { DelcomLightWrapper.CurrentLEDStates[color] = action; success = true; break; } else { System.Threading.Thread.Sleep(LEDWaitTime); } } } } return(success); }
/// <summary> /// Internal method to set a single color of light. /// </summary> private bool SetSingleLight(DelcomLightColor color, DelcomLightState newState) { bool result = false; lock (this.lightStates) { DelcomLightState currentState; if (this.lightStates.TryGetValue(color, out currentState) && currentState == newState) { // This color is already in appropriate state. result = true; } else { for (int i = 0; i < DelcomLightWrapper.MaxLightRetries; i++) { if (Delcom.DelcomLEDControl(this.deviceHandle, (byte)color, (byte)newState) == 0) { this.lightStates[color] = newState; result = true; break; } else { // Not to log each failure because a) API does not provide any error detail and // b) caller will make an error log if all retries fail. System.Threading.Thread.Sleep(DelcomLightWrapper.LightRetryInterval); } } } } return(result); }
public static bool DelcomLEDAllAction(uint hUSB, LightStates action) { bool success = false; List <LightColors> lightsRemaining = new List <LightColors>(); //lock here so multiple threads don't try to toggle LEDs at once //(though presently only one instance of the program works with the light at a time anyway) lock (DelcomLightWrapper.CurrentLEDStates) { foreach (KeyValuePair <LightColors, LightStates> lightAndState in DelcomLightWrapper.CurrentLEDStates) { if (lightAndState.Value != action) { lightsRemaining.Add(lightAndState.Key); } } if (lightsRemaining.Count == 0) { //all LEDs already in appropriate state success = true; } else { for (int i = 0; i < LEDMaxFailures; i++) { List <LightColors> lightsFailed = new List <LightColors>(); foreach (LightColors remainingLight in lightsRemaining) { if (Delcom.DelcomLEDControl(hUSB, (byte)remainingLight, (byte)action) == 0) { DelcomLightWrapper.CurrentLEDStates[remainingLight] = action; } else { lightsFailed.Add(remainingLight); } } if (lightsFailed.Count == 0) { success = true; break; } lightsRemaining = lightsFailed; System.Threading.Thread.Sleep(LEDWaitTime); } } } return(success); }
/// <summary> /// Helper method to set the light value to the currently managed states upon reconnection. /// This is inteneded after the reconnection and we do not make retry opertaion. /// In the worst case, the light state becomes bad state, but next regular request will fix it. /// </summary> private bool ResyncLights() { bool result = true; var colors = new List <DelcomLightColor>(this.lightStates.Keys); foreach (var lightColorState in lightStates) { Delcom.DelcomLEDControl(this.deviceHandle, (byte)lightColorState.Key, (byte)lightColorState.Value); } return(result); }
internal override void TurnOff() { Delcom.DelcomLEDControl(_device, Delcom.BLUELED, Delcom.LEDOFF); }
internal override void ChangeColourToAmber() { Delcom.DelcomLEDControl(_device, Delcom.YELLOWLED, Delcom.LEDON); }
internal override void ChangeColourToGreen() { Delcom.DelcomLEDControl(_device, Delcom.GREENLED, Delcom.LEDON); }
internal override void ChangeColourToRed() { Delcom.DelcomLEDControl(_device, Delcom.REDLED, Delcom.LEDON); }