コード例 #1
0
        /// <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();
        }
コード例 #2
0
        /// <summary>
        ///     Change the color of the light, including options to flash or show for only a particular duration
        /// </summary>
        /// <param name="inputColor"></param>
        /// <param name="steady"></param>
        /// <param name="duration"></param>
        public void ChangeColor(DelcomColor inputColor, bool steady, TimeSpan?duration)
        {
            lock (this)
            {
                changeColorRequestId++;

                if (inputColor == DelcomColor.Off)
                {
                    if (!DelcomLightWrapper.DelcomLEDAllAction(this.hUSB, DelcomLightWrapper.LightStates.Off) && Program.RunFromConsole)
                    {
                        Trace.TraceInformation(DateTime.Now + ": LED failure: all off");
                        Trace.Flush();
                    }
                }
                else
                {
                    // convert from the publicly exposed color to the internal value
                    DelcomLightWrapper.LightColors color = ConvertColor(inputColor);

                    DelcomLightWrapper.LightStates action = steady ? DelcomLightWrapper.LightStates.On
                                                                   : DelcomLightWrapper.LightStates.Flash;

                    if (!DelcomLightWrapper.DelcomLEDAllAction(this.hUSB, DelcomLightWrapper.LightStates.Off) && Program.RunFromConsole)
                    {
                        Trace.TraceInformation(DateTime.Now + ": LED failure: all off");
                        Trace.Flush();
                    }
                    if (!DelcomLightWrapper.DelcomLEDAction(this.hUSB, color, action) && Program.RunFromConsole)
                    {
                        Trace.TraceInformation(DateTime.Now + ": LED failure: " + Enum.GetName(typeof(DelcomLightWrapper.LightColors), color) +
                                               " " + Enum.GetName(typeof(DelcomLightWrapper.LightStates), action));
                        Trace.Flush();
                    }

                    // We need to only have the light on for the requested duration
                    if (duration != null)
                    {
                        // Create a callback that will safely turn the light off
                        TimerCallback callback = new TimerCallback(delegate(object state)
                        {
                            // Get the current button action (as of the callback)
                            int currentButtonAction = -1;
                            lock (this)
                            {
                                currentButtonAction = this.changeColorRequestId;
                            }

                            int rememberedButtonAction = (int)state;

                            // Compare the current buttonaction to the remembered action
                            if (currentButtonAction == rememberedButtonAction)
                            {
                                // Only turn the light off if they still match, otherwise we've moved on to a new action
                                if (!DelcomLightWrapper.DelcomLEDAllAction(this.hUSB, DelcomLightWrapper.LightStates.Off) && Program.RunFromConsole)
                                {
                                    Trace.TraceInformation(DateTime.Now + ": LED failure: all off");
                                    Trace.Flush();
                                }
                            }
                            else
                            {
                                // Another request has come in while waiting for the timer to fire, ignore the delayed request to turn the light off
                            }
                        });

                        // Make a timer
                        Timer ledTimer = new Timer(
                            callback,
                            changeColorRequestId,
                            duration.Value,
                            TimeSpan.Zero);
                    }
                }
            }
        }