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); }
/// <summary> /// Find and open a Delcom device. this.deviceHandle is set upon success. /// </summary> /// <returns>true on success.</returns> private bool OpenDevice() { if (this.deviceHandle != InvalidDevcieHandle) { this.CloseDevice(); } StringBuilder deviceName = new StringBuilder(Delcom.MAXDEVICENAMELEN); // Search for the first match USB device, For USB IO Chips use Delcom.USBIODS // With Generation 2 HID devices, you can pass a TypeId of 0 to open any Delcom device. int findResult = Delcom.DelcomGetNthDevice(Delcom.USBDELVI, 0, deviceName); if (findResult == 0) { Trace.TraceError("Device was not found"); } else { uint newDeviceHandle = Delcom.DelcomOpenDevice(deviceName, 0); if (newDeviceHandle == InvalidDevcieHandle) { Trace.TraceError("Device was found, but failed to be connected. device = {0}", deviceName.ToString()); } else { this.deviceHandle = newDeviceHandle; // Disable auto confirmation mode where the buzzer will sound when the button is pressed. Delcom.DelcomEnableAutoConfirm(this.deviceHandle, 0); } } return(this.deviceHandle != InvalidDevcieHandle); }
/// <summary> /// Close current device. No-op if no device is opened. /// </summary> private void CloseDevice() { if (this.deviceHandle != InvalidDevcieHandle) { Delcom.DelcomCloseDevice(this.deviceHandle); this.deviceHandle = InvalidDevcieHandle; } }
/// <summary> /// Get current button state. /// This internally checks the connection and try reconnection if needed. /// </summary> public DelcomButtonState GetButtonState() { if (!this.DeviceIsConnected()) { Trace.TraceWarning("Delcom light device seems disconnected. Reconnecting."); ReopenDevice(); } return((DelcomButtonState)Delcom.DelcomGetButtonStatus(this.deviceHandle)); }
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); }
public DelcomBuildLight() { var deviceName = new StringBuilder(Delcom.MAXDEVICENAMELEN); var doesDeviceExist = Delcom.DelcomGetNthDevice(Delcom.USBDELVI, 0, deviceName); if (doesDeviceExist == 0) { throw new IOException("Device does not exist"); } _device = Delcom.DelcomOpenDevice(deviceName, 0); }
/// <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); }
/// <summary> /// Method used to determine whether a specific device is still connected /// </summary> /// <param name="hUSB"></param> /// <returns></returns> public static bool isButtonConnected(uint hUSB) { int deviceVersion = Delcom.DelcomReadDeviceVersion(hUSB); //If no longer connected we will get a return value of 0 or 255 (manual says 0 but in practice we get 255) if (deviceVersion == 0 || deviceVersion == 255) { return(false); } return(true); }
public static uint OpenDelcomDevice() { int Result; uint hUSB; StringBuilder DeviceName = new StringBuilder(Delcom.MAXDEVICENAMELEN); // Search for the first match USB device, For USB IO Chips use Delcom.USBIODS // With Generation 2 HID devices, you can pass a TypeId of 0 to open any Delcom device. Result = Delcom.DelcomGetNthDevice(Delcom.USBDELVI, 0, DeviceName); hUSB = Delcom.DelcomOpenDevice(DeviceName, 0); // open the device return(hUSB); }
/// <summary> /// Constructor /// </summary> /// <param name="stateMachineInputCallback">delegate to call when there's an event to report</param> public DelcomLight(MainAppLogic.EnqueueStateMachineInput stateMachineInputCallback, TimeSpan holdTime) { hUSB = DelcomLightWrapper.TryOpeningDelcomDevice(); Delcom.DelcomEnableAutoConfirm(hUSB, 0); // Make sure we always start turned off DelcomLightWrapper.DelcomLEDAllAction(hUSB, DelcomLightWrapper.LightStates.Off); // remember the delegate so we can invoke when we get input this.stateMachineInputCallback = stateMachineInputCallback; //Initialize hold threshold from argument passed from Main this.holdThreshold = holdTime; // start a background thread to poll the device for input BackgroundWorker bgw1 = new BackgroundWorker(); bgw1.DoWork += delegate { this.BackgroundPollingWorker(); }; bgw1.RunWorkerAsync(); }
/// <summary> /// Determine whether the current device is still connected /// </summary> private bool DeviceIsConnected() { if (this.deviceHandle == InvalidDevcieHandle) { return(false); } // If no longer connected we will get a return value of 0 or 255 (manual says 0 but in practice we get 255). int deviceVersion = Delcom.DelcomReadDeviceVersion(this.deviceHandle); if (deviceVersion == 0 || deviceVersion == 255) { // This frequently happens (once in a few minutes) even on the healthy system. // Retry once before reporting back. Thread.Sleep(DelcomLightWrapper.DeviceConnectionCheckRetryInterval); deviceVersion = Delcom.DelcomReadDeviceVersion(this.deviceHandle); if (deviceVersion == 0 || deviceVersion == 255) { return(false); } } return(true); }
internal override void TurnOff() { Delcom.DelcomLEDControl(_device, Delcom.BLUELED, Delcom.LEDOFF); }
public static void CloseDelcomDevice(uint hUSB) { Delcom.DelcomCloseDevice(hUSB); }
internal override void ChangeColourToRed() { Delcom.DelcomLEDControl(_device, Delcom.REDLED, Delcom.LEDON); }
internal override void ChangeColourToGreen() { Delcom.DelcomLEDControl(_device, Delcom.GREENLED, Delcom.LEDON); }
internal override void ChangeColourToAmber() { Delcom.DelcomLEDControl(_device, Delcom.YELLOWLED, Delcom.LEDON); }
public static ButtonState DelcomGetButtonStatus(uint hUSB) { return((ButtonState)Delcom.DelcomGetButtonStatus(hUSB)); }