/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Gets the port value /// /// This is really just doing the equivalent of a shell command /// echo <value_as_string> > /sys/class/gpio/gpio<gpioID>/value /// /// </summary> /// <returns>true or false - the ports value</returns> /// <history> /// 01 Dec 16 Cynic - Originally written /// </history> public bool Read() { string outStr = System.IO.File.ReadAllText(RPIDefinitions.SYSFS_GPIODIR + RPIDefinitions.SYSFS_GPIODIRNAMEBASE + GpioUtils.GpioIDToString(GpioID) + "/" + RPIDefinitions.SYSFS_GPIOVALUE); if (outStr.Trim() == "0") { return(false); } else { return(true); } }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Sets the port value /// /// This is really just doing the equivalent of a shell command /// echo <value_as_string> > /sys/class/gpio/gpio<gpioID>/value /// /// </summary> /// <param name="valueToSet>the value to set/param> /// <history> /// 01 Dec 16 Cynic - Originally written /// </history> public void Write(bool valueToSet) { string valueStr; if (valueToSet == true) { valueStr = "1"; } else { valueStr = "0"; } // set the value now System.IO.File.WriteAllText(RPIDefinitions.SYSFS_GPIODIR + RPIDefinitions.SYSFS_GPIODIRNAMEBASE + GpioUtils.GpioIDToString(GpioID) + "/" + RPIDefinitions.SYSFS_GPIOVALUE, valueStr); }
// ######################################################################### // ### Port Manipulation Code // ######################################################################### #region /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Opens the port. Throws an exception on failure, including if the port is /// already open. /// /// This is really just doing the equivalent of a shell command /// echo <gpioID> > /sys/class/gpio/export /// after which the /sys/class/gpio/gpio<gpioID> directory should exist. /// If it already exists the port is in use by someone else /// /// </summary> /// <history> /// 01 Dec 16 Cynic - Originally written /// </history> protected override void OpenPort() { // some tests if (GpioID == GpioEnum.GPIO_NONE) { throw new Exception("Cannot open port. Invalid port: " + GpioID.ToString()); } // do the open System.IO.File.WriteAllText(RPIDefinitions.SYSFS_GPIODIR + RPIDefinitions.SYSFS_GPIOEXPORT, GpioUtils.GpioIDToString(GpioID)); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Closes the port. Throws an exception on failure, including if the port is /// already closed /// /// This is really just doing the equivalent of a shell command /// echo <gpioID> > /sys/class/gpio/unexport /// after which the /sys/class/gpio/gpio<gpioID> directory should not exist. /// /// </summary> /// <history> /// 01 Dec 16 Cynic - Originally written /// </history> public override void ClosePort() { // do the close System.IO.File.WriteAllText(RPIDefinitions.SYSFS_GPIODIR + RPIDefinitions.SYSFS_GPIOUNEXPORT, GpioUtils.GpioIDToString(GpioID)); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Sets the port direction at the SysFs level /// /// This is really just doing the equivalent of a shell command /// echo <direction_as_string> > /sys/class/gpio/gpio<gpioID>/direction /// /// </summary> /// <param name="portDirIn">the port direction INPUT/OUTPUT</param> /// <history> /// 01 Dec 16 Cynic - Originally written /// </history> protected void SetSysFsDirection() { string dirStr; if (PortDirection() == FSPortDirectionEnum.PORTDIR_INPUT) { dirStr = "in"; } else if (PortDirection() == FSPortDirectionEnum.PORTDIR_OUTPUT) { dirStr = "out"; } else if (PortDirection() == FSPortDirectionEnum.PORTDIR_INPUTOUTPUT) { // handled elsewhere return; } else { // should never happen throw new Exception("unknown port direction:" + PortDirection().ToString()); } // set the direction now System.IO.File.WriteAllText(RPIDefinitions.SYSFS_GPIODIR + RPIDefinitions.SYSFS_GPIODIRNAMEBASE + GpioUtils.GpioIDToString(GpioID) + "/" + RPIDefinitions.SYSFS_GPIODIRECTION, dirStr); }