Exemplo n.º 1
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.º 2
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);
 }
Exemplo n.º 3
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.º 4
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.º 5
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.º 6
0
        /// <summary>
        /// Disables the device by setting the reset pin low.
        /// </summary>
        public void Disable()
        {
            if (_reset == -1)
            {
                throw new InvalidOperationException("No reset pin configured.");
            }

            _masterGpioController.Write(_reset, PinValue.Low);

            // Registers will all be reset when re-enabled
            _bankStyle  = BankStyle.Sequential;
            _increments = true;
            _disabled   = true;
        }
Exemplo n.º 7
0
 /// <summary>
 /// Stop the motor.
 /// </summary>
 public void Stop()
 {
     _steps = 0;
     _stopwatch?.Stop();
     _controller.Write(_pin1, PinValue.Low);
     _controller.Write(_pin2, PinValue.Low);
     _controller.Write(_pin3, PinValue.Low);
     _controller.Write(_pin4, PinValue.Low);
 }
Exemplo n.º 8
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.º 9
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.º 10
0
 public void Command(params byte[] cmds)
 {
     _controller.Write(_dcPin, PinValue.Low); // Command Mode
     _device.Write(cmds);
 }
Exemplo n.º 11
0
 public void Write(int pinNumber, PinValue value)
 {
     _controller.Write(pinNumber, value);
 }
Exemplo n.º 12
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.º 13
0
 public void Forward()
 {
     _gpio.Write(_in1, PinValue.Low);
     _gpio.Write(_in2, PinValue.High);
 }
 private void WriteToServo(int servoPin, PinValue pinValue)
 {
     _logger.LogTrace($"Writing {pinValue} to pin {servoPin}");
     _gpioController.Write(servoPin, PinValue.Low);
     _logger.LogTrace($"Wrote {pinValue} to pin {servoPin}");
 }
Exemplo n.º 15
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.
            }