public bool SetGpioValue(int pin, GpioPinState state)
        {
            if (!PinController.IsValidPin(pin))
            {
                return(false);
            }

            try {
                GpioPin GpioPin = (GpioPin)Pi.Gpio[pin];

                if (GpioPin.PinMode == GpioPinDriveMode.Output)
                {
                    GpioPin.Write((GpioPinValue)state);
                    CastDriver <IGpioControllerDriver>(this).Logger.Trace($"Configured ({pin}) gpio pin to ({state.ToString()}) state.");
                    CastDriver <IGpioControllerDriver>(this).UpdatePinConfig(new Pin(pin, state));
                    return(true);
                }

                return(false);
            }
            finally {
                Pin?pinConfig = GetPinConfig(pin);
                if (pinConfig != null)
                {
                    CastDriver <IGpioControllerDriver>(this).UpdatePinConfig(pinConfig);
                }
            }
        }
Exemple #2
0
 void Awake()
 {
     sr        = GetComponent <SpriteRenderer>();
     sr.sprite = OpenSprite;
     output    = GetComponentInChildren <PinController>();
     pressers  = new List <Collider2D>();
 }
        public void TestPinReadProps()
        {
            var fileIo = new Mock <IFileIo>();

            fileIo.Setup(x => x.ReadAllText("/sys/class/pwm/pwmchip0/pwm43/polarity")).Returns("normal");
            fileIo.Setup(x => x.ReadAllText("/sys/class/pwm/pwmchip0/pwm43/duty_cycle")).Returns("39");
            fileIo.Setup(x => x.ReadAllText("/sys/class/pwm/pwmchip0/pwm43/period")).Returns("53");
            fileIo.Setup(x => x.ReadAllText("/sys/class/pwm/pwmchip0/pwm43/enable")).Returns("1");

            var pinMapper = new Mock <IPinMapper>();

            pinMapper.Setup(x => x.MapPinToSysfs("20")).Returns(43);
            pinMapper.Setup(x => x.MapPinToSysfs("23")).Returns(44);

            using (var service = new PinController(fileIo.Object, pinMapper.Object))
            {
                var pin1 = service.GetPwmPin("20");
                var pin2 = service.GetPwmPin("23");

                Assert.Equal(pin1.Polarity, PwmPolarity.Normal);
                Assert.Equal(pin1.Polarity, PwmPolarity.Normal);
                Assert.Equal(pin1.DutyCycle, 39);
                Assert.Equal(pin1.Period, 53);
                Assert.True(pin1.Enabled);

                Assert.Equal(pin2.Polarity, PwmPolarity.Normal);
                Assert.Equal(pin2.DutyCycle, 0);
                Assert.Equal(pin2.Period, 0);
                Assert.False(pin2.Enabled);
            }
        }
Exemple #4
0
        public void TestViewTest()
        {
            PinController controller = new PinController();
            var           result     = controller.PinView();

            Assert.AreEqual("PinView", result.ViewName);
        }
Exemple #5
0
 void SetInterface()
 {
     Transform[] children = transform.GetComponentsInChildren <Transform>(true);
     foreach (Transform child in children)
     {
         if (child.name == "Input")
         {
             InputPin       = child.GetComponent <PinController>();
             InputPin.input = true;
         }
         else if (child.name.Contains("Output"))
         {
             if (child.name.Contains("1"))
             {
                 OutputPins[0]       = child.GetComponent <PinController>();
                 OutputPins[0].input = false;
             }
             else if (child.name.Contains("2"))
             {
                 OutputPins[1]       = child.GetComponent <PinController>();
                 OutputPins[1].input = false;
             }
         }
     }
 }
Exemple #6
0
        public int GpioPhysicalPinNumber(int bcmPin)
        {
            if (!PinController.IsValidPin(bcmPin) || !IsDriverProperlyInitialized)
            {
                CastDriver <IGpioControllerDriver>(this)?.Logger.Log("The specified pin is invalid.");
                return(-1);
            }

            try {
                if (DriverController == null)
                {
                    return(-1);
                }

                DriverController.OpenPin(bcmPin);

                if (!DriverController.IsPinOpen(bcmPin))
                {
                    return(-1);
                }

                CastDriver <IGpioControllerDriver>(this)?.Logger.Info("System.Devices.Gpio driver doesn't support PhysicalPinNumber conversion.");
                return(-1);
            }
            finally {
                if (DriverController != null)
                {
                    if (DriverController.IsPinOpen(bcmPin))
                    {
                        DriverController.ClosePin(bcmPin);
                    }
                }
            }
        }
Exemple #7
0
        internal override bool SetGpioValue(int pin, GpioPinState state)
        {
            if (!IsWiringPiInstalled())
            {
                return(false);
            }

            if (!GpioCore.IsAllowedToExecute || !IsDriverInitialized)
            {
                return(false);
            }

            if (!PinController.IsValidPin(pin))
            {
                return(false);
            }

            Pin pinConfig = GetPinConfig(pin);

            if (pinConfig.Mode != GpioPinMode.Output)
            {
                return(false);
            }

            return(WriteValue(pin, state));
        }
Exemple #8
0
    public WireController SetConnection(PinController pin)
    {
        if (connectedPins >= 2)
        {
            return(null);
        }

        if (connectedPins == 1)
        {
            int i = connections[0] == null?1:0;
            if (pin.transform.parent == connections[i].transform.parent)
            {
                return(null);
            }

            if (pin.input == connections[i].input)
            {
                return(null);
            }
        }

        connections[connectedPins] = pin;
        connectedPins++;
        return(this);
    }
Exemple #9
0
        internal override bool SetGpioValue(int pin, GpioPinMode mode)
        {
            if (!PinController.IsValidPin(pin) || !IsDriverInitialized)
            {
                return(false);
            }

            try {
                if (DriverController == null)
                {
                    return(false);
                }

                if (!DriverController.IsPinModeSupported(pin, (PinMode)mode))
                {
                    return(false);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    DriverController.OpenPin(pin);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    return(false);
                }

                DriverController.SetPinMode(pin, (PinMode)mode);
                return(true);
            }
            finally {
                ClosePin(pin);
            }
        }
Exemple #10
0
        public bool SetGpioValue(int pin, Enums.GpioPinMode mode, Enums.GpioPinState state)
        {
            if (!IsWiringPiInstalled())
            {
                return(false);
            }

            if (!PiGpioController.IsAllowedToExecute || !IsDriverProperlyInitialized)
            {
                return(false);
            }

            if (!PinController.IsValidPin(pin))
            {
                CastDriver <IGpioControllerDriver>(this)?.Logger.Log("The specified pin is invalid.");
                return(false);
            }

            try {
                if (SetMode(pin, mode))
                {
                    return(WriteValue(pin, state));
                }

                return(false);
            }
            finally {
                Pin?pinConfig = GetPinConfig(pin);
                if (pinConfig != null)
                {
                    CastDriver <IGpioControllerDriver>(this).UpdatePinConfig(pinConfig);
                }
            }
        }
Exemple #11
0
        internal override int GpioPhysicalPinNumber(int bcmPin)
        {
            if (!PinController.IsValidPin(bcmPin) || !IsDriverInitialized)
            {
                return(-1);
            }

            try {
                if (DriverController == null)
                {
                    return(-1);
                }

                if (!DriverController.IsPinOpen(bcmPin))
                {
                    DriverController.OpenPin(bcmPin);
                }

                if (!DriverController.IsPinOpen(bcmPin))
                {
                    return(-1);
                }

                return(-1);
            }
            finally {
                ClosePin(bcmPin);
            }
        }
Exemple #12
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 #13
0
        internal override bool GpioDigitalRead(int pin)
        {
            if (!PinController.IsValidPin(pin) || !IsDriverInitialized)
            {
                return(false);
            }

            try {
                if (DriverController == null)
                {
                    return(false);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    DriverController.OpenPin(pin);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    return(false);
                }

                return(!(DriverController.Read(pin) == PinValue.High));
            }
            finally {
                ClosePin(pin);
            }
        }
Exemple #14
0
        internal override bool SetGpioValue(int pin, GpioPinState state)
        {
            if (!PinController.IsValidPin(pin) || !IsDriverInitialized)
            {
                return(false);
            }

            try {
                if (DriverController == null)
                {
                    return(false);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    DriverController.OpenPin(pin);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    return(false);
                }

                DriverController.Write(pin, state == GpioPinState.Off ? PinValue.High : PinValue.Low);
                return(true);
            }
            finally {
                ClosePin(pin);
            }
        }
        private void OnEventResult(ref PinEventConfiguration config)
        {
            if (config == null || !PinController.IsValidPin(config.GpioPin))
            {
                return;
            }

            if (config.Current.Equals(config.Previous))
            {
                return;
            }

            OnValueChangedEventArgs onValueChangedEventArgs = new OnValueChangedEventArgs(config.GpioPin, (CurrentValue)config.Current, (PreviousValue)config.Previous);

            switch (config.EventState)
            {
            case PinEventState.Activated when config.Current.DigitalValue:
                OnActivatedEventArgs onActivatedEventArgs = new OnActivatedEventArgs(config.GpioPin, (CurrentValue)config.Current);
                ParallelExecuteOnEach(config.EventState, (p) => p.OnActivated(this, onActivatedEventArgs)).RunSynchronously();
                break;

            case PinEventState.Deactivated when !config.Current.DigitalValue:
                OnDeactivatedEventArgs onDeactivatedEventArgs = new OnDeactivatedEventArgs(config.GpioPin, (CurrentValue)config.Current);
                ParallelExecuteOnEach(config.EventState, (p) => p.OnDeactivated(this, onDeactivatedEventArgs)).RunSynchronously();
                break;

            case PinEventState.Both:
                break;
            }

            ParallelExecuteOnEach(config.EventState, (p) => p.OnValueChanged(this, onValueChangedEventArgs)).RunSynchronously();
        }
Exemple #16
0
        internal override GpioPinState GpioPinStateRead(int pin)
        {
            if (!PinController.IsValidPin(pin) || !IsDriverInitialized)
            {
                return(GpioPinState.Off);
            }

            try {
                if (DriverController == null)
                {
                    return(GpioPinState.Off);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    DriverController.OpenPin(pin);
                }

                if (!DriverController.IsPinOpen(pin))
                {
                    return(GpioPinState.Off);
                }

                return(DriverController.Read(pin) == PinValue.High ? GpioPinState.Off : GpioPinState.On);
            }
            finally {
                ClosePin(pin);
            }
        }
Exemple #17
0
        public bool GpioDigitalRead(int pin)
        {
            if (!IsWiringPiInstalled())
            {
                return(false);
            }

            if (!PiGpioController.IsAllowedToExecute || !IsDriverProperlyInitialized)
            {
                return(false);
            }

            if (!PinController.IsValidPin(pin))
            {
                CastDriver <IGpioControllerDriver>(this)?.Logger.Log("The specified pin is invalid.");
                return(false);
            }

            try {
                return(ReadState(pin) == Enums.GpioPinState.Off ? false : true);
            }
            finally {
                Pin?pinConfig = GetPinConfig(pin);
                if (pinConfig != null)
                {
                    CastDriver <IGpioControllerDriver>(this).UpdatePinConfig(pinConfig);
                }
            }
        }
        public bool RegisterEvent(EventConfig config)
        {
            if (!PinController.IsValidPin(config.GpioPin))
            {
                Logger.Warning("The specified pin is invalid.");
                return(false);
            }

            Generator gen = new Generator(config);

            gen.Poll();

            for (int i = 0; i < 5; i++)
            {
                if (gen.IsEventRegistered)
                {
                    break;
                }

                Task.Delay(30).Wait();
            }

            if (!gen.IsEventRegistered)
            {
                return(false);
            }

            Events.Add(config.GpioPin, gen);
            return(gen.IsEventRegistered);
        }
Exemple #19
0
        public void TestGettingValue()
        {
            var fileIo = new Mock <IFileIo>();

            fileIo.Setup(x => x.ReadAllText("/sys/class/gpio/gpio43/value")).Returns("1");
            var pinMapper = new Mock <IPinMapper>();

            pinMapper.Setup(x => x.MapPinToSysfs("99")).Returns(43);
            var service = new PinController(fileIo.Object, pinMapper.Object);
            var pin     = service.GetGpioPin("99");
            var value   = pin.PinValue;

            Assert.True(value);
            value = pin.PinValue;
            Assert.True(value);
            value = pin.PinValue;

            Assert.True(value);

            pinMapper.Verify(x => x.MapPinToSysfs("99"), Times.Once);
            fileIo.Verify(x => x.WriteAllText("/sys/class/gpio/export", "43"), Times.Once);
            fileIo.Verify(x => x.WriteAllText("/sys/class/gpio/gpio43/direction", "out"), Times.Never);
            fileIo.Verify(x => x.WriteAllText("/sys/class/gpio/gpio43/direction", "in"), Times.Once);
            fileIo.Verify(x => x.WriteAllText("/sys/class/gpio/gpio43/value", "1"), Times.Exactly(0));
            fileIo.Verify(x => x.WriteAllText("/sys/class/gpio/gpio43/value", "0"), Times.Exactly(0));
            fileIo.Verify(x => x.ReadAllText("/sys/class/gpio/gpio43/value"), Times.Exactly(3));
        }
Exemple #20
0
            static void set(int pin)
            {
                if (Controller == null || PinController == null)
                {
                    return;
                }

                Pin?pinStatus = PinController.GetDriver()?.GetPinConfig(pin);

                if (pinStatus == null)
                {
                    return;
                }

                if (pinStatus.IsPinOn)
                {
                    PinController.GetDriver()?.SetGpioValue(pin, GpioPinMode.Output, GpioPinState.Off);
                    Logger.Log($"Successfully set {pin} pin to OFF.", LogLevels.Green);
                }
                else
                {
                    PinController.GetDriver()?.SetGpioValue(pin, GpioPinMode.Output, GpioPinState.On);
                    Logger.Log($"Successfully set {pin} pin to ON.", LogLevels.Green);
                }
            }
Exemple #21
0
 internal Generator(GpioCore _core, EventConfig _config, ILogger _logger)
 {
     Logger = _logger;
     Core   = _core;
     Config = _config;
     Driver = PinController.GetDriver() ?? throw new DriverNotInitializedException();
     Init();
 }
Exemple #22
0
        internal override bool GpioDigitalRead(int pin)
        {
            if (!PinController.IsValidPin(pin) || !IsDriverInitialized)
            {
                return(false);
            }

            return(((GpioPin)Pi.Gpio[pin]).Read());
        }
Exemple #23
0
        public bool TogglePinState(int pinNumber)
        {
            if (!PinController.IsValidPin(pinNumber))
            {
                return(false);
            }

            (COMMAND_KEY + $" toggle {pinNumber}").ExecuteBash(false);
            return(true);
        }
Exemple #24
0
        private bool WriteValue(int pinNumber, GpioPinState state)
        {
            if (!PinController.IsValidPin(pinNumber))
            {
                return(false);
            }

            CommandLine.Execute(COMMAND_KEY + $" write {pinNumber} {(int) state}");
            return(true);
        }
Exemple #25
0
        internal override bool TogglePinState(int pinNumber)
        {
            if (!PinController.IsValidPin(pinNumber))
            {
                return(false);
            }

            CommandLine.Execute(COMMAND_KEY + $" toggle {pinNumber}");
            return(true);
        }
Exemple #26
0
        public void TestGetDirectionPath()
        {
            var fileIo    = new Mock <IFileIo>();
            var pinMapper = new Mock <IPinMapper>();

            pinMapper.Setup(x => x.MapPinToSysfs("21")).Returns(33);
            var service = new PinController(fileIo.Object, pinMapper.Object);
            var pin     = service.GetGpioPin("21");

            Assert.Equal("/sys/class/gpio/gpio33/direction", pin.GetDirectionPath());
        }
Exemple #27
0
        internal override int GpioPhysicalPinNumber(int bcmPin)
        {
            if (!PinController.IsValidPin(bcmPin))
            {
                return(-1);
            }

            GpioPin GpioPin = (GpioPin)Pi.Gpio[bcmPin];

            return(GpioPin.PhysicalPinNumber);
        }
Exemple #28
0
        private bool WriteValue(int pinNumber, Enums.GpioPinState state)
        {
            if (!PinController.IsValidPin(pinNumber))
            {
                CastDriver <IGpioControllerDriver>(this)?.Logger.Log("The specified pin is invalid.");
                return(false);
            }

            (COMMAND_KEY + $" write {pinNumber} {(int) state}").ExecuteBash(false);
            return(true);
        }
Exemple #29
0
        internal override GpioPinState GpioPinStateRead(int pin)
        {
            if (!PinController.IsValidPin(pin) || !IsDriverInitialized)
            {
                return(GpioPinState.Off);
            }

            GpioPin gpioPin = (GpioPin)Pi.Gpio[pin];

            return((GpioPinState)gpioPin.ReadValue());
        }
Exemple #30
0
        private bool TogglePin(int pinNumber)
        {
            if (!PinController.IsValidPin(pinNumber))
            {
                CastDriver <IGpioControllerDriver>(this)?.Logger.Log("The specified pin is invalid.");
                return(false);
            }

            (COMMAND_KEY + $" toggle {pinNumber}").ExecuteBash(false);
            return(true);
        }