示例#1
0
        //no need to setup pin this is done for you
        public bool InputPin(RaspiGpio 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);
        }
示例#2
0
        //this gets called automatically when we try and output to, or input from, a pin
        private void SetupPin(RaspiGpio 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);
            }
        }
示例#3
0
        //no need to setup pin this is done for you
        public void OutputPin(RaspiGpio 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);
            }
        }
示例#4
0
        //if for any reason you want to unexport a particular pin use this, otherwise just call CleanUpAllPins when you're done
        public void UnexportPin(RaspiGpio 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);
                }
            }
        }
示例#5
0
        //this gets called automatically when we try and output to, or input from, a pin
        private void SetupPin(RaspiGpio 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);
        }
示例#6
0
 private string GetPinNumber(RaspiGpio pin)
 {
     return ((int)pin).ToString(); //e.g. returns 17 for enum value of gpio17
 }
示例#7
0
        //if for any reason you want to unexport a particular pin use this, otherwise just call CleanUpAllPins when you're done
        public void UnexportPin(RaspiGpio 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);
            }
        }
示例#8
0
        //no need to setup pin this is done for you
        public void OutputPin(RaspiGpio 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);
        }
示例#9
0
        //no need to setup pin this is done for you
        public bool InputPin(RaspiGpio 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;
        }
示例#10
0
 private string GetPinNumber(RaspiGpio pin)
 {
     return(((int)pin).ToString()); //e.g. returns 17 for enum value of gpio17
 }