示例#1
0
 public GpioPinController(PiController controller) => Controller = controller;
示例#2
0
 public GpioEventManager(PiController piController, GpioPinController pinController)
 {
     PiController  = piController;
     PinController = pinController;
 }
示例#3
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 (Controller == null)
            {
                Logger.Log("Malfunctioning PinController.", LogLevels.Warn);
                return(false);
            }

            if (!PiController.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}");

            GpioPinConfig beforePinStatus = Controller.GetGpioConfig(relayPin);

            if (beforePinStatus.IsPinOn)
            {
                Controller.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 '.':
                    Controller.SetGpioWithTimeout(relayPin, GpioPinMode.Output, GpioPinState.On, TimeSpan.FromMilliseconds(300));
                    break;

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

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

                    break;
                }
            }

            GpioPinConfig afterPinStatus = Controller.GetGpioConfig(relayPin);

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

            return(true);
        }