public RaspberryPiPinProvider() { inputPins = new List<PinConfiguration>() { ConnectorPin.P1Pin16.Input().PullDown(), ConnectorPin.P1Pin18.Input().PullDown() }; outputPins = new List<PinConfiguration>() { ConnectorPin.P1Pin11.Output(), ConnectorPin.P1Pin12.Output(), ConnectorPin.P1Pin13.Output(), ConnectorPin.P1Pin15.Output() }; connection = new GpioConnection(); inputPins.ForEach(x => { x.OnStatusChanged(state => { if (InputPinStateChange != null) { InputPinStateChange(inputPins.IndexOf(x), state); } }); connection.Add(x); }); outputPins.ForEach(x => connection.Add(x)); }
public RaspPiGpioNode() { // GPIO init hardware interface gpioPinsConnection = new GpioConnection(); foreach (var x in GpioToPin) { gpioPinsConnection.Add(x.Value.Output()); } if (!gpioPinsConnection.IsOpened) { gpioPinsConnection.Open(); } // Done DebugEx.TraceLog("RaspberryPIGPIO plugin up and running !! "); }
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); } } }
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); } }
public RaspNode(Transport transport) { // Hardware interface gpioPinsConnection = new GpioConnection(); foreach (var x in LedToPin) { gpioPinsConnection.Add(x.Value.Output()); } if (!gpioPinsConnection.IsOpened) { gpioPinsConnection.Open(); } // Node transport this.Transport = (transport != Transport.None) ? transport : Transport.YPCHANNEL; }
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; } } }
public static void Main() { Console.Title = "Raspberry-LED Domotica client"; if (!Helpers.IsLinux) { Console.WriteLine("Sorry, almost everything in this script can only run on the Raspberry Pi."); Console.WriteLine("Press enter to close the script."); Console.Read(); Environment.Exit(0); } Console.CancelKeyPress += delegate { Console.WriteLine("Stopping the program"); HubConnection.Closed -= StartHubConnection; HubConnection.Closed += null; HubConnection.Stop(); Console.WriteLine("Stopped SignalR communication"); for (var i = 0; i == 32;) { string str = $"gpio{i}"; if (Directory.Exists(Path.Combine("/sys/class/gpio", str))) { driver.Release((ProcessorPin) i); } i++; } gpio.Close(); Console.WriteLine("Stopped driver allocating"); Thread.Sleep(1000); Environment.Exit(0); }; // Connection to the signalr hub HubConnection = new HubConnection("http://192.168.1.100"); RaspberryHub = HubConnection.CreateHubProxy("Raspberry"); // If the server decides to close the connection we need to start it again HubConnection.Closed += StartHubConnection; // Starts the connection StartHubConnection(); gpio = new GpioConnection(); driver = new GpioConnectionDriver(); I2CDriver = new I2cDriver(ConnectorPin.P1Pin3.ToProcessor(), ConnectorPin.P1Pin5.ToProcessor()); ArduinoConnection = I2CDriver.Connect(0x04); var switchButton = ConnectorPin.P1Pin13.Input().Revert().OnStatusChanged(x => { Console.WriteLine(x); RaspberryHub.Invoke("SendChangedValue", ConnectorPin.P1Pin37, x ? "On" : "Off"); }); var doorSensor = ConnectorPin.P1Pin7.Input().PullUp().OnStatusChanged(x => { RaspberryHub.Invoke("SendChangedValue", ConnectorPin.P1Pin7, x ? "Open" : "Closed"); }); var motionSensor = ConnectorPin.P1Pin13.Input().OnStatusChanged(x => { RaspberryHub.Invoke("SendChangedValue", ConnectorPin.P1Pin11, x ? "Detected" : "Not detected"); Console.WriteLine( DateTime.Now + ":Motion {0}", x ? "Detected" : "Not detected"); }); // gpio.Add(switchButton); // gpio.Add(doorSensor); // gpio.Add(motionSensor); RaspberryHub.On<string>("ChangePiLed", pinnumber => { int ledid = int.Parse(pinnumber); var procpin = ((ConnectorPin) ledid).ToProcessor(); string str = string.Format("gpio{0}",procpin.ToString().Replace("Pin0","").Replace("Pin","")); if (!Directory.Exists(Path.Combine("/sys/class/gpio", str))) { Console.WriteLine($"OutputPin {procpin} is not allocated!\nAllocating now."); driver.Allocate(procpin, PinDirection.Output); Console.WriteLine("Pin allocated"); } driver.Write(procpin, !driver.Read(procpin)); RaspberryHub.Invoke("SendChangedValue", pinnumber, driver.Read(procpin) ? "On" : "Off"); }); RaspberryHub.On<string, string>("SetupConfig", (pinnumber, type) => { int pin = int.Parse(pinnumber); if (pin > 7) { Action<bool> onstatusaction = b => { RaspberryHub.Invoke("SendChangedValue", pin, driver.Read(((ConnectorPin) pin).ToProcessor()) ? "On" : "Off"); }; string str = string.Format("gpio{0}", ((ConnectorPin)pin).ToProcessor().ToString().Replace("Pin0", "").Replace("Pin", "")); Console.WriteLine(str); if (!Directory.Exists(Path.Combine("/sys/class/gpio", str))) { Console.WriteLine("Adding button"); var button = CreatePinConfig.CreateOutputPinConfiguration((ConnectorPin) pin, onstatusaction, "Button"); gpio.Add(button); } } }); RaspberryHub.On<int, string>("GetPinStatus", (pin, type) => { driver.Read(((ConnectorPin) pin).ToProcessor()); string status = string.Empty; if (type.Equals("Button")) { status = driver.Read(((ConnectorPin) pin).ToProcessor()) ? "Pressed" : "Not pressed"; } if (type.Equals("LED")) { status = driver.Read(((ConnectorPin) pin).ToProcessor()) ? "On" : "Off"; } if (type.Equals("Door sensor")) { status = driver.Read(((ConnectorPin)pin).ToProcessor()) ? "Open" : "Closed"; } RaspberryHub.Invoke("SendChangedValue", pin, status); }); ServerWorkThread objThread = new ServerWorkThread(); SendToArduino(1); while (true) { Thread.Sleep(50); ReadFromArduino(); //objThread.HandleConnection(objThread.mySocket.Accept()); } }