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));
        }
        private void Run(string[] args)
        {
            var led = ProcessorPin.Pin18
                      .Output()
                      .Name("LED")
                      .Revert()
                      .Enable();

            using (var connection = new GpioConnection(led))
            {
                var button = ProcessorPin.Pin2
                             .Input()
                             .Name("Button")
                             .Revert()
                             .Switch()
                             .Enable()
                             .OnStatusChanged(b =>
                {
                    Console.WriteLine("Button/LED switched {0}", b ? "On" : "Off");
                    connection.Pins["LED"].Toggle();
                });

                connection.Add(button);

                Console.WriteLine("Press Enter to quit...");
                Console.ReadLine();
            }
        }
        static bool ConnectPort(string[] args)
        {
            //Connect to the right UART port (may be USB in Windows/Unix/Mac or a Raspberry Mainboard)
            if (g_bIoTBoard)
            {
                //Define pins to control baudrate (GPIO2 on Pin21) and force a HW reset of the MWSUB3G (Pin12)
                OutputPinConfiguration pinGPIO2 = ConnectorPin.P1Pin21.Output();
                m_pinConnection = new GpioConnection(pinGPIO2);
                OutputPinConfiguration pinRESET = ConnectorPin.P1Pin12.Output();
                m_pinConnection.Add(pinRESET);

                //Reset sequence
                m_pinConnection[pinRESET] = false;
                Thread.Sleep(100);
                m_pinConnection[pinGPIO2] = true; //true for 500Kbps, change to false for 2400bps low speed
                m_pinConnection[pinRESET] = true;
                Thread.Sleep(2500);               //wait for initialization firmware code to finish startup

                //Open COM port from Raspberry mainboard
                string sCOMPort = "/dev/ttyAMA0";
                g_objRFE.ConnectPort(sCOMPort, g_nBaudrate, true);
                Console.WriteLine("Connected to port " + sCOMPort);
            }
            else if (args.Contains("/p:AUTO", StringComparer.Ordinal))
            {
                //This is any non-IoT platform with a single device connected to USB
                if (g_objRFE.GetConnectedPorts())
                {
                    if (g_objRFE.ValidCP2101Ports.Length == 1)
                    {
                        bool bForceBaudrate = (RFECommunicator.IsRaspberry() && g_nBaudrate > 115200);
                        g_objRFE.ConnectPort(g_objRFE.ValidCP2101Ports[0], g_nBaudrate, RFECommunicator.IsUnixLike() && !RFECommunicator.IsMacOS(), bForceBaudrate);
                    }
                }
                if (g_objRFE.PortConnected)
                {
                    Console.WriteLine("Connected to port " + g_objRFE.ValidCP2101Ports[0]);
                }
                else
                {
                    Console.WriteLine("ERROR: no port available, please review your connection");
                    return(false);
                }
            }
            else
            {
                //Use specified port from command line
                int nPos = Array.FindIndex(args, x => x.StartsWith("/p:"));
                if (nPos >= 0)
                {
                    string sCOMPort = args[nPos].Replace("/p:", "");
                    Console.WriteLine("Trying manual port: " + sCOMPort);
                    g_objRFE.ConnectPort(sCOMPort, g_nBaudrate, RFECommunicator.IsUnixLike() && !RFECommunicator.IsMacOS());
                    Console.WriteLine("Connected to port " + sCOMPort);
                }
            }

            return(g_objRFE.PortConnected);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            bool updating = false;
            var  redLED   = ConnectorPin.P1Pin11.ToProcessor();
            var  greenLED = ConnectorPin.P1Pin07.Output();
            var  driver   = GpioConnectionSettings.DefaultDriver;

            driver.Allocate(redLED, PinDirection.Output);
            DF1Comm.DF1Comm df1            = new DF1Comm.DF1Comm();
            var             gpioConnection = new GpioConnection(greenLED);
            var             Abutton        = ConnectorPin.P1Pin12.Input()
                                             .Revert()
                                             .OnStatusChanged(a =>
            {
                if (a && !updating)
                {
                    updating = true;
                    driver.Write(redLED, true);
                    DownloadProgram(df1, Properties.Settings.Default.FileA, Properties.Settings.Default.SerialPort);
                    driver.Write(redLED, false);
                    gpioConnection.Blink(greenLED, TimeSpan.FromSeconds(5));
                    updating = false;
                }
            });
            var Bbutton = ConnectorPin.P1Pin16.Input()
                          .Revert()
                          .OnStatusChanged(b =>
            {
                if (b && !updating)
                {
                    updating = true;
                    driver.Write(redLED, true);
                    DownloadProgram(df1, Properties.Settings.Default.FileB, Properties.Settings.Default.SerialPort);
                    driver.Write(redLED, false);
                    gpioConnection.Blink(greenLED, TimeSpan.FromSeconds(5));
                    updating = false;
                }
            });

            gpioConnection.Add(Abutton);
            gpioConnection.Add(Bbutton);
            Console.Read();
        }
Exemplo n.º 6
0
        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;
        }
Exemplo n.º 7
0
        public void setup(string mode_)
        {
            try
            {
                mode = mode_;
                Console.WriteLine("Drive reports: Mode: " + mode_);


                connection = new GpioConnection(pin1);
                connection.Add(pin2);
                connection.Add(pin3);
                connection.Add(pin4);

                Console.WriteLine("GPIO connection is up...");


                if (connection.IsOpened)
                {
                    Console.WriteLine("Drive reports: GPIO connected");
                    string pins = "";

                    foreach (ConnectedPin p in connection.Pins)
                    {
                        pins = pins + ":" + p.Configuration.Pin;
                    }
                    Console.WriteLine("Connected pins:" + pins);
                }

                ////Init.WiringPiSetupGpio();

                ////WiringPi.GPIO.SoftPwm.Create(pin,13, 180);



                //Console.WriteLine("WiringPI is up");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR while GPIO connection was set up");
            }
        }
        /// <summary>
        /// Starts the specified behavior on the connection.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="behavior">The behavior.</param>
        public static void Start(this GpioConnection connection, PinsBehavior behavior)
        {
            foreach (var configuration in behavior.Configurations)
            {
                if (!connection.Contains(configuration))
                {
                    connection.Add(configuration);
                }
            }

            behavior.Start(connection);
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            connection.Add(pin4);
            Console.WriteLine("Host IP?");
            string host = Console.ReadLine();

            Thread do_steering = new Thread(steer);

            Init.WiringPiSetupGpio();

            WiringPi.GPIO.SoftPwm.Create(pin, 90, 180);


            do_steering.Start();

            Connect(host);
        }
Exemplo n.º 10
0
        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;
        }
Exemplo n.º 11
0
        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 !! ");
        }
Exemplo n.º 12
0
        public FormBatteryGauge()
        {
            //ushort d = 0x7fff;
            //byte[] byteArray = BitConverter.GetBytes(d).Reverse().ToArray();

            //var p = 0.34E-3;

            InitializeComponent();
            _LTC2943Service = new LTC2943Service();
            _LTC2943Service.OnGaugeChanged += _LTC2943Service_OnGaugeChanged;
            _LTC2943Service.OnUnderCharge  += _LTC2943Service_OnUnderCharge;

            _gpioConnectionGlobalPin = new GpioConnection();
            OutputPinConfiguration opc = ProcessorPin.Gpio06.Output();

            _gpioConnectionGlobalPin.Add(opc);
            _modemResetPin = _gpioConnectionGlobalPin.Pins[ProcessorPin.Gpio06];
        }
Exemplo n.º 13
0
        //setup the Raspberry.IO.GeneralPurpose driver with opur list of inputs and outputs
        public GpioController(Dictionary <String, InputPin> inputs, Dictionary <String, OutputPin> outputs)
            : base(inputs, outputs)
        {
            //outputs need to know the driver that is being used
            var driver = GpioConnectionSettings.DefaultDriver;

            foreach (var output in Outputs)
            {
                ((GpioOutputPin)(output.Value)).Driver = driver;
            }

            //connection needs to know the list of inputs we setup
            _connection = new GpioConnection();
            foreach (var input in Inputs)
            {
                input.Value.InputChangedEventHandler += new InputChangedEventHandler(InputChanged);
                _connection.Add(((GpioInputPin)input.Value).PinConfig);
            }
        }
Exemplo n.º 14
0
        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);
                }
            }
        }
Exemplo n.º 15
0
        private void ToogleGPIO(ProcessorPin selectedGPIO)
        {
            OutputPinConfiguration _gpio = selectedGPIO.Output();

            try
            {
                if (_connectionGlobalPin == null)
                {
                    _connectionGlobalPin = new GpioConnection(_gpio);
                }

                if (!_connectionGlobalPin.Contains(_gpio))
                {
                    _connectionGlobalPin.Add(_gpio);
                }

                _connectionGlobalPin.Pins[_gpio].Enabled = !_connectionGlobalPin.Pins[_gpio].Enabled;
                CommonHelper.Logger.Info("GPIO {0}: enabled", _connectionGlobalPin.Pins[_gpio].Enabled);
            }
            catch (Exception e)
            {
                CommonHelper.Logger.Error(e, "GPIO Error : {0}", e.Message);
            }
        }
Exemplo n.º 16
0
        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();
        }
Exemplo n.º 17
0
        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);
            }
        }
Exemplo n.º 18
0
        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;
                }
            }
        }
Exemplo n.º 19
0
        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();
        }
Exemplo n.º 20
0
 private static void ConnectionGlobalPinAdd(PinConfiguration pc)
 {
     //if (CommonHelper.IsBoard)
     _gpioConnectionGlobalPin.Add(pc);
 }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            //create instance of settings object to get settings from config file
            Properties.Settings settings = new Properties.Settings();

            //set idleTimout to value from PLCupdater.exe.config file
            idleTimeout = settings.idleTimeout;

            //Initialize timer to shutdown device when idle
            timer = new Timer(new TimerCallback(idleShutdown), null, idleTimeout, Timeout.Infinite);

            //flag to prevent triggering update while one is already in progress
            bool updating = false;

            //pin definitions

            //blue LED on pin 12, provision as a low-level pin
            ProcessorPin blueLED = ConnectorPin.P1Pin12.ToProcessor();

            //red LED on pin 18, provision as a low-level pin
            ProcessorPin redLED = ConnectorPin.P1Pin18.ToProcessor();

            //green LED on pin 16, provision as a managed output
            OutputPinConfiguration greenLED = ConnectorPin.P1Pin16.Output();

            //create a low-level connection driver for red LED and blue LED
            IGpioConnectionDriver driver = GpioConnectionSettings.DefaultDriver;

            driver.Allocate(redLED, PinDirection.Output);
            driver.Allocate(blueLED, PinDirection.Output);

            //turn blue LED on to indicate program is ready
            driver.Write(blueLED, true);

            //create instance of DF1 protocol serial connection class
            DF1Comm.DF1Comm df1 = new DF1Comm.DF1Comm();

            //create high-level connection for green LED and buttons
            //allows for blinking LEDs and OnStatusChanged events for buttons
            GpioConnection gpioConnection = new GpioConnection(greenLED);

            //Program A download on pin 15, reverse input so that it's normally open instead of normally closed
            //Download program A to PLC when pressed
            InputPinConfiguration A_Down = ConnectorPin.P1Pin15.Input()
                                           .Revert()
                                           .OnStatusChanged(a =>
            {
                //if the button is pressed and update is not currently running, start update
                if (a && !updating)
                {
                    //set updating flag to true
                    updating = true;

                    //start update to transfer program A to the PLC using serial port from the config
                    DownloadProgram(df1, driver, gpioConnection, redLED, greenLED, settings.FileA, settings.SerialPort);

                    //set updating flag back to false
                    updating = false;
                }
            });

            //Program B download on pin 7, reverse input so that it's normally open instead of normally closed
            //Download program B to PLC when pressed
            InputPinConfiguration B_Down = ConnectorPin.P1Pin7.Input()
                                           .Revert()
                                           .OnStatusChanged(a =>
            {
                //if the button is pressed and update is not currently running, start update
                if (a && !updating)
                {
                    //set updating flag to true
                    updating = true;

                    //start update to transfer program A to the PLC using serial port from the config
                    DownloadProgram(df1, driver, gpioConnection, redLED, greenLED, settings.FileB, settings.SerialPort);

                    //set updating flag back to false
                    updating = false;
                }
            });

            //Progam A upload on pin 13, reverse input so that it's normally open instead of normally closed
            //Upload program A from PLC when pressed
            var A_Up = ConnectorPin.P1Pin13.Input()
                       .Revert()
                       .OnStatusChanged(b =>
            {
                //if the button is pressed and update is not currently running, start update
                if (b && !updating)
                {
                    //set updating flag to true
                    updating = true;

                    //start update to transfer program B to the PLC using serial port from the config
                    //DownloadProgram(df1, driver, gpioConnection, redLED, greenLED, settings.FileB, settings.SerialPort);
                    UploadProgram(df1, driver, gpioConnection, redLED, greenLED, settings.FileA, settings.SerialPort);

                    //set updating flag back to false
                    updating = false;
                }
            });

            //Progam B upload on pin 11, reverse input so that it's normally open instead of normally closed
            //Upload program B from PLC when pressed
            var B_Up = ConnectorPin.P1Pin11.Input()
                       .Revert()
                       .OnStatusChanged(b =>
            {
                //if the button is pressed and update is not currently running, start update
                if (b && !updating)
                {
                    //set updating flag to true
                    updating = true;

                    //start update to transfer program B to the PLC using serial port from the config
                    //DownloadProgram(df1, driver, gpioConnection, redLED, greenLED, settings.FileB, settings.SerialPort);
                    UploadProgram(df1, driver, gpioConnection, redLED, greenLED, settings.FileB, settings.SerialPort);

                    //set updating flag back to false
                    updating = false;
                }
            });

            //add the button configurations to the high-level connection
            gpioConnection.Add(A_Up);
            gpioConnection.Add(B_Up);
            gpioConnection.Add(A_Down);
            gpioConnection.Add(B_Down);

            //prevent program from exiting
            Console.ReadKey();
        }
Exemplo n.º 22
0
        //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);
            }
        }