Exemplo n.º 1
0
        /// <summary>
        /// Initializes the bit mode settings.
        /// </summary>
        protected override void InitializeBitMode()
        {
            // Prep the pins
            _controller.OpenPin(_rsPin, PinMode.Output);

            if (_rwPin != -1)
            {
                _controller.OpenPin(_rwPin, PinMode.Output);
            }
            if (_backlight != -1)
            {
                _controller.OpenPin(_backlight, PinMode.Output);
                if (_backlightBrightness > 0)
                {
                    // Turn on the backlight
                    _controller.Write(_backlight, PinValue.High);
                }
            }
            _controller.OpenPin(_enablePin, PinMode.Output);

            for (int i = 0; i < _dataPins.Length; ++i)
            {
                _controller.OpenPin(_dataPins[i], PinMode.Output);
            }

            // The HD44780 self-initializes when power is turned on to the following settings:
            //
            //  - 8 bit, 1 line, 5x7 font
            //  - Display, cursor, and blink off
            //  - Increment with no shift
            //
            // It is possible that the initialization will fail if the power is not provided
            // within specific tolerances. As such, we'll always perform the software based
            // initialization as described on pages 45/46 of the HD44780 data sheet. We give
            // a little extra time to the required waits.

            if (_dataPins.Length == 8)
            {
                // Init to 8 bit mode
                DelayMicroseconds(50_000, checkBusy: false);
                Send(0b0011_0000);
                DelayMicroseconds(5_000, checkBusy: false);
                Send(0b0011_0000);
                DelayMicroseconds(100, checkBusy: false);
                Send(0b0011_0000);
            }
            else
            {
                // Init to 4 bit mode, setting _rspin to low as we're writing 4 bits directly.
                // (Send writes the whole byte in two 4bit/nybble chunks)
                _controller.Write(_rsPin, PinValue.Low);
                DelayMicroseconds(50_000, checkBusy: false);
                WriteBits(0b0011, 4);
                DelayMicroseconds(5_000, checkBusy: false);
                WriteBits(0b0011, 4);
                DelayMicroseconds(100, checkBusy: false);
                WriteBits(0b0011, 4);
                WriteBits(0b0010, 4);
            }
        }
Exemplo n.º 2
0
        public async Task Initialize()
        {
            try
            {
                await _semaphore.WaitAsync();

                if (!_initialized)
                {
                    _gpioController.OpenPin(_configurationService.SaunaOutputGpioPin, PinMode.Output);
                    _gpioController.Write(_configurationService.SaunaOutputGpioPin, PinValue.High);

                    _gpioController.OpenPin(_configurationService.InfraredOutputGpioPin, PinMode.Output);
                    _gpioController.Write(_configurationService.InfraredOutputGpioPin, PinValue.High);

                    _gpioController.OpenPin(_configurationService.SaunaInputGpioPin, PinMode.Input);
                    _gpioController.OpenPin(_configurationService.InfraredInputGpioPin, PinMode.Input);

                    _initialized = true;
                }
            }
            finally
            {
                _semaphore.Release();
            }
        }
Exemplo n.º 3
0
        public void InitGpio()
        {
            try
            {
                gpioController = GpioController.Instance;

                //Set Direction of pins (Input or Output)

                pin17 = gpioController.OpenPin(17);
                pin17.SetDriveMode(GpioPinDriveMode.Input);

                pin22 = gpioController.OpenPin(22);
                pin22.SetDriveMode(GpioPinDriveMode.Input);

                //Init value
                dt1            = DateTime.Now;
                stateChange    = 0;
                oldStatusPin17 = "";

                DeviceReady = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Init Gpio Error : " + ex.Message);
                DeviceReady = false;
            }
        }
Exemplo n.º 4
0
        private void SetRelays(List <Relay> relays)
        {
            foreach (var relay in relays)
            {
                _gpioController.OpenPin(relay.GpioPin);
            }

            _relays = relays;
        }
 public void OpenPin(int pinNumber)
 {
     try
     {
         _controller.OpenPin(pinNumber);
         _openPins.Add(pinNumber);
     }
     catch (IOException ex)
     {
         throw new IOException($"Error opening pin {pinNumber}: " + ex.Message, ex);
     }
 }
Exemplo n.º 6
0
        public IActionResult Get(int pinId)
        {
            GpioPinValue pinStatus;

            Console.WriteLine("About to get pin status.");
            var pin = gpioController.OpenPin(pinId);

            pinStatus = pin.Read();

            Console.WriteLine("Returning pin status.");
            return(Ok(pinStatus.ToString()));
        }
Exemplo n.º 7
0
        public IActionResult Get(int pinId)
        {
            GpioPinValue pinStatus;

            _logger.LogInformation("About to get pin status.");
            var pin = _gpioController.OpenPin(pinId);

            pinStatus = pin.Read();

            _logger.LogInformation("Returning pin status.");
            return(Ok(pinStatus.ToString()));
        }
Exemplo n.º 8
0
        public Motor(IGpioController gpio, byte in1, byte in2)
        {
            _gpio = gpio;
            _in1  = in1;
            _in2  = in2;

            _gpio.OpenPin(in1, PinMode.Output);
            _gpio.OpenPin(in2, PinMode.Output);

            _gpio.Write(in1, PinValue.Low);
            _gpio.Write(in2, PinValue.Low);
        }
Exemplo n.º 9
0
        private void Initialise(byte trig_Pin, byte echo_Pin, int timeoutMilliseconds)
        {
            _trig_Pin = trig_Pin;
            _echo_Pin = echo_Pin;

            TimeoutMilliseconds = timeoutMilliseconds;

            _gpio.OpenPin(trig_Pin, PinMode.Output);
            _gpio.OpenPin(echo_Pin, PinMode.Input);

            _gpio.Write(trig_Pin, PinValue.Low);
        }
Exemplo n.º 10
0
        public SpiInterface(SpiDevice device, IGpioController controller, int dcPin, int rstPin)
        {
            _device     = device ?? throw new ArgumentNullException(nameof(device));
            _controller = controller ?? throw new ArgumentNullException(nameof(controller));
            _dcPin      = dcPin;
            _rstPin     = rstPin;

            _disposables.Add(_controller.OpenPin(_dcPin, PinMode.Output));
            _disposables.Add(_controller.OpenPin(_rstPin, PinMode.Output));

            _controller.Write(_rstPin, PinValue.Low);
            _controller.Write(_rstPin, PinValue.High);
        }
Exemplo n.º 11
0
        public int CheckPin(int pinId)
        {
            GpioPinValue pinStatus;

            _logger.LogInformation("About to get pin status:" + pinId);
            var pin = _gpioController.OpenPin(pinId);

            pinStatus = pin.Read();

            _logger.LogInformation("Returning pin status.");
            _logger.LogInformation(pinStatus.ToString());
            return((int)pinStatus);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Initialize a Uln2003 class.
        /// </summary>
        /// <param name="pin1">The GPIO pin number which corresponds pin A on ULN2003 driver board.</param>
        /// <param name="pin2">The GPIO pin number which corresponds pin B on ULN2003 driver board.</param>
        /// <param name="pin3">The GPIO pin number which corresponds pin C on ULN2003 driver board.</param>
        /// <param name="pin4">The GPIO pin number which corresponds pin D on ULN2003 driver board.</param>
        /// <param name="controller">The controller.</param>
        public Uln2003(int pin1, int pin2, int pin3, int pin4, IGpioController controller = null)
        {
            _pin1 = pin1;
            _pin2 = pin2;
            _pin3 = pin3;
            _pin4 = pin4;

            _controller = controller ?? new GpioController();

            _controller.OpenPin(_pin1, PinMode.Output);
            _controller.OpenPin(_pin2, PinMode.Output);
            _controller.OpenPin(_pin3, PinMode.Output);
            _controller.OpenPin(_pin4, PinMode.Output);
        }
Exemplo n.º 13
0
        /// <summary>
        /// A general purpose parallel I/O expansion for I2C or SPI applications.
        /// </summary>
        /// <param name="bus">The bus the device is connected to.</param>
        /// <param name="reset">The output pin number that is connected to the hardware reset.</param>
        /// <param name="interruptA">The input pin number that is connected to the interrupt for Port A (INTA).</param>
        /// <param name="interruptB">The input pin number that is connected to the interrupt for Port B (INTB).</param>
        /// <param name="masterController">
        /// The controller for the reset and interrupt pins. If not specified, the default controller will be used.
        /// </param>
        /// <param name="bankStyle">
        /// The current bank style of the ports. This does not set the bank style- it tells us what the bank style is.
        /// It is *highly* recommended not to change the bank style from the default as there is no direct way to
        /// detect what style the chip is in and most apps will fail if the chip is not set to defaults. This setting
        /// has no impact on 8-bit expanders.
        /// </param>
        protected Mcp23xxx(BusAdapter bus, int reset = -1, int interruptA = -1, int interruptB = -1, IGpioController masterController = null, BankStyle bankStyle = BankStyle.Sequential)
        {
            _bus       = bus;
            _bankStyle = bankStyle;

            _reset      = reset;
            _interruptA = interruptA;
            _interruptB = interruptB;

            // Only need master controller if there are external pins provided.
            if (_reset != -1 || _interruptA != -1 || _interruptB != -1)
            {
                _masterGpioController = masterController ?? new GpioController();

                if (_interruptA != -1)
                {
                    _masterGpioController.OpenPin(_interruptA, PinMode.Input);
                }

                if (_interruptB != -1)
                {
                    _masterGpioController.OpenPin(_interruptB, PinMode.Input);
                }

                if (_reset != -1)
                {
                    _masterGpioController.OpenPin(_reset, PinMode.Output);
                    Disable();
                }
            }

            if (!_disabled)
            {
                // Set all of the pins to input, GPIO outputs to low, and set input polarity to match the input.
                // This is the normal power-on / reset state of the Mcp23xxx chips.
                if (PinCount == 8)
                {
                    InternalWriteByte(Register.IODIR, 0xFF, Port.PortA);
                    InternalWriteByte(Register.GPIO, 0x00, Port.PortA);
                    InternalWriteByte(Register.IPOL, 0x00, Port.PortA);
                }
                else
                {
                    InternalWriteUInt16(Register.IODIR, 0xFFFF);
                    InternalWriteUInt16(Register.GPIO, 0x0000);
                    InternalWriteUInt16(Register.IPOL, 0x0000);
                }
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Remote I/O expander for I2C-bus with interrupt.
        /// </summary>
        /// <param name="device">The I2C device.</param>
        /// <param name="interrupt">The interrupt pin number, if present.</param>
        /// <param name="gpioController">
        /// The GPIO controller for the <paramref name="interrupt"/>.
        /// If not specified, the default controller will be used.
        /// </param>
        public Pcx857x(I2cDevice device, int interrupt = -1, IGpioController gpioController = null)
        {
            Device     = device ?? throw new ArgumentNullException(nameof(device));
            _interrupt = interrupt;

            if (_interrupt != -1)
            {
                _masterGpioController = gpioController ?? new GpioController();
                _masterGpioController.OpenPin(_interrupt, PinMode.Input);
            }

            // These controllers do not have commands, setting the pins to high designates
            // them as able to recieve input. As we don't want to set high on pins intended
            // for output we'll set all of the pins to low for our initial state.
            if (PinCount == 8)
            {
                WriteByte(0x00);
            }
            else
            {
                InternalWriteUInt16(0x0000);
            }

            _pinModes = 0xFFFF;
        }
Exemplo n.º 15
0
        private void InitLed(int pinNumber)
        {
            _ts.pinNumber = pinNumber;
            _gpio.OpenPin(_ts.pinNumber, PinMode.Output);
            _gpio.Write(_ts.pinNumber, PinValue.Low);

            _ts.tmr = new Timer(BlinkTime_Tick, _ts, Timeout.Infinite, Timeout.Infinite);
        }
Exemplo n.º 16
0
 private void InitializeRelayDevice(RelayDevice device)
 {
     controller.OpenPin(device.GpioId);
     controller.SetPinMode(device.GpioId, PinMode.Output);
     // Relay needs high
     controller.Write(device.GpioId, PinValue.High);
     // Store in map for quick access
     deviceMap.Add(device.Id, device);
 }
    public void Tweet(int milliseconds)
    {
        var pin = _gpio.OpenPin(PIN);

        pin.SetDriveMode(GpioPinDriveMode.Output);     // open the gpio pin before writing or unauthorizedexception occurs
        pin.Write(GpioPinValue.High);
        Thread.Sleep(milliseconds);
        pin.Write(GpioPinValue.Low);
    }
Exemplo n.º 18
0
 public Button(IGpioController controller, GpioEnum gpio, ILoggerService loggerService = null)
 {
     ButtonPin = controller.OpenPin(gpio);
     ButtonPin.SetDriveMode(GpioPinDriveModeEnum.InputPullUp);
     if (loggerService != null)
     {
         TurnedOn  += (s, a) => loggerService.Log(LogLevelEnum.Information, $"{Name} is turned on");
         TurnedOff += (s, a) => loggerService.Log(LogLevelEnum.Information, $"{Name} is turned off");
     }
 }
        public GpioService(
            IOptions <GpioOptions> gpioOptions,
            ILogger <GpioService> logger)
        {
            _gpioOptions = gpioOptions;
            _logger      = logger;

            _gpioController = new GpioController();
            _gpioController.OpenPin(_gpioOptions.Value.ServoPin, PinMode.Output);
        }
Exemplo n.º 20
0
        public void AcquirePin(GpioEnum pin)
        {
            lock (m_Pins)
            {
                if (m_Pins.ContainsKey(pin))
                {
                    throw new UnauthorizedAccessException();
                }

                var gpioPin = m_GpioController.OpenPin(pin);
                gpioPin.SetDriveMode(GpioPinDriveModeEnum.Output);
                m_Pins[pin] = new SoftPwmPin(gpioPin);
            }
        }
Exemplo n.º 21
0
            public void runBlink()
            {
                var pin = gpioController.OpenPin(ledPin);

                pin.SetDriveMode(GpioPinDriveMode.Output);

                while (true)
                {
                    pin.Write(GpioPinValue.High); // LED ON
                    System.Threading.Thread.Sleep(2000);

                    pin.Write(GpioPinValue.Low); // LED OFF
                    System.Threading.Thread.Sleep(2000);
                }
            }
Exemplo n.º 22
0
            private void Initialize()
            {
                // Prep the pins
                _controller.OpenPin(_rsPin, PinMode.Output);

                if (_rwPin != -1)
                {
                    _controller.OpenPin(_rwPin, PinMode.Output);

                    // Set to write. Once we enable reading have reading pull high and reset
                    // after reading to give maximum performance to write (i.e. assume that
                    // the pin is low when writing).
                    _controller.Write(_rwPin, PinValue.Low);
                }
                if (_backlight != -1)
                {
                    _controller.OpenPin(_backlight, PinMode.Output);
                    if (_backlightBrightness > 0)
                    {
                        // Turn on the backlight
                        _controller.Write(_backlight, PinValue.High);
                    }
                }
                _controller.OpenPin(_enablePin, PinMode.Output);

                for (int i = 0; i < _dataPins.Length; ++i)
                {
                    _controller.OpenPin(_dataPins[i], PinMode.Output);
                }

                // The HD44780 self-initializes when power is turned on to the following settings:
                //
                //  - 8 bit, 1 line, 5x7 font
                //  - Display, cursor, and blink off
                //  - Increment with no shift
                //
                // It is possible that the initialization will fail if the power is not provided
                // within specific tolerances. As such, we'll always perform the software based
                // initialization as described on pages 45/46 of the HD44780 data sheet. We give
                // a little extra time to the required waits as described.

                if (_dataPins.Length == 8)
                {
                    // Init to 8 bit mode (this is the default, but other drivers
                    // may set the controller to 4 bit mode, so reset to be safe.)
                    DelayHelper.DelayMilliseconds(50, allowThreadYield: true);
                    WriteBits(0b0011_0000, 8);
                    DelayHelper.DelayMilliseconds(5, allowThreadYield: true);
                    WriteBits(0b0011_0000, 8);
                    DelayHelper.DelayMicroseconds(100, allowThreadYield: true);
                    WriteBits(0b0011_0000, 8);
                }
                else
                {
                    // Init to 4 bit mode, setting _rspin to low as we're writing 4 bits directly.
                    // (Send writes the whole byte in two 4bit/nybble chunks)
                    _controller.Write(_rsPin, PinValue.Low);
                    DelayHelper.DelayMilliseconds(50, allowThreadYield: true);
                    WriteBits(0b0011, 4);
                    DelayHelper.DelayMilliseconds(5, allowThreadYield: true);
                    WriteBits(0b0011, 4);
                    DelayHelper.DelayMicroseconds(100, allowThreadYield: true);
                    WriteBits(0b0011, 4);
                    WriteBits(0b0010, 4);
                }

                // The busy flag can NOT be checked until this point.
            }
Exemplo n.º 23
0
        /// <summary>
        /// Initializes the display by setting the specified columns and lines.
        /// </summary>
        private void Initialize(int rows)
        {
            // While the chip supports 5x10 pixel characters for one line displays they
            // don't seem to be generally available. Supporting 5x10 would require extra
            // support for CreateCustomCharacter

            if (SetTwoLineMode(rows))
            {
                _displayFunction |= DisplayFunction.TwoLine;
            }

            _displayControl |= DisplayControl.DisplayOn;
            _displayMode    |= DisplayEntryMode.Increment;

            // Prep the pins
            _controller.OpenPin(_rsPin, PinMode.Output);

            if (_rwPin != -1)
            {
                _controller.OpenPin(_rwPin, PinMode.Output);
            }
            if (_backlight != -1)
            {
                _controller.OpenPin(_backlight, PinMode.Output);
                if (_backlightBrightness > 0)
                {
                    // Turn on the backlight
                    _controller.Write(_backlight, PinValue.High);
                }
            }
            _controller.OpenPin(_enablePin, PinMode.Output);

            for (int i = 0; i < _dataPins.Length; ++i)
            {
                _controller.OpenPin(_dataPins[i], PinMode.Output);
            }

            // The HD44780 self-initializes when power is turned on to the following settings:
            //
            //  - 8 bit, 1 line, 5x7 font
            //  - Display, cursor, and blink off
            //  - Increment with no shift
            //
            // It is possible that the initialization will fail if the power is not provided
            // within specific tolerances. As such, we'll always perform the software based
            // initialization as described on pages 45/46 of the HD44780 data sheet. We give
            // a little extra time to the required waits.

            if (_dataPins.Length == 8)
            {
                // Init to 8 bit mode
                DelayMicroseconds(50_000, checkBusy: false);
                Send(0b0011_0000);
                DelayMicroseconds(5_000, checkBusy: false);
                Send(0b0011_0000);
                DelayMicroseconds(100, checkBusy: false);
                Send(0b0011_0000);
            }
            else
            {
                // Init to 4 bit mode, setting _rspin to low as we're writing 4 bits directly.
                // (Send writes the whole byte in two 4bit/nybble chunks)
                _controller.Write(_rsPin, PinValue.Low);
                DelayMicroseconds(50_000, checkBusy: false);
                WriteBits(0b0011, 4);
                DelayMicroseconds(5_000, checkBusy: false);
                WriteBits(0b0011, 4);
                DelayMicroseconds(100, checkBusy: false);
                WriteBits(0b0011, 4);
                WriteBits(0b0010, 4);
            }

            // The busy flag cannot be checked until this point.

            Send((byte)_displayFunction);
            Send((byte)_displayControl);
            Send((byte)_displayMode);
            Clear();
        }
Exemplo n.º 24
0
 public static IDisposable OpenPinAsDisposable(this IGpioController controller, int pinNumber)
 {
     controller.OpenPin(pinNumber);
     return(Disposable.Create(() => controller.ClosePin(pinNumber)));
 }
Exemplo n.º 25
0
Arquivo: Led.cs Projeto: shrekjxf/Rpi
 public Led(IGpioController controller, GpioEnum gpio, bool initialValue = false)
 {
     Pin = controller.OpenPin(gpio);
     Pin.Write(initialValue ? GpioPinValueEnum.Low : GpioPinValueEnum.High);
     Pin.SetDriveMode(GpioPinDriveModeEnum.Output);
 }
Exemplo n.º 26
0
        /// <summary>
        /// A general purpose driver for the Microchip MCP25 CAN controller device family.
        /// </summary>
        /// <param name="spiDevice">The SPI device used for communication.</param>
        /// <param name="reset">The output pin number that is connected to Reset.</param>
        /// <param name="tx0rts">The output pin number that is connected to Tx0RTS.</param>
        /// <param name="tx1rts">The output pin number that is connected to Tx1RTS.</param>
        /// <param name="tx2rts">The output pin number that is connected to Tx2RTS.</param>
        /// <param name="interrupt">The input pin number that is connected to INT.</param>
        /// <param name="rx0bf">The input pin number that is connected to Rx0BF.</param>
        /// <param name="rx1bf">The input pin number that is connected to Rx1BF.</param>
        /// <param name="clkout">The input pin number that is connected to CLKOUT.</param>
        /// <param name="gpioController">
        /// The GPIO controller for defined external pins. If not specified, the default controller will be used.
        /// </param>
        public Mcp25xxx(
            SpiDevice spiDevice,
            int reset     = -1,
            int tx0rts    = -1,
            int tx1rts    = -1,
            int tx2rts    = -1,
            int interrupt = -1,
            int rx0bf     = -1,
            int rx1bf     = -1,
            int clkout    = -1,
            IGpioController gpioController = null)
        {
            _spiDevice = spiDevice;

            _reset     = reset;
            _tx0rts    = tx0rts;
            _tx1rts    = tx1rts;
            _tx2rts    = tx2rts;
            _interrupt = interrupt;
            _rx0bf     = rx0bf;
            _rx1bf     = rx1bf;
            _clkout    = clkout;

            // Only need master controller if there are external pins provided.
            if (_reset != -1 ||
                _tx0rts != -1 ||
                _tx1rts != -1 ||
                _tx2rts != -1 ||
                _interrupt != -1 ||
                _rx0bf != -1 ||
                _rx1bf != -1 ||
                _clkout != -1)
            {
                _gpioController = gpioController ?? new GpioController();

                if (_reset != -1)
                {
                    _gpioController.OpenPin(_reset, PinMode.Output);
                    ResetPin = PinValue.Low;
                }

                if (_tx0rts != -1)
                {
                    _gpioController.OpenPin(_tx0rts, PinMode.Output);
                }

                if (_tx1rts != -1)
                {
                    _gpioController.OpenPin(_tx1rts, PinMode.Output);
                }

                if (_tx2rts != -1)
                {
                    _gpioController.OpenPin(_tx2rts, PinMode.Output);
                }

                if (_interrupt != -1)
                {
                    _gpioController.OpenPin(_interrupt, PinMode.Input);
                }

                if (_rx0bf != -1)
                {
                    _gpioController.OpenPin(_rx0bf, PinMode.Input);
                }

                if (_rx1bf != -1)
                {
                    _gpioController.OpenPin(_rx1bf, PinMode.Input);
                }

                if (_clkout != -1)
                {
                    _gpioController.OpenPin(_clkout, PinMode.Input);
                }
            }
        }
Exemplo n.º 27
0
 public IDisposable OpenPin(int pinNumber)
 {
     _controller.OpenPin(pinNumber);
     _openPins.Add(pinNumber);
     return(Disposable.Create(() => ClosePin(pinNumber)));
 }