Beispiel #1
0
        /// <summary>
        /// Export the GPIO setting the direction. This creates the /sys/class/gpio/gpioXX directory.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="direction"></param>
        private static void ExportPin(GPIOPins pin, DirectionEnum direction)
        {
            Initialize();

            // If the pin is already exported, check it's in the proper direction
            if (_exportedPins.Keys.Contains((int)pin))
            {
                // If the direction matches, return out of the function. If not, change the direction
                if (_exportedPins[(int)pin] == direction)
                {
                    return;
                }
            }

            // Set the direction on the pin and update the exported list
            // BCM2835_GPIO_FSEL_INPT = 0
            // BCM2835_GPIO_FSEL_OUTP = 1
            bcm2835_gpio_fsel((uint)pin, direction == DirectionEnum.IN ? (uint)0 : (uint)1);
            if (direction == DirectionEnum.IN)
            {
                // BCM2835_GPIO_PUD_OFF = 0b00 = 0
                // BCM2835_GPIO_PUD_DOWN = 0b01 = 1
                // BCM2835_GPIO_PUD_UP = 0b10 = 2
                bcm2835_gpio_set_pud((uint)pin, 0);
            }
            _exportedPins[(int)pin] = direction;
        }
Beispiel #2
0
        /// <summary>
        /// Export the GPIO setting the direction. This creates the /sys/class/gpio/gpioXX directory.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="direction"></param>
        private static void ExportPin(GPIOPins pin, DirectionEnum direction)
        {
            // If the pin is already exported, check it's in the proper direction
            if (_exportedPins.Keys.Contains((int)pin))
                // If the direction matches, return out of the function. If not, change the direction
                if (_exportedPins[(int)pin] == direction)
                    return;
                else
                {
                    // Set the direction on the pin and update the exported list
                    File.WriteAllText(GPIO_PATH + "gpio" + GetGPIONumber(pin) + "/direction", direction.ToString().ToLower());
                    _exportedPins[(int)pin] = direction;
                    return;
                }

            if (!Directory.Exists(GPIO_PATH + "gpio" + GetGPIONumber(pin)))
            {
                Debug.WriteLine("Exporting " + GetGPIONumber(pin));
                //export
                File.WriteAllText(GPIO_PATH + "export", GetGPIONumber(pin));
            }

            // set i/o direction
            Debug.WriteLine("Setting direction on pin " + pin + "/gpio " + (int)pin + " as " + direction);
            File.WriteAllText(GPIO_PATH + "gpio" + GetGPIONumber(pin) + "/direction", direction.ToString().ToLower());

            // Update the list of exported pins
            _exportedPins[(int)pin] = direction;
        }
Beispiel #3
0
        /// <summary>
        /// Creates a variable of type LiquidCrystal. The display can be controlled using 4 or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those lines unconnected. The RW pin can be tied to ground instead of connected to a pin on the Arduino; if so, omit it from this function's parameters.
        /// </summary>
        /// <param name="fourBitMode"></param>
        /// <param name="rs">The number of the CPU pin that is connected to the RS (register select) pin on the LCD.</param>
        /// <param name="rw">The number of the CPU pin that is connected to the RW (Read/Write) pin on the LCD (optional).</param>
        /// <param name="enable">the number of the CPU pin that is connected to the enable pin on the LCD.</param>
        /// <param name="d0"></param>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <param name="d3"></param>
        /// <param name="d4"></param>
        /// <param name="d5"></param>
        /// <param name="d6"></param>
        /// <param name="d7"></param>
        public RaspPiGPIOFileLcdTransferProvider(bool fourBitMode, GPIOPins rs, GPIOPins rw, GPIOPins enable,
                                                 GPIOPins d0, GPIOPins d1, GPIOPins d2, GPIOPins d3,
                                                 GPIOPins d4, GPIOPins d5, GPIOPins d6, GPIOPins d7)
        {
            _fourBitMode = fourBitMode;

            if (rs == GPIOPins.GPIO_NONE)
            {
                throw new ArgumentException("rs");
            }
            _rsPort = new GPIOFile(rs);

            // we can save 1 pin by not using RW. Indicate by passing GPIO.GPIOPins.GPIO_NONE instead of pin#
            if (rw != GPIOPins.GPIO_NONE) // (RW is optional)
            {
                _rwPort = new GPIOFile(rw);
            }

            if (enable == GPIOPins.GPIO_NONE)
            {
                throw new ArgumentException("enable");
            }
            _enablePort = new GPIOFile(enable);

            var dataPins = new[] { d0, d1, d2, d3, d4, d5, d6, d7 };

            _dataPorts = new GPIOFile[8];
            for (int i = 0; i < 8; i++)
            {
                if (dataPins[i] != GPIOPins.GPIO_NONE)
                {
                    _dataPorts[i] = new GPIOFile(dataPins[i]);
                }
            }
        }
 public LCDWrapper(GPIOPins rs, GPIOPins enable, GPIOPins d0, GPIOPins d1, GPIOPins d2, GPIOPins d3, GPIOPins d4, GPIOPins d5, GPIOPins d6, GPIOPins d7, int columns = 40, int rows = 2, DisplayMode displayMode = DisplayMode.LCD_ONLY)
 {
     DisplayMode = displayMode;
     Columns = columns;
     Rows = rows;
     if (DisplayMode != DisplayMode.CONSOLE_ONLY) {
         transferProvider = new RaspPiGPIOMemLcdTransferProvider(
             fourBitMode: false,
             rs: rs,
             rw: GPIOPins.GPIO_NONE,
             enable: enable,
             d0: d0,
             d1: d1,
             d2: d2,
             d3: d3,
             d4: d4,
             d5: d5,
             d6: d6,
             d7: d7
         );
         lcd = new Lcd(transferProvider);
         lcd.Begin(Convert.ToByte(columns), Convert.ToByte(rows));
         lcd.Backlight = true;
         lcd.BlinkCursor = false;
         lcd.ShowCursor = false;
         lcd.Visible = true;
     }
     if (DisplayMode != DisplayMode.LCD_ONLY) {
         Console.Clear();
         Console.ForegroundColor = ConsoleColor.White;
         Console.BackgroundColor = ConsoleColor.Black;
         Console.CursorVisible = false;
     }
 }
Beispiel #5
0
        /// <summary>
        /// Static method to read a value to the specified pin. When using the static methods ensure you use the CleanUp() method when done.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <returns>The value read from the pin</returns>
        public static bool Read(GPIOPins pin)
        {
            bool returnValue = false;

            ExportPin(pin, DirectionEnum.IN);

            string filename = GPIO_PATH + "gpio" + GetGPIONumber(pin) + "/value";

            if (File.Exists(filename))
            {
                string readValue = File.ReadAllText(filename);
                if (readValue.Length > 0 && readValue[0] == '1')
                {
                    returnValue = true;
                }
            }
            else
            {
                throw new Exception(string.Format("Cannot read from {0}. File does not exist", pin));
            }

            Debug.WriteLine("input from pin " + pin + "/gpio " + (int)pin + ", value was " + returnValue);

            return(returnValue);
        }
Beispiel #6
0
        /// <summary>
        /// Export the GPIO setting the direction. This creates the /sys/class/gpio/gpioXX directory.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="direction"></param>
        private static void ExportPin(GPIOPins pin, DirectionEnum direction)
        {
            // If the pin is already exported, check it's in the proper direction
            if (_exportedPins.Keys.Contains((int)pin))
            {
                // If the direction matches, return out of the function. If not, change the direction
                if (_exportedPins[(int)pin] == direction)
                {
                    return;
                }
                else
                {
                    // Set the direction on the pin and update the exported list
                    File.WriteAllText(GPIO_PATH + "gpio" + GetGPIONumber(pin) + "/direction", direction.ToString().ToLower());
                    _exportedPins[(int)pin] = direction;
                    return;
                }
            }

            if (!Directory.Exists(GPIO_PATH + "gpio" + GetGPIONumber(pin)))
            {
                Debug.WriteLine("Exporting " + GetGPIONumber(pin));
                //export
                File.WriteAllText(GPIO_PATH + "export", GetGPIONumber(pin));
            }

            // set i/o direction
            Debug.WriteLine("Setting direction on pin " + pin + "/gpio " + (int)pin + " as " + direction);
            File.WriteAllText(GPIO_PATH + "gpio" + GetGPIONumber(pin) + "/direction", direction.ToString().ToLower());

            // Update the list of exported pins
            _exportedPins[(int)pin] = direction;
        }
Beispiel #7
0
        public static IGPIO CreatePin(GPIOPins pin, GPIODirection dir = GPIODirection.Out, PinState pinState = PinState.Low)
        {
            if (_ExportedPins.ContainsKey(pin))
            {
                if (_ExportedPins[pin].Direction != dir)
                {
                    _ExportedPins[pin].Direction = dir;
                }
                if (_ExportedPins[pin].State != pinState)
                {
                    _ExportedPins[pin].State = pinState;
                }
                return(_ExportedPins[pin]);
            }
            try
            {
                return(new GPIO(pin, dir, pinState));
            }
#if DEBUG
            catch (Exception e)
            {
                throw new Exception("Unable to create pin " + (uint)pin + ". Infomation: " + e.ToString());
            }
#else
            catch             //stuff like lib load problems, wrong exports, etc...
            {
                return(null);
            }
#endif
        }
Beispiel #8
0
 public PinStateListener(GPIOPins pin)
 {
     Console.WriteLine("constructor");
     if (!SetPin(pin))
     {
         throw LastException;
     }
 }
Beispiel #9
0
        /// <summary>
        /// Unexport the GPIO. This removes the /sys/class/gpio/gpioXX directory.
        /// </summary>
        /// <param name="pin">The pin to unexport</param>
        private static void UnexportPin(GPIOPins pin)
        {
            Debug.WriteLine("unexporting pin " + pin);
            File.WriteAllText(GPIO_PATH + "unexport", GetGPIONumber(pin));

            // Remove the pin from the list of exported pins
            _exportedPins.Remove((int)pin);
        }
Beispiel #10
0
        public McuSerial(GPIOPins clockPin, GPIOPins ioPin, bool clockStartValue = false)
        {
            this.clockPin = clockPin;
            this.ioPin = ioPin;
            this.clockValue = clockStartValue;

            GPIOMem.Write(clockPin, clockValue);
            GPIOMem.Write(ioPin, false);
        }
Beispiel #11
0
        /// <summary>
        /// Static method to read a value to the specified pin.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <returns>The value read from the pin</returns>
        public static bool Read(GPIOPins pin)
        {
            ExportPin(pin, DirectionEnum.IN);

            uint value       = bcm2835_gpio_lev((uint)pin);
            bool returnValue = value == 0 ? false : true;

            Debug.WriteLine("input from pin " + pin + "/gpio " + (int)pin + ", value was " + returnValue);

            return(returnValue);
        }
Beispiel #12
0
        /// <summary>
        /// Static method to write a value to the specified pin.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="value">The value to write to the pin</param>
        public static void Write(GPIOPins pin, bool value)
        {
            if (pin == GPIOPins.GPIO_NONE)
            {
                return;
            }

            ExportPin(pin, DirectionEnum.OUT);

            bcm2835_gpio_write((uint)pin, value ? (uint)1 : (uint)0);
            Debug.WriteLine("output to pin " + pin + "/gpio " + (int)pin + ", value was " + value);
        }
Beispiel #13
0
        /// <summary>
        /// Static method to write a value to the specified pin. When using the static methods ensure you use the CleanUp() method when done.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="value">The value to write to the pin</param>
        public static void Write(GPIOPins pin, bool value)
        {
            if (pin == GPIOPins.GPIO_NONE)
            {
                return;
            }

            ExportPin(pin, DirectionEnum.OUT);

            string writeValue = value ? "1" : "0";

            File.WriteAllText(GPIO_PATH + "gpio" + GetGPIONumber(pin) + "/value", writeValue);
            Debug.WriteLine("output to pin " + pin + "/gpio " + (int)pin + ", value was " + value);
        }
        public string RequestListener(HttpListenerRequest request)
        {
            GPIOMem gpio     = null;
            string  rtnValue = null;

            try
            {
                // log out requests for debugging
                Console.WriteLine(request.Url);

                // set pin direction via "dir" argument
                GPIODirection dir = (GPIODirection)((new[] { "OUT", "TRUE", "1" }).Contains(request.QueryString["dir"].ToUpper()) ? 1 : 0);

                // set pin number via "pin" argument
                GPIOPins pin = (GPIOPins)int.Parse(request.QueryString["pin"]);

                gpio = new GPIOMem((GPIOPins)pin, dir);

                if (dir == GPIODirection.Out)
                {
                    // set pin state via "state" argument
                    bool state = (new[] { "HIGH", "1", "TRUE", "T" }).Contains(request.QueryString["state"].ToUpper());

                    gpio.Write(state);
                    rtnValue = state.ToString();
                }
                else
                {
                    rtnValue = (gpio.Read() == PinState.High).ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR:");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.Source);
                Console.WriteLine(ex.StackTrace);
                rtnValue = "ERROR";
            }
            finally
            {
                gpio.Dispose();
            }

            return(rtnValue);
        }
Beispiel #15
0
        /// <summary>
        /// Export the GPIO setting the direction. This creates the /sys/class/gpio/gpioXX directory.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="direction"></param>
        private static void ExportPin(GPIOPins pin, DirectionEnum direction)
        {
            Initialize();

            // If the pin is already exported, check it's in the proper direction
            if (_exportedPins.Keys.Contains((int)pin))
                // If the direction matches, return out of the function. If not, change the direction
                if (_exportedPins[(int)pin] == direction)
                    return;
                else
                {
                    // Set the direction on the pin and update the exported list
                    bcm2835_gpio_fsel((uint)pin, direction == DirectionEnum.IN ? (uint)0 : (uint)1);
                    _exportedPins[(int)pin] = direction;
                    return;
                }
        }
Beispiel #16
0
		/// <summary>
		/// Access to the specified GPIO setup with the specified direction with the specified initial value
		/// </summary>
		/// <param name="pin">The GPIO pin</param>
		/// <param name="direction">Direction</param>
		/// <param name="initialValue">Initial Value</param>
		protected GPIO(GPIOPins pin, GPIODirection direction, bool initialValue) {
			if (pin == GPIOPins.GPIO_NONE) throw new ArgumentException("Invalid pin");
			lock (_exportedPins) {
				if (_exportedPins.ContainsKey(pin))
					throw new Exception("Cannot use pin with multiple instances. Unexport the previous instance with Dispose() first! (pin " + (uint)pin + ")");
				_exportedPins[pin] = new WeakReference(this);

				_pin = pin;
				try {
					PinDirection = direction;
					Write(initialValue);
				}
				catch {
					Dispose();
					throw;
				}
			}
		}
Beispiel #17
0
        /// <summary>
        /// Access to the specified GPIO setup with the specified direction with the specified initial value
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="direction">Direction</param>
        /// <param name="initialValue">Initial Value</param>
        private GPIO(GPIOPins pin, GPIODirection direction, PinState pinState)
        {
            if (pin == GPIOPins.GPIO_NONE)
            {
                throw new ArgumentException("Invalid pin");
            }

            if (_ExportedPins.ContainsKey(pin))
            {
                throw new Exception("Cannot use pin with multiple instances. Unexport the previous instance with Dispose() first! (pin " + (byte)pin + ")");
            }
            _ExportedPins[pin] = this;
            _pin = pin;

            this.Direction = direction;
            this.State     = pinState;
            Write(pinState);
        }
Beispiel #18
0
        /// <summary>
        /// Creates a pin if it has not already been created (exported), creates a GPIOMem if possible, otherwise falls back to GPIOFile.
        /// </summary>
        /// <param name="pin">The pin to create or export</param>
        /// <param name="dir">The direction the pin is to have</param>
        /// <returns>The GPIO instance representing the pin</returns>
        public static GPIO CreatePin(GPIOPins pin, GPIODirection dir)
        {
            lock (_exportedPins)
                if (_exportedPins.ContainsKey(pin))
                {
                    if (_exportedPins[pin].PinDirection != dir)
                    {
                        _exportedPins[pin].PinDirection = dir;
                    }
                    return(_exportedPins[pin]);
                }

            try {
                return(new GPIOMem(pin, dir));
            }
#if DEBUG
            catch (Exception e) {
                System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOMem because: " + e.ToString());
            }
#else
            catch             //stuff like lib load problems, wrong exports, etc...
            {
            }
#endif
            try {
                return(new GPIOFile(pin, dir));
            }
#if DEBUG
            catch (Exception e) {
                System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOFile because: " + e.ToString());
            }
#else
            catch             //stuff like GPIO Sys FS does not exist or is not responding, open by another process, etc...
            {
            }
#endif

#if DEBUG
            System.Diagnostics.Debug.WriteLine("Using debug GPIO pin for pin number " + (uint)pin);
            return(new GPIODebug(pin, dir));
#else
            throw new Exception("Cannot access GPIO pin " + (uint)pin + ". Make sure libbcm2835.so is accessible, or that GPIO SYSFS is working and not in use by another process");
#endif
        }
Beispiel #19
0
 public bool SetPin(GPIOPins pin)
 {
     try
     {
         if (_running)
         {
             return(false);
         }
         //                _input = GPIO.CreatePin(pin, GPIODirection.In);
         _input = new GPIOFile(pin, GPIODirection.In);
         return(true);
     }
     catch (Exception exc)
     {
         Console.WriteLine("ceva a crapat: " + exc.Message);
         LastException = exc;
         return(false);
     }
 }
Beispiel #20
0
		/// <summary>
		/// Export the GPIO setting the direction. This creates the /sys/class/gpio/gpioXX directory.
		/// </summary>
		/// <param name="pin">The GPIO pin</param>
		/// <param name="direction"></param>
		private static void ExportPin(GPIOPins pin, DirectionEnum direction) {
			Initialize();

			// If the pin is already exported, check it's in the proper direction
			if (_exportedPins.Keys.Contains((int)pin))
				// If the direction matches, return out of the function. If not, change the direction
				if (_exportedPins[(int)pin] == direction)
					return;

			// Set the direction on the pin and update the exported list
			// BCM2835_GPIO_FSEL_INPT = 0
			// BCM2835_GPIO_FSEL_OUTP = 1
			bcm2835_gpio_fsel((uint)pin, direction == DirectionEnum.IN ? (uint)0 : (uint)1);
			if (direction == DirectionEnum.IN)
				// BCM2835_GPIO_PUD_OFF = 0b00 = 0
				// BCM2835_GPIO_PUD_DOWN = 0b01 = 1
				// BCM2835_GPIO_PUD_UP = 0b10 = 2
				bcm2835_gpio_set_pud((uint)pin, 0);
			_exportedPins[(int)pin] = direction;
		}
Beispiel #21
0
        /// <summary>
        /// Access to the specified GPIO setup with the specified direction with the specified initial value
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="direction">Direction</param>
        /// <param name="initialValue">Initial Value</param>
        protected GPIO(GPIOPins pin, GPIODirection direction, bool initialValue)
        {
            if (pin == GPIOPins.GPIO_NONE)
            {
                throw new ArgumentException("Invalid pin");
            }
            lock (_exportedPins) {
                if (_exportedPins.ContainsKey(pin))
                {
                    throw new Exception("Cannot use pin with multiple instances. Unexport the previous instance with Dispose() first! (pin " + (uint)pin + ")");
                }
                _exportedPins[pin] = this;

                _pin = pin;
                try {
                    PinDirection = direction;
                    Write(initialValue);
                }
                catch {
                    Dispose();
                    throw;
                }
            }
        }
        /// <summary>
        /// Creates a variable of type LiquidCrystal. The display can be controlled using 4 or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those lines unconnected. The RW pin can be tied to ground instead of connected to a pin on the Arduino; if so, omit it from this function's parameters. 
        /// </summary>
        /// <param name="fourBitMode"></param>
        /// <param name="rs">The number of the CPU pin that is connected to the RS (register select) pin on the LCD.</param>
        /// <param name="rw">The number of the CPU pin that is connected to the RW (Read/Write) pin on the LCD (optional).</param>
        /// <param name="enable">the number of the CPU pin that is connected to the enable pin on the LCD.</param>
        /// <param name="d0"></param>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <param name="d3"></param>
        /// <param name="d4"></param>
        /// <param name="d5"></param>
        /// <param name="d6"></param>
        /// <param name="d7"></param>
        public RaspPiGPIOFileLcdTransferProvider(bool fourBitMode, GPIOPins rs, GPIOPins rw, GPIOPins enable, 
                                                 GPIOPins d0, GPIOPins d1, GPIOPins d2, GPIOPins d3, 
                                                 GPIOPins d4, GPIOPins d5, GPIOPins d6, GPIOPins d7)
        {
            _fourBitMode = fourBitMode;

            if (rs == GPIOPins.GPIO_NONE) throw new ArgumentException("rs");
            _rsPort = new GPIOFile(rs);

            // we can save 1 pin by not using RW. Indicate by passing GPIO.GPIOPins.GPIO_NONE instead of pin#
            if (rw != GPIOPins.GPIO_NONE) // (RW is optional)
                _rwPort = new GPIOFile(rw);

            if (enable == GPIOPins.GPIO_NONE) throw new ArgumentException("enable");
            _enablePort = new GPIOFile(enable);

            var dataPins = new[] { d0, d1, d2, d3, d4, d5, d6, d7};
            _dataPorts = new GPIOFile[8];
            for (int i = 0; i < 8; i++)
            {
                if (dataPins[i] != GPIOPins.GPIO_NONE)
                    _dataPorts[i] = new GPIOFile(dataPins[i]);
            }
        }
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with the specified initial value
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 /// <param name="initialValue">Initial Value</param>
 public GPIODebug(GPIOPins pin, GPIODirection direction, bool initialValue)
     : base(pin, direction, initialValue)
 {
 }
 /// <summary>
 /// Access to the specified GPIO setup as an output port with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 public GPIODebug(GPIOPins pin)
     : this(pin, GPIODirection.Out, false)
 {
 }
Beispiel #25
0
		/// <summary>
		/// Creates a pin if it has not already been created (exported), creates a GPIOMem if possible, otherwise falls back to GPIOFile.
		/// </summary>
		/// <param name="pin">The pin to create or export</param>
		/// <param name="dir">The direction the pin is to have</param>
		/// <returns>The GPIO instance representing the pin</returns>
		public static GPIO CreatePin(GPIOPins pin, GPIODirection dir) {
			lock (_exportedPins) {
				if (_exportedPins.ContainsKey(pin) ) {
					var reference = _exportedPins[pin];
					if (reference.IsAlive) {
						var gpio = (GPIO)_exportedPins[pin].Target;
						if (gpio.PinDirection != dir)
							gpio.PinDirection = dir;
						return gpio;
					}
					else {
						_exportedPins.Remove(pin);
					}
				}

			}

			try {
				return new GPIOMem(pin, dir);
			}
#if DEBUG
			catch (Exception e) {
				System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOMem because: " + e.ToString());
			}
#else
			catch //stuff like lib load problems, wrong exports, etc...
			{
			}
#endif
			try {
				return new GPIOFile(pin, dir);
			}
#if DEBUG
			catch (Exception e) {
				System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOFile because: " + e.ToString());
			}
#else
			catch //stuff like GPIO Sys FS does not exist or is not responding, open by another process, etc...
			{
			}
#endif

#if DEBUG
			System.Diagnostics.Debug.WriteLine("Using debug GPIO pin for pin number " + (uint)pin);
			return new GPIODebug(pin, dir);
#else
			throw new Exception("Cannot access GPIO pin " + (uint)pin + ". Make sure libbcm2835.so is accessible, or that GPIO SYSFS is working and not in use by another process");
#endif
		}
Beispiel #26
0
		/// <summary>
		/// Sets a pin to output the give value.
		/// 
		/// Creates (exports) the pin if needed, and sets it to Out direction.
		/// </summary>
		/// <param name="pin">The pin who's value to set</param>
		/// <param name="value">The value to set</param>
		public static void Write(GPIOPins pin, bool value) {
			CreatePin(pin, GPIODirection.Out).Write(value);
		}
Beispiel #27
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with the specified initial value
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 /// <param name="initialValue">Initial Value</param>
 public GPIOMem(GPIOPins pin, DirectionEnum direction, bool initialValue)
     : base(pin, direction, initialValue)
 {
     ExportPin(pin, direction);
     Write(pin, initialValue);
 }
Beispiel #28
0
 /// <summary>
 /// Access to the specified GPIO setup as an output port with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 public GPIOMem(GPIOPins pin)
     : base(pin,DirectionEnum.OUT,false)
 {
 }
Beispiel #29
0
 /// <summary>
 /// Access to the specified GPIO setup as an output port with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 public GPIOMem(GPIOPins pin)
     : base(pin, DirectionEnum.OUT, false)
 {
 }
Beispiel #30
0
 public static void DirectPin(GPIOPins pin, GPIODirection direction)
 {
     bcm2835_gpio_fsel(pin, direction);
 }
Beispiel #31
0
 protected static extern void bcm2835_gpio_set_pud(GPIOPins pin, GPIOResistor resistorMode);
Beispiel #32
0
 protected static extern bool bcm2835_gpio_lev(GPIOPins pin);
Beispiel #33
0
 protected static extern void bcm2835_gpio_write(GPIOPins pin, PinState pinState);
Beispiel #34
0
 protected static extern void bcm2835_gpio_fsel(GPIOPins pin, GPIODirection direction);
Beispiel #35
0
 public static void SetResistor(GPIOPins pin, GPIOResistor resistorMode)
 {
     bcm2835_gpio_set_pud(pin, resistorMode);
 }
Beispiel #36
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 public GPIODebug(GPIOPins pin, GPIODirection direction)
     : this(pin, direction, false)
 {
 }
Beispiel #37
0
 /// <summary>
 /// Static method to read a value to the specified pin. Always returns false when using the GPIODebug class.
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <returns>The value read from the pin</returns>
 public static bool Read(GPIOPins pin)
 {
     return false;
 }
Beispiel #38
0
        static void Main(string [] args)
        {
            /*string assemblyFolder = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
             * NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration (assemblyFolder + "/NLog/NLog.config", true);*/



            Console.Title = "Trigger Device By Ali Deym and Biftor (C) 2017-2018";


            WriteLineNoLog("Creating logger instance...");

            logger = new FileLogger()
            {
                FileNameTemplate = "main-########.log"
            };


            WriteLine("Logger instance created.");


            WriteLine("Trigger device (C) 2017 - 2018 by:");
            WriteLine("Ali Deym, Biftor (Parham Abedi).");
            WriteLine("");
            WriteLine("");
            WriteLine("Initializing device...");

            WriteLine("");
            WriteLine("");



            WriteLine("Trigger Program v" + triggerDeviceVersion.X + "." + triggerDeviceVersion.Y + "." + triggerDeviceVersion.Z);

            WriteLine("");
            WriteLine("Device info: ");


            var OS = Environment.OSVersion;

            WriteLine("Device: " + Environment.MachineName);
            WriteLine("Username: "******"OS: " + OS.VersionString + " (" + Enum.GetName(typeof(PlatformID), OS.Platform) + ")");
            WriteLine("CPU Cores: " + Environment.ProcessorCount + " Cores.");


            WriteLine("");
            WriteLine("");

            WriteLine("Checking for GPIO config...");


            try {
                if (File.Exists(".gpio"))
                {
                    var content = File.ReadAllText(".gpio").Trim();

                    //if (int.TryParse (content, out int result)) {

                    if (Enum.TryParse(content, out GPIOPins configuredPin))
                    {
                        interruptPin = configuredPin;

                        WriteLine("Changing GPIO pin to: " + content + ".");
                    }

                    //}
                }
                else
                {
                    WriteLine("Creating default GPIO configuration...");

                    File.WriteAllText(".gpio", "GPIO_17");
                }

                if (File.Exists(".ledgpio"))
                {
                    var content = File.ReadAllText(".ledgpio").Trim();


                    if (Enum.TryParse(content, out GPIOPins configuredPin))
                    {
                        ledPin = configuredPin;

                        WriteLine("Changing LED GPIO pin to: " + content + ".");
                    }
                }
                else
                {
                    WriteLine("Creating default LED GPIO configuration...");

                    File.WriteAllText(".ledgpio", "GPIO_23");
                }


                if (File.Exists(".flashgpio"))
                {
                    var content = File.ReadAllText(".flashgpio").Trim();


                    if (Enum.TryParse(content, out GPIOPins configuredPin))
                    {
                        flashPin = configuredPin;

                        WriteLine("Changing Flash GPIO pin to: " + content + ".");
                    }
                }
                else
                {
                    WriteLine("Creating default Flash GPIO configuration...");

                    File.WriteAllText(".flashgpio", "GPIO_21");
                }
            } catch (Exception ex) {
                WriteLine("GPIO config ERROR: " + ex.ToString());
            }

            WriteLine("Initializing GPIO Pin: " + Enum.GetName(typeof(GPIOPins), interruptPin) + "...");


            LEDTimer = DateTime.Now;

            /* Initialize GPIO Pin: */
            pin = new GPIOMem(interruptPin, GPIODirection.In);

            flash = new GPIOMem(flashPin, GPIODirection.Out);
            led   = new GPIOMem(ledPin, GPIODirection.Out);

            lastInterruptState = PinState.High;


            interruptThread = new Thread(interruptCheck);

            interruptThread.Priority = ThreadPriority.AboveNormal;



            /* Network manager */
            WriteLine("Listening network on IP: (" + IPAddress + ")");



            /* Bifler listener */
            WriteLine("Starting libBifler listener...");

            netManager = new NetworkManager(IPAddress, true);

            netManager.Initialize(triggerDeviceVersion);

            WriteLine("Successfully listening libBifler.");



            /* Radio listener */
            WriteLine("Starting radio listener...");

            radioManager = new NetworkManager(IPAddress, true, true);

            radioManager.Initialize(triggerDeviceVersion);

            WriteLine("Successfully listening radio.");


            WriteLine("Registering methods...");

            /* Register netManager packet's read methods into their origins. */
            RegisterMethods();
            RegisterRadioMethods();


            WriteLine("Started listening network successfully.");



            WriteLine("Initializing GPS Device...");


            /*var ports = SerialPort.GetPortNames ();
             * WriteLine ("COM Ports (" + ports.Length + "): ");
             *
             * foreach (var port in ports) {
             *      WriteLine ("\t" + port);
             * }
             *
             *
             * if (ports.Length == 1) {
             *      WriteLine ("Creating GPS device on Port: " + ports [0] + "...");
             *
             *      gpsDevice = new GPSHandler (ports [0], sendGPSData);
             *
             *      WriteLine ("SerialPort running for GPS.");
             * }
             */

            /* Run GPIO Scheduler. */

            WriteLine("Starting GPIO Interrupt.");
            interruptThread.Start();
        }
Beispiel #39
0
 public static PinState ReadPin(GPIOPins pin)
 {
     return(bcm2835_gpio_lev(pin) ? PinState.High : PinState.Low);
 }
Beispiel #40
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with the specified initial value
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 /// <param name="initialValue">Initial Value</param>
 public GPIOMem(GPIOPins pin, DirectionEnum direction, bool initialValue)
     : base(pin, direction, initialValue)
 {
     ExportPin(pin, direction);
     Write(pin, initialValue);
 }
Beispiel #41
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 public GPIOMem(GPIOPins pin, DirectionEnum direction)
     : base(pin, direction, false)
 {
     ExportPin(pin, direction);
 }
Beispiel #42
0
 /// <summary>
 /// Access to the specified GPIO setup as an output port with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 public GPIO(GPIOPins pin)
     : this(pin,DirectionEnum.OUT,false)
 {
 }
Beispiel #43
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with the specified initial value
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 /// <param name="initialValue">Initial Value</param>
 public GPIO(GPIOPins pin, DirectionEnum direction, bool initialValue)
 {
     this._pin = pin;
 }
Beispiel #44
0
		static extern bool bcm2835_gpio_lev(GPIOPins pin);
Beispiel #45
0
		/// <summary>
		/// Gets the value of a given pin.
		/// 
		/// Creates (exports) the pin if needed, and sets it to In direction.
		/// </summary>
		/// <param name="pin">The pin who's value to get</param>
		/// <returns>The value of the pin</returns>
		public static bool Read(GPIOPins pin) {
			return CreatePin(pin, GPIODirection.In).Read();
		}
Beispiel #46
0
		static extern void bcm2835_gpio_set_pud(GPIOPins pin, uint pud);
Beispiel #47
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 public GPIO(GPIOPins pin, DirectionEnum direction)
     : this(pin, direction, false)
 {
 }
 public RaspPiGPIOFileLcdTransferProvider(GPIOPins rs, GPIOPins rw, GPIOPins enable, GPIOPins d4, GPIOPins d5, GPIOPins d6, GPIOPins d7)
     : this(true, rs, rw, enable, GPIOPins.GPIO_NONE, GPIOPins.GPIO_NONE, GPIOPins.GPIO_NONE, GPIOPins.GPIO_NONE, d4, d5, d6, d7)
 {
 }
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 public GPIODebug(GPIOPins pin, GPIODirection direction)
     : this(pin, direction, false)
 {
 }
 public RaspPiGPIOFileLcdTransferProvider(GPIOPins rs, GPIOPins rw, GPIOPins enable, GPIOPins d0, GPIOPins d1, GPIOPins d2, GPIOPins d3, GPIOPins d4, GPIOPins d5, GPIOPins d6, GPIOPins d7)
     : this(false, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)
 {
 }
Beispiel #51
0
 /// <summary>
 /// Access to the specified GPIO setup as an output port with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 public GPIODebug(GPIOPins pin)
     : this(pin,GPIODirection.Out,false)
 {
 }
Beispiel #52
0
 protected static string GetGPIONumber(GPIOPins pin)
 {
     return ((int)pin).ToString(); //e.g. returns 17 for enum value of gpio17
 }
Beispiel #53
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with the specified initial value
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 /// <param name="initialValue">Initial Value</param>
 public GPIODebug(GPIOPins pin, GPIODirection direction, bool initialValue)
     : base(pin, direction, initialValue)
 {
 }
Beispiel #54
0
        /// <summary>
        /// Static method to write a value to the specified pin.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <param name="value">The value to write to the pin</param>
        public static void Write(GPIOPins pin, bool value)
        {
            if (pin == GPIOPins.GPIO_NONE)
                return;

            ExportPin(pin, DirectionEnum.OUT);

            bcm2835_gpio_write((uint)pin, value ? (uint)1 : (uint)0);
            Debug.WriteLine("output to pin " + pin + "/gpio " + (int)pin + ", value was " + value);
        }
Beispiel #55
0
 /// <summary>
 /// Static method to write a value to the specified pin. Does nothing when using the GPIODebug class.
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="value">The value to write to the pin</param>
 public static void Write(GPIOPins pin, bool value)
 {
 }
Beispiel #56
0
        /// <summary>
        /// Static method to read a value to the specified pin.
        /// </summary>
        /// <param name="pin">The GPIO pin</param>
        /// <returns>The value read from the pin</returns>
        public static bool Read(GPIOPins pin)
        {
            ExportPin(pin, DirectionEnum.IN);

            uint value = bcm2835_gpio_lev((uint)pin);
            bool returnValue = value == 0 ? false : true;
            Debug.WriteLine("input from pin " + pin + "/gpio " + (int)pin + ", value was " + returnValue);

            return returnValue;
        }
Beispiel #57
0
 /// <summary>
 /// Access to the specified GPIO setup with the specified direction with an initial value of false (0)
 /// </summary>
 /// <param name="pin">The GPIO pin</param>
 /// <param name="direction">Direction</param>
 public GPIOMem(GPIOPins pin, DirectionEnum direction)
     : base(pin, direction, false)
 {
     ExportPin(pin, direction);
 }
Beispiel #58
0
 public static void WritePin(GPIOPins pin, PinState pinState)
 {
     bcm2835_gpio_write(pin, pinState);
 }
Beispiel #59
0
		static extern void bcm2835_gpio_fsel(GPIOPins pin, bool mode_out);
Beispiel #60
0
		static extern void bcm2835_gpio_write(GPIOPins pin, bool value);