public async Task <bool> RelayMorseCycle(string textToConvert, int relayPin)
        {
            if (Helpers.IsNullOrEmpty(textToConvert))
            {
                Logger.Log("The specified text is either null or empty.", Enums.LogLevels.Warn);
                return(false);
            }

            if (relayPin <= 0)
            {
                Logger.Log("Please specify a valid relay pin to run the cycle.", Enums.LogLevels.Warn);
                return(false);
            }

            Logger.Log($"Converting {textToConvert} to morse...", Enums.LogLevels.Trace);
            string Morse = MorseCore.ConvertToMorseCode(textToConvert);

            if (Helpers.IsNullOrEmpty(Morse))
            {
                Logger.Log("Conversion to morse failed. cannot proceed.", Enums.LogLevels.Warn);
                return(false);
            }

            Logger.Log($"TEXT >> {textToConvert}");
            Logger.Log($"MORSE >> {Morse}");

            if (Core.Config.RelayPins != null && Core.Config.RelayPins.Count() > 0)
            {
                foreach (int pin in Core.Config.RelayPins)
                {
                    if (pin.Equals(relayPin))
                    {
                        GpioPinConfig pinStatus = Controller.GetPinConfig(pin);

                        if (pinStatus.PinValue == GpioPinValue.Low)
                        {
                            Controller.SetGpioValue(pin, GpioPinDriveMode.Output, GpioPinValue.High);
                        }

                        break;
                    }
                }
            }

            if (!MorseCore.IsValidMorse(Morse))
            {
                Logger.Log("The specified morse is not valid!", Enums.LogLevels.Warn);
                return(false);
            }

            string pauseBetweenLetters = "_";                 // One Time Unit
            string pauseBetweenWords   = "_______";           // Seven Time Unit

            Morse = Morse.Replace("  ", pauseBetweenWords);
            Morse = Morse.Replace(" ", pauseBetweenLetters);

            foreach (char character in Morse.ToCharArray())
            {
                switch (character)
                {
                case '.':
                    Controller.SetGpioWithTimeout(relayPin, GpioPinDriveMode.Output, GpioPinValue.Low, TimeSpan.FromMilliseconds(300));
                    break;

                case '-':
                    Controller.SetGpioWithTimeout(relayPin, GpioPinDriveMode.Output, GpioPinValue.Low, TimeSpan.FromMilliseconds(300 * 3));
                    break;

                case '_':
                    await Task.Delay(300).ConfigureAwait(false);

                    break;
                }
            }

            if (Core.Config.RelayPins != null && Core.Config.RelayPins.Count() > 0)
            {
                foreach (int pin in Core.Config.RelayPins)
                {
                    GpioPinConfig pinStatus = Controller.GetPinConfig(pin);

                    if (pinStatus.PinValue == GpioPinValue.Low)
                    {
                        Controller.SetGpioValue(pin, GpioPinDriveMode.Output, GpioPinValue.High);
                    }
                }
            }

            return(true);
        }
示例#2
0
        public async Task <bool> RelayMorseCycle(string textToConvert, int relayPin)
        {
            if (string.IsNullOrEmpty(textToConvert))
            {
                Logger.Log("The specified text is either null or empty.", LogLevels.Warn);
                return(false);
            }

            if (Driver == null)
            {
                Logger.Log("Malfunctioning PinController.", LogLevels.Warn);
                return(false);
            }

            if (!PinController.IsValidPin(relayPin))
            {
                Logger.Log("Please specify a valid relay pin to run the cycle.", LogLevels.Warn);
                return(false);
            }

            Logger.Log($"Converting to Morse...", LogLevels.Info);
            string Morse = MorseCore.ConvertToMorseCode(textToConvert);

            if (string.IsNullOrEmpty(Morse))
            {
                Logger.Log("Conversion to Morse failed. cannot proceed.", LogLevels.Warn);
                return(false);
            }

            Logger.Log($"TEXT >> {textToConvert}");
            Logger.Log($"MORSE >> {Morse}");

            Pin beforePinStatus = Driver.GetPinConfig(relayPin);

            if (beforePinStatus.IsPinOn)
            {
                Driver.SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.Off);
            }

            if (!MorseCore.IsValidMorse(Morse))
            {
                Logger.Log("The specified Morse is invalid!", LogLevels.Warn);
                return(false);
            }

            string pauseBetweenLetters = "_";                 // One Time Unit
            string pauseBetweenWords   = "_______";           // Seven Time Unit

            Morse = Morse.Replace("  ", pauseBetweenWords);
            Morse = Morse.Replace(" ", pauseBetweenLetters);

            foreach (char character in Morse.ToCharArray())
            {
                switch (character)
                {
                case '.':
                    Driver.SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.On, TimeSpan.FromMilliseconds(300));
                    break;

                case '-':
                    Driver.SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.On, TimeSpan.FromMilliseconds(300 * 3));
                    break;

                case '_':
                    await Task.Delay(300).ConfigureAwait(false);

                    break;
                }
            }

            Pin afterPinStatus = Driver.GetPinConfig(relayPin);

            if (afterPinStatus.IsPinOn)
            {
                Driver.SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.Off);
            }

            return(true);
        }
示例#3
0
        public async Task ExecuteAsync(Parameter parameter)
        {
            if (!IsInitSuccess)
            {
                return;
            }

            if (parameter.Parameters.Length > MaxParameterCount)
            {
                ShellIO.Error("Too many arguments.");
                return;
            }

            await Sync.WaitAsync().ConfigureAwait(false);

            MorseCore morseCore = MorseRelayTranslator.GetCore();
            string    morse     = string.Empty;

            try {
                if (OnExecuteFunc != null)
                {
                    if (OnExecuteFunc.Invoke(parameter))
                    {
                        return;
                    }
                }

                switch (parameter.ParameterCount)
                {
                case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]):
                    morse = morseCore.ConvertToMorseCode(parameter.Parameters[0]);

                    if (string.IsNullOrEmpty(morse) || !morseCore.IsValidMorse(morse))
                    {
                        ShellIO.Error("Failed to verify generated morse code.");
                        return;
                    }

                    ShellIO.Info(">>> " + morse);
                    return;

                case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]):
                    morse = morseCore.ConvertToMorseCode(parameter.Parameters[0]);

                    if (string.IsNullOrEmpty(morse) || !morseCore.IsValidMorse(morse))
                    {
                        ShellIO.Error("Failed to verify generated morse code.");
                        return;
                    }

                    ShellIO.Info(">>> " + morse);
                    int relayNumber;

                    try {
                        if (!int.TryParse(parameter.Parameters[1], out relayNumber))
                        {
                            ShellIO.Error("Relay number argument is invalid.");
                            return;
                        }
                    }
                    catch (IndexOutOfRangeException) {
                        ShellIO.Error("The specified relay number is invalid is greater than all the available relay numbers.");
                        return;
                    }

                    if (!PinController.IsValidPin(Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNumber]))
                    {
                        ShellIO.Error("The specified pin is invalid.");
                        return;
                    }

                    await Program.CoreInstance.GetGpioCore().GetMorseTranslator().RelayMorseCycle(morse, Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNumber]).ConfigureAwait(false);

                    ShellIO.Info("Completed!");
                    return;

                default:
                    ShellIO.Error("Command seems to be in incorrect syntax.");
                    return;
                }
            }
            catch (Exception e) {
                ShellIO.Exception(e);
                return;
            }
            finally {
                Sync.Release();
            }
        }
        public async Task ExecuteAsync(Parameter parameter)
        {
            if (!IsInitSuccess)
            {
                return;
            }

            if (parameter.Parameters.Length > MaxParameterCount)
            {
                ShellOut.Error("Too many arguments.");
                return;
            }

            await Sync.WaitAsync().ConfigureAwait(false);

            MorseCore morseCore = GpioMorseTranslator.GetCore();
            string    morse     = string.Empty;

            try {
                if (OnExecuteFunc != null)
                {
                    if (OnExecuteFunc.Invoke(parameter))
                    {
                        return;
                    }
                }

                switch (parameter.ParameterCount)
                {
                case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]):
                    morse = morseCore.ConvertToMorseCode(parameter.Parameters[0]);

                    if (string.IsNullOrEmpty(morse) || !morseCore.IsValidMorse(morse))
                    {
                        ShellOut.Error("Failed to verify generated morse code.");
                        return;
                    }

                    ShellOut.Info(">>> " + morse);
                    return;

                case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]):
                    morse = morseCore.ConvertToMorseCode(parameter.Parameters[0]);

                    if (string.IsNullOrEmpty(morse) || !morseCore.IsValidMorse(morse))
                    {
                        ShellOut.Error("Failed to verify generated morse code.");
                        return;
                    }

                    ShellOut.Info(">>> " + morse);
                    GpioMorseTranslator?translator = PiGpioController.GetMorseTranslator();

                    if (translator == null || !translator.IsTranslatorOnline)
                    {
                        ShellOut.Error("Morse translator might be offline.");
                        return;
                    }

                    if (!int.TryParse(parameter.Parameters[1], out int relayNumber))
                    {
                        ShellOut.Error("Relay number argument is invalid.");
                        return;
                    }

                    if (!PinController.IsValidPin(PiGpioController.AvailablePins.OutputPins[relayNumber]))
                    {
                        ShellOut.Error("The specified pin is invalid.");
                        return;
                    }

                    await translator.RelayMorseCycle(morse, PiGpioController.AvailablePins.OutputPins[relayNumber]);

                    ShellOut.Info("Completed!");
                    return;

                default:
                    ShellOut.Error("Command seems to be in incorrect syntax.");
                    return;
                }
            }
            catch (Exception e) {
                ShellOut.Exception(e);
                return;
            }
            finally {
                Sync.Release();
            }
        }
示例#5
0
        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));
        }