-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
92 lines (77 loc) · 2.75 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.IO;
using System.Linq;
using System.Threading;
using HidLibrary;
using Newtonsoft.Json;
namespace Bongos
{
public class Program
{
private static int pollRate;
private static HidDevice bongos;
private static BongoManager manager;
private static OutputHandler handler;
private static Thread polling;
static void Main(string[] args)
{
if (args.Length < 1) WriteErrorToConsoleAndExit("Please specify a config file");
BongoConfig config = null;
try
{
String json = "";
using (StreamReader sr = new StreamReader(args[0]))
{
json += sr.ReadToEnd();
}
config = JsonConvert.DeserializeObject<BongoConfig>(json);
}
catch (IOException)
{
WriteErrorToConsoleAndExit("File not found");
}
if (config == null) WriteErrorToConsoleAndExit("Failed to read config file");
manager = new BongoManager(config.micThreshold);
switch (config.output.ToLower().Trim())
{
case ("keyboard"):
handler = new KeyboardHandler(manager, config.keyboardMapping);
break;
case ("vjoy"):
handler = new VJoyHandler(manager, config.vJoyMapping, config.vJoyId, config.micSensitivity);
break;
default:
WriteErrorToConsoleAndExit("Unsupported output method");
break;
}
bongos = HidDevices.Enumerate(config.vendorID, config.productID).FirstOrDefault();
if (bongos == null) WriteErrorToConsoleAndExit("No device found");
bongos.OpenDevice();
bongos.Removed += () => WriteErrorToConsoleAndExit("Device disconnected");
pollRate = config.pollRate;
polling = new Thread(PollDevice);
polling.Start();
Console.WriteLine($"Beginning bongo translation with method '{config.output}'");
while (polling.IsAlive)
{
Console.ReadKey();
}
}
private static void PollDevice()
{
while (true)
{
manager?.UpdateState(bongos?.ReadReport(1).Data);
Thread.Sleep(1000 / pollRate);
}
}
public static void WriteErrorToConsoleAndExit(string message)
{
Console.WriteLine($"Error: {message}, press any key to exit.");
Console.ReadKey();
bongos?.CloseDevice();
polling?.Abort();
Environment.Exit(1);
}
}
}