public static bool Send(byte[] bytes) { serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); rwPinConnection.Toggle(rwPinConfig); Program.Delay(5); serialPort.Write(bytes, 0, 4); Program.Delay(5); rwPinConnection.Toggle(rwPinConfig); return(true); }
static private void set_pin(OutputPinConfiguration pin, int state_to_be, int pinstate) { if (pinstate != state_to_be) { connection.Toggle(pin); } }
private void Run(string[] args) { Console.WriteLine("Press any key to exit..."); var led = ProcessorPin.Pin23 .Output(); using (var connection = new GpioConnection(led)) { while (!Console.KeyAvailable) { connection.Toggle(led); System.Threading.Thread.Sleep(250); } } // Another approach using a behavior with finite number of blinks (10) //BlinkBehavior behavior = new BlinkBehavior(new PinConfiguration[] { led }) { Interval = new TimeSpan(0, 0, seconds: 1), Count = 10 }; //connection.Start(behavior); //while (!Console.KeyAvailable) //{ // System.Threading.Thread.Sleep(200); //} //connection.Stop(behavior); }
private void Ring() { Console.WriteLine("Ringer Start"); if (!GPIO[RingerPowerPin.Pin]) { GPIO.Toggle(RingerPowerPin.Pin); } try { while (true) { int ms = 1000 / ringHz; for (int f = 0; f < ringPattern.Length; f++) //Loop for each number in the ring pattern array { for (int i = 0; i < ringHz * ringPattern[f]; i++) //Oscillate solenoid for period defind in pattern { GPIO.Toggle(RingerOscillatorPin); Thread.Sleep(ms); } Thread.Sleep((int)(ringPattern[++f] * 1000)); } } } catch (System.Threading.ThreadAbortException) { Console.WriteLine("Ringer Stop"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally //reset outputs { if (GPIO[RingerOscillatorPin.Pin]) { GPIO.Toggle(RingerOscillatorPin.Pin); } if (GPIO[RingerPowerPin.Pin]) { GPIO.Toggle(RingerPowerPin.Pin); } } }
static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); ILog log = LogManager.GetLogger("GPIO"); log.Debug("Start"); PinConfiguration output = ConnectorPin.P1Pin36.Output().Name("Output1"); PinConfiguration[] outputs = new PinConfiguration[] { output }; GpioConnection gpio = new GpioConnection(outputs); //gpio.Open(); 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); gpio.Open(); ElectricPotential volts = ElectricPotential.FromVolts(0); while (!Console.KeyAvailable) { var v = referenceVoltage * (double)inputPin.Read().Relative; Console.WriteLine("{0} mV", v.Millivolts); if ((Math.Abs(v.Millivolts - volts.Millivolts) > 100)) { volts = ElectricPotential.FromMillivolts(v.Millivolts); Console.WriteLine("Voltage ch0: {0}", volts.Millivolts.ToString()); } gpio.Toggle("Output1"); Thread.Sleep(2000); } gpio.Close(); //bool bShutdown = false; //while(!bShutdown) //{ // gpio.Toggle(output); // log.Debug("Toggle output"); // Thread.Sleep(5000); //} }
public void Light(string input) { if (input == "1") { Console.WriteLine("...turning light on"); connection.Toggle(led1); } if (input == "0") { Console.WriteLine("...turning light off"); } }
static void Main(string[] args) { var led1 = ConnectorPin.P1Pin11.Output(); var connection = new GpioConnection(led1); for (var i = 0; i < 100; i++) { connection.Toggle(led1); System.Threading.Thread.Sleep(250); } connection.Close(); }
private static void ToggleLightSwitch(Light light) { if (_gpio != null && _pin11 != null) { var pinState = _gpio.Pins[ConnectorPin.P1Pin11].Enabled; var pinOn = RelayActiveLow ? !pinState : pinState; // some relays are ON with low voltage signal - if so negate the state so the logic works the same if ((light == Light.On && !pinOn) || (light == Light.Off && pinOn)) { _gpio.Toggle(_pin11); } } }
static void Main(string[] args) { OutputPinConfiguration pin12 = ConnectorPin.P1Pin12.Output(); pin12.Enabled = false; using (GpioConnection connection = new GpioConnection(pin12)) { while (!Console.KeyAvailable) { connection.Toggle(pin12); Thread.Sleep(250); } } }
private void Run(string[] args) { Console.WriteLine("Press any key to exit..."); var led = ProcessorPin.Pin18 .Output(); using (var connection = new GpioConnection(led)) { while (!Console.KeyAvailable) { connection.Toggle(led); System.Threading.Thread.Sleep(250); } } }
protected void togglePump2SSR(object sender, EventArgs e) { var ssr = ConnectorPin.P1Pin36.Output(); var connection = new GpioConnection(ssr); if (Pump2Status.Text == "Off") { Pump2Status.Text = "On"; connection.Toggle(ssr); } else { Pump2Status.Text = "Off"; connection.Close(); } }
static void Main(string[] args) { var led1 = ConnectorPin.P1Pin07.Output(); using (var connection = new GpioConnection(led1)) { for (var i = 0; i < 100; i++) { Console.Write("."); connection.Toggle(led1); System.Threading.Thread.Sleep(2000); } connection.Close(); } }
private void BlinkTest() { var led1Config = ConnectorPin.P1Pin11.Output(); var led1Con = new GpioConnection(led1Config); led1Con.Open(); for (var i = 0; i < 30; i++) { led1Con.Toggle(led1Config); System.Threading.Thread.Sleep(1000); } led1Con.Close(); }
static void Main() { Console.WriteLine("Initializing GPIO Connection..."); //create a connection to the desired GPIO pins //refer to the pinouts diagram for the correct pin OutputPinConfiguration red = ConnectorPin.P1Pin36.Output(); OutputPinConfiguration green = ConnectorPin.P1Pin32.Output(); var connection = new GpioConnection(green, red); Console.Write("1. Who is the President of the United States? "); Console.WriteLine("a. Puff Daddy"); Console.WriteLine("b. Hillary Clinton"); Console.WriteLine("c. Barack Obama"); Console.WriteLine("d. George Bush"); string ans = Console.ReadLine().ToLower(); /*if (ans == "c") { * OutputPinConfiguration led = green; * }else { * OutputPinConfiguration led = red; * }*/ OutputPinConfiguration led = ans == "c" ? green : red; for (int ctr = 0; ctr < 10; ctr++) { connection.Toggle(led); Thread.Sleep(150); } /*Console.WriteLine("Answer: ");*/ /*Console.Write("2. Summer Solstice is the shortest day of the year."); * Console.WriteLine("a. True"); * Console.WriteLine("b. False"); * string ans = Console.ReadLine().ToLower(); * * OutputPinConfiguration led = ans == "a" ? green : red;*/ //TODO: Add program code here connection.Close(); }
protected void toggleHeatSSR(int ssrNumber, string onOff) { var ssr = ConnectorPin.P1Pin31.Output(); onOff = onOff.ToLower(); if (ssrNumber == 1) { ssr = ConnectorPin.P1Pin33.Output(); } var connection = new GpioConnection(ssr); if (onOff == "on") { connection.Toggle(ssr); } else { connection.Close(); } }
public void SwitchStation(int stationId) { if (stationId == 0) { //switch all stations off AllOutputsOff(); return; } foreach (Station station in stations) { if (stationId == station.Id) { if (!station.OutputState) { Log(string.Format("Turning on Station{0} OutputState:{1}", station.Name, station.OutputState)); connection.Toggle(station.Name); station.OutputState = true; bUpdateStatus = true; } //for all stations except station 8 (shelter), we activate the pump //for station 8, we activate the tank relay if (station.Id == 8) { if (!TankRelayOutputState) { Log("Turning on tank relay"); connection.Toggle("TankRelay"); TankRelayOutputState = true; bUpdateStatus = true; } if (PumpOperationOutputState) { Log("Turning off pump"); connection.Toggle("PumpOperation"); PumpOperationOutputState = false; bUpdateStatus = true; } } else { if (TankRelayOutputState) { Log("Turning off tank relay"); connection.Toggle("TankRelay"); TankRelayOutputState = false; bUpdateStatus = true; } if (!PumpOperationOutputState) { Log("Turning on pump"); connection.Toggle("PumpOperation"); PumpOperationOutputState = true; bUpdateStatus = true; } } } else { if (station.OutputState != false) { Log(string.Format("Turning off Station{0} OutputState:{1}", station.Name, station.OutputState)); connection.Toggle(station.Name); station.OutputState = false; bUpdateStatus = true; } } Irrigating = true; } }
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(); }
private static void TestDriveCommands(string[] args) { Console.WriteLine("Testing drive commands"); if (args.Length <= 1) { Console.WriteLine("No commands provided"); } var connection = new GpioConnection(driveForward, driveBackward, driveTurnLeft, driveTurnRight); var command = ""; for (int i = 1; i < args.Length - 1; i++) { command = args[i]; switch (command) { case "U": connection.Toggle(driveForward); System.Threading.Thread.Sleep(300); connection.Toggle(driveForward); break; case "D": connection.Toggle(driveBackward); System.Threading.Thread.Sleep(300); connection.Toggle(driveBackward); break; case "UL": connection.Toggle(driveForward); connection.Toggle(driveTurnLeft); System.Threading.Thread.Sleep(400); connection.Toggle(driveForward); connection.Toggle(driveTurnLeft); break; case "UR": connection.Toggle(driveForward); connection.Toggle(driveTurnRight); System.Threading.Thread.Sleep(400); connection.Toggle(driveForward); connection.Toggle(driveTurnRight); break; case "DL": connection.Toggle(driveBackward); connection.Toggle(driveTurnLeft); System.Threading.Thread.Sleep(400); connection.Toggle(driveBackward); connection.Toggle(driveTurnLeft); break; case "DR": connection.Toggle(driveBackward); connection.Toggle(driveTurnRight); System.Threading.Thread.Sleep(400); connection.Toggle(driveBackward); connection.Toggle(driveTurnRight); break; default: Console.WriteLine("Unknown command {0} Expected command to be one of U D UL UR DL DR", command); break; } } connection.Close(); }
static void Main(string[] args) { Console.WriteLine("Starting..."); //var mpc = new MpcClient("192.168.1.123"); var mpc = new MpcClient(); GpioConnection gpio = null; var p11First = true; var p12First = true; var p13First = true; var p15First = true; var p16First = true; var buttonPressed = false; var buttonPressedStart = DateTime.MinValue; var p40 = ConnectorPin.P1Pin40.Output().Disable(); var p11 = ConnectorPin.P1Pin11.Input().PullUp(); p11.OnStatusChanged((off) => { if (p11First) { p11First = false; return; } if (!off) { mpc.PlayPauseToggle(); buttonPressed = true; buttonPressedStart = DateTime.Now; } if (off && buttonPressed) { if (buttonPressedStart.AddSeconds(1.5) < DateTime.Now) { // on press for 1.5 seconds, toggle port to enable/disable speaker (in favor of headphone out) gpio?.Toggle(p40); mpc.PlayPauseToggle(); } } }); var p12 = ConnectorPin.P1Pin12.Input().PullUp(); p12.OnStatusChanged((off) => { if (p12First) { p12First = false; return; } if (!off) { mpc.VolumeUp(); } }); var p13 = ConnectorPin.P1Pin13.Input().PullUp(); p13.OnStatusChanged((off) => { if (p13First) { p13First = false; return; } if (!off) { mpc.VolumeDown(); } }); var p15 = ConnectorPin.P1Pin15.Input().PullUp(); p15.OnStatusChanged((off) => { if (p15First) { p15First = false; return; } if (!off) { mpc.Next(); } }); var p16 = ConnectorPin.P1Pin16.Input().PullUp(); p16.OnStatusChanged((off) => { if (p16First) { p16First = false; return; } if (!off) { mpc.Previous(); } }); var p03 = ConnectorPin.P1Pin03.Output(); gpio = new GpioConnection(p03, p11, p12, p13, p15, p16, p40); mpc.ResetVolume(); var blinkThread = new System.Threading.Thread(new System.Threading.ThreadStart(() => { while (true) { // blink power led when ready gpio.Toggle(p03); System.Threading.Thread.Sleep(1000); } })); blinkThread.Start(); while (true) { Console.WriteLine("Waiting for playlist name..."); var inp = Console.ReadLine(); mpc.Stop(); mpc.ClearPlaylist(); mpc.LoadPlaylist(inp); mpc.Play(); } }
private static void TestDrive() { Console.WriteLine("Testing arrows with car driving"); ConsoleKeyInfo cki; var connection = new GpioConnection(driveForward, driveBackward, driveTurnLeft, driveTurnRight); Console.WriteLine("Press the Escape (Esc) key to quit: \n"); do { cki = Console.ReadKey(); Console.Write(" --- You pressed "); Console.WriteLine(cki.Key.ToString()); switch (cki.Key) { case ConsoleKey.UpArrow: //connection.Blink(driveForward, new TimeSpan(0, 0, 0, 0, 300)); //Hack as we don't have immediate mode in managed code //Blink appears to have issues as it uses Timer connection.Toggle(driveForward); System.Threading.Thread.Sleep(300); connection.Toggle(driveForward); break; case ConsoleKey.DownArrow: connection.Toggle(driveBackward); System.Threading.Thread.Sleep(300); connection.Toggle(driveBackward); break; case ConsoleKey.LeftArrow: connection.Toggle(driveForward); connection.Toggle(driveTurnLeft); System.Threading.Thread.Sleep(400); connection.Toggle(driveForward); connection.Toggle(driveTurnLeft); break; case ConsoleKey.RightArrow: connection.Toggle(driveForward); connection.Toggle(driveTurnRight); System.Threading.Thread.Sleep(400); connection.Toggle(driveForward); connection.Toggle(driveTurnRight); break; default: break; } } while (cki.Key != ConsoleKey.Escape); connection.Close(); }