/// <summary> /// Initializes a new instance of the <see cref="PullDownSwitchDevice" /> class. /// </summary> /// <param name="switchConnectorPin">The switch connector pin.</param> /// <param name="gpioConnectionDriver">The gpio connection driver.</param> public PullDownSwitchDevice(ConnectorPin switchConnectorPin, IGpioConnectionDriver gpioConnectionDriver) { this.PinConfiguration = switchConnectorPin.Input().PullDown(); this.PinConfiguration.OnStatusChanged(this.OnSwitchChanged); using (var switchPin = gpioConnectionDriver.In(switchConnectorPin)) { this.State = switchPin.Read(); } }
public GPIOAlarm(int id, string name, string address) { Id = id; Name = name; Address = address; pin = GPIOService.GetGPIOPin(Address); pinConfig = pin.Input().Name(Name).OnStatusChanged(b => { State = b ? true : false; //Console.WriteLine("Alarm {0} {1}", Name, b ? "on" : "off"); AlarmStatusChangedEventArgs e = new AlarmStatusChangedEventArgs(); e.Value = b; OnStatusChanged(e); }); Id = id; GPIOService.Gpio.Add(pinConfig); }
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++; }
/// <summary> /// Initializes a new instance of the <see cref="Ky040Device" /> class. /// </summary> /// <param name="clockConnectorPin">The clock connector pin.</param> /// <param name="dataConnectorPin">The data connector pin.</param> /// <param name="buttonConnectorPin">The button connector pin.</param> /// <param name="gpioConnectionDriverFactory">The gpio connection driver factory.</param> /// <param name="rotaryEncoderReporter">The ky040 reporter.</param> public Ky040Device( ConnectorPin clockConnectorPin, ConnectorPin dataConnectorPin, ConnectorPin buttonConnectorPin, IGpioConnectionDriverFactory?gpioConnectionDriverFactory = null, IRotaryEncoderReporter?rotaryEncoderReporter = null) { this.gpioConnectionDriverFactory = GpioConnectionDriverFactory.EnsureGpioConnectionDriverFactory(gpioConnectionDriverFactory); this.gpioConnectionDriver = this.gpioConnectionDriverFactory.Get(); this.rotaryEncoderReporter = rotaryEncoderReporter; this.rotaryEncoderReporter?.SetSource(typeof(IRotaryEncoderReporter), this); this.clkPinConfiguration = clockConnectorPin.Input().PullUp(); this.clkPinConfiguration.OnStatusChanged(this.OnEncoderChanged); this.dtPinConfiguration = dataConnectorPin.Input().PullUp(); this.dtPinConfiguration.OnStatusChanged(this.OnEncoderChanged); this.buttonPinConfiguration = buttonConnectorPin.Input().PullUp(); this.buttonPinConfiguration.OnStatusChanged(this.OnButtonPressed); this.clockProcessorPin = clockConnectorPin.ToProcessor(); this.dataProcessorPin = dataConnectorPin.ToProcessor(); }
/// <summary> /// Initializes a new instance of the <see cref="RemotePiDevice" /> class. /// </summary> /// <param name="shutdownInConnectorPin">The shutdown in connector pin.</param> /// <param name="shutdownOutConnectorPin">The shutdown out connector pin.</param> /// <param name="operatingSystemShutdown">The operation system shutdown.</param> /// <param name="shutdownTimeSpan">The shutdown time span.</param> /// <param name="gpioConnectionDriverFactory">The gpio connection driver factory.</param> /// <param name="dateTime">The date time.</param> /// <param name="threadFactory">The thread factory.</param> public RemotePiDevice( ConnectorPin shutdownInConnectorPin, ConnectorPin shutdownOutConnectorPin, IOperatingSystemShutdown operatingSystemShutdown, TimeSpan shutdownTimeSpan, IGpioConnectionDriverFactory?gpioConnectionDriverFactory = null, IDateTime?dateTime = null, IThreadFactory?threadFactory = null) { this.gpioConnectionDriverFactory = GpioConnectionDriverFactory.EnsureGpioConnectionDriverFactory(gpioConnectionDriverFactory); this.gpioConnectionDriver = this.gpioConnectionDriverFactory.Get(); this.shutdownInConnectorPin = shutdownInConnectorPin; this.shutdownOutConnectorPin = shutdownOutConnectorPin; this.operatingSystemShutdown = operatingSystemShutdown; this.shutdownTimeSpan = shutdownTimeSpan < TimeSpan.FromSeconds(4) ? TimeSpan.FromSeconds(4) : shutdownTimeSpan; this.dateTime = dateTime ?? new DateTimeProvider(); this.thread = ThreadFactory.EnsureThreadFactory(threadFactory).Create(); var pinConfiguration = shutdownInConnectorPin.Input().PullDown(); pinConfiguration.OnStatusChanged(this.OnShutdown); this.gpioConnection = new GpioConnection(new GpioConnectionDriverFactory(this.gpioConnectionDriver), pinConfiguration); }
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); }
public void InitGpio() { outputs = new PinConfiguration[] { Station1OutputPin.Output().Name("Station1"), Station2OutputPin.Output().Name("Station2"), Station3OutputPin.Output().Name("Station3"), Station4OutputPin.Output().Name("Station4"), Station5OutputPin.Output().Name("Station5"), Station6OutputPin.Output().Name("Station6"), Station7OutputPin.Output().Name("Station7"), Station8OutputPin.Output().Name("Station8"), PumpOperationPin.Output().Name("PumpOperation"), TankRelayOutputPin.Output().Name("TankRelay"), SpareOutputPin.Output().Name("Spare"), ResetRelayOutputPin.Output().Name("ResetRelay") }; connection = new GpioConnection(outputs); connection.Add(LowPressureFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("LowPressureFaultInput {0}", b ? "on" : "off"); bLowPressureFaultState = b; CreateEvent(EventType.IOEvent, string.Format("Input {0} on", LowPressureFaultInputPin.ToString())); CreateEvent(EventType.FaultEvent, string.Format("Low pressure fault {0}", b ? "detected" : "cleared")); })); connection.Add(HighPressureFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("HighPressureFaultInput {0}", b ? "on" : "off"); bHighPressureFaultState = b; CreateEvent(EventType.IOEvent, string.Format("Input {0} {1}", HighPressureFaultInputPin.ToString(), b ? "on" : "off")); CreateEvent(EventType.FaultEvent, string.Format("High pressure fault {0}", b ? "detected" : "cleared")); })); connection.Add(LowWellFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("LowWellFaultInput {0}", b ? "on" : "off"); bLowWellFaultState = b; CreateEvent(EventType.IOEvent, string.Format("Input {0} {1}", LowWellFaultInputPin.ToString(), b ? "on" : "off")); CreateEvent(EventType.FaultEvent, string.Format("Low well fault {0}", b ? "detected" : "cleared")); if (b) { dtFaultStartDate = DateTime.Now; Log(string.Format("Initializing timeout at {0}", dtFaultStartDate.ToString())); ChangeState(State.WaitForTimeout); } else { ChangeState(State.Monitor); } })); connection.Add(OverloadFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("OverloadFaultInput {0}", b ? "on" : "off"); bOverloadFaultState = b; })); //ElectricPotential referenceVoltage = ElectricPotential.FromVolts(3.3); var driver = new MemoryGpioConnectionDriver(); //GpioConnectionSettings.DefaultDriver; Mcp3008SpiConnection spi = new Mcp3008SpiConnection( driver.Out(adcClock), driver.Out(adcCs), driver.In(adcMiso), driver.Out(adcMosi)); spiInput = spi.In(Mcp3008Channel.Channel0); connection.Open(); }
//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); } }
/// <summary> /// Initializes a new instance of the <see cref="PullDownButtonDevice"/> class. /// </summary> /// <param name="buttonConnectorPin">The button connector pin.</param> public PullDownButtonDevice(ConnectorPin buttonConnectorPin) { this.PinConfiguration = buttonConnectorPin.Input().PullDown(); this.PinConfiguration.OnStatusChanged(this.OnButtonPressed); }
static void Main(string[] args) { Console.WriteLine("GPIOTestHarness"); bool Station1OutputState = false; bool Station2OutputState = false; bool Station3OutputState = false; bool Station4OutputState = false; //var Output1 = Station1OutputPin.Output(); //var Output2 = Station2OutputPin.Output(); //var Output3 = Station3OutputPin.Output(); //var Output4 = Station4OutputPin.Output(); var pins = new PinConfiguration[] { Station1OutputPin.Output().Name("Output1"), Station2OutputPin.Output().Name("Output2"), Station3OutputPin.Output().Name("Output3"), Station4OutputPin.Output().Name("Output4") }; //var settings = new GpioConnectionSettings(); var connection = new GpioConnection(pins); var Input1 = LowPressureFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("LowPressureFaultInput {0}", b ? "on" : "off"); if (Station1OutputState != b) { connection.Toggle("Output1"); Station1OutputState = b; } }); connection.Add(Input1); var Input2 = HighPressureFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("HighPressureFaultInput {0}", b ? "on" : "off"); if (Station2OutputState != b) { connection.Toggle("Output2"); Station2OutputState = b; } }); connection.Add(Input2); var Input3 = LowWellFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("LowWellFaultInput {0}", b ? "on" : "off"); if (Station3OutputState != b) { connection.Toggle("Output3"); Station3OutputState = b; } }); connection.Add(Input3); var Input4 = OverloadFaultInputPin.Input().OnStatusChanged(b => { Console.WriteLine("OverloadFaultInput {0}", b ? "on" : "off"); if (Station4OutputState != b) { connection.Toggle("Output4"); Station4OutputState = b; } }); connection.Add(Input4); ElectricPotential referenceVoltage = ElectricPotential.FromVolts(3.3); var driver = new MemoryGpioConnectionDriver(); //GpioConnectionSettings.DefaultDriver; Mcp3008SpiConnection spi = new Mcp3008SpiConnection( driver.Out(adcClock), driver.Out(adcCs), driver.In(adcMiso), driver.Out(adcMosi)); IInputAnalogPin inputPin = spi.In(Mcp3008Channel.Channel0); connection.Open(); ElectricPotential volts = ElectricPotential.FromVolts(0); while (!Console.KeyAvailable) { var v = referenceVoltage * (double)inputPin.Read().Relative; if ((Math.Abs(v.Millivolts - volts.Millivolts) > 100)) { volts = ElectricPotential.FromMillivolts(v.Millivolts); Console.WriteLine("Voltage ch0: {0}", volts.Millivolts.ToString()); } } connection.Close(); }