コード例 #1
0
        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);
            }
        }
コード例 #2
0
        private void readBufs(int handle)
        {
            byte[] buf       = new byte[4096];
            int    bytesread = 0;

            do
            {
                bytesread = UniStd.read(handle, ref buf, Convert.ToUInt32(buf.Length));
                if (bytesread < 0)
                {
                    throw new IOException(string.Format("Failed to read - error {0}.", bytesread));
                }
            } while (bytesread > 0);
            if (UniStd.lseek(handle, 0, 0) < 0)
            {
                throw new IOException("Failed to seek.");
            }
        }