static void Main(string[] args) { var options = ParseOptions(args); if (options == null) { return; } IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(); var sp = serviceCollection.BuildServiceProvider(); sp.GetRequiredService <LoggerFactory>().AddConsole().AddDebug(); log = sp.GetRequiredService <ILogger <Program> >(); log.LogInformation("-=Pca9685 Sample=-"); log.LogInformation("Running {0}", System.Runtime.InteropServices.RuntimeInformation.OSDescription); log.LogInformation("Options:" + Environment.NewLine + options); var pulse = CalculatePulse(options.PwmFrequency, 50); log.LogInformation("Pulse={0}", pulse); if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux)) { log.LogWarning("Windows detected. Exiting"); return; } log.LogInformation("Connecting..."); try { using (var driver = new I2cDriver(options.SdaPin.ToProcessor(), options.SclPin.ToProcessor())) { log.LogInformation("Creating device..."); var device = new Pca9685Connection(sp, driver.Connect(options.DeviceAddress)); log.LogInformation("Setting frequency..."); device.SetPwmUpdateRate(options.PwmFrequency); while (!Console.KeyAvailable) { log.LogInformation("Set channel={0} to {1}", options.Channel, options.PwmOn); device.SetPwm(options.Channel, 0, options.PwmOn); Thread.Sleep(1000); log.LogInformation("Set channel={0} to {1}", options.Channel, options.PwmOff); device.SetPwm(options.Channel, 0, options.PwmOff); Thread.Sleep(1000); } log.LogInformation("Key pressed. Exiting."); } } catch (InvalidOperationException e) { log.LogError(new EventId(0), e, "Failed to connect? Do you have a Pca9685 IC attached to the i2c line and powered on?"); } }
private void RunLed(Pca9685Connection device, PwmChannel channel, int sleepMs, CancellationToken cancelToken) { // PMW ticks range from 0 to 4095 int increment = 100; int startCycleTick = 0; device.SetPwm(channel, 0, 0); while (!cancelToken.IsCancellationRequested) { // up we go for (int endCycleTick = 100; endCycleTick < 4100; endCycleTick += increment) { device.SetPwm(channel, startCycleTick, endCycleTick); Thread.Sleep(sleepMs); } // after we've gone up, show kindness and see if we're done if (cancelToken.IsCancellationRequested) { break; } // and down we go for (int endCycleTick = 4095; endCycleTick > 0; endCycleTick -= increment) { device.SetPwm(channel, startCycleTick, endCycleTick); Thread.Sleep(sleepMs); } } }
private void Run(string[] args) { using (var driver = new I2cDriver(ProcessorPin.Pin2, ProcessorPin.Pin3)) { // which pwm channel; allow override from args[0] as 0..n int c = 0; if (args.Length > 0) { Int32.TryParse(args[0], out c); } PwmChannel channel = (PwmChannel)c; Console.WriteLine($"Using PCA9685 channel {channel.ToString()}"); // allow override of min/max cycle if (args.Length > 1) { Int32.TryParse(args[1], out _servoMin); } if (args.Length > 2) { Int32.TryParse(args[2], out _servoMax); } int i2cAddress = 0x40; Console.WriteLine($"Using i2C address 0x{i2cAddress:x}"); // device support and prep channel var device = new Pca9685Connection(driver.Connect(i2cAddress)); device.SetPwm(channel, 0, 0); int freq = 60; Console.WriteLine($"Setting PWM frequency to {freq}"); var pwmFrequency = Frequency.FromHertz(freq); device.SetPwmUpdateRate(pwmFrequency); Console.WriteLine("* * * When entering values, prefix the input with \"!\" to specify a raw pulse value rather than a degree value (e.g. !680). * * *"); Console.WriteLine("\nPress any key to exit...\n"); // loop until ENTER string input; do { Console.Write("Enter rotation 0 to 180 or RETURN to exit? "); input = Console.ReadLine().Trim(); int cycle; if (TryParse(input, out cycle)) { Console.WriteLine($"Setting PWM channel {channel.ToString()} to {cycle}"); device.SetPwm(channel, 0, cycle); } } while (input != string.Empty); } }
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); } }
public void setPin(int pin, int value) { if ((pin < 0) || (pin > 12)) { Console.WriteLine("PWM pin must be between 0 and 12 inclusive"); return; } if ((value != 0) && (value != 1)) { Console.WriteLine("Pin value must be 0 or 1!"); } if (value == 0) { _pwm.SetPwm(pin, 0, 4096); } if (value == 1) { _pwm.SetPwm(pin, 4096, 0); } }