Skip to content

Commit a16a0ce

Browse files
Improved readability
1 parent 7f63710 commit a16a0ce

File tree

9 files changed

+166
-80
lines changed

9 files changed

+166
-80
lines changed

MIDI-Keyboard/Program.cs

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace keyBordL
4+
namespace MIDIKeyboard
55
{
66
// Token: 0x02000002 RID: 2
77
internal class Program
@@ -14,36 +14,30 @@ internal class Program
1414
private static void Main(string[] args)
1515
{
1616
if (args.Length > 0 && args[0] == "run") {
17-
Program.runPogram = true;
17+
Program.runProgram = true;
1818
}
1919

2020
Console.Title = "MIDI KEYS";
2121

22-
while (Program.runForm)
23-
{
22+
while (runForm) {
2423
Console.Clear();
25-
if (Program.runFastSetup)
26-
{
24+
if (runFastSetup) {
2725
FastSetup.FastSetup_(runFastSetup);
2826
runFastSetup = false;
2927
}
30-
if (Program.runPogram)
31-
{
32-
Run.Run_(runPogram);
33-
runPogram = false;
28+
if (runProgram) {
29+
Run.Run.Run_(runProgram);
30+
runProgram = false;
3431
}
35-
if (Program.runViewer)
36-
{
32+
if (runViewer) {
3733
Viewer.Viewer_(runViewer);
3834
runViewer = false;
3935
}
40-
if (Program.runOutViewer)
41-
{
36+
if (runOutViewer) {
4237
OutViewer.OutViewer_(runOutViewer);
4338
runOutViewerVisual = false;
4439
}
45-
if (Program.runOutViewerVisual)
46-
{
40+
if (runOutViewerVisual) {
4741
OutViewerVisual.OutViewerVisual_(runOutViewerVisual);
4842
runOutViewerVisual = false;
4943
}
@@ -57,8 +51,7 @@ private static void Main(string[] args)
5751
Console.Write("2. setup\n3. visual setup");
5852
Console.ResetColor();
5953
Console.Write("\n4. run\n5. viewer\n6. viewer output\n7. viewer output visual\n8. quit\n");
60-
switch (Console.ReadKey().Key)
61-
{
54+
switch (Console.ReadKey().Key) {
6255
case ConsoleKey.D1:
6356
Program.runFastSetup = true;
6457
break;
@@ -69,7 +62,7 @@ private static void Main(string[] args)
6962
Program.runVisualSetup = true;
7063
break;
7164
case ConsoleKey.D4:
72-
Program.runPogram = true;
65+
Program.runProgram = true;
7366
break;
7467
case ConsoleKey.D5:
7568
Program.runViewer = true;
@@ -91,8 +84,7 @@ private static void Main(string[] args)
9184
public static int GetTeken()
9285
{
9386
Program.teken++;
94-
switch (Program.teken)
95-
{
87+
switch (Program.teken) {
9688
case 1:
9789
return 9;
9890
case 2:
@@ -270,7 +262,7 @@ public static int GetTeken()
270262
private static bool runVisualSetup = false;
271263
#pragma warning restore IDE0052 // Remove unread private members
272264

273-
private static bool runPogram = false;
265+
private static bool runProgram = false;
274266

275267
private static bool runViewer = false;
276268
private static bool runOutViewer = false;

MIDI-Keyboard/dataFolder/midi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33

4-
namespace keyBordL.dataFolder
4+
namespace MIDIKeyboard.dataFolder
55
{
66
public class InputPort
77
{

MIDI-Keyboard/miscellaneous/Miscellaneous.cs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace MIDIKeyboard.miscellaneous
1+
using System;
2+
3+
namespace MIDIKeyboard.miscellaneous
24
{
35
static class Miscellaneous
46
{
@@ -10,5 +12,26 @@ static public int GetID(char c)
1012

1113
return cId;
1214
}
15+
16+
public static void Error(string s)
17+
{
18+
Console.ForegroundColor = ConsoleColor.Red;
19+
Console.WriteLine(s);
20+
Console.ResetColor();
21+
Console.WriteLine("Press any key to ignore and continue...");
22+
Console.ReadKey();
23+
}
24+
public static void Error(string s, object[] os)
25+
{
26+
Console.ForegroundColor = ConsoleColor.Red;
27+
Console.WriteLine(s, os);
28+
Console.ResetColor();
29+
Console.WriteLine("Press any key to ignore and continue...");
30+
Console.ReadKey();
31+
}
32+
public static void Error(string s, object o1) => Error(s, new object[] { o1 });
33+
public static void Error(string s, object o1, object o2) => Error(s, new object[] { o1, o2 });
34+
public static void Error(string s, object o1, object o2, object o3) => Error(s, new object[] { o1, o2, o3 });
35+
public static void Error(string s, object o1, object o2, object o3, object o4) => Error(s, new object[] { o1, o2, o3, o4 });
1336
}
1437
}

MIDI-Keyboard/programChapter/FastSetup.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using keyBordL.dataFolder;
4+
using MIDIKeyboard.dataFolder;
55

6-
namespace keyBordL
6+
namespace MIDIKeyboard
77
{
88
class FastSetup
99
{

MIDI-Keyboard/programChapter/OutViewer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using keyBordL.dataFolder;
4+
using MIDIKeyboard.dataFolder;
55

6-
namespace keyBordL
6+
namespace MIDIKeyboard
77
{
88
class OutViewer
99
{

MIDI-Keyboard/programChapter/OutViewerVisual.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Text;
5-
using keyBordL.dataFolder;
5+
using MIDIKeyboard.dataFolder;
66

7-
namespace keyBordL
7+
namespace MIDIKeyboard
88
{
99
class OutViewerVisual
1010
{

MIDI-Keyboard/programChapter/Viewer.cs

+15-28
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System;
22
using System.Text;
3-
using keyBordL.dataFolder;
3+
using MIDIKeyboard.dataFolder;
44

5-
namespace keyBordL
5+
namespace MIDIKeyboard
66
{
77
class Viewer
88
{
99

10-
private static InputPort midi = new InputPort();
10+
private static readonly InputPort midi = new InputPort();
1111

1212
public static bool Viewer_(bool runViewer)
1313
{
@@ -16,8 +16,7 @@ public static bool Viewer_(bool runViewer)
1616
Console.WriteLine("what midi port do you whant to use");
1717
{
1818
Console.ForegroundColor = ConsoleColor.Black;
19-
for (int i = 0; i < midi.InputCount(); i++)
20-
{
19+
for (int i = 0; i < midi.InputCount(); i++) {
2120
if (i % 2 == 0)
2221
Console.BackgroundColor = ConsoleColor.Gray;
2322
else
@@ -28,24 +27,18 @@ public static bool Viewer_(bool runViewer)
2827
Console.ResetColor();
2928
}
3029
Console.Write("port: ");
31-
int resultat2;
32-
int.TryParse(Console.ReadLine(), out resultat2);
30+
int.TryParse(Console.ReadLine(), out int resultat2);
3331
Console.WriteLine("value set to " + resultat2);
3432
Console.WriteLine("press ESC to exit");
3533
int chanel = resultat2;
3634
midi.Open(chanel);
3735
midi.Start();
3836
int old = 0;
39-
int value = 0;
40-
string valueHex = "";
41-
string hex4 = "0000";
4237
bool sw = false;
4338

4439

45-
while (runViewer)
46-
{
47-
if (Console.KeyAvailable)
48-
{
40+
while (runViewer) {
41+
if (Console.KeyAvailable) {
4942

5043
StringBuilder sb = new StringBuilder();
5144

@@ -67,10 +60,8 @@ public static bool Viewer_(bool runViewer)
6760
byte bkey = (byte)key.KeyChar;
6861
if (bkey > 96 && bkey < 123)
6962
bkey -= 32;
70-
else
71-
{
72-
switch (key.Key)
73-
{
63+
else {
64+
switch (key.Key) {
7465
case ConsoleKey.Backspace:
7566
bkey = 8;
7667
break;
@@ -411,30 +402,26 @@ public static bool Viewer_(bool runViewer)
411402

412403

413404
}
414-
value = midi.p;
415-
if (old != value)
416-
{
405+
int value = midi.p;
406+
if (old != value) {
417407

418408

419409
sw = false;
420410

421-
valueHex = midi.pS;
411+
string valueHex = midi.pS;
422412

423-
hex4 = valueHex.Substring(valueHex.Length - 4);
413+
string hex4 = valueHex.Substring(valueHex.Length - 4);
424414

425415

426-
if (hex4.Substring(hex4.Length - 2) == "D0")
427-
{
416+
if (hex4.Substring(hex4.Length - 2) == "D0") {
428417
Console.ForegroundColor = ConsoleColor.Gray;
429418
Console.Write(valueHex.PadLeft(6, ' ').Substring(0, 4));
430419
Console.ForegroundColor = ConsoleColor.DarkYellow;
431420
Console.Write("D0 ");
432421
Console.ForegroundColor = ConsoleColor.DarkGray;
433422
Console.WriteLine(value);
434423
Console.ForegroundColor = ConsoleColor.White;
435-
}
436-
else
437-
{
424+
} else {
438425
/*
439426
if (valueHex.Length > 4)
440427
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using MIDIKeyboard.dataFolder;
5+
using static MIDIKeyboard.miscellaneous.Miscellaneous;
6+
7+
namespace MIDIKeyboard.Run
8+
{
9+
class LoadData
10+
{
11+
public static bool Load() => LoadFile() && SendOutputData() & LoadKeyData();
12+
13+
static string[] array;
14+
15+
static bool LoadFile(string path = "data.txt")
16+
{
17+
Console.Write("Loading data from path \"{0}\"...", path);
18+
if (File.Exists(path) && (array = File.ReadAllLines(path)).Length > 2) {
19+
Console.WriteLine(" Done.");
20+
return true;
21+
}
22+
Console.WriteLine(" Error.");
23+
Error("Error -> No data.");
24+
return false;
25+
}
26+
27+
static bool SendOutputData()
28+
{
29+
if (array[0].Split(',').Length > 1) {
30+
Console.Write("Sending output data...");
31+
string[] ColorArray = array[0].Split(',');
32+
33+
InputPort midiOut = new InputPort();
34+
midiOut.OpenOut(int.Parse(ColorArray[1].Trim()));
35+
36+
//send output
37+
for (int i = 2; i + 1 < ColorArray.Length; i += 2) {
38+
midiOut.MidiOutMsg((byte)int.Parse(ColorArray[i].Trim()), (byte)int.Parse(ColorArray[i + 1].Trim()));
39+
}
40+
41+
midiOut.CloseOut();
42+
Console.WriteLine(" Done.");
43+
return true;
44+
}
45+
Console.WriteLine("No output data detected.");
46+
return true;
47+
}
48+
49+
static bool LoadKeyData()
50+
{
51+
bool noErrors = true;
52+
Console.WriteLine("loading from file...");
53+
int chanel = int.Parse(array[0].Split(',')[0]);
54+
55+
for (int lineID = 0; lineID < array.Length; lineID++) {
56+
if (array[lineID] != string.Concat(chanel)) {
57+
string[] loadedValues = array[lineID].Split(new char[] { ',' });
58+
int[] tempValues = new int[loadedValues.Length];
59+
StringBuilder log = new StringBuilder();
60+
for (int i = 0; i < tempValues.Length; i++) {
61+
string value = loadedValues[i].Trim();
62+
if (!int.TryParse(value, out tempValues[i])) {
63+
string[] convertValue;
64+
if ((convertValue = value.Split('\'')).Length > 1) {
65+
tempValues[i] = GetID(convertValue[1][0]);
66+
if (convertValue[0][convertValue[0].Length - 1] == '-') {
67+
if (i < 2 || i > 1 && tempValues[1] != 1) {
68+
noErrors = false;
69+
Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value);
70+
} else
71+
tempValues[i] = -tempValues[i];
72+
}
73+
} else if ((convertValue = value.Split('"')).Length > 1) {
74+
tempValues[i] = GetID(convertValue[1][0]);
75+
if (convertValue[0][convertValue[0].Length - 1] == '-') {
76+
if (i < 2 || i > 1 && tempValues[1] != 1) {
77+
noErrors = false;
78+
Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value);
79+
} else
80+
tempValues[i] = -tempValues[i];
81+
}
82+
} else if (value.Length == 1) {
83+
tempValues[i] = GetID(value[0]);
84+
} else {
85+
if (value.Length == 2 && value[0] == '-') {
86+
tempValues[i] = -GetID(value[1]);
87+
} else {
88+
noErrors = false;
89+
Error("Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineID, array[lineID], value.Length > 1 ? "s" : "", value);
90+
}
91+
}
92+
}
93+
log.Append(value);
94+
log.Append(' ');
95+
}
96+
Program.values.Add(tempValues);
97+
Console.WriteLine(log);
98+
}
99+
}
100+
return noErrors;
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)