public async Task InitController(IGpioControllerDriver?_driver, bool isUnixSys, NumberingScheme _scheme) { if (IsInitSuccess || _driver == null) { return; } IsAllowedToExecute = isUnixSys && Helpers.GetPlatform() == OSPlatform.Linux; if (!IsAllowedToExecute) { Logger.Warning("Running OS platform is unsupported."); return; } PinController.InitPinController(_driver, _scheme); IGpioControllerDriver?driver = PinController.GetDriver(); if (driver == null || !driver.IsDriverInitialized) { Logger.Warning($"{_driver.DriverName} failed to initialize properly. Restart of entire application is recommended."); throw new DriverInitializationFailedException(_driver.DriverName.ToString()); } GeneratePinConfiguration(driver); await InitEvents().ConfigureAwait(false); IsInitSuccess = true; }
internal async Task InitController(GpioControllerDriver?driver, NumberingScheme numberingScheme) { if (IsInitSuccess || driver == null) { return; } if (!IsAllowedToExecute) { Logger.Warn("Running OS platform is unsupported."); return; } PinController.InitPinController(driver); GpioControllerDriver?gpioDriver = PinController.GetDriver(); if (driver == null || !driver.IsDriverInitialized) { Logger.Warn($"{gpioDriver.DriverName} failed to initialize properly. Restart of entire application is recommended."); throw new DriverInitializationFailedException(gpioDriver.DriverName.ToString()); } if (!await PinConfig.LoadConfiguration().ConfigureAwait(false)) { GeneratePinConfiguration(); await PinConfig.SaveConfig().ConfigureAwait(false); } EventGenerator.InitEventGeneration(); IsInitSuccess = true; }
static void set(int pin) { if (Controller == null || PinController == null) { return; } Pin?pinStatus = PinController.GetDriver()?.GetPinConfig(pin); if (pinStatus == null) { return; } if (pinStatus.IsPinOn) { PinController.GetDriver()?.SetGpioValue(pin, GpioPinMode.Output, GpioPinState.Off); Logger.Log($"Successfully set {pin} pin to OFF.", LogLevels.Green); } else { PinController.GetDriver()?.SetGpioValue(pin, GpioPinMode.Output, GpioPinState.On); Logger.Log($"Successfully set {pin} pin to ON.", LogLevels.Green); } }
internal Generator(GpioCore _core, EventConfig _config, ILogger _logger) { Logger = _logger; Core = _core; Config = _config; Driver = PinController.GetDriver() ?? throw new DriverNotInitializedException(); Init(); }
internal GpioCore(PinsWrapper pins, Core core, bool shouldGracefullyShutdown = true) { Core = core ?? throw new ArgumentNullException(nameof(core)); Pins = pins; IsGracefullShutdownRequested = shouldGracefullyShutdown; PinController = new PinController(this); EventGenerator = new InternalEventGenerator(Core, PinController.GetDriver()); MorseTranslator = new MorseRelayTranslator(this); BluetoothController = new BluetoothController(this); SoundController = new SoundController(this); PinConfig = new PinConfig(); }
internal async Task <MorseCycleResult> RelayMorseCycle(string textToConvert, int relayPin) { if (string.IsNullOrEmpty(textToConvert)) { return(new MorseCycleResult(false, null, null)); } if (PinController.GetDriver() == null) { Logger.Warn("Driver isn't started yet."); return(new MorseCycleResult(false, null, null)); } Logger.Trace($"Converting to Morse..."); string morse = MorseCore.ConvertToMorseCode(textToConvert); if (string.IsNullOrEmpty(morse)) { Logger.Warn("Conversion to Morse failed. Cannot proceed."); return(new MorseCycleResult(false, null, null)); } Logger.Trace($"TEXT >> {textToConvert}"); Logger.Trace($"MORSE >> {morse}"); Pin beforePinStatus = PinController.GetDriver().GetPinConfig(relayPin); if (beforePinStatus.IsPinOn) { PinController.GetDriver().SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.Off); } if (!MorseCore.IsValidMorse(morse)) { Logger.Warn("The specified Morse is invalid!"); return(new MorseCycleResult(false, null, null)); } string pauseBetweenLetters = "_"; // One Time Unit string pauseBetweenWords = "_______"; // Seven Time Unit morse = morse.Replace(" ", pauseBetweenWords); morse = morse.Replace(" ", pauseBetweenLetters); char[] morseCharArray = morse.ToCharArray(); for (int i = 0; i < morseCharArray.Length; i++) { char charecter = morseCharArray[i]; switch (charecter) { case '.': PinController.GetDriver().SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.On, TimeSpan.FromMilliseconds(300), true); continue; case '-': PinController.GetDriver().SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.On, TimeSpan.FromMilliseconds(300 * 3), true); continue; case '_': await Task.Delay(300).ConfigureAwait(false); continue; } } Pin afterPinStatus = PinController.GetDriver().GetPinConfig(relayPin); if (afterPinStatus.IsPinOn) { PinController.GetDriver().SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.Off); } return(new MorseCycleResult(false, textToConvert, morse)); }
public async Task ExecuteAsync(Parameter parameter) { if (!IsInitSuccess) { return; } if (!Core.CoreInitiationCompleted) { ShellOut.Error("Cannot execute as the core hasn't been successfully started yet."); return; } if (parameter.Parameters.Length > MaxParameterCount) { ShellOut.Error("Too many arguments."); return; } if (!PiGpioController.IsAllowedToExecute) { ShellOut.Error("Gpio functions are not allowed to execute."); ShellOut.Info("Gpio pin controlling functions are only available on raspberry pi with an OS such as Raspbian."); return; } await Sync.WaitAsync().ConfigureAwait(false); try { if (OnExecuteFunc != null) { if (OnExecuteFunc.Invoke(parameter)) { return; } } if (parameter.Parameters == null || parameter.Parameters.Length == 0) { ShellOut.Error("Gpio pin, Pin state, pin mode values are not specified."); return; } if (string.IsNullOrEmpty(parameter.Parameters[0])) { ShellOut.Error("Gpio pin is invalid or not specified."); return; } int pin; GpioPinMode pinMode; GpioPinState pinState; bool isSet; IGpioControllerDriver?driver = PinController.GetDriver(); if (driver == null || !driver.IsDriverProperlyInitialized) { ShellOut.Error("Internal error occurred with the gpio driver. Please restart the assistant."); return; } switch (parameter.ParameterCount) { case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]): ShellOut.Info("Note: as only 1 argument is specified, the default value will be set for the specified pin."); if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellOut.Error("Failed to parse gpio pin value."); return; } ShellOut.Info($"{pin} will be set to Output mode and configured in On state."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Core.Config.OutputModePins.Contains(pin)) { ShellOut.Error("Specified gpio pin is an invalid."); return; } isSet = driver.TogglePinState(pin); if (!isSet) { ShellOut.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellOut.Info($"Successfully configured {pin} gpio pin."); return; case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]) && parameter.Parameters[0].Equals("relay", StringComparison.OrdinalIgnoreCase): if (!int.TryParse(parameter.Parameters[1], out int relayNum)) { ShellOut.Error("Failed to parse relay number value."); return; } if (!PinController.IsValidPin(PiGpioController.AvailablePins.OutputPins[relayNum])) { ShellOut.Error($"The pin ' {PiGpioController.AvailablePins.OutputPins[relayNum]} ' is invalid."); return; } isSet = driver.TogglePinState(PiGpioController.AvailablePins.OutputPins[relayNum]); if (!isSet) { ShellOut.Error($"Failed to configure {PiGpioController.AvailablePins.OutputPins[relayNum]} gpio pin. Please validate the pin argument."); return; } ShellOut.Info($"Successfully configured {PiGpioController.AvailablePins.OutputPins[relayNum]} gpio pin."); return; case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]): if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellOut.Error("Failed to parse gpio pin value."); return; } if (!int.TryParse(parameter.Parameters[1], out int modeVal)) { ShellOut.Error("Failed to parse gpio pin mode value."); return; } pinMode = (GpioPinMode)modeVal; ShellOut.Info($"{pin} will be set to {pinMode.ToString()} mode and configured in On state."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Core.Config.OutputModePins.Contains(pin)) { ShellOut.Error("Specified gpio pin is an invalid."); return; } isSet = driver.SetGpioValue(pin, pinMode, GpioPinState.On); if (!isSet) { ShellOut.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellOut.Info($"Successfully configured {pin} gpio pin."); return; case 3 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]) && !string.IsNullOrEmpty(parameter.Parameters[2]): if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellOut.Error("Failed to parse gpio pin value."); return; } if (!int.TryParse(parameter.Parameters[1], out int pinModeVal)) { ShellOut.Error("Failed to parse gpio pin mode value."); return; } if (!int.TryParse(parameter.Parameters[2], out int stateVal)) { ShellOut.Error("Failed to parse gpio pin state value."); return; } pinMode = (GpioPinMode)pinModeVal; pinState = (GpioPinState)stateVal; ShellOut.Info($"{pin} will be set to {pinMode.ToString()} mode and configured in {pinState} state."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Core.Config.OutputModePins.Contains(pin)) { ShellOut.Error("Specified gpio pin is an invalid."); return; } isSet = driver.SetGpioValue(pin, pinMode, pinState); if (!isSet) { ShellOut.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellOut.Info($"Successfully configured {pin} gpio pin."); return; case 4 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]) && !string.IsNullOrEmpty(parameter.Parameters[2]) && !string.IsNullOrEmpty(parameter.Parameters[3]): if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellOut.Error("Failed to parse gpio pin value."); return; } if (!int.TryParse(parameter.Parameters[1], out int modeValue)) { ShellOut.Error("Failed to parse gpio pin mode value."); return; } if (!int.TryParse(parameter.Parameters[2], out int stateValue)) { ShellOut.Error("Failed to parse gpio pin state value."); return; } if (!int.TryParse(parameter.Parameters[2], out int delayValue)) { ShellOut.Error("Failed to parse gpio pin state value."); return; } pinMode = (GpioPinMode)modeValue; pinState = (GpioPinState)stateValue; ShellOut.Info($"{pin} will be set to {pinMode.ToString()} mode and configured in {pinState} state and set back by a delay of {delayValue} minutes."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Core.Config.OutputModePins.Contains(pin)) { ShellOut.Error("Specified gpio pin is an invalid."); return; } isSet = driver.SetGpioValue(pin, pinMode, pinState, TimeSpan.FromMinutes(delayValue)); if (!isSet) { ShellOut.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellOut.Info($"Successfully configured {pin} gpio pin."); return; default: ShellOut.Error("Command seems to be in incorrect syntax."); return; } } catch (Exception e) { ShellOut.Exception(e); return; } finally { Sync.Release(); } }