- Fix LEDs not always being set

- Code clean-up
This commit is contained in:
David Khachaturov 2020-06-11 09:09:18 +01:00
parent f316ade501
commit 6d2b4b3918
2 changed files with 9 additions and 57 deletions

View file

@ -382,8 +382,6 @@ namespace BetterJoyForCemu {
BlinkHomeLight();
a[0] = leds_;
Subcommand(0x30, a, 1);
Subcommand(0x40, new byte[] { (imu_enabled ? (byte)0x1 : (byte)0x0) }, 1, true);
Subcommand(0x48, new byte[] { 0x01 }, 1, true);
@ -392,6 +390,8 @@ namespace BetterJoyForCemu {
Subcommand(0x3, new byte[] { 0x30 }, 1, true);
DebugPrint("Done with init.", DebugType.COMMS);
SetPlayerLED(leds_);
HIDapi.hid_set_nonblocking(handle, 1);
return 0;
@ -405,7 +405,7 @@ namespace BetterJoyForCemu {
byte[] a = Enumerable.Repeat((byte)0xFF, 25).ToArray();
a[0] = 0x18;
a[1] = 0x01;
Subcommand(0x38, a, 25, false);
Subcommand(0x38, a, 25);
}
public void SetHomeLight(bool on) {
@ -417,12 +417,12 @@ namespace BetterJoyForCemu {
a[0] = 0x10;
a[1] = 0x01;
}
Subcommand(0x38, a, 25, true, false);
Subcommand(0x38, a, 25);
}
private void SetHCIState(byte state) {
byte[] a = { state };
Subcommand(0x06, a, 1, false);
Subcommand(0x06, a, 1);
}
public void PowerOff() {
@ -492,15 +492,12 @@ namespace BetterJoyForCemu {
state = state_.NOT_ATTACHED;
}
// TODO: Improve this loop, make USB not laggy
private byte ts_en;
private int ReceiveRaw() {
if (handle == IntPtr.Zero) return -2;
byte[] raw_buf = new byte[report_len];
int ret = HIDapi.hid_read_timeout(handle, raw_buf, new UIntPtr(report_len), 5);
if (ret > 0) {
// Process packets as soon as they come
for (int n = 0; n < 3; n++) {
@ -720,8 +717,7 @@ namespace BetterJoyForCemu {
}
}
// TODO: Fix?
private Thread PollThreadObj; // pro times out over time randomly if it was USB and then bluetooth??
private Thread PollThreadObj;
private void Poll() {
stop_polling = false;
int attempts = 0;
@ -734,11 +730,8 @@ namespace BetterJoyForCemu {
int a = ReceiveRaw();
if (a > 0 && state > state_.DROPPED) {
state = state_.IMU_DATA_OK;
attempts = 0;
} else if (attempts > 240) {
state = state_.DROPPED;
form.AppendTextBox("Dropped.\r\n");

View file

@ -89,14 +89,13 @@ namespace BetterJoyForCemu {
}
void CheckForNewControllersTime(Object source, ElapsedEventArgs e) {
CleanUp();
if (Config.IntValue("ProgressiveScan") == 1) {
CheckForNewControllers();
}
}
public void CheckForNewControllers() {
CleanUp();
// move all code for initializing devices here and well as the initial code from Start()
bool isLeft = false;
IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0);
@ -299,7 +298,7 @@ namespace BetterJoyForCemu {
foreach (Joycon v in j) {
if (Boolean.Parse(ConfigurationManager.AppSettings["AutoPowerOff"]))
v.PowerOff();
else
v.Detach();
if (v.out_xbox != null) {
@ -316,49 +315,9 @@ namespace BetterJoyForCemu {
}
}
// Custom timer class because system timers have a limit of 15.6ms
class HighResTimer {
double interval = 0;
double frequency = 0;
Thread thread;
public delegate void ActionDelegate();
ActionDelegate func;
bool run = false;
public HighResTimer(double f, ActionDelegate a) {
frequency = f;
interval = 1.0 / f;
func = a;
}
public void Start() {
run = true;
thread = new Thread(new ThreadStart(Run));
thread.IsBackground = true;
thread.Start();
}
void Run() {
while (run) {
func();
int timeToSleep = (int)(interval * 1000);
Thread.Sleep(timeToSleep);
}
}
public void Stop() {
run = false;
}
}
class Program {
public static PhysicalAddress btMAC = new PhysicalAddress(new byte[] { 0, 0, 0, 0, 0, 0 });
public static UdpServer server;
static double pollsPerSecond = 120.0;
public static ViGEmClient emClient;