示例#1
0
        static void Main(string[] args)
        {
            SoftwarePwmChannel pwmChannel = null;

            Console.WriteLine("Hello PWM!");
            //注册退出事件
            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs eventArgs) =>
            {
                Console.WriteLine("\nPWM Exit!");
            };
            try
            {
                using (pwmChannel = new SoftwarePwmChannel(17))
                {
                    pwmChannel.Start();
                    for (double fill = 0.0; fill <= 1.0; fill += 0.01)
                    {
                        pwmChannel.DutyCycle = fill;
                        Thread.Sleep(300);
                    }
                    for (double fill = 1.0; fill >= 0.0; fill -= 0.01)
                    {
                        pwmChannel.DutyCycle = fill;
                        Thread.Sleep(300);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
示例#2
0
 public void LedSoftPWMOff(CancellationToken cancellationToken)
 {
     lock (_locker)
     {
         IsRunning             = false;
         using PwmChannel _pwm = new SoftwarePwmChannel(_softpwmPin, frequency: 400, dutyCycle: 0);
         _pwm.Stop();
     }
 }
示例#3
0
        public RgbLed(int redPin = 4, int greenPin = 19, int bluePin = 6)
        {
            redChannel   = new SoftwarePwmChannel(redPin, 400, 1);
            greenChannel = new SoftwarePwmChannel(greenPin, 400, 1);
            blueChannel  = new SoftwarePwmChannel(bluePin, 400, 1);

            redChannel.Start();
            greenChannel.Start();
            blueChannel.Start();
        }
示例#4
0
        /// <summary>
        /// Entry point for example program
        /// </summary>
        /// <param name="args">Command line arguments</param>
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello PWM!");

            using (var pwmChannel = new SoftwarePwmChannel(17, 200, 0))
            {
                pwmChannel.Start();
                for (double fill = 0.0; fill <= 1.0; fill += 0.01)
                {
                    pwmChannel.DutyCycle = fill;
                    Thread.Sleep(500);
                }
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            using PwmChannel red   = new SoftwarePwmChannel(pinNumber: 18, frequency: 400, dutyCycle: 0);
            using PwmChannel green = new SoftwarePwmChannel(pinNumber: 23, frequency: 400, dutyCycle: 0);
            using PwmChannel blue  = new SoftwarePwmChannel(pinNumber: 24, frequency: 400, dutyCycle: 0);

            red.Start();
            green.Start();
            blue.Start();

            Breath(red, green, blue);

            red.Stop();
            green.Stop();
            blue.Stop();
        }
        protected override void Startup()
        {
            _logger.LogTrace("Opening pin {0} as an output.", _pin);

            if (!_controller.IsPinOpen(_pin))
            {
                _controller.OpenPin(_pin, PinMode.Output);
            }
            else if (_controller.GetPinMode(_pin) != PinMode.Output)
            {
                _controller.SetPinMode(_pin, PinMode.Output);
            }

            _logger.LogTrace("Setting up software PWM channel.");
            _pwmChannel = new SoftwarePwmChannel(_pin, _frequency, dutyCycle: 0.0, usePrecisionTimer: true);
        }
示例#7
0
        protected override void Startup()
        {
            foreach (int pin in _pins)
            {
                _logger.LogTrace("Opening pin {0} as an output.", pin);

                if (!_controller.IsPinOpen(pin))
                {
                    _controller.OpenPin(pin, PinMode.Output);
                }
                else if (_controller.GetPinMode(pin) != PinMode.Output)
                {
                    _controller.SetPinMode(pin, PinMode.Output);
                }
            }

            _logger.LogTrace("Setting up software PWMs.");
            _redPwmChannel   = new SoftwarePwmChannel(_redPin, _frequency, dutyCycle: 1.0, usePrecisionTimer: true);
            _greenPwmChannel = new SoftwarePwmChannel(_greenPin, _frequency, dutyCycle: 1.0, usePrecisionTimer: true);
            _bluePwmChannel  = new SoftwarePwmChannel(_bluePin, _frequency, dutyCycle: 1.0, usePrecisionTimer: true);
        }
示例#8
0
        public Task LedSoftPWMOn(CancellationToken cancellationToken)
        {
            lock (_locker)
            {
                IsRunning = true;
                return(Task.Run(() =>
                {
                    int brightness = 0;
                    using PwmChannel _pwm = new SoftwarePwmChannel(_softpwmPin, frequency: 400, dutyCycle: 0);
                    _pwm.Start();
                    try
                    {
                        while (IsRunning)
                        {
                            while (brightness != 255)
                            {
                                _pwm.DutyCycle = brightness / 255D;
                                brightness++;
                                Thread.Sleep(10);
                            }

                            while (brightness != 0)
                            {
                                _pwm.DutyCycle = brightness / 255D;
                                brightness--;
                                Thread.Sleep(10);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }));
            }
        }
示例#9
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            using PwmChannel pwmChannel1 = PwmChannel.Create(0, 0, 50);
            using ServoMotor servoMotor1 = new ServoMotor(
                      pwmChannel1,
                      180,
                      700,
                      2400);

            using PwmChannel pwmChannel2 = PwmChannel.Create(0, 1, 50);
            using ServoMotor servoMotor2 = new ServoMotor(
                      pwmChannel2,
                      180,
                      700,
                      2400);

            using SoftwarePwmChannel pwmChannel3 = new SoftwarePwmChannel(27, 50, 0.5);
            using ServoMotor servoMotor3         = new ServoMotor(
                      pwmChannel3,
                      180,
                      900,
                      2100);

            servoMotor1.Start();
            servoMotor2.Start();
            servoMotor3.Start();

            connection = new HubConnectionBuilder()
                         .WithUrl("https://192.168.1.162:5001/chathub", conf =>
            {
                conf.HttpMessageHandlerFactory = (x) => new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
                };
            })
                         .Build();

            try
            {
                connection.On <string, string>("ReceiveMessage", (user, message) =>
                {
                    if (user == "servo1")
                    {
                        MoveToAngle(servoMotor1, Int32.Parse(message));
                    }
                    else if (user == "servo2")
                    {
                        MoveToAngle(servoMotor2, Int32.Parse(message));
                    }
                    else if (user == "servo3")
                    {
                        MoveToAngle(servoMotor3, Int32.Parse(message));
                    }
                    Console.WriteLine($"{message} posted by: {user}");
                });

                await connection.StartAsync();
            }
            catch (System.Exception)
            {
                throw;
            }


            //
            // Keep the code running...
            //
            while (true)
            {
            }

            servoMotor1.Stop();
            servoMotor2.Stop();
            servoMotor3.Stop();
        }
示例#10
0
 public LedStripHandler(LedStripPins pins)
 {
     _redChannel   = new SoftwarePwmChannel(pins.RedPin, 400, 0.8, true);
     _greenChannel = new SoftwarePwmChannel(pins.GreenPin, 400, 0.8, true);
     _blueChannel  = new SoftwarePwmChannel(pins.BluePin, 10, 0.8, true);
 }