diff --git a/MIDI-Keyboard/Program.cs b/MIDI-Keyboard/Program.cs index b394bc2..b7d30ce 100644 --- a/MIDI-Keyboard/Program.cs +++ b/MIDI-Keyboard/Program.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace keyBordL +namespace MIDIKeyboard { // Token: 0x02000002 RID: 2 internal class Program @@ -14,36 +14,30 @@ internal class Program private static void Main(string[] args) { if (args.Length > 0 && args[0] == "run") { - Program.runPogram = true; + Program.runProgram = true; } Console.Title = "MIDI KEYS"; - while (Program.runForm) - { + while (runForm) { Console.Clear(); - if (Program.runFastSetup) - { + if (runFastSetup) { FastSetup.FastSetup_(runFastSetup); runFastSetup = false; } - if (Program.runPogram) - { - Run.Run_(runPogram); - runPogram = false; + if (runProgram) { + Run.Run.Run_(runProgram); + runProgram = false; } - if (Program.runViewer) - { + if (runViewer) { Viewer.Viewer_(runViewer); runViewer = false; } - if (Program.runOutViewer) - { + if (runOutViewer) { OutViewer.OutViewer_(runOutViewer); runOutViewerVisual = false; } - if (Program.runOutViewerVisual) - { + if (runOutViewerVisual) { OutViewerVisual.OutViewerVisual_(runOutViewerVisual); runOutViewerVisual = false; } @@ -57,8 +51,7 @@ private static void Main(string[] args) Console.Write("2. setup\n3. visual setup"); Console.ResetColor(); Console.Write("\n4. run\n5. viewer\n6. viewer output\n7. viewer output visual\n8. quit\n"); - switch (Console.ReadKey().Key) - { + switch (Console.ReadKey().Key) { case ConsoleKey.D1: Program.runFastSetup = true; break; @@ -69,7 +62,7 @@ private static void Main(string[] args) Program.runVisualSetup = true; break; case ConsoleKey.D4: - Program.runPogram = true; + Program.runProgram = true; break; case ConsoleKey.D5: Program.runViewer = true; @@ -91,8 +84,7 @@ private static void Main(string[] args) public static int GetTeken() { Program.teken++; - switch (Program.teken) - { + switch (Program.teken) { case 1: return 9; case 2: @@ -270,7 +262,7 @@ public static int GetTeken() private static bool runVisualSetup = false; #pragma warning restore IDE0052 // Remove unread private members - private static bool runPogram = false; + private static bool runProgram = false; private static bool runViewer = false; private static bool runOutViewer = false; diff --git a/MIDI-Keyboard/dataFolder/midi.cs b/MIDI-Keyboard/dataFolder/midi.cs index 5c716d2..c738868 100644 --- a/MIDI-Keyboard/dataFolder/midi.cs +++ b/MIDI-Keyboard/dataFolder/midi.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.InteropServices; -namespace keyBordL.dataFolder +namespace MIDIKeyboard.dataFolder { public class InputPort { diff --git a/MIDI-Keyboard/miscellaneous/Miscellaneous.cs b/MIDI-Keyboard/miscellaneous/Miscellaneous.cs index 5d218e7..e3f746d 100644 --- a/MIDI-Keyboard/miscellaneous/Miscellaneous.cs +++ b/MIDI-Keyboard/miscellaneous/Miscellaneous.cs @@ -1,4 +1,6 @@ -namespace MIDIKeyboard.miscellaneous +using System; + +namespace MIDIKeyboard.miscellaneous { static class Miscellaneous { @@ -10,5 +12,26 @@ static public int GetID(char c) return cId; } + + public static void Error(string s) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(s); + Console.ResetColor(); + Console.WriteLine("Press any key to ignore and continue..."); + Console.ReadKey(); + } + public static void Error(string s, object[] os) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(s, os); + Console.ResetColor(); + Console.WriteLine("Press any key to ignore and continue..."); + Console.ReadKey(); + } + public static void Error(string s, object o1) => Error(s, new object[] { o1 }); + public static void Error(string s, object o1, object o2) => Error(s, new object[] { o1, o2 }); + public static void Error(string s, object o1, object o2, object o3) => Error(s, new object[] { o1, o2, o3 }); + public static void Error(string s, object o1, object o2, object o3, object o4) => Error(s, new object[] { o1, o2, o3, o4 }); } } diff --git a/MIDI-Keyboard/programChapter/FastSetup.cs b/MIDI-Keyboard/programChapter/FastSetup.cs index b516d38..a5dba59 100644 --- a/MIDI-Keyboard/programChapter/FastSetup.cs +++ b/MIDI-Keyboard/programChapter/FastSetup.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.IO; -using keyBordL.dataFolder; +using MIDIKeyboard.dataFolder; -namespace keyBordL +namespace MIDIKeyboard { class FastSetup { diff --git a/MIDI-Keyboard/programChapter/OutViewer.cs b/MIDI-Keyboard/programChapter/OutViewer.cs index 789fcbe..ca313e0 100644 --- a/MIDI-Keyboard/programChapter/OutViewer.cs +++ b/MIDI-Keyboard/programChapter/OutViewer.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.IO; -using keyBordL.dataFolder; +using MIDIKeyboard.dataFolder; -namespace keyBordL +namespace MIDIKeyboard { class OutViewer { diff --git a/MIDI-Keyboard/programChapter/OutViewerVisual.cs b/MIDI-Keyboard/programChapter/OutViewerVisual.cs index 2f771f2..1b1901c 100644 --- a/MIDI-Keyboard/programChapter/OutViewerVisual.cs +++ b/MIDI-Keyboard/programChapter/OutViewerVisual.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.IO; using System.Text; -using keyBordL.dataFolder; +using MIDIKeyboard.dataFolder; -namespace keyBordL +namespace MIDIKeyboard { class OutViewerVisual { diff --git a/MIDI-Keyboard/programChapter/Viewer.cs b/MIDI-Keyboard/programChapter/Viewer.cs index d9c285e..0fbe396 100644 --- a/MIDI-Keyboard/programChapter/Viewer.cs +++ b/MIDI-Keyboard/programChapter/Viewer.cs @@ -1,13 +1,13 @@ using System; using System.Text; -using keyBordL.dataFolder; +using MIDIKeyboard.dataFolder; -namespace keyBordL +namespace MIDIKeyboard { class Viewer { - private static InputPort midi = new InputPort(); + private static readonly InputPort midi = new InputPort(); public static bool Viewer_(bool runViewer) { @@ -16,8 +16,7 @@ public static bool Viewer_(bool runViewer) Console.WriteLine("what midi port do you whant to use"); { Console.ForegroundColor = ConsoleColor.Black; - for (int i = 0; i < midi.InputCount(); i++) - { + for (int i = 0; i < midi.InputCount(); i++) { if (i % 2 == 0) Console.BackgroundColor = ConsoleColor.Gray; else @@ -28,24 +27,18 @@ public static bool Viewer_(bool runViewer) Console.ResetColor(); } Console.Write("port: "); - int resultat2; - int.TryParse(Console.ReadLine(), out resultat2); + int.TryParse(Console.ReadLine(), out int resultat2); Console.WriteLine("value set to " + resultat2); Console.WriteLine("press ESC to exit"); int chanel = resultat2; midi.Open(chanel); midi.Start(); int old = 0; - int value = 0; - string valueHex = ""; - string hex4 = "0000"; bool sw = false; - while (runViewer) - { - if (Console.KeyAvailable) - { + while (runViewer) { + if (Console.KeyAvailable) { StringBuilder sb = new StringBuilder(); @@ -67,10 +60,8 @@ public static bool Viewer_(bool runViewer) byte bkey = (byte)key.KeyChar; if (bkey > 96 && bkey < 123) bkey -= 32; - else - { - switch (key.Key) - { + else { + switch (key.Key) { case ConsoleKey.Backspace: bkey = 8; break; @@ -411,20 +402,18 @@ public static bool Viewer_(bool runViewer) } - value = midi.p; - if (old != value) - { + int value = midi.p; + if (old != value) { sw = false; - valueHex = midi.pS; + string valueHex = midi.pS; - hex4 = valueHex.Substring(valueHex.Length - 4); + string hex4 = valueHex.Substring(valueHex.Length - 4); - if (hex4.Substring(hex4.Length - 2) == "D0") - { + if (hex4.Substring(hex4.Length - 2) == "D0") { Console.ForegroundColor = ConsoleColor.Gray; Console.Write(valueHex.PadLeft(6, ' ').Substring(0, 4)); Console.ForegroundColor = ConsoleColor.DarkYellow; @@ -432,9 +421,7 @@ public static bool Viewer_(bool runViewer) Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(value); Console.ForegroundColor = ConsoleColor.White; - } - else - { + } else { /* if (valueHex.Length > 4) { diff --git a/MIDI-Keyboard/programChapter/run/LoadData.cs b/MIDI-Keyboard/programChapter/run/LoadData.cs new file mode 100644 index 0000000..2c33f01 --- /dev/null +++ b/MIDI-Keyboard/programChapter/run/LoadData.cs @@ -0,0 +1,103 @@ +using System; +using System.IO; +using System.Text; +using MIDIKeyboard.dataFolder; +using static MIDIKeyboard.miscellaneous.Miscellaneous; + +namespace MIDIKeyboard.Run +{ + class LoadData + { + public static bool Load() => LoadFile() && SendOutputData() & LoadKeyData(); + + static string[] array; + + static bool LoadFile(string path = "data.txt") + { + Console.Write("Loading data from path \"{0}\"...", path); + if (File.Exists(path) && (array = File.ReadAllLines(path)).Length > 2) { + Console.WriteLine(" Done."); + return true; + } + Console.WriteLine(" Error."); + Error("Error -> No data."); + return false; + } + + static bool SendOutputData() + { + if (array[0].Split(',').Length > 1) { + Console.Write("Sending output data..."); + string[] ColorArray = array[0].Split(','); + + InputPort midiOut = new InputPort(); + midiOut.OpenOut(int.Parse(ColorArray[1].Trim())); + + //send output + for (int i = 2; i + 1 < ColorArray.Length; i += 2) { + midiOut.MidiOutMsg((byte)int.Parse(ColorArray[i].Trim()), (byte)int.Parse(ColorArray[i + 1].Trim())); + } + + midiOut.CloseOut(); + Console.WriteLine(" Done."); + return true; + } + Console.WriteLine("No output data detected."); + return true; + } + + static bool LoadKeyData() + { + bool noErrors = true; + Console.WriteLine("loading from file..."); + int chanel = int.Parse(array[0].Split(',')[0]); + + for (int lineID = 0; lineID < array.Length; lineID++) { + if (array[lineID] != string.Concat(chanel)) { + string[] loadedValues = array[lineID].Split(new char[] { ',' }); + int[] tempValues = new int[loadedValues.Length]; + StringBuilder log = new StringBuilder(); + for (int i = 0; i < tempValues.Length; i++) { + string value = loadedValues[i].Trim(); + if (!int.TryParse(value, out tempValues[i])) { + string[] convertValue; + if ((convertValue = value.Split('\'')).Length > 1) { + tempValues[i] = GetID(convertValue[1][0]); + if (convertValue[0][convertValue[0].Length - 1] == '-') { + if (i < 2 || i > 1 && tempValues[1] != 1) { + noErrors = false; + Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value); + } else + tempValues[i] = -tempValues[i]; + } + } else if ((convertValue = value.Split('"')).Length > 1) { + tempValues[i] = GetID(convertValue[1][0]); + if (convertValue[0][convertValue[0].Length - 1] == '-') { + if (i < 2 || i > 1 && tempValues[1] != 1) { + noErrors = false; + Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value); + } else + tempValues[i] = -tempValues[i]; + } + } else if (value.Length == 1) { + tempValues[i] = GetID(value[0]); + } else { + if (value.Length == 2 && value[0] == '-') { + tempValues[i] = -GetID(value[1]); + } else { + noErrors = false; + Error("Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineID, array[lineID], value.Length > 1 ? "s" : "", value); + } + } + } + log.Append(value); + log.Append(' '); + } + Program.values.Add(tempValues); + Console.WriteLine(log); + } + } + return noErrors; + } + } +} diff --git a/MIDI-Keyboard/programChapter/Run.cs b/MIDI-Keyboard/programChapter/run/Run.cs similarity index 90% rename from MIDI-Keyboard/programChapter/Run.cs rename to MIDI-Keyboard/programChapter/run/Run.cs index e8aaf98..4c98025 100644 --- a/MIDI-Keyboard/programChapter/Run.cs +++ b/MIDI-Keyboard/programChapter/run/Run.cs @@ -1,12 +1,12 @@ using System; using System.IO; using System.Text; -using keyBordL.dataFolder; +using MIDIKeyboard.dataFolder; using WindowsInput; using WindowsInput.Native; using static MIDIKeyboard.miscellaneous.Miscellaneous; -namespace keyBordL +namespace MIDIKeyboard.Run { class Run { @@ -230,26 +230,7 @@ private static string AddonButton(bool keyStatus) return (" + " + addonButtonKey.ToString()); } - private static void Error(string s) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(s); - Console.ResetColor(); - Console.WriteLine("Press any key to ignore and continue..."); - Console.ReadKey(); - } - private static void Error(string s, object[] os) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(s, os); - Console.ResetColor(); - Console.WriteLine("Press any key to ignore and continue..."); - Console.ReadKey(); - } - private static void Error(string s, object o1) => Error(s, new object[] { o1 }); - private static void Error(string s, object o1, object o2) => Error(s, new object[] { o1, o2 }); - private static void Error(string s, object o1, object o2, object o3) => Error(s, new object[] { o1, o2, o3 }); - private static void Error(string s, object o1, object o2, object o3, object o4) => Error(s, new object[] { o1, o2, o3, o4 }); + } }