/// <summary> /// Gets raspberry device pin value. /// </summary> /// <param name="pin">Raspberry pin</param> /// <returns></returns> public PinValue GetPinValue(RaspberyPin pin) { if (!this.IsInitialized) { throw new Exception("Raspberry device not initialized"); } try { string pinAddress = pin.Code.ToString().Split('_').Last().Replace("0", ""); double value = 0; if (Environment.OSVersion.Platform == PlatformID.Unix) { value = double.Parse(("cat /sys/class/gpio/gpio" + pinAddress + "/value").Bash()); } if (value > 0) { this._devicePins.Where(p => p.Code == pin.Code).SingleOrDefault().Value = PinValue.High; } else { this._devicePins.Where(p => p.Code == pin.Code).SingleOrDefault().Value = PinValue.Low; } } catch (Exception ex) { throw ex; } return(this._devicePins.Where(p => p.Code == pin.Code).SingleOrDefault().Value); }
/// <summary> /// Initialize raspberry device. /// </summary> public void Initialize() { if (!IsInitialized) { this._devicePins = new List <RaspberyPin>(); foreach (PinCode p in Enum.GetValues(typeof(PinCode))) { RaspberyPin pin = new RaspberyPin(p); this._devicePins.Add(pin); this.InitializePin(pin); } } this.IsInitialized = true; }
/// <summary> /// Initialize raspberry device pin. /// </summary> /// <param name="pin">Raspberry pin</param> private void InitializePin(RaspberyPin pin) { try { string pinAddress = pin.Code.ToString().Split('_').Last().Replace("0", ""); if (pin.Type == PinType.GPIO) { if (Environment.OSVersion.Platform == PlatformID.Unix) { ("echo " + pinAddress + " > /sys/class/gpio/unexport").Bash(); ("echo " + pinAddress + " > /sys/class/gpio/export").Bash(); ("echo out > /sys/class/gpio/gpio" + pinAddress + "/direction").Bash(); } } this._devicePins.Where(p => p.Code == pin.Code).SingleOrDefault().Initialized = true; } catch (Exception ex) { throw ex; } }
/// <summary> /// Sets raspberry device pin value. /// </summary> /// <param name="pin">Raspberry pin</param> /// <param name="pinValue">Raspberry pin value</param> public void SetPinValue(RaspberyPin pin, PinValue pinValue) { if (!this.IsInitialized) { throw new Exception("Raspberry device not initialized"); } if (!RaspberyPin.IsGPIOPin(pin.Code)) { throw new Exception("Can not set pin value to non GPIO pin"); } try { string pinAddress = pin.Code.ToString().Split('_').Last().Replace("0", ""); string value = ""; switch (pinValue) { case PinValue.High: value = "1"; break; case PinValue.Low: value = "0"; break; } if (Environment.OSVersion.Platform == PlatformID.Unix) { ("echo " + value + " > /sys/class/gpio/gpio" + pinAddress + "/value").Bash(); } this._devicePins.Where(p => p.Code == pin.Code).SingleOrDefault().Value = pinValue; } catch (Exception ex) { throw ex; } }
/// <summary> /// Sets raspbery device pin direction. /// </summary> /// <param name="pin">Raspberry pin</param> /// <param name="pinDirection">Raspberry pin direction</param> public void SetPinDirection(RaspberyPin pin, PinDirection pinDirection) { if (!this.IsInitialized) { throw new Exception("Raspberry device not initialized"); } if (!RaspberyPin.IsGPIOPin(pin.Code)) { throw new Exception("Can not set pin direction to non GPIO pin"); } try { string pinAddress = pin.Code.ToString().Split('_').Last().Replace("0", ""); string direction = ""; switch (pinDirection) { case PinDirection.In: direction = "in"; break; case PinDirection.Out: direction = "out"; break; } if (Environment.OSVersion.Platform == PlatformID.Unix) { ("echo " + direction + " > /sys/class/gpio/gpio" + pinAddress + "/direction").Bash(); } this._devicePins.Where(p => p.Code == pin.Code).SingleOrDefault().Direction = pinDirection; } catch (Exception ex) { throw ex; } }
/// <summary> /// Dipose raspbery pin. /// </summary> /// <param name="pin">Raspberry pin</param> private void DisposePin(RaspberyPin pin) { if (!this.IsInitialized) { throw new Exception("Raspberry device not initialized"); } try { string pinAddress = pin.Code.ToString().Split('_').Last().Replace("0", ""); if (pin.Type == PinType.GPIO) { if (Environment.OSVersion.Platform == PlatformID.Unix) { ("echo " + pinAddress + " > /sys/class/gpio/unexport").Bash(); } } this._devicePins.Where(p => p.Code == pin.Code).SingleOrDefault().Initialized = false; } catch (Exception ex) { throw ex; } }