Exemple #1
0
        private void Run(object obj)
        {
            try
            {
                var controller = new GpioController(PinNumberingScheme.Logical);
                using (controller)
                {
#if LED
                    controller.OpenPin(LedPin, PinMode.Output); //noop, this is the default
                    Console.WriteLine($"Pin-{LedPin} enabled for PinMode.{controller.GetPinMode(LedPin)}");
#endif
                    controller.OpenPin(PirSensorPin, PinMode.Input); // write access /sys/class/gpio/gpio23/direction
                    Console.WriteLine($"Pin-{PirSensorPin} enabled for PinMode.{controller.GetPinMode(PirSensorPin)}");

                    // blocking infinite call
                    this.RunGpioLoop(controller);

                    // end of using block: dispose controller
                    Console.WriteLine("cleanup...");
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "GPIO Controller failed");
            }
            this._pollingThread = null;
        }
 [Trait("SkipOnTestRun", "Windows_NT")] // Currently, the Windows Driver is defaulting to InputPullDown instead of Input when Closed/Opened.
 public void OpenPinDefaultsModeToInput()
 {
     using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
     {
         controller.OpenPin(OutputPin);
         Assert.Equal(PinMode.Input, controller.GetPinMode(OutputPin));
         controller.SetPinMode(OutputPin, PinMode.Output);
         controller.ClosePin(OutputPin);
         controller.OpenPin(OutputPin);
         Assert.Equal(PinMode.Input, controller.GetPinMode(OutputPin));
     }
 }
Exemple #3
0
        public void GetPinMode()
        {
            _mockedGpioDriver.Setup(x => x.OpenPinEx(1));
            _mockedGpioDriver.Setup(x => x.GetPinModeEx(1)).Returns(PinMode.Output);
            var ctrl = new GpioController(PinNumberingScheme.Logical, _mockedGpioDriver.Object);

            Assert.NotNull(ctrl);
            // Not open
            Assert.Throws <InvalidOperationException>(() => ctrl.GetPinMode(1));
            ctrl.OpenPin(1);
            Assert.Equal(PinMode.Output, ctrl.GetPinMode(1));
        }
Exemple #4
0
        internal override Pin GetPinConfig(int pinNumber)
        {
            if (!PinController.IsValidPin(pinNumber) || DriverController == null || !IsDriverInitialized)
            {
                return(new Pin());
            }

            if (DriverController == null)
            {
                return(new Pin());
            }

            try {
                if (!DriverController.IsPinOpen(pinNumber))
                {
                    DriverController.OpenPin(pinNumber);
                }

                if (!DriverController.IsPinOpen(pinNumber))
                {
                    return(new Pin());
                }

                PinValue value  = DriverController.Read(pinNumber);
                PinMode  mode   = DriverController.GetPinMode(pinNumber);
                Pin      config = new Pin(pinNumber, value == PinValue.High ? GpioPinState.Off : GpioPinState.On, mode == PinMode.Input ? GpioPinMode.Input : GpioPinMode.Output);
                return(config);
            }
            finally {
                ClosePin(pinNumber);
            }
        }
Exemple #5
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);
        }
Exemple #6
0
        [Trait("SkipOnTestRun", "Windows_NT")] // Currently, the Windows Driver is defaulting to InputPullDown, and it seems this cannot be changed
        public void OpenPinDefaultsModeToLastMode(PinMode modeToTest)
        {
            var driver = GetTestDriver();

            if (driver is SysFsDriver)
            {
                // See issue #1581. There seems to be a library-version issue or some other random cause for this test to act differently on different hardware.
                return;
            }

            // This works for input/output on most systems, but not on pullup/down, since sometimes the hardware doesn't have read possibilities for those (ie. the Raspi 3)
            using (GpioController controller = new GpioController(GetTestNumberingScheme(), driver))
            {
                controller.OpenPin(OutputPin);
                controller.SetPinMode(OutputPin, modeToTest);
                Assert.Equal(modeToTest, controller.GetPinMode(OutputPin));
                controller.ClosePin(OutputPin);
            }

            // Close controller, to make sure we're not caching
            using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
            {
                controller.OpenPin(OutputPin);
                Thread.Sleep(100);
                Assert.Equal(modeToTest, controller.GetPinMode(OutputPin));
            }
        }
Exemple #7
0
 public void OpenPinDefaultsModeToLastModeIncludingPulls()
 {
     // This is only fully supported on the Pi4
     using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
     {
         controller.OpenPin(OutputPin);
         controller.SetPinMode(OutputPin, PinMode.InputPullDown);
         controller.ClosePin(OutputPin);
         controller.OpenPin(OutputPin);
         if (IsRaspi4())
         {
             Assert.Equal(PinMode.InputPullDown, controller.GetPinMode(OutputPin));
         }
         else
         {
             Assert.Equal(PinMode.Input, controller.GetPinMode(OutputPin));
         }
     }
 }
 public void ThrowsInvalidOperationExceptionWhenPinIsNotOpened()
 {
     using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
     {
         Assert.Throws <InvalidOperationException>(() => controller.Write(OutputPin, PinValue.High));
         Assert.Throws <InvalidOperationException>(() => controller.Read(InputPin));
         Assert.Throws <InvalidOperationException>(() => controller.ClosePin(OutputPin));
         Assert.Throws <InvalidOperationException>(() => controller.SetPinMode(OutputPin, PinMode.Output));
         Assert.Throws <InvalidOperationException>(() => controller.GetPinMode(OutputPin));
     }
 }
Exemple #9
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);
     }
 }
Exemple #10
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);
     }
 }
        public void OnPostToggle()
        {
            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);

            mode = controller.GetPinMode(pin);
            if (!PinMode.Output.Equals(mode))
            {
                controller.SetPinMode(pin, PinMode.Output);
            }
            controller.Write(pin, PinValue.High.Equals(x) ? PinValue.Low : PinValue.High);
        }
Exemple #12
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);
        }
        public void Setup()
        {
            //Enable 5V Control
            GpioController gpioController = A.Dummy <GpioController>();

            FakeItEasy.Fake.GetFakeManager(gpioController);
            var generalPurposeInputOutputController       = A.Fake <GeneralPurposeInputOutputController>(options => new GeneralPurposeInputOutputController(gpioController));
            List <IController> motorControllerInitObjects = new List <IController>()
            {
                generalPurposeInputOutputController
            };

            MotorController = A.Fake <L298N>(options => options.WithArgumentsForConstructor(() => new L298N(generalPurposeInputOutputController)));
            MotorController.ChannelAlphaId = new ComponentChannel("LeftChannel");
            MotorController.ChannelBetaId  = new ComponentChannel("RightChannel");
            var channelAMotor1 = GenerateMotor(MotorController.ChannelAlphaId, "FrontLeft", PositionLocation.FrontLeft);
            var channelBMotor1 = GenerateMotor(MotorController.ChannelBetaId, "FrontRight", PositionLocation.FrontRight);

            A.CallTo(() => gpioController.GetPinMode(channelAMotor1.DrivePin.Value.PinNumber)).Returns(PinMode.Output);

            MotorController.Motors.Add(channelAMotor1);
            MotorController.Motors.Add(channelBMotor1);
        }
Exemple #14
0
 public PinMode GetPinMode(int pinNumber)
 {
     return(_controller.GetPinMode(pinNumber));
 }
 public PinMode GetPinMode(byte pin) => _controller.GetPinMode(pin);