diff --git a/README.md b/README.md index 3fefc37..d61c89a 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,12 @@ You need ExtPlane Plugin installed ([Download](https://github.com/vranki/ExtPlan * __set [landing lights on / landing lights off]__ please * Taxi lights control * __set [taxi lights on / taxi lights off]__ please +* Engine control + * __[start / light up / kill / shut down] the engine number [one / two]]__ please + * __introduce fuel [into / to] number engine [one / two]__ please +* APU control + * __[start / light up / stop / shutdown] APU__ please + * __APU [on / off]__ please __Cheat sheet:__ * __Needed__ diff --git a/X-Plane Voice Control/Commands/ApuControl.cs b/X-Plane Voice Control/Commands/ApuControl.cs new file mode 100644 index 0000000..8ceb915 --- /dev/null +++ b/X-Plane Voice Control/Commands/ApuControl.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Speech.Recognition; +using System.Speech.Synthesis; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using ExtPlaneNet; + +namespace X_Plane_Voice_Control.Commands +{ + class ApuControl : ControlTemplate + { + private readonly string[] _apuOnStrings = { "start", "light up" }; + private readonly string[] _apuOffString = { "stop", "shutdown" }; + private readonly string[] _apuStatus = { "on", "off" }; + + public ApuControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer) + { + var apuGrammar = new GrammarBuilder(); + apuGrammar.Append("please", 0, 1); + apuGrammar.Append(new Choices(_apuOnStrings.Concat(_apuOffString).ToArray()), 0, 1); + apuGrammar.Append("APU"); + apuGrammar.Append(new Choices(_apuStatus), 0, 1); + apuGrammar.Append("please", 0, 1); + Grammar = new Grammar(apuGrammar); + RecognitionPattern = Constants.DeserializeRecognitionPattern(apuGrammar.DebugShowPhrases); + } + + public sealed override Grammar Grammar { get; } + public override string RecognitionPattern { get; } + + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("laminar/B738/switches/apu_start"); + } + + public override void OnTrigger(RecognitionResult rResult, string phrase) + { + var apuStatus = XPlaneInterface.GetDataRef("laminar/B738/switches/apu_start"); + if (_apuOnStrings.Any(phrase.Contains) || phrase.Contains(_apuStatus[0])) + { + XPlaneInterface.SetDataRef(apuStatus.Name, 0); + SpeechSynthesizer.SpeakAsync("APU is starting up"); + } + else if (_apuOffString.Any(phrase.Contains) || phrase.Contains(_apuStatus[1])) + { + XPlaneInterface.SetDataRef(apuStatus.Name, 2); + SpeechSynthesizer.SpeakAsync("APU is shutting down"); + } + } + } +} diff --git a/X-Plane Voice Control/Commands/BrakeControl.cs b/X-Plane Voice Control/Commands/BrakeControl.cs index 2896b8f..b26db2e 100644 --- a/X-Plane Voice Control/Commands/BrakeControl.cs +++ b/X-Plane Voice Control/Commands/BrakeControl.cs @@ -24,13 +24,17 @@ public BrakeControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) brakeGrammar.Append("please", 0, 1); Grammar = new Grammar(brakeGrammar); RecognitionPattern = Constants.DeserializeRecognitionPattern(brakeGrammar.DebugShowPhrases); - XPlaneInterface.Subscribe("laminar/B738/annunciator/parking_brake"); } public sealed override Grammar Grammar { get; } public override string RecognitionPattern { get; } + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("laminar/B738/annunciator/parking_brake"); + } + public override void OnTrigger(RecognitionResult rResult, string phrase) { var value = XPlaneInterface.GetDataRef("laminar/B738/annunciator/parking_brake").Value; diff --git a/X-Plane Voice Control/Commands/ComFrequencyControl.cs b/X-Plane Voice Control/Commands/ComFrequencyControl.cs index 2d52cbe..df328d2 100644 --- a/X-Plane Voice Control/Commands/ComFrequencyControl.cs +++ b/X-Plane Voice Control/Commands/ComFrequencyControl.cs @@ -24,12 +24,16 @@ public ComFrequencyControl(ExtPlaneInterface interface_, SpeechSynthesizer synth frequencyGrammar.Append(Constants.NumberChoices, 1, 3); Grammar = new Grammar(frequencyGrammar); RecognitionPattern = Constants.DeserializeRecognitionPattern(frequencyGrammar.DebugShowPhrases); - XPlaneInterface.Subscribe("sim/cockpit2/radios/actuators/com1_standby_frequency_hz"); - XPlaneInterface.Subscribe("sim/cockpit2/radios/actuators/com2_standby_frequency_hz"); } public sealed override Grammar Grammar { get; } public override string RecognitionPattern { get; } + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("sim/cockpit2/radios/actuators/com1_standby_frequency_hz"); + XPlaneInterface.Subscribe("sim/cockpit2/radios/actuators/com2_standby_frequency_hz"); + } + public override void OnTrigger(RecognitionResult rResult, string phrase) { var index = phrase.IndexOf("com", StringComparison.InvariantCulture); diff --git a/X-Plane Voice Control/Commands/ComSwapControl.cs b/X-Plane Voice Control/Commands/ComSwapControl.cs index 6dcd0ef..9cb76d5 100644 --- a/X-Plane Voice Control/Commands/ComSwapControl.cs +++ b/X-Plane Voice Control/Commands/ComSwapControl.cs @@ -23,6 +23,11 @@ public ComSwapControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesize public override Grammar Grammar { get; } public override string RecognitionPattern { get; } + public override void DataRefSubscribe() + { + + } + public override void OnTrigger(RecognitionResult result, string phrase) { var index = phrase.IndexOf("com", StringComparison.InvariantCulture); diff --git a/X-Plane Voice Control/Commands/ControlTemplate.cs b/X-Plane Voice Control/Commands/ControlTemplate.cs index 2f175ec..d5ed7b4 100644 --- a/X-Plane Voice Control/Commands/ControlTemplate.cs +++ b/X-Plane Voice Control/Commands/ControlTemplate.cs @@ -25,6 +25,8 @@ protected ControlTemplate(ExtPlaneInterface interface_, SpeechSynthesizer synthe public abstract string RecognitionPattern { get; } + public abstract void DataRefSubscribe(); + public abstract void OnTrigger(RecognitionResult result, string phrase); } } diff --git a/X-Plane Voice Control/Commands/EngineFuelControl.cs b/X-Plane Voice Control/Commands/EngineFuelControl.cs new file mode 100644 index 0000000..0dab016 --- /dev/null +++ b/X-Plane Voice Control/Commands/EngineFuelControl.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Speech.Recognition; +using System.Speech.Synthesis; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using ExtPlaneNet; + +namespace X_Plane_Voice_Control.Commands +{ + class EngineFuelControl : ControlTemplate + { + private readonly string[] _engineNumbersStrings = { "one", "two" }; + + public EngineFuelControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer) + { + var engineGrammar = new GrammarBuilder(); + engineGrammar.Append("please", 0, 1); + engineGrammar.Append("introduce fuel"); + engineGrammar.Append(new Choices("into", "to")); + engineGrammar.Append("number", 0, 1); + engineGrammar.Append("engine"); + engineGrammar.Append(new Choices(_engineNumbersStrings)); + engineGrammar.Append("please", 0, 1); + Grammar = new Grammar(engineGrammar); + + RecognitionPattern = Constants.DeserializeRecognitionPattern(engineGrammar.DebugShowPhrases); + } + + public sealed override Grammar Grammar { get; } + public override string RecognitionPattern { get; } + + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("laminar/B738/engine/mixture_ratio2"); + XPlaneInterface.Subscribe("laminar/B738/engine/mixture_ratio1"); + } + + public override void OnTrigger(RecognitionResult rResult, string phrase) + { + var valueOne = XPlaneInterface.GetDataRef("laminar/B738/engine/mixture_ratio1"); + var valueTwo = XPlaneInterface.GetDataRef("laminar/B738/engine/mixture_ratio2"); + if (phrase.Contains(_engineNumbersStrings[0])) + { + XPlaneInterface.SetDataRef(valueOne.Name, 1); + SpeechSynthesizer.SpeakAsync("Introducing fuel into engine number one"); + } + else if (phrase.Contains(_engineNumbersStrings[1])) + { + XPlaneInterface.SetDataRef(valueTwo.Name, 1); + SpeechSynthesizer.SpeakAsync("Introducing fuel into engine number two"); + } + + } + } +} diff --git a/X-Plane Voice Control/Commands/EngineStartUp.cs b/X-Plane Voice Control/Commands/EngineStartUp.cs new file mode 100644 index 0000000..8209f3a --- /dev/null +++ b/X-Plane Voice Control/Commands/EngineStartUp.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Speech.Recognition; +using System.Speech.Synthesis; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using ExtPlaneNet; + +namespace X_Plane_Voice_Control.Commands +{ + class EngineStartUpControl : ControlTemplate + { + private readonly string[] _engineNumbersStrings = { "one", "two" }; + private readonly string[] _engineStartStrings = { "start", "light up" }; + private readonly string[] _engineShutdownStrings = { "kill", "shut down" }; + + public EngineStartUpControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : base(interface_, synthesizer) + { + var engineGrammar = new GrammarBuilder(); + engineGrammar.Append("please", 0, 1); + engineGrammar.Append(new Choices(_engineStartStrings.Concat(_engineShutdownStrings).ToArray())); + engineGrammar.Append("the", 0, 1); + engineGrammar.Append("engine"); + engineGrammar.Append("number", 0, 1); + engineGrammar.Append(new Choices(_engineNumbersStrings)); + engineGrammar.Append("please", 0, 1); + Grammar = new Grammar(engineGrammar); + RecognitionPattern = Constants.DeserializeRecognitionPattern(engineGrammar.DebugShowPhrases); + } + + public sealed override Grammar Grammar { get; } + public override string RecognitionPattern { get; } + + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("laminar/B738/engine/starter1_pos"); + XPlaneInterface.Subscribe("laminar/B738/engine/starter2_pos"); + XPlaneInterface.Subscribe("laminar/B738/engine/mixture_ratio2"); + XPlaneInterface.Subscribe("laminar/B738/engine/mixture_ratio1"); + } + + public override void OnTrigger(RecognitionResult rResult, string phrase) + { + var valueOne = XPlaneInterface.GetDataRef("laminar/B738/engine/starter1_pos"); + var valueTwo = XPlaneInterface.GetDataRef("laminar/B738/engine/starter2_pos"); + if (phrase.Contains(_engineNumbersStrings[0])) + { + if (_engineStartStrings.Any(phrase.Contains)) + { + XPlaneInterface.SetDataRef(valueOne.Name, 0); + SpeechSynthesizer.SpeakAsync("Starting engine number one"); + } + else if (_engineShutdownStrings.Any(phrase.Contains)) + { + XPlaneInterface.SetDataRef("laminar/B738/engine/mixture_ratio1", 0); + SpeechSynthesizer.SpeakAsync("Shutting down engine number one"); + } + } + else if (phrase.Contains(_engineNumbersStrings[1])) + { + if (_engineStartStrings.Any(phrase.Contains)) + { + XPlaneInterface.SetDataRef(valueTwo.Name, 0); + SpeechSynthesizer.SpeakAsync("Starting engine number two"); + } + else if (_engineShutdownStrings.Any(phrase.Contains)) + { + XPlaneInterface.SetDataRef("laminar/B738/engine/mixture_ratio2", 0); + SpeechSynthesizer.SpeakAsync("Shutting down engine number two"); + } + } + + } + } +} diff --git a/X-Plane Voice Control/Commands/FlapControl.cs b/X-Plane Voice Control/Commands/FlapControl.cs index 8282bbb..78181e8 100644 --- a/X-Plane Voice Control/Commands/FlapControl.cs +++ b/X-Plane Voice Control/Commands/FlapControl.cs @@ -27,13 +27,17 @@ public FlapControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) flapGrammar.Append(_aNotch, 0, 1); flapGrammar.Append("please", 0, 1); Grammar = new Grammar(flapGrammar); - XPlaneInterface.Subscribe("sim/flightmodel/controls/flaprqst"); RecognitionPattern = Constants.DeserializeRecognitionPattern(flapGrammar.DebugShowPhrases); } public sealed override Grammar Grammar { get; } public override string RecognitionPattern { get; } + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("sim/flightmodel/controls/flaprqst"); + } + public override void OnTrigger(RecognitionResult rResult, string phrase) { var value = XPlaneInterface.GetDataRef("sim/flightmodel/controls/flaprqst").Value; diff --git a/X-Plane Voice Control/Commands/GearControl.cs b/X-Plane Voice Control/Commands/GearControl.cs index 25b6009..13a0560 100644 --- a/X-Plane Voice Control/Commands/GearControl.cs +++ b/X-Plane Voice Control/Commands/GearControl.cs @@ -17,20 +17,23 @@ public GearControl(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) gearGrammar.Append(new Choices("gear up", "gear down")); gearGrammar.Append("please", 0, 1); Grammar = new Grammar(gearGrammar); - XPlaneInterface.Subscribe("laminar/B738/switches/landing_gear"); + RecognitionPattern = Constants.DeserializeRecognitionPattern(gearGrammar.DebugShowPhrases); + } + + public sealed override Grammar Grammar { get; } + public override string RecognitionPattern { get; } + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("laminar/B738/switches/landing_gear"); XPlaneInterface.Subscribe("laminar/B738/annunciator/nose_gear_transit"); XPlaneInterface.Subscribe("laminar/B738/annunciator/nose_gear_safe"); XPlaneInterface.Subscribe("laminar/B738/annunciator/left_gear_transit"); XPlaneInterface.Subscribe("laminar/B738/annunciator/left_gear_safe"); XPlaneInterface.Subscribe("laminar/B738/annunciator/right_gear_transit"); XPlaneInterface.Subscribe("laminar/B738/annunciator/right_gear_safe"); - RecognitionPattern = Constants.DeserializeRecognitionPattern(gearGrammar.DebugShowPhrases); } - public sealed override Grammar Grammar { get; } - public override string RecognitionPattern { get; } - public override void OnTrigger(RecognitionResult rResult, string phrase) { double valueToSet = phrase.Contains("up") ? 0 : 2; diff --git a/X-Plane Voice Control/Commands/LandingLights.cs b/X-Plane Voice Control/Commands/LandingLights.cs index fe47ca7..69f76da 100644 --- a/X-Plane Voice Control/Commands/LandingLights.cs +++ b/X-Plane Voice Control/Commands/LandingLights.cs @@ -27,13 +27,17 @@ public LandingLights(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer landingLightsGrammar.Append(new Choices(_landingLightsOnStrings.Concat(_landingLiggOffStrings).ToArray())); landingLightsGrammar.Append("please", 0, 1); Grammar = new Grammar(landingLightsGrammar); - XPlaneInterface.Subscribe("sim/cockpit2/switches/landing_lights_switch"); RecognitionPattern = Constants.DeserializeRecognitionPattern(landingLightsGrammar.DebugShowPhrases); } public sealed override Grammar Grammar { get; } public override string RecognitionPattern { get; } + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("sim/cockpit2/switches/landing_lights_switch"); + } + public override void OnTrigger(RecognitionResult rResult, string phrase) { var dataref = XPlaneInterface.GetDataRef("sim/cockpit2/switches/landing_lights_switch"); diff --git a/X-Plane Voice Control/Commands/TaxiLights.cs b/X-Plane Voice Control/Commands/TaxiLights.cs index 6690c99..5a43b2e 100644 --- a/X-Plane Voice Control/Commands/TaxiLights.cs +++ b/X-Plane Voice Control/Commands/TaxiLights.cs @@ -24,13 +24,17 @@ public TaxiLights(ExtPlaneInterface interface_, SpeechSynthesizer synthesizer) : brakeGrammar.Append(new Choices(_taxiLightsOnStrings.Concat(_taxiLightsOffStrings).ToArray())); brakeGrammar.Append("please", 0, 1); Grammar = new Grammar(brakeGrammar); - XPlaneInterface.Subscribe("laminar/B738/toggle_switch/taxi_light_brightness_pos"); RecognitionPattern = Constants.DeserializeRecognitionPattern(brakeGrammar.DebugShowPhrases); } public sealed override Grammar Grammar { get; } public override string RecognitionPattern { get; } + public override void DataRefSubscribe() + { + XPlaneInterface.Subscribe("laminar/B738/toggle_switch/taxi_light_brightness_pos"); + } + public override void OnTrigger(RecognitionResult rResult, string phrase) { var value = XPlaneInterface.GetDataRef("laminar/B738/toggle_switch/taxi_light_brightness_pos").Value; diff --git a/X-Plane Voice Control/MainForm.cs b/X-Plane Voice Control/MainForm.cs index ca2ce4e..8949431 100644 --- a/X-Plane Voice Control/MainForm.cs +++ b/X-Plane Voice Control/MainForm.cs @@ -47,6 +47,12 @@ private void ButtonListen_Click(object sender, EventArgs e) continue; var instance = (ControlTemplate)Activator.CreateInstance(type, _extPlaneInterface, _synthesizer); + try { instance.DataRefSubscribe(); } + catch + { + // ignored + } + Console.WriteLine(instance.RecognitionPattern); _abstractCommands.Add(instance); _speechRecognitionEngine.LoadGrammarAsync(instance.Grammar); @@ -67,10 +73,9 @@ private void SpeechRecognize_SpeechRecognized(object sender, SpeechRecognizedEve { var class_ = _abstractCommands.First(x => x.Grammar == e.Result.Grammar); class_.OnTrigger(e.Result, e.Result.Text); - SetLabelText(e.Result.Text); + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Recognized text: {e.Result.Text}. Calling Class {class_.GetType().Name}"); + Console.ForegroundColor = ConsoleColor.White; } - - public void SetLabelText(string text) => label1.Text = text; } } diff --git a/X-Plane Voice Control/X-Plane Voice Control.csproj b/X-Plane Voice Control/X-Plane Voice Control.csproj index 6117097..6ab9e77 100644 --- a/X-Plane Voice Control/X-Plane Voice Control.csproj +++ b/X-Plane Voice Control/X-Plane Voice Control.csproj @@ -53,8 +53,11 @@ + + +