コード例 #1
0
        /// <summary>
        /// Updates the output of the toy.
        /// </summary>
        public override void UpdateOutputs()
        {
            if (OutputController != null && Layers.Count > 0)
            {
                int      value = GetResultingValue();
                float    ratio = value / 255.0f;
                RGBColor RGB   = new RGBColor((int)((_Color.Red * ratio) + 0.5f),
                                              (int)((_Color.Blue * ratio) + 0.5f),
                                              (int)((_Color.Green * ratio) + 0.5f));

                int OutputNumber = 0;
                switch (ColorOrder)
                {
                case RGBOrderEnum.RBG:
                    OutputData[OutputNumber]     = FadingCurve.MapValue(RGB.Red);
                    OutputData[OutputNumber + 2] = FadingCurve.MapValue(RGB.Green);
                    OutputData[OutputNumber + 1] = FadingCurve.MapValue(RGB.Blue);
                    break;

                case RGBOrderEnum.GRB:
                    OutputData[OutputNumber]     = FadingCurve.MapValue(RGB.Green);
                    OutputData[OutputNumber + 1] = FadingCurve.MapValue(RGB.Red);
                    OutputData[OutputNumber + 2] = FadingCurve.MapValue(RGB.Blue);
                    break;

                case RGBOrderEnum.GBR:
                    OutputData[OutputNumber]     = FadingCurve.MapValue(RGB.Green);
                    OutputData[OutputNumber + 1] = FadingCurve.MapValue(RGB.Blue);
                    OutputData[OutputNumber + 2] = FadingCurve.MapValue(RGB.Red);
                    break;

                case RGBOrderEnum.BRG:
                    OutputData[OutputNumber]     = FadingCurve.MapValue(RGB.Blue);
                    OutputData[OutputNumber + 1] = FadingCurve.MapValue(RGB.Red);
                    OutputData[OutputNumber + 2] = FadingCurve.MapValue(RGB.Green);
                    break;

                case RGBOrderEnum.BGR:
                    OutputData[OutputNumber]     = FadingCurve.MapValue(RGB.Blue);
                    OutputData[OutputNumber + 1] = FadingCurve.MapValue(RGB.Green);
                    OutputData[OutputNumber + 2] = FadingCurve.MapValue(RGB.Red);
                    break;

                case RGBOrderEnum.RGB:
                default:
                    OutputData[OutputNumber]     = FadingCurve.MapValue(RGB.Red);
                    OutputData[OutputNumber + 1] = FadingCurve.MapValue(RGB.Green);
                    OutputData[OutputNumber + 2] = FadingCurve.MapValue(RGB.Blue);
                    break;
                }

                OutputController.SetValues((LedNumber - 1) * 3, OutputData);
            }
            ;
        }
コード例 #2
0
ファイル: Motor.cs プロジェクト: wimkuijp/DirectOutput
        /// <summary>
        /// Updates the output of the toy.
        /// </summary>
        public override void UpdateOutputs()
        {
            if (Output != null)
            {
                int P = FadingCurve.MapValue(GetResultingValue().Limit(0, 255));

                if (P != 0)
                {
                    P = ((int)((double)(MaxPower >= MinPower ? MaxPower - MinPower : MinPower - MaxPower) / 255 * P) + MinPower).Limit(MinPower, MaxPower);
                }



                if (P == 0)
                {
                    TurnedOffAfterMaxRunTime = false;
                }

                if (!TurnedOffAfterMaxRunTime)
                {
                    if (CurrentMotorPower == 0)
                    {
                        //Motor is currently off
                        if (P > 0)
                        {
                            //need to turn the motor on

                            if (KickstartDurationMs > 0 && KickstartPower > 0 && P <= KickstartPower)
                            {
                                //Kickstart is defined, start with kickstart

                                TargetMotorPower = P;

                                if (!KickstartActive)
                                {
                                    CurrentMotorPower = KickstartPower;
                                    Output.Value      = (byte)CurrentMotorPower;
                                    KickstartActive   = true;
                                    AlarmHandler.RegisterAlarm(KickstartDurationMs, KickStartEnd);
                                }
                            }
                            else
                            {
                                //Just turn the motor on
                                CurrentMotorPower = P;
                                TargetMotorPower  = P;
                                Output.Value      = (byte)P;
                                KickstartActive   = false;
                            }

                            if (MaxRunTimeMs > 0)
                            {
                                AlarmHandler.RegisterAlarm(MaxRunTimeMs, MaxRunTimeMotorStop);
                            }
                        }
                    }
                    else if (KickstartActive)
                    {
                        //Motor is in kickstart phase
                        if (P > 0)
                        {
                            //Need to change the motor power
                            TargetMotorPower = P;
                        }
                        else
                        {
                            //Turn off motor
                            AlarmHandler.UnregisterAlarm(KickStartEnd);
                            AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop);
                            TargetMotorPower  = 0;
                            CurrentMotorPower = 0;
                            Output.Value      = 0;
                        }
                    }
                    else
                    {
                        //Motor is on
                        if (P == 0)
                        {
                            //Turn of motor
                            AlarmHandler.UnregisterAlarm(KickStartEnd);
                            AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop);
                            TargetMotorPower  = 0;
                            CurrentMotorPower = 0;
                            Output.Value      = 0;
                        }
                        else if (P != CurrentMotorPower)
                        {
                            //Power has changed
                            CurrentMotorPower = P;
                            TargetMotorPower  = P;
                            Output.Value      = (byte)P;
                        }
                    }
                }
            }
        }