/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <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> /// 28 Aug 14 Cynic - Originally written /// </history> public bool Read() { string outStr = System.IO.File.ReadAllText(BBBDefinitions.SYSFS_GPIODIR + BBBDefinitions.SYSFS_GPIODIRNAMEBASE + GpioUtils.GpioIDToString(GpioID) + "/" + BBBDefinitions.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> /// 28 Aug 14 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(BBBDefinitions.SYSFS_GPIODIR + BBBDefinitions.SYSFS_GPIODIRNAMEBASE + GpioUtils.GpioIDToString(GpioID) + "/" + BBBDefinitions.SYSFS_GPIOVALUE, valueStr); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Constructor /// </summary> /// <param name="gpioIDIn">The gpio we open the port on</param> /// <history> /// 28 Aug 14 Cynic - Originally written /// </history> public Port(GpioEnum gpioIDIn) { // set our GpioConfig object now gpioCfgObject = GpioUtils.GetGpioConfigForGpio(gpioIDIn); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <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> /// 28 Aug 14 Cynic - Originally written /// </history> public override void ClosePort() { // do the close System.IO.File.WriteAllText(BBBDefinitions.SYSFS_GPIODIR + BBBDefinitions.SYSFS_GPIOUNEXPORT, GpioUtils.GpioIDToString(GpioID)); }
// ######################################################################### // ### 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> /// 28 Aug 14 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(BBBDefinitions.SYSFS_GPIODIR + BBBDefinitions.SYSFS_GPIOEXPORT, 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> /// 28 Aug 14 Cynic - Originally written /// </history> protected void SetSysFsDirection() { string dirStr; if (PortDirection() == PortDirectionEnum.PORTDIR_INPUT) { dirStr = "in"; } else if (PortDirection() == PortDirectionEnum.PORTDIR_OUTPUT) { dirStr = "out"; } else { // should never happen throw new Exception("unknown port direction:" + PortDirection().ToString()); } // set the direction now System.IO.File.WriteAllText(BBBDefinitions.SYSFS_GPIODIR + BBBDefinitions.SYSFS_GPIODIRNAMEBASE + GpioUtils.GpioIDToString(GpioID) + "/" + BBBDefinitions.SYSFS_GPIODIRECTION, dirStr); }