private void openEdgeDetectionFileHandle()
        {
            // close Handle if open
            if (this.deviceHandle != 0)
            {
                try
                {
                    FCntl.close(this.deviceHandle);
                }
                finally
                {
                    this.deviceHandle = 0;
                }
            }

            // open Handle readonly
            var result = FCntl.open(this.deviceName, FCntl.O_RDONLY);

            if (result < 0)
            {
                throw new IOException(string.Format("Failed to open device - error {0}.", result));
            }
            this.deviceHandle = result;

            // clear pending interrupts
            readBufs(this.deviceHandle);
        }
        private void setEdgeDetection(byte pin, EdgeDetectionMode edge)
        {
            if (edge == EdgeDetectionMode.none)
            {
                // open file handle to gpio export
                var fd = FCntl.open("/sys/class/gpio/unexport", FCntl.O_WRONLY);
                if (fd < 0)
                {
                    throw new IOException(string.Format("Failed to open gpio class - error {0}.", fd));
                }
                // write pin number to export gpio pin
                // (don't check for errors because it raises if already unexported..)
                var buf = System.Text.UTF8Encoding.UTF8.GetBytes(pin.ToString() + Environment.NewLine);
                UniStd.write(fd, buf, Convert.ToUInt32(buf.Length));
                FCntl.close(fd);
            }
            else
            {
                // open file handle to gpio export
                var fd = FCntl.open("/sys/class/gpio/export", FCntl.O_WRONLY);
                if (fd < 0)
                {
                    throw new IOException(string.Format("Failed to open gpio class - error {0}.", fd));
                }
                // write pin number to export gpio pin
                // (don't check for errors because it raises if already exported..)
                var buf = System.Text.UTF8Encoding.UTF8.GetBytes(pin.ToString() + Environment.NewLine);
                UniStd.write(fd, buf, Convert.ToUInt32(buf.Length));
                FCntl.close(fd);

                // wait short delay for export to complete
                System.Threading.Thread.Sleep(50);

                // open file handle to gpio direction
                fd = FCntl.open(String.Format("/sys/class/gpio/gpio{0}/direction", pin), FCntl.O_WRONLY);
                if (fd < 0)
                {
                    throw new IOException(string.Format("Failed to open gpio direction - error {0}.", fd));
                }
                // write pin number to export gpio direction
                buf = System.Text.UTF8Encoding.UTF8.GetBytes("in" + Environment.NewLine);
                UniStd.write(fd, buf, Convert.ToUInt32(buf.Length));
                FCntl.close(fd);

                // open file handle to gpio edge
                fd = FCntl.open(String.Format("/sys/class/gpio/gpio{0}/edge", pin), FCntl.O_WRONLY);
                if (fd < 0)
                {
                    throw new IOException(string.Format("Failed to open gpio edge - error {0}.", fd));
                }
                // write pin number to export gpio direction
                buf = System.Text.UTF8Encoding.UTF8.GetBytes(edge.ToString() + Environment.NewLine);
                UniStd.write(fd, buf, Convert.ToUInt32(buf.Length));
                FCntl.close(fd);
            }
        }
Exemple #3
0
        public void Close()
        {
            if (this.DeviceHandle == 0)
            {
                throw new IOException("Device is already closed.");
            }
            var result = FCntl.close(this.DeviceHandle);

            if (result < 0)
            {
                throw new IOException(string.Format("Failed to close device - error {0}.", result));
            }
            this.DeviceHandle = 0;
            this.FreeTxRxBuffers();
        }
Exemple #4
0
        public void Close()
        {
            if (this.DeviceHandle == 0)
            {
                throw new System.IO.IOException("Device is already closed.");
            }
            var result = FCntl.close(this.DeviceHandle);

            if (result < 0)
            {
                throw new System.IO.IOException("Failed to close device.");
            }
            this.DeviceHandle = 0;
            this.FreeTxRxBuffers();
        }
        private void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.
                    if (cancelTokenSource != null)
                    {
                        cancelTokenSource.Cancel();
                        if (this.pollTimeout >= 0)
                        {
                            // only wait for task to finish if poll timeout is not infinite
                            cancelTokenSource.Token.WaitHandle.WaitOne();
                        }
                        cancelTokenSource = null;
                    }
                    if (this.deviceHandle != 0)
                    {
                        try
                        {
                            FCntl.close(this.deviceHandle);
                        }
                        finally
                        {
                            this.deviceHandle = 0;
                        }
                    }
                    if (this.pin > 0)
                    {
                        setEdgeDetection(this.pin, EdgeDetectionMode.none);
                    }
                }

                // Note disposing has been done.
                disposed = true;
            }
        }