Beispiel #1
0
        private void InitGpio(BcmPin pin)
        {
            var gpio = Pi.Gpio[pin];

            gpio.InputPullMode = GpioPinResistorPullMode.PullUp;
            gpio.PinMode       = GpioPinDriveMode.Input;
        }
Beispiel #2
0
        public IGpioPin CreatePin(BcmPin id, GpioPinDriveMode mode = GpioPinDriveMode.Output)
        {
            var pin = new TestGpioPin(id);

            pin.PinMode = mode;
            return(pin);
        }
        public static GpioPinLock ObtainLock(BcmPin pin)
        {
            if (!Directory.Exists(LocksLocation))
            {
                Debug.WriteLine($"Gpio lock folder not found. Creating new one at {LocksLocation}");

                Directory.CreateDirectory(LocksLocation);

                Debug.WriteLine($"Gpio lock folder created.");
            }

            while (true)
            {
                try
                {
                    var pinName = Enum.GetName(typeof(BcmPin), pin);

                    Debug.WriteLine($"Requesting lock for {pinName}");

                    var path       = CreateLockFilePath(pin);
                    var lockStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                    lockStream.Lock(0, 0);

                    Debug.WriteLine($"{pinName} locked");

                    return(new GpioPinLock(pin, lockStream));
                }
                catch (IOException ex)
                {
                    Debug.WriteLine(ex.Message);
                    Thread.Sleep(50);
                }
            }
        }
Beispiel #4
0
        public IGpioPin CreatePin(BcmPin id, GpioPinDriveMode mode = GpioPinDriveMode.Output)
        {
            IGpioPin pin = Pi.Gpio[id];

            pin.PinMode = mode;
            return(pin);
        }
        public static void SetPinValue(BcmPin pinNumber, bool value, int?duration = null)
        {
            var pinName = Enum.GetName(typeof(BcmPin), pinNumber);

            Debug.WriteLine($"Setting value  of {pinName} to {value}");

            var pin = Pi.Gpio[pinNumber];

            if (pin.PinMode != GpioPinDriveMode.Output)
            {
                pin.PinMode = GpioPinDriveMode.Output;
            }

            using (GpioPinLock.ObtainLock(pinNumber))
            {
                pin.Write(value);
            }

            Debug.WriteLine($"Value of {pinName} is now {value}");

            if (duration.HasValue)
            {
                Debug.WriteLine($"Waiting for {duration}ms before setting {pinName} to {!value}.");

                Thread.Sleep(duration.Value);

                pin.Write(!value);

                Debug.WriteLine($"Value of {pinName} is now {value}");
            }
        }
 public WaterControllerContext(string id, BcmPin pin, double litersPerSecond)
 {
     TimeProvider    = new TestTimeProvider();
     GpioPin         = new TestGpioPin(pin);
     GpioPin.PinMode = GpioPinDriveMode.Output;
     GpioPin.Write(WaterController.PIN_OFF);
     WaterController = new WaterController(id, new LoggerFactory(), GpioPin, litersPerSecond, TimeProvider);
 }
Beispiel #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GpioPin"/> class.
        /// </summary>
        /// <param name="bcmPinNumber">The BCM pin number.</param>
        private GpioPin(BcmPin bcmPinNumber)
        {
            BcmPin       = bcmPinNumber;
            BcmPinNumber = (int)bcmPinNumber;

            WiringPiPinNumber = BcmToWiringPiPinNumber(bcmPinNumber);
            PhysicalPinNumber = Definitions.BcmToPhysicalPinNumber(SystemInfo.GetBoardRevision(), bcmPinNumber);
            Header            = (BcmPinNumber >= 28 && BcmPinNumber <= 31) ? GpioHeader.P5 : GpioHeader.P1;
        }
Beispiel #8
0
        static async Task Blink(BcmPin pinNumber, CancellationToken cancellationToken)
        {
            var pin = Pi.Gpio[pinNumber];

            pin.PinMode = GpioPinDriveMode.Output;

            while (!cancellationToken.IsCancellationRequested)
            {
                pin.Write(!pin.Value);
                await Task.Delay(500);
            }

            pin.Write(false);
        }
Beispiel #9
0
        private void CheckButton(BcmPin pin, Dictionary <BcmPin, bool> buttonState)
        {
            if (!Pi.Gpio[pin].Read() && !buttonState[pin])
            {
                buttonState[pin] = true;
                OnKeyPressed.Invoke((int)pin);
            }

            if (Pi.Gpio[pin].Read() && buttonState[pin])
            {
                OnKeyReleased.Invoke((int)pin);
                buttonState[pin] = false;
            }
        }
        public static bool GetPinValue(BcmPin pinNumber)
        {
            Debug.WriteLine($"Reading value  of {Enum.GetName(typeof(BcmPin), pinNumber)}");

            var pin = Pi.Gpio[pinNumber];

            if (pin.PinMode != GpioPinDriveMode.Input)
            {
                pin.PinMode = GpioPinDriveMode.Input;
            }

            var pinValue = pin.Read();

            Debug.WriteLine($"Value of {Enum.GetName(typeof(BcmPin), pinNumber)} is {pinValue}");

            return(pinValue);
        }
Beispiel #11
0
 public DHT(BcmPin datatPin, DHTSensorTypes sensor)
 {
     if (datatPin != 0)
     {
         _dataPin      = Pi.Gpio[datatPin];
         _firstReading = true;
         _prevReading  = DateTime.MinValue;
         _data         = new UInt32[6];
         _sensorType   = sensor;
         //Init the data pin
         _dataPin.PinMode = GpioPinDriveMode.Output;
         _dataPin.Write(GpioPinValue.High);
     }
     else
     {
         throw new ArgumentException("Parameter cannot be null.", "dataPin");
     }
 }
Beispiel #12
0
        /// <summary>
        ///  Setup the wheel
        /// </summary>
        /// <param name="pinPwm">Pin used for the PWM</param>
        /// <param name="pinFwd">Pin used to enable forward</param>
        /// <param name="pinRev">Pin used to enable reverse</param>
        public Wheel(BcmPin pinPwm, BcmPin pinFwd, BcmPin pinRev)
        {
            PinFwd         = (GpioPin)Pi.Gpio[pinFwd];
            PinFwd.PinMode = GpioPinDriveMode.Output;
            PinFwd.Write(true);

            PinRev         = (GpioPin)Pi.Gpio[pinRev];
            PinRev.PinMode = GpioPinDriveMode.Output;
            PinRev.Write(true);

            // TODO: Check out:
            // https://raspberrypi.stackexchange.com/questions/4906/control-hardware-pwm-frequency
            // https://stackoverflow.com/questions/20081286/controlling-a-servo-with-raspberry-pi-using-the-hardware-pwm-with-wiringpi

            PinPwm         = (GpioPin)Pi.Gpio[pinPwm];
            PinPwm.PinMode = GpioPinDriveMode.PwmOutput;
            PinPwm.PwmMode = PwmMode.MarkSign;

            // pwmFrequency in Hz = 19.2e6 Hz / pwmClock / pwmRange.
            PinPwm.PwmClockDivisor = 16; // 1 is 4096, possible values are all powers of 2 starting from 2 to 2048
            PinPwm.PwmRange        = 1024;
            PinPwm.PwmRegister     = 0;  // (int)(pin.PwmRange * decimal duty); // This goes from 0 to PwmRange-1
        }
Beispiel #13
0
 /// <summary>
 /// BCMs to physical pin number.
 /// </summary>
 /// <param name="rev">The rev.</param>
 /// <param name="bcmPin">The BCM pin.</param>
 /// <returns>The physical pin number.</returns>
 public static int BcmToPhysicalPinNumber(BoardRevision rev, BcmPin bcmPin) =>
 rev == BoardRevision.Rev1 ? GpioToPhysR1[(int)bcmPin] : GpioToPhysR2[(int)bcmPin];
 private GpioPinLock(BcmPin pin, FileStream lockStream)
 {
     LockStream = lockStream;
     Pin        = pin;
 }
Beispiel #15
0
 public MockGpioPin(BcmPin id)
 {
     BcmPin  = id;
     PinMode = GpioPinDriveMode.Input;
 }
 private static string CreateLockFilePath(BcmPin pin)
 {
     return(LocksLocation + Enum.GetName(typeof(BcmPin), pin));
 }
 public async Task <IGpioPinModel> SetPinConfiguration(BcmPin bcmPinNumbe, IGpioPinModel pinConfiguration)
 {
     throw new NotImplementedException();
 }
 public IGpioPinModel GetPinConfiguration(BcmPin bcmPin)
 {
     return(GetPinConfiguration(BcmPinToNumber(bcmPin)));
 }
 private int BcmPinToNumber(BcmPin bcmPin) =>
 _gpioController.Where(t => t.BcmPin == bcmPin).Select(t => t.BcmPinNumber).First();
Beispiel #20
0
 public IGpioPin this[BcmPin bcmPin] => this[(int)bcmPin];
Beispiel #21
0
 /// <inheritdoc />
 public IGpioPin this[BcmPin bcmPin] => throw new NotImplementedException();