Ejemplo n.º 1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="outPin">OUT Pin</param>
        /// <param name="pinNumberingScheme">Pin Numbering Scheme</param>
        public Hcsr501(int outPin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
        {
            _outPin = outPin;

            _controller = new GpioController(pinNumberingScheme);
            _controller.OpenPin(outPin, PinMode.Input);
            _controller.RegisterCallbackForPinValueChangedEvent(outPin, PinEventTypes.None, Sensor_ValueChanged);
        }
Ejemplo n.º 2
0
        public GpioController(GpioDriver driver, PinNumberingScheme numbering)
        {
            Driver    = driver;
            Numbering = numbering;
            _pins     = new Pin[driver.PinCount];

            driver.ValueChanged += OnPinValueChanged;
        }
Ejemplo n.º 3
0
 public Sonar(IOptions <SonarConfig> sonarConfig, CommunicationObject communicationObject, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
 {
     _triggerPin = sonarConfig.Value.TriggerPin;
     _echoPin    = sonarConfig.Value.EchoPin;
     _borderDistanceCentimeters = sonarConfig.Value.BorderDistanceCentimeters;
     _pinNumberingScheme        = pinNumberingScheme;
     _communicationObject       = communicationObject;
 }
Ejemplo n.º 4
0
        private void Initialize(GpioDriver driver, PinNumberingScheme numbering)
        {
            Driver    = driver;
            Numbering = numbering;
            _pins     = new Dictionary <int, GpioPin>();

            driver.ValueChanged += OnPinValueChanged;
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initialize a TM1637
 /// </summary>
 /// <param name="pinClk">The clock pin</param>
 /// <param name="pinDio">The data pin</param>
 /// <param name="pinNumberingScheme">Use the logical or physical pin layout</param>
 /// <param name="gpioController">A Gpio Controller if you want to use a specific one</param>
 public Tm1637(int pinClk, int pinDio, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, IGpioController gpioController = null)
 {
     _pinClk     = pinClk;
     _pinDio     = pinDio;
     _controller = gpioController != null ? (GpioController)gpioController : new GpioController(pinNumberingScheme);
     _controller.OpenPin(_pinClk, PinMode.Output);
     _controller.OpenPin(_pinDio, PinMode.Output);
     _brightness = 7;
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Create a DHT sensor
        /// </summary>
        /// <param name="pin">The pin number (GPIO number)</param>
        /// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
        public DhtBase(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
        {
            _protocol   = CommunicationProtocol.OneWire;
            _controller = new GpioController(pinNumberingScheme);
            _pin        = pin;

            _controller.OpenPin(_pin);
            // delay 1s to make sure DHT stable
            Thread.Sleep(1000);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a new instance of the HC-SCR501.
        /// </summary>
        /// <param name="outPin">OUT Pin</param>
        /// <param name="pinNumberingScheme">Pin Numbering Scheme</param>
        /// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
        /// <param name="shouldDispose">True to dispose the Gpio Controller</param>
        public Hcsr501(int outPin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController gpioController = null, bool shouldDispose = true)
        {
            _outPin = outPin;

            _shouldDispose = gpioController == null ? true : shouldDispose;
            _controller    = gpioController ?? new GpioController(pinNumberingScheme);
            _controller.OpenPin(outPin, PinMode.Input);
            _controller.RegisterCallbackForPinValueChangedEvent(outPin, PinEventTypes.Falling, Sensor_ValueChanged);
            _controller.RegisterCallbackForPinValueChangedEvent(outPin, PinEventTypes.Rising, Sensor_ValueChanged);
        }
Ejemplo n.º 8
0
        protected internal override int ConvertPinNumber(int pinNumber, PinNumberingScheme from, PinNumberingScheme to)
        {
            VerifyPinIsInValidRange(pinNumber, nameof(pinNumber));
            if (from == PinNumberingScheme.Gpio && to == PinNumberingScheme.Gpio)
            {
                return(pinNumber);
            }

            throw new NotSupportedException("Only PinNumberingScheme.Gpio numbering scheme is supported");
        }
Ejemplo n.º 9
0
        protected internal override int ConvertPinNumber(int bcmPinNumber, PinNumberingScheme from, PinNumberingScheme to)
        {
            ValidatePinNumber(bcmPinNumber);

            if (from != PinNumberingScheme.Bcm || to != PinNumberingScheme.Bcm)
            {
                throw new NotSupportedException("Only BCM numbering scheme is supported");
            }

            return(bcmPinNumber);
        }
Ejemplo n.º 10
0
        protected internal override int ConvertPinNumber(int gpioPinNumber, PinNumberingScheme from, PinNumberingScheme to)
        {
            ValidatePinNumber(gpioPinNumber);

            if (from != PinNumberingScheme.Gpio || to != PinNumberingScheme.Gpio)
            {
                throw new NotSupportedException("Only Gpio numbering scheme is supported");
            }

            return(gpioPinNumber);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Create a DHT sensor
        /// </summary>
        /// <param name="pin">The pin number (GPIO number)</param>
        /// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
        /// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
        /// <param name="shouldDispose">True to dispose the Gpio Controller</param>
        public DhtBase(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController gpioController = null, bool shouldDispose = true)
        {
            _protocol      = CommunicationProtocol.OneWire;
            _shouldDispose = gpioController == null ? true : shouldDispose;
            _controller    = gpioController ?? new GpioController(pinNumberingScheme);
            _pin           = pin;

            _controller.OpenPin(_pin);
            // delay 1s to make sure DHT stable
            Thread.Sleep(1000);
        }
Ejemplo n.º 12
0
Archivo: Tm1637.cs Proyecto: dotnet/iot
 /// <summary>
 /// Initialize a TM1637
 /// </summary>
 /// <param name="pinClk">The clock pin</param>
 /// <param name="pinDio">The data pin</param>
 /// <param name="pinNumberingScheme">Use the logical or physical pin layout</param>
 /// <param name="gpioController">A Gpio Controller if you want to use a specific one</param>
 /// <param name="shouldDispose">True to dispose the Gpio Controller</param>
 public Tm1637(int pinClk, int pinDio, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical,
               GpioController?gpioController = null, bool shouldDispose = true)
 {
     _pinClk        = pinClk;
     _pinDio        = pinDio;
     _controller    = gpioController ?? new GpioController(pinNumberingScheme);
     _shouldDispose = shouldDispose || gpioController is null;
     _controller.OpenPin(_pinClk, PinMode.Output);
     _controller.OpenPin(_pinDio, PinMode.Output);
     _brightness = 7;
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Creates a new instance of the HC-SCR04 sonar.
        /// </summary>
        /// <param name="triggerPin">Trigger pulse input.</param>
        /// <param name="echoPin">Trigger pulse output.</param>
        /// <param name="pinNumberingScheme">Pin Numbering Scheme</param>
        public Hcsr04(int triggerPin, int echoPin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
        {
            _echo       = echoPin;
            _trigger    = triggerPin;
            _controller = new GpioController(pinNumberingScheme);

            _controller.OpenPin(_echo, PinMode.Input);
            _controller.OpenPin(_trigger, PinMode.Output);

            _controller.Write(_trigger, PinValue.Low);
        }
Ejemplo n.º 14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="dtPin"></param>
 /// <param name="sckPin"></param>
 /// <param name="calibrationWeightSystem">Determines which calibration unit will be used</param>
 /// <param name="calibrationImperialUnit">Only needs entered if weight system is Imperial</param>
 /// <param name="calibrationMetricUnit">Only needs entered if weight system is Metric</param>
 /// <param name="pinNumberingScheme"></param>
 /// <param name="gain"></param>
 /// <param name="msb">Boolean indicating that the HX711 outputs the most significant bit first</param>
 public HX711Settings(int dtPin, int sckPin, WeightSystem calibrationWeightSystem = WeightSystem.Metric,
                      ImperialUnit calibrationImperialUnit = ImperialUnit.Pounds, MetricUnit calibrationMetricUnit = MetricUnit.Grams,
                      double calibrationValue = 0, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, bool msb = true)
 {
     DTPin  = dtPin;
     SCKPin = sckPin;
     MSB    = msb;
     CalibrationWeightSystem = calibrationWeightSystem;
     CalibrationImperialUnit = calibrationImperialUnit;
     CalibrationMetricUnit   = calibrationMetricUnit;
     CalibrationValue        = calibrationValue;
     PinNumberingScheme      = pinNumberingScheme;
 }
Ejemplo n.º 15
0
        internal SystemDeviceDriver?InitDriver(PinNumberingScheme numberingScheme)
        {
            if (!PiController.IsAllowedToExecute)
            {
                Logger.Warning("Failed to initialize Gpio Controller Driver.");
                IsDriverProperlyInitialized = false;
                return(null);
            }

            Controller = new GpioController(numberingScheme);
            IsDriverProperlyInitialized = true;
            return(this);
        }
Ejemplo n.º 16
0
        protected internal override int ConvertPinNumber(int bcmPinNumber, PinNumberingScheme from, PinNumberingScheme to)
        {
            if (bcmPinNumber < 0 || bcmPinNumber >= PinCount)
            {
                throw new ArgumentOutOfRangeException(nameof(bcmPinNumber));
            }

            if (from != PinNumberingScheme.BCM || to != PinNumberingScheme.BCM)
            {
                throw new NotSupportedException("Only BCM numbering scheme is supported");
            }

            return(bcmPinNumber);
        }
Ejemplo n.º 17
0
        public GpioController(PinNumberingScheme numbering = PinNumberingScheme.Gpio)
        {
            GpioDriver driver;
            bool       isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

            if (isLinux)
            {
                driver = new UnixDriver();
            }
            else
            {
                throw new NotSupportedException($"Platform '{RuntimeInformation.OSDescription}' not supported");
            }

            Initialize(driver, numbering);
        }
Ejemplo n.º 18
0
        protected internal override int ConvertPinNumber(int pinNumber, PinNumberingScheme from, PinNumberingScheme to)
        {
            int result = -1;

            switch (from)
            {
            case PinNumberingScheme.BCM:
                switch (to)
                {
                case PinNumberingScheme.BCM:
                    result = pinNumber;
                    break;

                case PinNumberingScheme.Board:
                    //throw new NotImplementedException();
                    break;

                default:
                    throw new NotSupportedPinNumberingSchemeException(to);
                }
                break;

            case PinNumberingScheme.Board:
                switch (to)
                {
                case PinNumberingScheme.Board:
                    result = pinNumber;
                    break;

                case PinNumberingScheme.BCM:
                    //throw new NotImplementedException();
                    break;

                default:
                    throw new NotSupportedPinNumberingSchemeException(to);
                }
                break;

            default:
                throw new NotSupportedPinNumberingSchemeException(from);
            }

            return(result);
        }
Ejemplo n.º 19
0
        protected internal override int ConvertPinNumber(int pinNumber, PinNumberingScheme from, PinNumberingScheme to)
        {
            int result = -1;

            switch (from)
            {
            case PinNumberingScheme.Bcm:
                switch (to)
                {
                case PinNumberingScheme.Bcm:
                    result = pinNumber;
                    break;

                case PinNumberingScheme.Board:
                    //throw new NotImplementedException();
                    break;

                default:
                    throw new NotSupportedException($"Unsupported GPIO pin numbering scheme {to}");
                }
                break;

            case PinNumberingScheme.Board:
                switch (to)
                {
                case PinNumberingScheme.Board:
                    result = pinNumber;
                    break;

                case PinNumberingScheme.Bcm:
                    //throw new NotImplementedException();
                    break;

                default:
                    throw new NotSupportedException($"Unsupported GPIO pin numbering scheme {to}");
                }
                break;

            default:
                throw new NotSupportedException($"Unsupported GPIO pin numbering scheme {from}");
            }

            return(result);
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Create a DHT12 sensor
 /// </summary>
 /// <param name="pin">The pin number (GPIO number)</param>
 /// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
 public Dht12(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
     : base(pin, pinNumberingScheme)
 {
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Initializes new instance of GpioController that will use the specified numbering scheme and driver.
 /// </summary>
 /// <param name="numberingScheme">The numbering scheme used to represent pins provided by the controller.</param>
 /// <param name="driver">The driver that manages all of the pin operations for the controller.</param>
 public GpioController(PinNumberingScheme numberingScheme, GpioDriver driver)
 {
     _driver         = driver;
     NumberingScheme = numberingScheme;
     _openPins       = new HashSet <int>();
 }
Ejemplo n.º 22
0
 public GpioController(GpioDriver driver, PinNumberingScheme numbering = PinNumberingScheme.Gpio)
 {
     Initialize(driver, numbering);
 }
 public NotSupportedPinNumberingSchemeException(PinNumberingScheme numbering)
     : base($"Unsupported GPIO pin numbering scheme {numbering}")
 {
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Creates a new instance of the HC-SCR04 sonar.
 /// </summary>
 /// <param name="triggerPin">Trigger pulse input.</param>
 /// <param name="echoPin">Trigger pulse output.</param>
 /// <param name="pinNumberingScheme">Pin Numbering Scheme</param>
 public Hcsr04(int triggerPin, int echoPin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
     : this(new GpioController(pinNumberingScheme), triggerPin, echoPin)
 {
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Creates a PWM channel
 /// </summary>
 /// <param name="chip">Chip number. Usually 0</param>
 /// <param name="channel">Channel on the given chip</param>
 /// <param name="frequency">Initial frequency</param>
 /// <param name="dutyCyclePercentage">Initial duty cycle</param>
 /// <param name="pin">The pin number for the pwm channel. Used if not hardwired (i.e. on the Raspi, it is possible to use different pins for the same PWM channel)</param>
 /// <param name="pinNumberingScheme">The pin numbering scheme for the pin</param>
 /// <returns>A pwm channel instance</returns>
 public PwmChannel CreatePwmChannel(int chip, int channel, int frequency, double dutyCyclePercentage,
                                    int pin, PinNumberingScheme pinNumberingScheme)
 {
     Initialize();
     return(new PwmChannelManager(this, pin, chip, channel, frequency, dutyCyclePercentage, CreateSimplePwmChannel));
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Create an SPI device instance
        /// </summary>
        /// <param name="connectionSettings">Connection parameters (contains bus number and CS pin number)</param>
        /// <param name="pinAssignment">The set of pins to use for SPI. The parameter can be null if the hardware requires a fixed mapping from
        /// pins to SPI for the given bus.</param>
        /// <param name="pinNumberingScheme">The numbering scheme in which the <paramref name="pinAssignment"/> is given</param>
        /// <returns>An SPI device instance</returns>
        public SpiDevice CreateSpiDevice(SpiConnectionSettings connectionSettings, int[] pinAssignment, PinNumberingScheme pinNumberingScheme)
        {
            Initialize();
            if (pinAssignment == null)
            {
                throw new ArgumentNullException(nameof(pinAssignment));
            }

            if (pinAssignment.Length != 3 && pinAssignment.Length != 4)
            {
                throw new ArgumentException($"Invalid argument. Must provide three or four pins for SPI", nameof(pinAssignment));
            }

            return(new SpiDeviceManager(this, connectionSettings, pinAssignment, CreateSimpleSpiDevice));
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Initializes new instance of GpioController that will use the specified numbering scheme.
 /// The controller will default to use the driver that best applies given the platform the program is executing on.
 /// </summary>
 /// <param name="numberingScheme">The numbering scheme used to represent pins provided by the controller.</param>
 public GpioController(PinNumberingScheme numberingScheme)
     : this(numberingScheme, GetBestDriverForBoard())
 {
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Gets the board-specific hardware mode for a particular pin and pin usage (i.e. the different ALTn modes on the raspberry pi)
        /// </summary>
        /// <param name="pinNumber">Pin number to use</param>
        /// <param name="usage">Requested usage</param>
        /// <param name="pinNumberingScheme">Pin numbering scheme for the pin provided (logical or physical)</param>
        /// <param name="bus">Optional bus argument, for SPI and I2C pins</param>
        /// <returns>
        /// A member of <see cref="RaspberryPi3Driver.AltMode"/> describing the mode the pin is in.</returns>
        private RaspberryPi3Driver.AltMode GetHardwareModeForPinUsage(int pinNumber, PinUsage usage, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, int bus = 0)
        {
            if (pinNumber >= PinCount)
            {
                throw new InvalidOperationException($"Invalid pin number {pinNumber}");
            }

            if (usage == PinUsage.Gpio)
            {
                // all pins support GPIO
                return(RaspberryPi3Driver.AltMode.Input);
            }

            if (usage == PinUsage.I2c)
            {
                // The Pi4 has a big number of pins that can become I2C pins
                switch (pinNumber)
                {
                // Busses 0 and 1 run on Alt0
                case 0:
                case 1:
                case 2:
                case 3:
                    return(RaspberryPi3Driver.AltMode.Alt0);

                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                    return(RaspberryPi3Driver.AltMode.Alt5);

                case 22:
                case 23:
                    return(RaspberryPi3Driver.AltMode.Alt5);
                }

                throw new NotSupportedException($"No I2C support on Pin {pinNumber}.");
            }

            if (usage == PinUsage.Pwm)
            {
                if (pinNumber == 12 || pinNumber == 13)
                {
                    return(RaspberryPi3Driver.AltMode.Alt0);
                }

                if (pinNumber == 18 || pinNumber == 19)
                {
                    return(RaspberryPi3Driver.AltMode.Alt5);
                }

                throw new NotSupportedException($"No Pwm support on Pin {pinNumber}.");
            }

            if (usage == PinUsage.Spi)
            {
                switch (pinNumber)
                {
                case 7:     // Pin 7 can be assigned to either SPI0 or SPI4
                    return(bus == 0 ? RaspberryPi3Driver.AltMode.Alt0 : RaspberryPi3Driver.AltMode.Alt3);

                case 8:
                case 9:
                case 10:
                case 11:
                    return(RaspberryPi3Driver.AltMode.Alt0);

                case 0:
                case 1:
                case 2:
                case 3:
                    return(RaspberryPi3Driver.AltMode.Alt3);

                case 4:
                case 5:
                case 6:
                    return(RaspberryPi3Driver.AltMode.Alt3);

                case 12:
                case 13:
                case 14:
                case 15:
                    return(RaspberryPi3Driver.AltMode.Alt3);

                case 16:
                case 17:
                    return(RaspberryPi3Driver.AltMode.Alt4);

                case 18:
                case 19:
                case 20:
                case 21:
                    return(bus == 6 ? RaspberryPi3Driver.AltMode.Alt3 : RaspberryPi3Driver.AltMode.Alt4);

                case 24:
                case 25:
                case 26:
                case 27:
                    return(RaspberryPi3Driver.AltMode.Alt5);
                }

                throw new NotSupportedException($"No SPI support on Pin {pinNumber}.");
            }

            if (usage == PinUsage.Uart)
            {
                switch (pinNumber)
                {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                    return(RaspberryPi3Driver.AltMode.Alt4);

                case 14:
                case 15:
                    if (bus == 0)
                    {
                        return(RaspberryPi3Driver.AltMode.Alt0);
                    }
                    else if (bus == 5)
                    {
                        return(RaspberryPi3Driver.AltMode.Alt4);
                    }
                    else if (bus == 1)
                    {
                        return(RaspberryPi3Driver.AltMode.Alt5);
                    }

                    break;

                case 16:
                case 17:
                    return((bus == 0) ? RaspberryPi3Driver.AltMode.Alt3 : RaspberryPi3Driver.AltMode.Alt5);
                }

                throw new NotSupportedException($"No Uart support on Pin {pinNumber}.");
            }

            throw new NotSupportedException($"There are no known pins for {usage}.");
        }
Ejemplo n.º 29
0
 /// <summary>
 /// Create a DHT22 sensor
 /// </summary>
 /// <param name="pin">The pin number (GPIO number)</param>
 /// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
 /// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
 /// <param name="shouldDispose">True to dispose the Gpio Controller</param>
 public Dht21(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController gpioController = null, bool shouldDispose = true)
     : base(pin, pinNumberingScheme, gpioController, shouldDispose)
 {
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GpioController"/> class that will use the specified numbering scheme and driver.
 /// </summary>
 /// <param name="numberingScheme">The numbering scheme used to represent pins provided by the controller.</param>
 protected AnalogController(PinNumberingScheme numberingScheme)
 {
     NumberingScheme = numberingScheme;
     _openPins       = new List <AnalogInputPin>();
 }