/// <summary>
        /// Construct an instance
        /// </summary>
        /// <param name="gpioChip">Number of the gpio Chip. Default 0</param>
        public LibGpiodDriver(int gpioChip = 0)
        {
            if (Environment.OSVersion.Platform != PlatformID.Unix)
            {
                throw new PlatformNotSupportedException($"{GetType().Name} is only supported on Linux/Unix.");
            }

            try
            {
                _pinNumberLock = new object();
                _chip          = Interop.libgpiod.gpiod_chip_open_by_number(gpioChip);
                if (_chip == null)
                {
                    throw ExceptionHelper.GetIOException(ExceptionResource.NoChipFound, Marshal.GetLastWin32Error());
                }

                _pinCount = Interop.libgpiod.gpiod_chip_num_lines(_chip);
                _pinNumberToEventHandler   = new ConcurrentDictionary <int, LibGpiodDriverEventHandler>();
                _pinNumberToSafeLineHandle = new ConcurrentDictionary <int, SafeLineHandle>();
            }
            catch (DllNotFoundException)
            {
                throw ExceptionHelper.GetPlatformNotSupportedException(ExceptionResource.LibGpiodNotInstalled);
            }
        }
Exemple #2
0
        public LibGpiodDriver()
        {
            SafeChipIteratorHandle iterator = null;

            try
            {
                iterator = Interop.GetChipIterator();
                if (iterator == null)
                {
                    throw ExceptionHelper.GetIOException(ExceptionResource.NoChipIteratorFound, Marshal.GetLastWin32Error());
                }
            }
            catch (DllNotFoundException)
            {
                throw ExceptionHelper.GetPlatformNotSupportedException(ExceptionResource.LibGpiodNotInstalled);
            }

            _chip = Interop.GetNextChipFromChipIterator(iterator);
            if (_chip == null)
            {
                throw ExceptionHelper.GetIOException(ExceptionResource.NoChipFound, Marshal.GetLastWin32Error());
            }

            _pinNumberToSafeLineHandle = new Dictionary <int, SafeLineHandle>(PinCount);
        }
        /// <inheritdoc/>
        protected override void Dispose(bool disposing)
        {
            if (_pinNumberToEventHandler != null)
            {
                foreach (KeyValuePair <int, LibGpiodDriverEventHandler> kv in _pinNumberToEventHandler)
                {
                    LibGpiodDriverEventHandler eventHandler = kv.Value;
                    eventHandler.Dispose();
                }

                _pinNumberToEventHandler.Clear();
            }

            if (_pinNumberToSafeLineHandle != null)
            {
                foreach (int pin in _pinNumberToSafeLineHandle.Keys)
                {
                    if (_pinNumberToSafeLineHandle.TryGetValue(pin, out SafeLineHandle pinHandle))
                    {
                        pinHandle?.Dispose();
                    }
                }

                _pinNumberToSafeLineHandle.Clear();
            }

            _chip?.Dispose();
            _chip = null;

            base.Dispose(disposing);
        }
Exemple #4
0
 public LibGpiodDriver(int gpioChip = 0)
 {
     try
     {
         _chip = Interop.libgpiod.gpiod_chip_open_by_number(gpioChip);
         if (_chip == null)
         {
             throw ExceptionHelper.GetIOException(ExceptionResource.NoChipFound, Marshal.GetLastWin32Error());
         }
         _pinNumberToSafeLineHandle = new Dictionary <int, SafeLineHandle>(PinCount);
     }
     catch (DllNotFoundException)
     {
         throw ExceptionHelper.GetPlatformNotSupportedException(ExceptionResource.LibGpiodNotInstalled);
     }
 }
        public LibGpiodDriver()
        {
            SafeChipIteratorHandle iterator = Interop.GetChipIterator();

            if (iterator == null)
            {
                throw ExceptionHelper.GetIOException(ExceptionResource.NoChipIteratorFound, Marshal.GetLastWin32Error());
            }

            _chip = Interop.GetNextChipFromChipIterator(iterator);
            if (_chip == null)
            {
                throw ExceptionHelper.GetIOException(ExceptionResource.NoChipFound, Marshal.GetLastWin32Error());
            }

            // Freeing other chips opened
            Interop.FreeChipIteratorNoCloseCurrentChip(iterator);
            _pinNumberToSafeLineHandle = new Dictionary <int, SafeLineHandle>(PinCount);
        }
        protected override void Dispose(bool disposing)
        {
            foreach (int pin in _pinNumberToSafeLineHandle.Keys)
            {
                if (_pinNumberToSafeLineHandle.TryGetValue(pin, out SafeLineHandle pinHandle))
                {
                    Interop.ReleaseGpiodLine(pinHandle);
                }

                _pinNumberToSafeLineHandle.Remove(pin);
            }

            if (_chip != null)
            {
                _chip.Dispose();
                _chip = null;
            }

            base.Dispose(disposing);
        }
 internal static extern SafeLineHandle gpiod_chip_get_line(SafeChipHandle chip, int offset);
 internal static extern int gpiod_chip_num_lines(SafeChipHandle chip);
Exemple #9
0
 internal static extern SafeLineHandle GetChipLineByOffset(SafeChipHandle chip, int offset);
Exemple #10
0
 internal static extern int GetNumberOfLines(SafeChipHandle chip);