コード例 #1
0
ファイル: LibGpio.cs プロジェクト: wilx2000/PiSharp
        /// <summary>
        /// Exports a given GPIO pin
        /// </summary>
        /// <param name="pinNumber">The pin number to export</param>
        private void Export(BroadcomPinNumber pinNumber)
        {
            // The simulator requires directories to be created first, but the RasPi does not and throws an exception.
            if (this.TestMode || !System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
            {
                var exportDir  = string.Format("gpio{0}", (int)pinNumber);
                var exportPath = Path.Combine(this.GetGpioPath(), exportDir);
                if (!Directory.Exists(exportPath))
                {
                    Directory.CreateDirectory(exportPath);
                    File.Create(Path.Combine(exportPath, "value"));
                }
            }

            using (var fileStream = new FileStream(Path.Combine(this.GetGpioPath(), "export"), FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite))
            {
                using (var streamWriter = new StreamWriter(fileStream))
                {
                    streamWriter.Write((int)pinNumber);
                    streamWriter.Flush();
                }
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', been exported", pinNumber));
        }
コード例 #2
0
ファイル: LibGpio.cs プロジェクト: wilx2000/PiSharp
        /// <summary>
        /// Sets the direction of a GPIO channel
        /// </summary>
        /// <param name="pinNumber">The pin number to set the direction of</param>
        /// <param name="direction">The direction to set it to</param>
        private void SetDirection(BroadcomPinNumber pinNumber, Direction direction)
        {
            var gpioId   = string.Format("gpio{0}", (int)pinNumber);
            var filePath = Path.Combine(gpioId, "direction");

            using (var streamWriter = new StreamWriter(File.Open(Path.Combine(GetGpioPath().ToString(), filePath), FileMode.OpenOrCreate)))
            {
                if (direction == Direction.Input)
                {
                    streamWriter.Write("in");
                }
                else
                {
                    streamWriter.Write("out");
                }
            }

            // Internally log the direction of this pin, so we can perform safety checks later.
            if (this.directions.ContainsKey(pinNumber))
            {
                this.directions[pinNumber] = direction;
            }
            else
            {
                this.directions.Add(pinNumber, direction);
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', now set to direction '{1}'", pinNumber, direction));
        }
コード例 #3
0
ファイル: LibGpio.cs プロジェクト: wilx2000/PiSharp
        /// <summary>
        /// Reads a value form a GPIO pin
        /// </summary>
        /// <param name="pinNumber">The pin number to read</param>
        /// <returns>The value at that pin</returns>
        /// <exception cref="InvalidOperationException">
        /// Thrown when an attempt to use an incorrectly configured channel is made
        /// </exception>
        public bool ReadValue(BroadcomPinNumber pinNumber)
        {
            // Check that the output is configured
            if (!this.directions.ContainsKey(pinNumber))
            {
                throw new InvalidOperationException("Attempt to read value from un-configured pin");
            }

            // Check that the channel is not being used incorrectly
            if (this.directions[pinNumber] == Direction.Output)
            {
                throw new InvalidOperationException("Attempt to read value form pin configured for output");
            }

            var gpioId   = string.Format("gpio{0}", (int)pinNumber);
            var filePath = Path.Combine(gpioId, "value");

            using (var fileStream = new FileStream(Path.Combine(this.GetGpioPath(), filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var streamReader = new StreamReader(fileStream))
                {
                    var rawValue = streamReader.ReadToEnd().Trim();
                    Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', has input value '{1}'", pinNumber, rawValue));
                    if (rawValue == "1")
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
        }
コード例 #4
0
ファイル: LibGpio.cs プロジェクト: wilx2000/PiSharp
        /// <summary>
        /// Outputs a value to a GPIO pin
        /// </summary>
        /// <param name="pinNumber">The pin number to use</param>
        /// <param name="value">The value to output</param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when an attempt to use an incorrectly configured channel is made
        /// </exception>
        public void OutputValue(BroadcomPinNumber pinNumber, bool value)
        {
            // Check that the output is configured
            if (!this.directions.ContainsKey(pinNumber))
            {
                throw new InvalidOperationException("Attempt to output value on un-configured pin");
            }

            // Check that the channel is not being used incorrectly
            if (this.directions[pinNumber] == Direction.Input)
            {
                throw new InvalidOperationException("Attempt to output value on pin configured for input");
            }

            var gpioId   = string.Format("gpio{0}", (int)pinNumber);
            var filePath = Path.Combine(gpioId, "value");

            using (var streamWriter = new StreamWriter(File.Open(Path.Combine(GetGpioPath().ToString(), filePath), FileMode.OpenOrCreate)))
            {
                if (value)
                {
                    streamWriter.Write("1");
                }
                else
                {
                    streamWriter.Write("0");
                }
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', outputting value '{1}'", pinNumber, value));
        }
コード例 #5
0
ファイル: LibGpio.cs プロジェクト: wilx2000/PiSharp
        /// <summary>
        /// Unexports a given GPIO pin number
        /// </summary>
        /// <param name="pinNumber">The pin number to unexport</param>
        private void UnExport(BroadcomPinNumber pinNumber)
        {
            using (var fileStream = new FileStream(Path.Combine(this.GetGpioPath(), "unexport"), FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
            {
                using (var streamWriter = new StreamWriter(fileStream))
                {
                    streamWriter.Write((int)pinNumber);
                }
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', un-exported", pinNumber));
        }
コード例 #6
0
ファイル: LibGpio.cs プロジェクト: wilx2000/PiSharp
        /// <summary>
        /// Closes a GPIO channel
        /// </summary>
        /// <param name="pinNumber">The Broadcom pin number to configure</param>
        public void CloseChannel(BroadcomPinNumber pinNumber)
        {
            var outputName = string.Format("gpio{0}", (int)pinNumber);
            var gpioPath   = Path.Combine(this.GetGpioPath(), outputName);

            if (Directory.Exists(gpioPath))
            {
                this.UnExport(pinNumber);

                // Sets direction back to input.
                this.SetDirection(pinNumber, Direction.Input);
            }



            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', configured for use", pinNumber));
        }
コード例 #7
0
ファイル: LibGpio.cs プロジェクト: wilx2000/PiSharp
        /// <summary>
        /// Configures a GPIO channel for use
        /// </summary>
        /// <param name="pinNumber">The Broadcom pin number to configure</param>
        /// <param name="direction">The direction to configure the pin for</param>
        public void SetupChannel(BroadcomPinNumber pinNumber, Direction direction)
        {
            var outputName = string.Format("gpio{0}", (int)pinNumber);
            var gpioPath   = Path.Combine(this.GetGpioPath(), outputName);

            // If already exported, unexport it before continuing
            if (Directory.Exists(gpioPath))
            {
                this.UnExport(pinNumber);
            }

            // Now export the channel
            this.Export(pinNumber);

            // Set the IO direction
            this.SetDirection(pinNumber, direction);

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', configured for use", pinNumber));
        }
コード例 #8
0
ファイル: LibGpio.cs プロジェクト: Zelxin/RPiKeg
        /// <summary>
        /// Sets the direction of a GPIO channel
        /// </summary>
        /// <param name="pinNumber">The pin number to set the direction of</param>
        /// <param name="direction">The direction to set it to</param>
        private void SetDirection(BroadcomPinNumber pinNumber, Direction direction)
        {
            var gpioId = string.Format("gpio{0}", (int)pinNumber);
            var filePath = Path.Combine(gpioId, "direction");
            using (var streamWriter = new StreamWriter(Path.Combine(this.GetGpioPath(), filePath), false))
            {
                if (direction == Direction.Input)
                {
                    streamWriter.Write("in");
                }
                else
                {
                    streamWriter.Write("out");
                }
            }

            // Internally log the direction of this pin, so we can perform safety checks later.
            if (this.directions.ContainsKey(pinNumber))
            {
                this.directions[pinNumber] = direction;
            }
            else
            {
                this.directions.Add(pinNumber, direction);
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', now set to direction '{1}'", pinNumber, direction));
        }
コード例 #9
0
ファイル: LibGpio.cs プロジェクト: Zelxin/RPiKeg
        /// <summary>
        /// Unexports a given GPIO pin number
        /// </summary>
        /// <param name="pinNumber">The pin number to unexport</param>
        private void UnExport(BroadcomPinNumber pinNumber)
        {
            using (var fileStream = new FileStream(Path.Combine(this.GetGpioPath(), "unexport"), FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
            {
                using (var streamWriter = new StreamWriter(fileStream))
                {
                    streamWriter.Write((int)pinNumber);
                }
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', un-exported", pinNumber));
        }
コード例 #10
0
ファイル: LibGpio.cs プロジェクト: Zelxin/RPiKeg
        /// <summary>
        /// Exports a given GPIO pin
        /// </summary>
        /// <param name="pinNumber">The pin number to export</param>
        private void Export(BroadcomPinNumber pinNumber)
        {
            try
            {
                var exportDir = string.Format("gpio{0}", (int)pinNumber);
                var exportPath = Path.Combine(this.GetGpioPath(), exportDir);
                if (!Directory.Exists(exportPath))
                {
                    Directory.CreateDirectory(exportPath);
                    File.Create(Path.Combine(exportPath, "value")).Close();
                }
            }
            catch (FileNotFoundException e)
            { 
                // Nasty hack - the simulator requires directories to be created first, but the RasPi does not and throws an exception.
               Debug.WriteLine("Error while creating directory: \n" + e.ToString());
            }

            using (var fileStream = new FileStream(Path.Combine(this.GetGpioPath(), "export"), FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite))
            {
                
                using (var streamWriter = new StreamWriter(fileStream))
                {
                    streamWriter.Write((int)pinNumber);
                    streamWriter.Flush();
                }
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', been exported", pinNumber));
        }
コード例 #11
0
ファイル: LibGpio.cs プロジェクト: Zelxin/RPiKeg
        /// <summary>
        /// Reads a value form a GPIO pin
        /// </summary>
        /// <param name="pinNumber">The pin number to read</param>
        /// <returns>The value at that pin</returns>
        /// <exception cref="InvalidOperationException">
        /// Thrown when an attempt to use an incorrectly configured channel is made
        /// </exception>
        public bool ReadValue(BroadcomPinNumber pinNumber)
        {
            // Check that the output is configured
            if (!this.directions.ContainsKey(pinNumber))
            {
                throw new InvalidOperationException("Attempt to read value from un-configured pin");
            }

            // Check that the channel is not being used incorrectly
            if (this.directions[pinNumber] == Direction.Output)
            {
                throw new InvalidOperationException("Attempt to read value form pin configured for output");
            }

            var gpioId = string.Format("gpio{0}", (int)pinNumber);
            var filePath = Path.Combine(gpioId, "value");
            using (var fileStream = new FileStream(Path.Combine(this.GetGpioPath(), filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var streamReader = new StreamReader(fileStream))
                {
                    var rawValue = streamReader.ReadToEnd().Trim();
                    Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', has input value '{1}'", pinNumber, rawValue));
                    if (rawValue == "1")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
コード例 #12
0
ファイル: LibGpio.cs プロジェクト: Zelxin/RPiKeg
        /// <summary>
        /// Outputs a value to a GPIO pin
        /// </summary>
        /// <param name="pinNumber">The pin number to use</param>
        /// <param name="value">The value to output</param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when an attempt to use an incorrectly configured channel is made
        /// </exception>
        public void OutputValue(BroadcomPinNumber pinNumber, bool value)
        {
            // Check that the output is configured
            if (!this.directions.ContainsKey(pinNumber))
            {
                throw new InvalidOperationException("Attempt to output value on un-configured pin");
            }
            
            // Check that the channel is not being used incorrectly
            if (this.directions[pinNumber] == Direction.Input)
            {
                throw new InvalidOperationException("Attempt to output value on pin configured for input");
            }

            var gpioId = string.Format("gpio{0}", (int)pinNumber);
            var filePath = Path.Combine(gpioId, "value");
            using (var streamWriter = new StreamWriter(Path.Combine(this.GetGpioPath(), filePath), false))
            {
                if (value)
                {
                    streamWriter.Write("1");
                }
                else
                {
                    streamWriter.Write("0");
                }
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', outputting value '{1}'", pinNumber, value));
        }
コード例 #13
0
ファイル: LibGpio.cs プロジェクト: Zelxin/RPiKeg
        /// <summary>
        /// Configures a GPIO channel for use
        /// </summary>
        /// <param name="pinNumber">The Broadcom pin number to configure</param>
        /// <param name="direction">The direction to configure the pin for</param>
        public void SetupChannel(BroadcomPinNumber pinNumber, Direction direction)
        {
            var outputName = string.Format("gpio{0}", CultureInfo.InvariantCulture, (int)pinNumber);
            var gpioPath = Path.Combine(this.GetGpioPath(), outputName);

            // If already exported, unexport it before continuing
            if (File.Exists(gpioPath))
            {
                this.UnExport(pinNumber);
            }

            // Now export the channel
            this.Export(pinNumber);

            // Set the IO direction
            this.SetDirection(pinNumber, direction);

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', configured for use", pinNumber));
        }
コード例 #14
0
ファイル: LibGpio.cs プロジェクト: MD-7/PiSharp
        /// <summary>
        /// Exports a given GPIO pin
        /// </summary>
        /// <param name="pinNumber">The pin number to export</param>
        private void Export(BroadcomPinNumber pinNumber)
        {
            // The simulator requires directories to be created first, but the RasPi does not and throws an exception.
            if (this.TestMode || Environment.OSVersion.Platform != PlatformID.Unix)
            {
                var exportDir = string.Format("gpio{0}", (int)pinNumber);
                var exportPath = Path.Combine(this.GetGpioPath(), exportDir);
                if (!Directory.Exists(exportPath))
                {
                    Directory.CreateDirectory(exportPath);
                    File.Create(Path.Combine(exportPath, "value")).Close();
                }
            }

            using (var fileStream = new FileStream(Path.Combine(this.GetGpioPath(), "export"), FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite))
            {

                using (var streamWriter = new StreamWriter(fileStream))
                {
                    streamWriter.Write((int)pinNumber);
                    streamWriter.Flush();
                }
            }

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', been exported", pinNumber));
        }
コード例 #15
0
ファイル: LibGpio.cs プロジェクト: mrinkartiso/PiSharp
        /// <summary>
        /// Closes a GPIO channel
        /// </summary>
        /// <param name="pinNumber">The Broadcom pin number to configure</param>
        public void CloseChannel(BroadcomPinNumber pinNumber)
        {
            var outputName = string.Format("gpio{0}", (int)pinNumber);
            var gpioPath = Path.Combine(this.GetGpioPath(), outputName);

            if (Directory.Exists(gpioPath))
            {
                this.UnExport(pinNumber);

                // Sets direction back to input.
                this.SetDirection(pinNumber, Direction.Input);
            }

            

            Debug.WriteLine(string.Format("[PiSharp.LibGpio] Broadcom GPIO number '{0}', configured for use", pinNumber));
        }