Exemplo n.º 1
0
        /// <summary>
        /// IOutputSegment implementation that uses GpioController.
        /// </summary>
        /// <param name="pins">The GPIO pins that should be used and are connected.</param>
        /// <param name="gpioController">The GpioController to use. If one isn't provided, one will be created.</param>
        /// <param name="shouldDispose">The policy to use (true, by default) for disposing the GPIO controller when disposing this instance.</param>
        public GpioOutputSegment(int[] pins, GpioController?gpioController = null, bool shouldDispose = true)
        {
            _shouldDispose = shouldDispose || gpioController is null;
            _controller    = gpioController ?? new GpioController();
            _pins          = pins;
            _segment       = new VirtualOutputSegment(_pins.Length);

            foreach (var pin in _pins)
            {
                _controller.OpenPin(pin, PinMode.Output);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initialize a new shift register connected through GPIO.
 /// </summary>
 /// <param name="pinMapping">The pin mapping to use by the binding.</param>
 /// <param name="bitLength">Bit length of register, including chained registers.</param>
 /// <param name="gpioController">The GPIO Controller used for interrupt handling.</param>
 /// <param name="shouldDispose">True (the default) if the GPIO controller shall be disposed when disposing this instance.</param>
 public ShiftRegister(ShiftRegisterPinMapping pinMapping, int bitLength, GpioController?gpioController = null, bool shouldDispose = true)
 {
     _shouldDispose = shouldDispose || gpioController is null;
     _controller    = gpioController ?? new GpioController();
     _pinMapping    = pinMapping;
     _serial        = _pinMapping.SerialDataInput;
     _clock         = _pinMapping.Clock;
     _latch         = _pinMapping.LatchEnable;
     _bitLength     = bitLength;
     _segment       = new VirtualOutputSegment(_bitLength);
     SetupPins();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new Charlieplex type that can be use for multiplex over a relatively small number of GPIO pins.
        /// </summary>
        /// <param name="pins">The set of pins to use.</param>
        /// <param name="nodeCount">The count of nodes (like LEDs) that will be addressable. If 0, then the Charlieplex maximum is used for the pins provided (n^2-n).</param>
        /// <param name="gpioController">The GPIO Controller used for interrupt handling.</param>
        /// <param name="shouldDispose">True (the default) if the GPIO controller shall be disposed when disposing this instance.</param>
        public CharlieplexSegment(int[] pins, int nodeCount = 0, GpioController?gpioController = null, bool shouldDispose = true)
        {
            if (pins.Length < 2)
            {
                throw new ArgumentException("2 or more pins must be provided.", nameof(pins));
            }

            int charlieCount = (pins.Length * pins.Length) - pins.Length;

            if (nodeCount > charlieCount)
            {
                throw new ArgumentException($"Maximum count is {charlieCount} based on {pins.Length} pins. {nodeCount} was specified as the count.", nameof(nodeCount));
            }

            if (nodeCount == 0)
            {
                nodeCount = charlieCount;
            }

            _shouldDispose  = shouldDispose || gpioController is null;
            _gpioController = gpioController ?? new();

            // first two pins will be needed as Output.
            _gpioController.OpenPin(pins[0], PinMode.Output);
            _gpioController.OpenPin(pins[1], PinMode.Output);

            // remaining pins should be input type
            // prevents participating in the circuit until needed
            for (int i = 2; i < pins.Length; i++)
            {
                _gpioController.OpenPin(pins[i], PinMode.Input);
            }

            _lastNode = new CharlieplexSegmentNode()
            {
                Anode   = pins[1],
                Cathode = pins[0]
            };
            _pins      = pins;
            _nodeCount = nodeCount;
            _nodes     = GetNodes(pins, nodeCount);
            _segment   = new VirtualOutputSegment(_nodeCount);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initialize a new shift register device connected through SPI.
 /// Uses 3 pins (SDI -> SDI, SCLK -> SCLK, CE0 -> LE)
 /// </summary>
 /// <param name="spiDevice">SpiDevice used for serial communication.</param>
 /// <param name="bitLength">Bit length of register, including chained registers.</param>
 public ShiftRegister(SpiDevice spiDevice, int bitLength)
 {
     _spiDevice = spiDevice ?? throw new ArgumentNullException(nameof(spiDevice));
     _bitLength = bitLength;
     _segment   = new VirtualOutputSegment(_bitLength);
 }