//no need to setup pin this is done for you public bool InputPin(enumPIN pin) { bool returnValue = false; //if we havent used the pin before, or if we used it as an output before, set it up if (!_InExported.Contains(pin) || _OutExported.Contains(pin)) { SetupPin(pin, enumDirection.IN); } string filename = GPIO_PATH + pin.ToString() + "/value"; if (File.Exists(filename)) { string readValue = File.ReadAllText(filename); if (readValue != null && readValue.Length > 0 && readValue[0] == '1') { returnValue = true; } } else { throw new Exception(string.Format("Cannot read from {0}. File does not exist", pin)); } if (DEBUG) { Console.WriteLine("input from pin " + pin + ", value was " + returnValue); } return(returnValue); }
//this gets called automatically when we try and output to, or input from, a pin private void SetupPin(enumPIN pin, enumDirection direction) { //unexport if it we're using it already if (_OutExported.Contains(pin) || _InExported.Contains(pin)) { UnexportPin(pin); } //export File.WriteAllText(GPIO_PATH + "export", GetPinNumber(pin)); if (DEBUG) { Console.WriteLine("exporting pin " + pin + " as " + direction); } // set i/o direction File.WriteAllText(GPIO_PATH + pin.ToString() + "/direction", direction.ToString().ToLower()); //record the fact that we've setup that pin if (direction == enumDirection.OUT) { _OutExported.Add(pin); } else { _InExported.Add(pin); } }
//no need to setup pin this is done for you public void OutputPin(enumPIN pin, bool value) { //if we havent used the pin before, or if we used it as an input before, set it up if (!_OutExported.Contains(pin) || _InExported.Contains(pin)) SetupPin(pin, enumDirection.OUT); string writeValue = "0"; if (value) writeValue = "1"; File.WriteAllText(GPIO_PATH + pin.ToString() + "/value", writeValue); if (DEBUG) Console.WriteLine("output to pin " + pin + ", value was " + value); }
//no need to setup pin this is done for you public bool InputPin(enumPIN pin) { bool returnValue = false; //if we havent used the pin before, or if we used it as an output before, set it up if (!_InExported.Contains(pin) || _OutExported.Contains(pin)) SetupPin(pin, enumDirection.IN); string filename = GPIO_PATH + pin.ToString() + "/value"; if (File.Exists(filename)) { string readValue = File.ReadAllText(filename); if (readValue != null && readValue.Length > 0 && readValue[0] == '1') returnValue = true; } else throw new Exception(string.Format("Cannot read from {0}. File does not exist", pin)); if (DEBUG) Console.WriteLine("input from pin " + pin + ", value was " + returnValue); return returnValue; }
//no need to setup pin this is done for you public void OutputPin(enumPIN pin, bool value) { //if we havent used the pin before, or if we used it as an input before, set it up if (!_OutExported.Contains(pin) || _InExported.Contains(pin)) { SetupPin(pin, enumDirection.OUT); } string writeValue = "0"; if (value) { writeValue = "1"; } File.WriteAllText(GPIO_PATH + pin.ToString() + "/value", writeValue); if (DEBUG) { Console.WriteLine("output to pin " + pin + ", value was " + value); } }
//if for any reason you want to unexport a particular pin use this, otherwise just call CleanUpAllPins when you're done public void UnexportPin(enumPIN pin) { bool found = false; if (_OutExported.Contains(pin)) { found = true; _OutExported.Remove(pin); } if (_InExported.Contains(pin)) { found = true; _InExported.Remove(pin); } if (found) { File.WriteAllText(GPIO_PATH + "unexport", GetPinNumber(pin)); if (DEBUG) { Console.WriteLine("unexporting pin " + pin); } } }
private string GetPinNumber(enumPIN pin) { return(((int)pin).ToString()); //e.g. returns 17 for enum value of gpio17 }
private string GetPinNumber(enumPIN pin) { return ((int)pin).ToString(); //e.g. returns 17 for enum value of gpio17 }
//if for any reason you want to unexport a particular pin use this, otherwise just call CleanUpAllPins when you're done public void UnexportPin(enumPIN pin) { bool found = false; if (_OutExported.Contains(pin)) { found = true; _OutExported.Remove(pin); } if (_InExported.Contains(pin)) { found = true; _InExported.Remove(pin); } if (found) { File.WriteAllText(GPIO_PATH + "unexport", GetPinNumber(pin)); if (DEBUG) Console.WriteLine("unexporting pin " + pin); } }
//this gets called automatically when we try and output to, or input from, a pin private void SetupPin(enumPIN pin, enumDirection direction) { //unexport if it we're using it already if (_OutExported.Contains(pin) || _InExported.Contains(pin)) UnexportPin(pin); //export File.WriteAllText(GPIO_PATH + "export", GetPinNumber(pin)); if (DEBUG) Console.WriteLine("exporting pin " + pin + " as " + direction); // set i/o direction File.WriteAllText(GPIO_PATH + pin.ToString() + "/direction", direction.ToString().ToLower()); //record the fact that we've setup that pin if (direction == enumDirection.OUT) _OutExported.Add(pin); else _InExported.Add(pin); }