/// <summary> /// Create a PwmChannel using a chip and channel. /// </summary> /// <remarks>Gpio pin 18 is on chip 0 and channel 0</remarks> public PwmChannel(int chip, int channel) { disposed = true; var chipPath = $"/sys/class/pwm/pwmchip{chip}"; if (!Directory.Exists(chipPath)) { throw new IOException($"Could not locate directory {chipPath}."); } var channelPath = $"{chipPath}/pwm{channel}"; if (!Directory.Exists(channelPath)) { Write($"{chipPath}/export", channel.ToString()); Pi.Wait(100); } if (!Directory.Exists(channelPath)) { throw new IOException($"Could not locate directory {channelPath}."); } var e = $"{channelPath}/enable"; var p = $"{channelPath}/period"; var d = $"{channelPath}/duty_cycle"; if (!File.Exists(e)) { throw new IOException($"File {e} does not exist."); } if (!File.Exists(p)) { throw new IOException($"File {p} does not exist."); } if (!File.Exists(d)) { throw new IOException($"File {d} does not exist."); } enableFile = new UnixFile(e, "w"); periodFile = new UnixFile(p, "w"); dutyCycleFile = new UnixFile(d, "w"); disposed = false; }
static void Write(UnixFile file, string text) { file.Reset(); file.Write(text); }
static void Write(string fileName, string text) { using (var file = new UnixFile(fileName)) file.Write(text); }