private static void Main() { const ConnectorPin measurePin = ConnectorPin.P1Pin7; Console.WriteLine("DHT-11/DHT-22 Sample: measure humidity and temperature"); Console.WriteLine(); Console.WriteLine("\tMeasure: {0}", measurePin); Console.WriteLine(); var driver = GpioConnectionSettings.GetBestDriver(GpioConnectionDriverCapabilities.CanChangePinDirectionRapidly); using (var pin = driver.InOut(measurePin)) using (var dhtConnection = new Dht11Connection(pin)) { while (!Console.KeyAvailable) { var data = dhtConnection.GetData(); if (data != null) { Console.WriteLine("{0:0.00}% humidity, {1:0.0}°C, {2} attempts", data.RelativeHumidity.Percent, data.Temperature.DegreesCelsius, data.AttemptCount); } else { Console.WriteLine("Unable to read data"); } Timer.Sleep(TimeSpan.FromSeconds(2)); } } }
private void Run(string[] args) { const ProcessorPin measurePin = ProcessorPin.Pin18; int seconds = 3; Console.WriteLine("DHT-11 / DHT-22: Measure temperature and humidity"); Console.WriteLine($"Measure: {measurePin} every {seconds} second(s)"); Console.WriteLine(); var driver = GpioConnectionSettings.GetBestDriver(GpioConnectionDriverCapabilities.CanChangePinDirectionRapidly); using (var pin = driver.InOut(measurePin)) using (var dhtConnection = new Dht11Connection(pin)) { while (!Console.KeyAvailable) { var data = dhtConnection.GetData(); if (data != null) { Console.WriteLine( $"{data.RelativeHumidity.Percent:0.00}% humidity, {data.Temperature.DegreesCelsius:0.0}°C / {data.Temperature.DegreesFahrenheit:0.0}°F, {data.AttemptCount} attempt(s)"); } else { Console.WriteLine("Unable to read sensor data"); } System.Threading.Thread.Sleep(seconds * 1000); } } }
private static void Start() { var _settings = new GpioConnectionSettings(); _settings.PollInterval = TimeSpan.FromSeconds(1);//.FromMilliseconds(50); _conPin = new GpioConnection(_settings); _conPin.Add(testPin.Input()); //_conPin.Toggle(testPin); _conPin.PinStatusChanged += _conPin_PinStatusChanged; }
public clsDialHookListener(InputPinConfiguration HookInput, InputPinConfiguration PulseDialInput) { HookIO = HookInput; DialIO = PulseDialInput; var config = new GpioConnectionSettings() { PollInterval = 5, }; GPIO = new GpioConnection(config, HookIO, DialIO); GPIO.PinStatusChanged += (object sender, PinStatusEventArgs e) => { //switch change event handler if (e.Configuration.Pin == HookIO.Pin) { if (GPIO[HookIO]) { HookPulseCount++; } HookWaitEvent.Set(); } else if (e.Configuration.Pin == DialIO.Pin) { if (GPIO[DialIO]) { DialPulseCount++; DialWaitEvent.Set(); } } else { Console.WriteLine("Huh?! Wrong IO: " + e.Configuration.Name); } }; DialListenerThread = new Thread(ListenDial) { Name = "DialListener", IsBackground = true, }; DialListenerThread.Start(); HookListenerThread = new Thread(ListenHookSwitch) { Name = "HookListener", IsBackground = true, }; HookListenerThread.Start(); }
static GpioGlobalConnection() { // if (!CommonHelper.IsBoard) // return; _settings = new GpioConnectionSettings { //Interval between pin checks. This is *really* important - higher values lead to missed values/borking. Lower //values are apparently possible, but may have 'severe' performance impact. Further testing needed. PollInterval = TimeSpan.FromMilliseconds(500) }; _gpioConnectionGlobalPin = new GpioConnection(_settings); _blinkPins = new ConcurrentDictionary <ProcessorPin, TimeSpan>(); }
public void Initialize() { if (Pin1 == Pin2) { throw new NullReferenceException("Set the Pins before calling Initialize()"); } _pinConfig1 = Pin1.Output(); _pinConfig2 = Pin2.Output(); _settings = new GpioConnectionSettings() { Driver = new GpioConnectionDriver() }; _connection = new GpioConnection(_settings); _connection.Add(_pinConfig1); _connection.Add(_pinConfig2); }
public clsRinger(OutputPinConfiguration RingerPower, OutputPinConfiguration RingerOscillator, float[] RingPattern = null) { if (RingPattern == null) { RingPattern = ringPattern_UK; } ringPattern = RingPattern; RingerPowerPin = RingerPower; RingerOscillatorPin = RingerOscillator; var GPIOconfig = new GpioConnectionSettings(); GPIO = new GpioConnection(GPIOconfig, RingerPowerPin, RingerOscillatorPin); RingerThread = new Thread(Ring); RingerThread.IsBackground = true; RingerThread.Name = "Ringer Thread"; }
public FlowSensor(ConnectorPin sensorPin) { _sensorPin = sensorPin; Console.WriteLine("start flow sensor"); var pushButton = sensorPin.Input().PullUp(); //Create the settings for the connection var settings = new GpioConnectionSettings(); //Interval between pin checks. This is *really* important - higher values lead to missed values/borking. Lower //values are apparently possible, but may have 'severe' performance impact. Further testing needed. settings.PollInterval = TimeSpan.FromMilliseconds(1); var connection = new GpioConnection(settings, pushButton); connection.PinStatusChanged += (sender, eventArgs) => brojac++; }
static void Main(string[] args) { var pin1 = ConnectorPin.P1Pin22.Input(); var driver = new GpioConnectionDriver(); var settings = new GpioConnectionSettings(); settings.Driver = driver; using (var hans = new GpioConnection(settings)) { hans.Add(pin1); while (true) { Console.WriteLine(settings.Driver.Read(pin1.Pin)); Thread.Sleep(100); } } }
private void Initialize() { driver = new GpioConnectionDriver(); settings = new GpioConnectionSettings() { Driver = driver }; //Configure pins LedPinGreen = ledPinGreen.Output(); LedPinYellow = ledPinYellow.Output(); LedPinRed = ledPinRed.Output(); InputPinA = inputPinA.Input(); InputPinB = inputPinB.Input(); InputPinC = inputPinC.Input(); InputPinD = inputPinD.Input(); OutputPinA = outputPinA.Output(); OutputPinB = outputPinB.Output(); OutputPinC = outputPinC.Output(); OutputPinD = outputPinD.Output(); BuzzerPin = buzzerPin.Output(); //Declaring a ButtonPressed handler ButtonPin = buttonPin.Input().Name("Button").Revert().Switch().Enable().OnStatusChanged(x => { OnButtonPressed(new EventArgs()); }); Pins = new PinConfiguration[] { LedPinGreen, LedPinYellow, LedPinRed, InputPinA, InputPinB, InputPinC, InputPinD, OutputPinA, OutputPinB, OutputPinC, OutputPinD, BuzzerPin, ButtonPin }; Connection = new GpioConnection(settings, Pins); }
private static void Main() { const ConnectorPin measurePin = ConnectorPin.P1Pin7; IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(); var sp = serviceCollection.BuildServiceProvider(); sp.GetRequiredService <LoggerFactory>().AddConsole().AddDebug(); var log = sp.GetRequiredService <ILogger <Program> >(); log.LogInformation("DHT-11/DHT-22 Sample: measure humidity and temperature"); log.LogInformation("\tMeasure: {0}", measurePin); var driver = GpioConnectionSettings.GetBestDriver(GpioConnectionDriverCapabilities.CanChangePinDirectionRapidly); using (var pin = driver.InOut(measurePin)) using (var dhtConnection = new Dht11Connection(sp, pin)) { while (!Console.KeyAvailable) { var data = dhtConnection.GetData(); if (data != null) { Console.WriteLine("{0:0.00}% humidity, {1:0.0}°C, {2} attempts", data.RelativeHumidity.Percent, data.Temperature.DegreesCelsius, data.AttemptCount); } else { Console.WriteLine("Unable to read data"); } Timer.Sleep(TimeSpan.FromSeconds(2)); } } }
static void Main(string[] args) { const ConnectorPin led1Pin = ConnectorPin.P1Pin26; const ConnectorPin led2Pin = ConnectorPin.P1Pin24; const ConnectorPin led3Pin = ConnectorPin.P1Pin22; const ConnectorPin led4Pin = ConnectorPin.P1Pin15; const ConnectorPin led5Pin = ConnectorPin.P1Pin13; const ConnectorPin led6Pin = ConnectorPin.P1Pin11; const ConnectorPin buttonPin = ConnectorPin.P1Pin03; Console.WriteLine("Chaser Sample: Sample a LED chaser with a switch to change behavior"); Console.WriteLine(); Console.WriteLine("\tLed 1: {0}", led1Pin); Console.WriteLine("\tLed 2: {0}", led2Pin); Console.WriteLine("\tLed 3: {0}", led3Pin); Console.WriteLine("\tLed 4: {0}", led4Pin); Console.WriteLine("\tLed 5: {0}", led5Pin); Console.WriteLine("\tLed 6: {0}", led6Pin); Console.WriteLine("\tSwitch: {0}", buttonPin); Console.WriteLine(); var driver = args.GetDriver(); // Declare outputs (leds) var leds = new PinConfiguration[] { led1Pin.Output().Name("Led1").Enable(), led2Pin.Output().Name("Led2"), led3Pin.Output().Name("Led3").Enable(), led4Pin.Output().Name("Led4"), led5Pin.Output().Name("Led5").Enable(), led6Pin.Output().Name("Led6") }; // Assign a behavior to the leds var behavior = new ChaserBehavior(leds) { Loop = args.GetLoop(), RoundTrip = args.GetRoundTrip(), Width = args.GetWidth(), Interval = TimeSpan.FromMilliseconds(args.GetSpeed()) }; // Alternate behaviors... /* * var random = new Random(); * var behavior = new PatternBehavior(leds, Enumerable.Range(0, 5).Select(i => random.Next(511))) * { * Loop = Helpers.GetLoop(args), * RoundTrip = Helpers.GetRoundTrip(args), * Interval = Helpers.GetSpeed(args) * };*/ /* * var behavior = new BlinkBehavior(leds) * { * Count = args.GetWidth(), * Interval = args.GetSpeed() * };*/ // Declare input (switchButton) interacting with the leds behavior var switchButton = buttonPin.Input() .Name("Switch") .Revert() .Switch() .Enable() .OnStatusChanged(b => { behavior.RoundTrip = !behavior.RoundTrip; Console.WriteLine("Button switched {0}", b ? "on" : "off"); }); // Create connection var settings = new GpioConnectionSettings { Driver = driver }; using (var connection = new GpioConnection(settings, leds)) { Console.WriteLine("Using {0}, frequency {1:0.##}hz", settings.Driver.GetType().Name, 1000.0 / args.GetSpeed()); Thread.Sleep(1000); connection.Add(switchButton); connection.Start(behavior); // Starting the behavior automatically registers the pins to the connection, if needed. Console.ReadKey(true); connection.Stop(behavior); } }
static void Main(string[] args) { //Declare our pins (connector 24 and 26 / processor 08 and 7) as INPUT pins, and apply pull-up resistors var pin1 = ConnectorPin.P1Pin24.Input().PullUp(); var pin2 = ConnectorPin.P1Pin26.Input().PullUp(); //Create the settings for the connection var settings = new GpioConnectionSettings(); //Interval between pin checks. This is *really* important - higher values lead to missed values/borking. Lower //values are apparently possible, but may have 'severe' performance impact. Further testing needed. settings.PollInterval = TimeSpan.FromMilliseconds(1); //Create a new GpioConnection with the settings per above, and including pin1 (24) and pin2 (26). var connection = new GpioConnection(settings, pin1, pin2); //Integer storing the number of detents turned - clockwise turns should increase this and vice versa. var encoderPos = 0; //Add an event handler to the connection. If either pin1 or pin2's value changes this will fire. connection.PinStatusChanged += (sender, eventArgs) => { //If pin 24 / Pin08 / pin1 has changed value... if (eventArgs.Configuration.Pin == ProcessorPin.Pin08) { //Set levA to this pin's value levA = eventArgs.Enabled; } //If any other pin (i.e. pin 26 / Pin7 / pin2) has changed value... else { //Set levB to this pin's value levB = eventArgs.Enabled; } //If the pin whose value changed is different to the *last* pin whose value changed... if (eventArgs.Configuration.Pin.ToString() != lastGpio) { //Update the last changed pin lastGpio = eventArgs.Configuration.Pin.ToString(); //If pin 24 / Pin08 / pin1's value changed and its value is now 0... if ((eventArgs.Configuration.Pin == ProcessorPin.Pin08) && (!eventArgs.Enabled)) { //If levB = 0 if (!levB) { //Encoder has turned 1 detent clockwise. Update the counter: encoderPos++; Console.WriteLine("UP: " + encoderPos); } } //Else if pin 26 / Pin7 / pin2's value changed and its value is now 1... else if ((eventArgs.Configuration.Pin == ProcessorPin.Pin7) && (eventArgs.Enabled)) { //If levA = 1 if (levA) { //Encoder has turned 1 detent anti-clockwise. Update the counter: encoderPos--; Console.WriteLine("DOWN: " + encoderPos); } } } }; }
/// <summary> /// // /// </summary> public void AsInput() { TempPin = ConnectorPinDetector(PinNumber); var driver = GpioConnectionSettings.GetBestDriver(GpioConnectionDriverCapabilities.CanWorkOnThirdPartyComputers); var pin = driver.InOut(TempPin); }
//const ConnectorPin Station12OutputPin = ConnectorPin.P1Pin36; static void Main(string[] args) { // Declare outputs (leds) var leds = new PinConfiguration[] { Station1OutputPin.Output().Name("Led1").Enable(), Station2OutputPin.Output().Name("Led2"), Station3OutputPin.Output().Name("Led3").Enable(), Station4OutputPin.Output().Name("Led4"), Station5OutputPin.Output().Name("Led5").Enable(), Station6OutputPin.Output().Name("Led6"), Station7OutputPin.Output().Name("Led7").Enable(), Station8OutputPin.Output().Name("Led8"), Station9OutputPin.Output().Name("Led9").Enable(), Station10OutputPin.Output().Name("Led10"), Station11OutputPin.Output().Name("Led11").Enable(), //Station12OutputPin.Output().Name("Led12") }; Console.WriteLine("Chaser Sample: Sample a LED chaser with a switch to change behavior"); Console.WriteLine(); Console.WriteLine("\tLed 1: {0}", Station1OutputPin); Console.WriteLine("\tLed 2: {0}", Station2OutputPin); Console.WriteLine("\tLed 3: {0}", Station3OutputPin); Console.WriteLine("\tLed 4: {0}", Station4OutputPin); Console.WriteLine("\tLed 5: {0}", Station5OutputPin); Console.WriteLine("\tLed 6: {0}", Station6OutputPin); Console.WriteLine("\tSwitch: {0}", PushButtonInputPin); Console.WriteLine(); // Assign a behavior to the leds int period = 250; var behavior = new ChaserBehavior(leds) { Loop = true, // args.GetLoop(), RoundTrip = true, // args.GetRoundTrip(), Width = 12, // args.GetWidth(), Interval = TimeSpan.FromMilliseconds(period) //TimeSpan.FromMilliseconds(args.GetSpeed()) }; var switchButton = LowPressureFaultInputPin.Input() //.Name("Switch") //.Revert() //.Switch() //.Enable() .OnStatusChanged(b => { behavior.RoundTrip = !behavior.RoundTrip; Console.WriteLine("Button switched {0}", b ? "on" : "off"); }); // Create connection var settings = new GpioConnectionSettings();// { Driver = driver }; using (var connection = new GpioConnection(settings, leds)) { Console.WriteLine("Using {0}, frequency {1:0.##}hz", settings.Driver.GetType().Name, 1000.0 / period); Thread.Sleep(1000); connection.Add(switchButton); connection.Start(behavior); // Starting the behavior automatically registers the pins to the connection, if needed. Console.ReadKey(true); connection.Stop(behavior); } }
static void Main(string[] args) { try { var driver = args.GetDriver(); var mainboard = Board.Current; if (!mainboard.IsRaspberryPi) { Console.WriteLine("'{0}' is not a valid processor for a Raspberry Pi.", mainboard.Processor); return; } // Declare outputs (leds) var leds = new PinConfiguration[] { ConnectorPin.P1Pin26.Output().Name("Led1").Enable(), ConnectorPin.P1Pin24.Output().Name("Led2"), ConnectorPin.P1Pin22.Output().Name("Led3").Enable(), ConnectorPin.P1Pin15.Output().Name("Led4"), ConnectorPin.P1Pin13.Output().Name("Led5").Enable(), ConnectorPin.P1Pin11.Output().Name("Led6") }; // Assign a behavior to the leds var behavior = new ChaserBehavior(leds) { Loop = args.GetLoop(), RoundTrip = args.GetRoundTrip(), Width = args.GetWidth(), Interval = args.GetSpeed() }; // Alternate behaviors... /* * var random = new Random(); * var behavior = new PatternBehavior(leds, Enumerable.Range(0, 5).Select(i => random.Next(511))) * { * Loop = Helpers.GetLoop(args), * RoundTrip = Helpers.GetRoundTrip(args), * Interval = Helpers.GetSpeed(args) * };*/ /* * var behavior = new BlinkBehavior(leds) * { * Count = args.GetWidth(), * Interval = args.GetSpeed() * };*/ // Declare input (switchButton) interacting with the leds behavior var switchButton = ConnectorPin.P1Pin03.Input() .Name("Switch") .Revert() .Switch() .Enable() .OnStatusChanged(b => { behavior.RoundTrip = !behavior.RoundTrip; Console.WriteLine("Button switched {0}", b ? "on" : "off"); }); // Create connection Console.WriteLine("Running on Raspberry firmware rev{0}, board rev{1}, processor {2}", mainboard.Firmware, mainboard.Revision, mainboard.Processor); var settings = new GpioConnectionSettings { Driver = driver }; using (var connection = new GpioConnection(settings, leds)) { Console.WriteLine("Using {0}, frequency {1:0.##}hz", settings.Driver.GetType().Name, 1000.0 / args.GetSpeed()); Thread.Sleep(1000); connection.Add(switchButton); connection.Start(behavior); // Starting the behavior automatically registers the pins to the connection, if needed. Console.ReadKey(true); connection.Stop(behavior); } } catch (Exception ex) { var currentException = ex; while (currentException != null) { Console.WriteLine("{0}: {1}", currentException.GetType().Name, currentException.Message); currentException = currentException.InnerException; } } }