Example #1
0
 public void ClosePin(int pinNumber)
 {
     if (_controller.IsPinOpen(pinNumber))
     {
         _controller.ClosePin(pinNumber);
     }
 }
Example #2
0
 public void On()
 {
     if (!_gpio.IsPinOpen(_powerPin))
     {
         _gpio.OpenPin(_powerPin, PinMode.Output);
     }
 }
Example #3
0
        public void WriteSpan()
        {
            _mockedGpioDriver.Setup(x => x.OpenPinEx(1));
            _mockedGpioDriver.Setup(x => x.OpenPinEx(2));
            _mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(1, PinMode.Output)).Returns(true);
            _mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(2, PinMode.Output)).Returns(true);
            _mockedGpioDriver.Setup(x => x.GetPinModeEx(1)).Returns(PinMode.Output);
            _mockedGpioDriver.Setup(x => x.GetPinModeEx(2)).Returns(PinMode.Output);
            _mockedGpioDriver.Setup(x => x.WriteEx(1, PinValue.High));
            _mockedGpioDriver.Setup(x => x.WriteEx(2, PinValue.Low));
            _mockedGpioDriver.Setup(x => x.ClosePinEx(1));
            _mockedGpioDriver.Setup(x => x.ClosePinEx(2));
            var ctrl = new GpioController(PinNumberingScheme.Logical, _mockedGpioDriver.Object);

            Assert.NotNull(ctrl);
            ctrl.OpenPin(1, PinMode.Output);
            ctrl.OpenPin(2, PinMode.Output);
            Assert.True(ctrl.IsPinOpen(1));
            Span <PinValuePair> towrite = stackalloc PinValuePair[2];

            towrite[0] = new PinValuePair(1, PinValue.High);
            towrite[1] = new PinValuePair(2, PinValue.Low);
            ctrl.Write(towrite);
            ctrl.ClosePin(1);
            ctrl.ClosePin(2);
            Assert.False(ctrl.IsPinOpen(1));
        }
 static void Main(string[] args)
 {
     try
     {
         var drvGpio = new LibGpiodDriver(GPIOCHIP);
         controller = new GpioController(PinNumberingScheme.Logical, drvGpio);
         //set value
         if (!controller.IsPinOpen(LED_PIN) && !controller.IsPinOpen(BUTTON_PIN))
         {
             controller.OpenPin(LED_PIN, PinMode.Output);
             controller.OpenPin(BUTTON_PIN, PinMode.Input);
         }
         else
         {
             Console.WriteLine("LED_PIN or BUTTON_PIN is busy");
             exitCode = -1;
             return;
         }
         controller.Write(LED_PIN, ledPinValue); //LED OFF
         //
         Console.WriteLine("CTRL+C to interrupt the read operation:");
         controller.RegisterCallbackForPinValueChangedEvent(BUTTON_PIN, PinEventTypes.Rising, (o, e) =>
         {
             ledPinValue = !ledPinValue;
             controller.Write(LED_PIN, ledPinValue);
             Console.WriteLine($"Press button, LED={ledPinValue}");
         });
         //Console
         ConsoleKeyInfo cki;
         while (true)
         {
             Console.Write("Press any key, or 'X' to quit, or ");
             Console.WriteLine("CTRL+C to interrupt the read operation:");
             // Start a console read operation. Do not display the input.
             cki = Console.ReadKey(true);
             // Announce the name of the key that was pressed .
             Console.WriteLine($"  Key pressed: {cki.Key}\n");
             // Exit if the user pressed the 'X' key.
             if (cki.Key == ConsoleKey.X)
             {
                 break;
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine($"Error: {ex}");
         exitCode = -2;
     }
     finally
     {
         if (controller is not null)
         {
             controller.Dispose();
         }
     }
     Environment.Exit(exitCode);
 }
Example #5
0
        public ActionResult SetPinOn(int pinNumber)
        {
            if (!gpioController.IsPinOpen(pinNumber))
            {
                gpioController.OpenPin(pinNumber, PinMode.Output);
            }

            gpioController.Write(pinNumber, PinValue.High);

            return(NoContent());
        }
        public void SwitchOn()
        {
            if (!_controller.IsPinOpen(_pin))
            {
                _controller.OpenPin(_pin, PinMode.Output);
                _logger?.LogDebug("GPIO pin opened: {pin}", _pin);
            }

            _controller.Write(_pin, PinValue.High);
            _logger?.LogDebug("PIN on: {pin}", _pin);
        }
Example #7
0
 public void IsPinOpenTest()
 {
     using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
     {
         Assert.False(controller.IsPinOpen(LedPin));
         controller.OpenPin(LedPin);
         Assert.True(controller.IsPinOpen(LedPin));
         controller.ClosePin(LedPin);
         Assert.False(controller.IsPinOpen(LedPin));
     }
 }
Example #8
0
 public void LedOn()
 {
     lock (_locker)
     {
         if (!_controller.IsPinOpen(_ledPin))
         {
             _controller.OpenPin(_ledPin, PinMode.Output);
         }
         _controller.Write(_ledPin, PinValue.High);
     }
 }
Example #9
0
 public void IsPinOpenOnInputTest()
 {
     using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
     {
         // Open pin in input mode (default)
         Assert.False(controller.IsPinOpen(LedPin));
         controller.OpenPin(LedPin, PinMode.Input);
         Assert.True(controller.IsPinOpen(LedPin));
         controller.ClosePin(LedPin);
         Assert.False(controller.IsPinOpen(LedPin));
     }
 }
Example #10
0
 public void OpenPin(int pin, PinValue onValue)
 {
     if (!_gpio.IsPinOpen(pin))
     {
         // There is a flash of low potential on an output between setting pin as output
         // and writing output to high. Sequence below charges internal capatitance before
         // setting pin as output. This minimalise initial low level on output before it is set high.
         // This might be important when active state = PinValue low.
         // To eliminate this completely, passive hardware integrator circuit should help.
         _gpio.OpenPin(pin, PinMode.InputPullUp);
         Task.Delay(10).Wait();
         _gpio.SetPinMode(pin, PinMode.Output);
         _gpio.Write(pin, !onValue);
     }
 }
Example #11
0
        public static PinState ReadState(int pin)
        {
            PinState state = new PinState
            {
                Pin     = pin,
                StateOn = false
            };

            try
            {
                GpioController controller = new GpioController();
                if (!controller.IsPinOpen(pin))
                {
                    controller.OpenPin(pin, PinMode.Input);
                }

                var mode = controller.GetPinMode(pin);
                if (!PinMode.Input.Equals(mode))
                {
                    controller.SetPinMode(pin, PinMode.Input);
                }
                var x = controller.Read(pin);
                if (x.Equals(PinValue.High))
                {
                    state.StateOn = true;
                }
            }
            catch (Exception ex)
            {
                //TODO: log Error
            }
            return(state);
        }
Example #12
0
        public ActionResult <string> Post([FromBody] string toggle)
        {
            try
            {
                GpioController controller = new GpioController();
                if (!controller.IsPinOpen(pin))
                {
                    controller.OpenPin(pin, PinMode.Output);
                }

                if ("on".Equals(toggle))
                {
                    controller.Write(pin, PinValue.High);
                    return(Ok("Toggled on"));
                }
                else
                {
                    controller.Write(pin, PinValue.Low);
                    return(Ok("Toggled off"));
                }
            }
            catch (Exception ex)
            {
                return(UnprocessableEntity(ex));
            }
        }
Example #13
0
 public void Start()
 {
     lock (_locker)
     {
         IsRunning = true;
         if (!_controller.IsPinOpen(_left))
         {
             _controller.OpenPin(_left, PinMode.Input);
         }
         if (!_controller.IsPinOpen(_right))
         {
             _controller.OpenPin(_right, PinMode.Input);
         }
         Task.Run(() => PerformContinuousReads());
     }
 }
Example #14
0
        /// <inheritdoc />
        public DeviceStatus CurrentStatus()
        {
            if (_controller == null)
            {
                return(DeviceStatus.Disconnected("GPIO controller not connected"));
            }

            if (!_controller.IsPinOpen(_pin))
            {
                return(DeviceStatus.Disconnected($"Pin {_pin} not open"));
            }

            var value = _controller.Read(_pin);

            if (value != PinValue.High && value != PinValue.Low)
            {
                return(DeviceStatus.Error($"Unkown value {value} on pin"));
            }
            else if (value == _readPinValue)
            {
                return(DeviceStatus.Closed($"{value} value on pin"));
            }
            else
            {
                return(DeviceStatus.Open($"{value} value on pin"));
            }
        }
Example #15
0
        public static string Toggle(PinState state)
        {
            try
            {
                GpioController controller = new GpioController();
                if (!controller.IsPinOpen(state.Pin))
                {
                    controller.OpenPin(state.Pin, PinMode.Output);
                }

                if (state.StateOn)
                {
                    controller.Write(state.Pin, PinValue.High);
                    return($"Toggled pin {state.Pin} on");
                }
                else
                {
                    controller.Write(state.Pin, PinValue.Low);
                    return($"Toggled pin {state.Pin} off");
                }
            }
            catch (Exception ex)
            {
                return(ex.ToString());
            }
        }
Example #16
0
        public void GpioControllerCreateOpenClosePin()
        {
            _mockedGpioDriver.Setup(x => x.OpenPinEx(1));
            _mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(1, PinMode.Output)).Returns(true);
            _mockedGpioDriver.Setup(x => x.GetPinModeEx(1)).Returns(PinMode.Output);
            _mockedGpioDriver.Setup(x => x.WriteEx(1, PinValue.High));
            _mockedGpioDriver.Setup(x => x.ClosePinEx(1));
            var ctrl = new GpioController(PinNumberingScheme.Logical, _mockedGpioDriver.Object);

            Assert.NotNull(ctrl);
            ctrl.OpenPin(1, PinMode.Output);
            Assert.True(ctrl.IsPinOpen(1));
            ctrl.Write(1, PinValue.High);
            ctrl.ClosePin(1);
            Assert.False(ctrl.IsPinOpen(1));
        }
Example #17
0
 public void Write(int pin, bool value)
 {
     if (!_controller.IsPinOpen(pin))
     {
         _controller.OpenPin(pin, PinMode.Output);
     }
     _controller.Write(pin, value ? PinValue.High : PinValue.Low);
 }
Example #18
0
        public void Start()
        {
            lock (_locker)
            {
                IsRunning = true;
                if (!_controller.IsPinOpen(_echo))
                {
                    _controller.OpenPin(_echo, PinMode.Input);
                }

                if (!_controller.IsPinOpen(_trigger))
                {
                    _controller.OpenPin(_trigger, PinMode.Output);
                    _controller.Write(_trigger, PinValue.Low);
                }
                Task.Run(() => PerformContinuousReads());
            }
        }
Example #19
0
        /// <inheritdoc />
        public void Connect(IRpiConnectionFactory factory)
        {
            _controller = factory.CreateGpio();

            if (!_controller.IsPinOpen(_pin))
            {
                _controller.OpenPin(_pin, PinMode.InputPullDown);
            }
        }
Example #20
0
        public void IsPinOpenOnOutputTest()
        {
            // Separate test to check the IsPinOpen works also when the PinMode is Output, See Bug #776
            using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
            {
                Assert.False(controller.IsPinOpen(LedPin));

                controller.OpenPin(LedPin, PinMode.Output);
                Assert.True(controller.IsPinOpen(LedPin));

                controller.Write(LedPin, PinValue.High);
                Assert.True(controller.IsPinOpen(LedPin));

                controller.Write(LedPin, PinValue.Low);
                Assert.True(controller.IsPinOpen(LedPin));
                controller.ClosePin(LedPin);
                Assert.False(controller.IsPinOpen(LedPin));
            }
        }
Example #21
0
        public Task SetPinAsync(int pinNumber, PinValue value)
        {
            if (!gpioController.IsPinOpen(pinNumber))
            {
                gpioController.OpenPin(pinNumber, PinMode.Output);
            }

            gpioController.Write(pinNumber, value);

            return(Task.CompletedTask);
        }
Example #22
0
 void EnsureOpenPin(Led led, PinMode pinMode)
 {
     if (!GpioController.IsPinOpen(led.Pin) || GpioController.GetPinMode(led.Pin) != pinMode)
     {
         if (GpioController.IsPinOpen(led.Pin))
         {
             GpioController.ClosePin(led.Pin);
         }
         GpioController.OpenPin(led.Pin, pinMode);
     }
 }
Example #23
0
 /// <summary>
 /// Ensures pin is open and is set to output mode
 /// </summary>
 private void PreparePin(int pinNumber)
 {
     if (!_gpioController.IsPinOpen(pinNumber))
     {
         _gpioController.OpenPin(pinNumber, PinMode.Output);
     }
     else if (_gpioController.GetPinMode(pinNumber) != PinMode.Output)
     {
         _gpioController.SetPinMode(pinNumber, PinMode.Output);
     }
 }
Example #24
0
        public RelayDevice(GpioController gpio, int pin)
        {
            _gpio = gpio;
            _pin  = pin;

            // Ensure the pin is closed so the
            // relay is off.
            if (_gpio.IsPinOpen(_pin))
            {
                _gpio.ClosePin(_pin);
            }
        }
Example #25
0
        public Relay(GpioController gpioController, int inPin)
        {
            this.gpioController = gpioController;
            InPin = inPin;

            if (!gpioController.IsPinOpen(InPin))
            {
                gpioController.OpenPin(InPin);
            }
            gpioController.SetPinMode(InPin, PinMode.Output);
            gpioController.Write(InPin, PinValue.High);
        }
Example #26
0
        public void Open(WebServerEventArgs e)
        {
            try
            {
                var rawUrl = e.Context.Request.RawUrl.TrimStart('/');
                var args   = rawUrl.Split('/');
                if (args.Length < 3)
                {
                    WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.BadRequest);
                    return;
                }

                var pinNumber = Convert.ToInt32(args[1]);

                if (!_controller.IsPinOpen(pinNumber))
                {
                    _controller.OpenPin(pinNumber);
                }

                if (args[2].ToLower() == "output")
                {
                    _controller.SetPinMode(pinNumber, PinMode.Output);
                }
                else if (args[2].ToLower() == "input")
                {
                    _controller.SetPinMode(pinNumber, PinMode.Input);
                }
                else
                {
                    WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.BadRequest);
                    return;
                }

                WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
            }
            catch (Exception)
            {
                WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.BadRequest);
            }
        }
Example #27
0
 static void Main(string[] args)
 {
     Console.WriteLine("Start!");
     try
     {
         int pin = 7;
         using (GpioController controller = new GpioController(PinNumberingScheme.Board))
         {
             //注册退出事件
             Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs eventArgs) =>
             {
                 if (controller.IsPinOpen(pin))
                 {
                     controller.ClosePin(pin);
                 }
                 controller.Dispose();
                 Console.WriteLine("Close pin and exit successful!");
             };
             // 设置引脚
             controller.OpenPin(pin, PinMode.Output);
             if (!controller.IsPinOpen(pin))
             {
                 Console.WriteLine($"Set pin {pin} output mode failed.");
             }
             while (true)
             {
                 // 打开
                 controller.Write(pin, PinValue.High);
                 Thread.Sleep(500);
                 // 关闭
                 controller.Write(pin, PinValue.Low);
                 Thread.Sleep(500);
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Example #28
0
        public PinDetails GetPinDetails(int pin)
        {
            var pinOpen = _gpioController.IsPinOpen(pin);

            if (!pinOpen)
            {
                _gpioController.OpenPin(pin);
            }
            var details = new PinDetails()
            {
                PinNumber = pin,
                IsOpen    = pinOpen,
                PinValue  = (int)_gpioController.Read(pin),
                PinMode   = _gpioController.GetPinMode(pin)
            };

            if (!pinOpen)
            {
                _gpioController.ClosePin(pin);
            }
            return(details);
        }
Example #29
0
        public LEDController(
            ILogger <LEDController> logger,
            GpioController controller
            )
        {
            _controller = controller;
            this.logger = logger;

            if (!controller.IsPinOpen(ledPin))
            {
                controller.OpenPin(ledPin, PinMode.Output);
            }
        }
Example #30
0
        public void ReadSpan()
        {
            _mockedGpioDriver.Setup(x => x.OpenPinEx(1));
            _mockedGpioDriver.Setup(x => x.OpenPinEx(2));
            _mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(1, PinMode.Input)).Returns(true);
            _mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(2, PinMode.Input)).Returns(true);
            _mockedGpioDriver.Setup(x => x.ReadEx(1)).Returns(PinValue.Low);
            _mockedGpioDriver.Setup(x => x.ReadEx(2)).Returns(PinValue.High);
            _mockedGpioDriver.Setup(x => x.ClosePinEx(1));
            _mockedGpioDriver.Setup(x => x.ClosePinEx(2));
            var ctrl = new GpioController(PinNumberingScheme.Logical, _mockedGpioDriver.Object);

            Assert.NotNull(ctrl);
            ctrl.OpenPin(1, PinMode.Input);
            ctrl.OpenPin(2, PinMode.Input);
            Assert.True(ctrl.IsPinOpen(1));

            // Invalid usage (we need to prefill the array)
            // Was this the intended use case?
            Assert.Throws <InvalidOperationException>(() =>
            {
                Span <PinValuePair> wrongArg = stackalloc PinValuePair[2];
                ctrl.Read(wrongArg);
            });

            Span <PinValuePair> toread = stackalloc PinValuePair[2];

            toread[0] = new PinValuePair(1, PinValue.Low);
            toread[1] = new PinValuePair(2, PinValue.Low);
            ctrl.Read(toread);
            Assert.Equal(1, toread[0].PinNumber);
            Assert.Equal(2, toread[1].PinNumber);
            Assert.Equal(PinValue.Low, toread[0].PinValue);
            Assert.Equal(PinValue.High, toread[1].PinValue);
            ctrl.ClosePin(1);
            ctrl.ClosePin(2);
            Assert.False(ctrl.IsPinOpen(1));
        }