Skip to content

Commit

Permalink
fix sensor selection on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie authored and CasperH2O committed Apr 8, 2024
1 parent e61aa50 commit 56553f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
34 changes: 23 additions & 11 deletions HandheldCompanion/Managers/SensorsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ private static void DeviceManager_UsbDeviceRemoved(PnPDevice device, DeviceEvent

private static void PickNextSensor()
{
if (ControllerManager.GetTargetController() is not null && ControllerManager.GetTargetController().HasMotionSensor())
// get current controller
IController controller = ControllerManager.GetTargetController();

if (controller is not null && controller.HasMotionSensor())
SettingsManager.SetProperty("SensorSelection", (int)SensorFamily.Controller);
else if (IDevice.GetCurrent().Capabilities.HasFlag(DeviceCapabilities.InternalSensor))
SettingsManager.SetProperty("SensorSelection", (int)SensorFamily.Windows);
Expand Down Expand Up @@ -121,11 +124,9 @@ private static void SettingsManager_SettingValueChanged(string name, object valu
case SensorFamily.Windows:
StopListening();
break;

case SensorFamily.SerialUSBIMU:
if (USBSensor is not null)
USBSensor.Close();
break;
case SensorFamily.Controller:
USBSensor?.Close();
break;
}

Expand All @@ -134,24 +135,35 @@ private static void SettingsManager_SettingValueChanged(string name, object valu

switch(sensorFamily)
{
case SensorFamily.Windows:
break;
case SensorFamily.SerialUSBIMU:
{
// get current USB sensor
USBSensor = SerialUSBIMU.GetCurrent();

if (USBSensor is null)
{
PickNextSensor();
break;

USBSensor.Open();
}

SerialPlacement placement = (SerialPlacement)SettingsManager.GetInt("SensorPlacement");
USBSensor.SetSensorPlacement(placement);
bool upsidedown = SettingsManager.GetBoolean("SensorPlacementUpsideDown");

USBSensor.Open();
USBSensor.SetSensorPlacement(placement);
USBSensor.SetSensorOrientation(upsidedown);
}
break;

case SensorFamily.Controller:
{
// get current controller
IController controller = ControllerManager.GetTargetController();
if (controller is null || !controller.Capabilities.HasFlag(ControllerCapabilities.MotionSensor))
{
PickNextSensor();
break;
}
}
break;
}

Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/Views/Pages/DevicePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private void SettingsManager_SettingValueChanged(string? name, object value)
break;
case "SensorSelection":
{
var idx = Convert.ToInt32(value);
int idx = Convert.ToInt32(value);

// default value
if (idx == -1)
Expand Down

0 comments on commit 56553f6

Please sign in to comment.