Example #1
0
        private static void Lcd(GpioDriver driver)
        {
            const int registerSelectPinNumber = 0;
            const int enablePinNumber         = 5;

            int[] dataPinNumbers = { 6, 16, 20, 21 };

            using (var controller = new GpioController(driver))
            {
                GpioPin   registerSelectPin = controller.OpenPin(registerSelectPinNumber);
                GpioPin   enablePin         = controller.OpenPin(enablePinNumber);
                GpioPin[] dataPins          = controller.OpenPins(dataPinNumbers);

                var lcd = new LcdController(registerSelectPin, enablePin, dataPins);
                lcd.Begin(16, 2);
                lcd.Print("hello, world!");

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    lcd.SetCursor(0, 1);
                    lcd.Print($"{watch.Elapsed.TotalSeconds:0.00} seconds");
                }
            }
        }
Example #2
0
        private static void ButtonWait(GpioDriver driver)
        {
            using (var controller = new GpioController(driver))
            {
                GpioPin button = controller.OpenPin(s_buttonPinNumber, PinMode.Input);

                if (button.IsModeSupported(PinMode.InputPullDown))
                {
                    button.Mode = PinMode.InputPullDown;
                }

                button.DebounceTimeout = TimeSpan.FromSeconds(1);
                button.NotifyEvents    = PinEvent.SyncRisingEdge;

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    bool eventDetected = button.WaitForEvent(TimeSpan.FromSeconds(1));

                    if (eventDetected)
                    {
                        Console.WriteLine("Button pressed!");
                    }
                }
            }
        }
Example #3
0
        private static void DetectButtonLed(GpioDriver driver)
        {
            using (var controller = new GpioController(driver))
            {
                GpioPin button = controller.OpenPin(s_buttonPinNumber, PinMode.Input);

                if (button.IsModeSupported(PinMode.InputPullDown))
                {
                    button.Mode = PinMode.InputPullDown;
                }

                GpioPin led = controller.OpenPin(s_ledPinNumber, PinMode.Output);

                button.DebounceTimeout     = TimeSpan.FromSeconds(1);
                button.NotifyEvents        = PinEvent.SyncFallingEdge;
                button.ValueChanged       += OnPinValueChanged2;
                button.EnableRaisingEvents = true;

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Example #4
0
        private static void Driver_DetectButton(GpioDriver driver)
        {
            using (driver)
            {
                PinMode buttonMode = PinMode.Input;

                if (driver.IsPinModeSupported(PinMode.InputPullDown))
                {
                    buttonMode = PinMode.InputPullDown;
                }

                driver.OpenPin(s_buttonPinNumber);
                driver.SetPinMode(s_buttonPinNumber, buttonMode);

                driver.SetDebounce(s_buttonPinNumber, TimeSpan.FromMilliseconds(100));
                driver.SetPinEventsToDetect(s_buttonPinNumber, PinEvent.SyncFallingRisingEdge);
                driver.ValueChanged += OnPinValueChanged1;
                driver.SetEnableRaisingPinEvents(s_buttonPinNumber, true);

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    Thread.Sleep(1 * 100);

                    if (s_buttonPressed)
                    {
                        Console.WriteLine($"Button press!");
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Creates an instance of the RaspberryPi3Driver.
        /// This driver works on Raspberry 3 or 4, both on Linux and on Windows
        /// </summary>
        public RaspberryPi3Driver()
        {
            if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                _linuxDriver = CreateInternalRaspberryPi3LinuxDriver(out RaspberryBoardInfo boardInfo);

                if (_linuxDriver == null)
                {
                    throw new PlatformNotSupportedException($"Not a supported Raspberry Pi type: " + boardInfo.BoardModel);
                }

                _setSetRegister   = (value) => _linuxDriver.SetRegister = value;
                _setClearRegister = (value) => _linuxDriver.ClearRegister = value;
                _getSetRegister   = () => _linuxDriver.SetRegister;
                _getClearRegister = () => _linuxDriver.ClearRegister;
                _internalDriver   = _linuxDriver;
            }
            else
            {
                _internalDriver   = CreateWindows10GpioDriver();
                _setSetRegister   = (value) => throw new PlatformNotSupportedException();
                _setClearRegister = (value) => throw new PlatformNotSupportedException();
                _getSetRegister   = () => throw new PlatformNotSupportedException();
                _getClearRegister = () => throw new PlatformNotSupportedException();
            }
        }
Example #6
0
        private static void Driver_DetectButtonLED(GpioDriver driver)
        {
            const int button = 18;
            const int led    = 26;

            using (driver)
            {
                driver.SetPinMode(button, PinMode.InputPullDown);
                driver.SetPinMode(led, PinMode.Output);

                driver.Debounce         = TimeSpan.FromSeconds(1);
                driver.PinValueChanged += OnPinValueChanged;
                driver.SetEventsToDetect(button, EventKind.SyncFallingEdge);

                EventKind events = driver.GetEventsToDetect(button);
                Console.WriteLine($"Events to detect: {events}");

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    Thread.Sleep(1 * 1000);
                }
            }
        }
Example #7
0
        private static void DetectButton(GpioDriver driver)
        {
            using (var controller = new GpioController(driver))
            {
                GpioPin button = controller.OpenPin(s_buttonPinNumber, PinMode.Input);

                if (button.IsModeSupported(PinMode.InputPullDown))
                {
                    button.Mode = PinMode.InputPullDown;
                }

                button.DebounceTimeout     = TimeSpan.FromMilliseconds(100);
                button.NotifyEvents        = PinEvent.SyncFallingRisingEdge;
                button.ValueChanged       += OnPinValueChanged1;
                button.EnableRaisingEvents = true;

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    Thread.Sleep(1 * 100);

                    if (s_buttonPressed)
                    {
                        Console.WriteLine($"Button press!");
                    }
                }
            }
        }
Example #8
0
        private static void Driver_ButtonWait(GpioDriver driver)
        {
            using (driver)
            {
                PinMode buttonMode = PinMode.Input;

                if (driver.IsPinModeSupported(PinMode.InputPullDown))
                {
                    buttonMode = PinMode.InputPullDown;
                }

                driver.OpenPin(s_buttonPinNumber);
                driver.SetPinMode(s_buttonPinNumber, buttonMode);

                driver.SetDebounce(s_buttonPinNumber, TimeSpan.FromSeconds(1));
                driver.SetPinEventsToDetect(s_buttonPinNumber, PinEvent.SyncRisingEdge);

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    bool eventDetected = driver.WaitForPinEvent(s_buttonPinNumber, TimeSpan.FromSeconds(1));

                    if (eventDetected)
                    {
                        Console.WriteLine("Button pressed!");
                    }
                }
            }
        }
Example #9
0
        private static void Driver_DetectButtonLed(GpioDriver driver)
        {
            using (driver)
            {
                PinMode buttonMode = PinMode.Input;

                if (driver.IsPinModeSupported(PinMode.InputPullDown))
                {
                    buttonMode = PinMode.InputPullDown;
                }

                driver.OpenPin(s_buttonPinNumber);
                driver.SetPinMode(s_buttonPinNumber, buttonMode);

                driver.OpenPin(s_ledPinNumber);
                driver.SetPinMode(s_ledPinNumber, PinMode.Output);

                driver.SetDebounce(s_buttonPinNumber, TimeSpan.FromSeconds(1));
                driver.SetPinEventsToDetect(s_buttonPinNumber, PinEvent.SyncFallingEdge);
                driver.ValueChanged += OnPinValueChanged2;
                driver.SetEnableRaisingPinEvents(s_buttonPinNumber, true);

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Example #10
0
 /// <summary>
 /// Creates a new custom board.
 /// </summary>
 /// <param name="gpioDriver">GPIO driver to use</param>
 /// <param name="i2cBusCreator">Function to create an I2C bus instance</param>
 /// <param name="spiDeviceCreator">Function to create an SPI device</param>
 /// <param name="pwmChannelCreator">Function to create a PWM channel</param>
 public CustomBoard(GpioDriver gpioDriver, Func <int, I2cBus> i2cBusCreator,
                    Func <SpiConnectionSettings, SpiDevice> spiDeviceCreator, Func <int, PwmChannel> pwmChannelCreator)
 {
     _gpioDriver        = gpioDriver;
     _i2CBusCreator     = i2cBusCreator;
     _spiDeviceCreator  = spiDeviceCreator;
     _pwmChannelCreator = pwmChannelCreator;
 }
Example #11
0
        public GpioEdm(ILoggerFactory loggerFactory)
        {
            Identifier = new GpioEdmIdentifier();
            _logger    = loggerFactory.GetLoggerFor(GetType());

            _gpioDriver      = new GpioDriver(loggerFactory.GetLoggerFor(typeof(GpioDriver)), new EApiStatusCodes());
            _eapiInitializer = new EapiInitializer();
        }
Example #12
0
        protected GpioController CreateGpioController()
        {
            GpioDriver gpioDriver = DriverFactory.CreateFromEnum <GpioDriver, GpioDriverType>(Driver);

            return(gpioDriver != null
                ? new GpioController(Scheme, gpioDriver)
                : new GpioController(Scheme));
        }
Example #13
0
 /// <summary>
 /// Initializes the gpio driver.
 /// </summary>
 /// <param name="pins">The available pins on the device.</param>
 /// <param name="scheme">The numbering scheme to use.</param>
 /// <returns>The driver.</returns>
 internal GpioControllerDriver(InternalLogger logger, PinsWrapper pins, GpioDriver driverName, PinConfig pinConfig, NumberingScheme scheme)
 {
     Logger              = logger ?? throw new ArgumentNullException(nameof(logger));
     Pins                = pins;
     NumberingScheme     = scheme;
     DriverName          = driverName;
     PinConfig           = pinConfig;
     IsDriverInitialized = true;
 }
Example #14
0
        private static void OnPinValueChanged(GpioDriver driver, int button)
        {
            const int led = 26;

            currentLedValue = currentLedValue == PinValue.High ? PinValue.Low : PinValue.High;
            Console.WriteLine($"Button pressed! LED value {currentLedValue}");

            driver.Output(led, currentLedValue);
        }
        /// <summary>
        /// Creates an instance of the RaspberryPi3Driver.
        /// This driver works on Raspberry 3 or 4, both on Linux and on Windows
        /// </summary>
        public RaspberryPi3Driver()
        {
            _internalDriver = new RaspberryPi3LinuxDriver();
            RaspberryPi3LinuxDriver linuxDriver = _internalDriver as RaspberryPi3LinuxDriver;

            _setSetRegister   = (value) => linuxDriver.SetRegister = value;
            _setClearRegister = (value) => linuxDriver.ClearRegister = value;
            _getSetRegister   = () => linuxDriver.SetRegister;
            _getClearRegister = () => linuxDriver.ClearRegister;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // not our instance
                _gpioDriver = null;
            }

            base.Dispose(disposing);
        }
        public InterruptSysFsDriver(GpioDriver gpioDriver)
            : base()
        {
            if (Environment.OSVersion.Platform != PlatformID.Unix)
            {
                throw new PlatformNotSupportedException($"{GetType().Name} is only supported on Linux/Unix.");
            }

            _gpioDriver           = gpioDriver;
            StatusUpdateSleepTime = TimeSpan.Zero; // This driver does not need this "magic sleep" as we're directly accessing the hardware registers
        }
Example #18
0
        private static void Spi_Pressure(GpioDriver driver)
        {
            using (var controller = new GpioController(driver, PinNumberingScheme.Bcm))
            {
                Pin csPin = controller.OpenPin(8);

                var settings = new SpiConnectionSettings(0, 0);
                var sensor   = new PressureTemperatureHumiditySensor(csPin, settings);
                Pressure(sensor);
            }
        }
Example #19
0
        private static void Spi_Pressure(GpioDriver driver)
        {
            using (var controller = new GpioController(driver))
            {
                GpioPin chipSelectPin = controller.OpenPin(s_chipSelectLinePinNumber);

                var settings = new SpiConnectionSettings(s_spiBusId, 0);
                var sensor   = new PressureTemperatureHumiditySensor(chipSelectPin, settings);
                Pressure(sensor);
            }
        }
Example #20
0
    /// <summary>
    /// Disposes this instance and closes all open pins associated with this controller.
    /// </summary>
    /// <param name="disposing">True to dispose all instances, false to dispose only unmanaged resources</param>
    protected virtual void Dispose(bool disposing)
    {
        foreach (int pin in _openPins.Keys)
        {
            // The list contains the pin in the current NumberingScheme
            ClosePinCore(pin);
        }

        _openPins.Clear();
        _driver?.Dispose();
        _driver = null !;
    }
Example #21
0
 internal RaspberryPi3Driver(RaspberryPi3LinuxDriver linuxDriver)
 {
     if (Environment.OSVersion.Platform == PlatformID.Unix)
     {
         _setSetRegister   = (value) => linuxDriver.SetRegister = value;
         _setClearRegister = (value) => linuxDriver.ClearRegister = value;
         _getSetRegister   = () => linuxDriver.SetRegister;
         _getClearRegister = () => linuxDriver.ClearRegister;
         _internalDriver   = linuxDriver;
     }
     else
     {
         throw new NotSupportedException("This ctor is for internal use only");
     }
 }
Example #22
0
        private static void ButtonPullDown(GpioDriver driver)
        {
            using (var controller = new GpioController(driver))
            {
                GpioPin button = controller.OpenPin(s_buttonPinNumber, PinMode.InputPullDown);
                GpioPin led    = controller.OpenPin(s_ledPinNumber, PinMode.Output);

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    PinValue value = button.Read();
                    led.Write(value);
                }
            }
        }
Example #23
0
        private static void ButtonLED(GpioDriver driver)
        {
            using (driver)
            {
                var button = new Pin(driver, PinNumberingScheme.BCM, 18, PinMode.Input);
                var led    = new Pin(driver, PinNumberingScheme.BCM, 26, PinMode.Output);

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    PinValue value = button.Read();
                    led.Write(value);
                }
            }
        }
Example #24
0
        private static void ButtonLed(GpioDriver driver)
        {
            using (var controller = new GpioController(driver, PinNumberingScheme.Bcm))
            {
                Pin button = controller.OpenPin(18, PinMode.Input);
                Pin led    = controller.OpenPin(26, PinMode.Output);

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    PinValue value = button.Read();
                    led.Write(value);
                }
            }
        }
Example #25
0
        private static void BlinkingLed(GpioDriver driver)
        {
            using (var controller = new GpioController(driver))
            {
                GpioPin led = controller.OpenPin(s_ledPinNumber, PinMode.Output);

                for (var i = 0; i < 5; ++i)
                {
                    led.Write(PinValue.High);
                    Thread.Sleep(TimeSpan.FromSeconds(1));

                    led.Write(PinValue.Low);
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Example #26
0
        private static void BlinkingLED(GpioDriver driver)
        {
            using (driver)
            {
                var led = new Pin(driver, PinNumberingScheme.BCM, 26, PinMode.Output);

                for (var i = 0; i < 5; ++i)
                {
                    led.Write(PinValue.High);
                    Thread.Sleep(1 * 1000);

                    led.Write(PinValue.Low);
                    Thread.Sleep(1 * 1000);
                }
            }
        }
Example #27
0
        private static void Driver_BlinkingLed(GpioDriver driver)
        {
            using (driver)
            {
                driver.OpenPin(s_ledPinNumber);
                driver.SetPinMode(s_ledPinNumber, PinMode.Output);

                for (var i = 0; i < 5; ++i)
                {
                    driver.Output(s_ledPinNumber, PinValue.High);
                    Thread.Sleep(TimeSpan.FromSeconds(1));

                    driver.Output(s_ledPinNumber, PinValue.Low);
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Example #28
0
        private static void Driver_BlinkingLED(GpioDriver driver)
        {
            const int led = 26;

            using (driver)
            {
                driver.SetPinMode(led, PinMode.Output);

                for (var i = 0; i < 5; ++i)
                {
                    driver.Output(led, PinValue.High);
                    Thread.Sleep(1 * 1000);

                    driver.Output(led, PinValue.Low);
                    Thread.Sleep(1 * 1000);
                }
            }
        }
Example #29
0
        private static void Driver_BlinkingLed(GpioDriver driver)
        {
            const int led = 26;

            using (driver)
            {
                driver.OpenPin(led);
                driver.SetPinMode(led, PinMode.Output);

                for (var i = 0; i < 5; ++i)
                {
                    driver.Output(led, PinValue.High);
                    Thread.Sleep(TimeSpan.FromSeconds(1));

                    driver.Output(led, PinValue.Low);
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Example #30
0
        private static void Driver_ButtonPullDown(GpioDriver driver)
        {
            using (driver)
            {
                driver.OpenPin(s_buttonPinNumber);
                driver.SetPinMode(s_buttonPinNumber, PinMode.InputPullDown);

                driver.OpenPin(s_ledPinNumber);
                driver.SetPinMode(s_ledPinNumber, PinMode.Output);

                Stopwatch watch = Stopwatch.StartNew();

                while (watch.Elapsed.TotalSeconds < 15)
                {
                    PinValue value = driver.Input(s_buttonPinNumber);
                    driver.Output(s_ledPinNumber, value);
                }
            }
        }