コード例 #1
0
        private IPwmDevice GetRealDevice()
        {
            PwmFrequency = new Frequency(60);
            DeviceAddress = 0x40;

            try
            {
                Log.Info("Creating pins");
                var sdaPin = SdaPin.ToProcessor();
                var sclPin = SclPin.ToProcessor();

                Log.Info("Creating i2cDriver");
                _i2cDriver = new I2cDriver(sdaPin, sclPin);
            }
            catch (Exception e)
            {
                Log.Error("Failed to initialise i2c driver. Did you forget sudo?", e);
            }

            Log.Info("Creating real device...");
            var device = new Pca9685Connection(_i2cDriver.Connect(DeviceAddress));
            Log.Info("Setting frequency...");
            device.SetPwmUpdateRate(PwmFrequency); //                        # Set frequency to 60 Hz


            IsConnected = true;
            return device;
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var options = ParseOptions(args);
            if (options == null)
                return;

            log.Info("-=Pca9685 Sample=-");
            log.Info(m => m("Running {0}", Environment.OSVersion));
            log.Info("Options:" + Environment.NewLine + options);

            var pulse = CalculatePulse(options.PwmFrequency, 50);
            log.Info(m => m("Pulse={0}", pulse));

            if (Environment.OSVersion.Platform != PlatformID.Unix)
            {
                log.Warn("Windows detected. Exiting");
                return;
            }

            log.Info("Connecting...");

            try
            {
                using (var driver = new I2cDriver(options.SdaPin.ToProcessor(), options.SclPin.ToProcessor()))
                {
                    log.Info("Creating device...");
                    var device = new Pca9685Connection(driver.Connect(options.DeviceAddress));

                    log.Info("Setting frequency...");
                    device.SetPwmUpdateRate(options.PwmFrequency);
                    while (!Console.KeyAvailable)
                    {
                        log.Info(m => m("Set channel={0} to {1}", options.Channel, options.PwmOn));
                        device.SetPwm(options.Channel, 0, options.PwmOn);
                        Thread.Sleep(1000);
                        log.Info(m => m("Set channel={0} to {1}", options.Channel, options.PwmOff));
                        device.SetPwm(options.Channel, 0, options.PwmOff);
                        Thread.Sleep(1000);
                    }
                    log.Info("Key pressed. Exiting.");
                }
            }
            catch (InvalidOperationException e)
            {
                log.Error("Failed to connect? Do you have a Pca9685 IC attached to the i2c line and powered on?", e);
            }
        }