Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 internal static extern SafeLineHandle gpiod_chip_get_line(SafeChipHandle chip, int offset);
Ejemplo n.º 8
0
 internal static extern int gpiod_chip_num_lines(SafeChipHandle chip);
Ejemplo n.º 9
0
 internal static extern SafeLineHandle GetChipLineByOffset(SafeChipHandle chip, int offset);
Ejemplo n.º 10
0
 internal static extern int GetNumberOfLines(SafeChipHandle chip);