Skip to content

Commit 5ab8e35

Browse files
author
Vit Nemecky
committed
address code review comments
1 parent c6ba049 commit 5ab8e35

8 files changed

Lines changed: 40 additions & 28 deletions

File tree

BrickController2/BrickController2/Extensions/ContainerBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal static ContainerBuilder RegisterInputDeviceService<TInputDeviceService,
5252
where TInputDeviceService : IInputDeviceService<TInputDevice>, IStartable
5353
where TInputDevice : class, IInputDevice
5454
{
55-
var registrationBuilder = RegisterInputDeviceService<TInputDeviceService>(builder)
55+
RegisterInputDeviceService<TInputDeviceService>(builder)
5656
.As<IInputDeviceService<TInputDevice>>();
5757
return builder;
5858
}

BrickController2/BrickController2/InputDeviceManagement/DI/InputDeviceManagementModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected override void Load(ContainerBuilder builder)
1111
{
1212
builder.RegisterType<InputDeviceManagerService>().As<IInputDeviceManagerService>().As<IInputDeviceEventServiceInternal>().As<IInputDeviceEventService>().SingleInstance();
1313

14-
// generic sensor based input device
14+
// generic sensor based input device, but just if supported and enabled in Settings
1515
builder.RegisterInputDeviceService<InputSensorService, OrientationSensorController>();
1616
}
1717
}

BrickController2/BrickController2/InputDeviceManagement/Sensors/InputSensorService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace BrickController2.InputDeviceManagement.Sensors;
77

88
internal class InputSensorService : InputDeviceServiceBase<OrientationSensorController>, IInputDeviceService<OrientationSensorController>
99
{
10-
internal const string OrientationSensorEnabledKey = "OrientationSensorEnabled";
10+
private const string TiltSensorEnabledKey = "TiltSensorEnabled";
1111

1212
private readonly IInputDeviceEventServiceInternal _deviceEventServiceInternal;
1313
private readonly IPreferencesService _preferencesService;
@@ -26,8 +26,8 @@ public InputSensorService(IInputDeviceManagerService inputDeviceManagerService,
2626

2727
public bool IsEnabled
2828
{
29-
get => _preferencesService.Get(OrientationSensorEnabledKey, false);
30-
set => _preferencesService.Set(OrientationSensorEnabledKey, value);
29+
get => _preferencesService.Get(TiltSensorEnabledKey, false); // by default OFF
30+
set => _preferencesService.Set(TiltSensorEnabledKey, value);
3131
}
3232

3333
public bool IsSupported => Sensor.IsSupported;

BrickController2/BrickController2/InputDeviceManagement/Sensors/OrientationSensorController.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using BrickController2.PlatformServices.InputDeviceService;
33
using Microsoft.Maui.Devices.Sensors;
44
using System;
5-
using System.Linq;
5+
using System.Collections.Generic;
66
using System.Numerics;
77

88
namespace BrickController2.InputDeviceManagement.Sensors;
@@ -11,13 +11,24 @@ public class OrientationSensorController : InputDeviceBase<IOrientationSensor>
1111
{
1212
private const double InversePI = 1.0 / Math.PI;
1313
private const double InverseHalfPI = 1.0 / (Math.PI / 2.0);
14+
private const int RoundingPrecision = 4;
15+
16+
private const string PitchName = "Pitch";
17+
private const string RollName = "Roll";
18+
private const string YawName = "Yaw";
19+
20+
private static readonly (InputDeviceEventType, string) PitchKey = (InputDeviceEventType.Axis, PitchName);
21+
private static readonly (InputDeviceEventType, string) RollKey = (InputDeviceEventType.Axis, RollName);
22+
private static readonly (InputDeviceEventType, string) YawKey = (InputDeviceEventType.Axis, YawName);
23+
24+
private readonly Dictionary<(InputDeviceEventType, string), float> _eventBuffer = new(3);
1425

1526
public OrientationSensorController(IInputDeviceEventServiceInternal service,
1627
IOrientationSensor sensor)
1728
: base(service, sensor)
1829
{
19-
Name = "Orientation";
20-
InputDeviceId = nameof(OrientationSensor);
30+
InputDeviceId = "Sensor";
31+
Name = "Tilt";
2132
}
2233

2334
public override void Start()
@@ -38,23 +49,18 @@ public override void Stop()
3849

3950
private void Orientation_ReadingChanged(object? sender, OrientationSensorChangedEventArgs e)
4051
{
41-
var (Pitch, Roll, Yaw) = QuaternionToEulerAngles(e.Reading.Orientation);
42-
43-
RaiseEvents(
44-
[
45-
(nameof(Pitch), (float)Pitch),
46-
(nameof(Roll), (float)Roll),
47-
(nameof(Yaw), (float)Yaw)
48-
]);
49-
}
52+
var (pitch, roll, yaw) = QuaternionToEulerAngles(e.Reading.Orientation);
5053

51-
private void RaiseEvents((string eventName, float value)[] axisEvents)
52-
{
53-
var events = axisEvents
54-
.Where(e => HasValueChanged(e.eventName, e.value))
55-
.ToDictionary(e => (InputDeviceEventType.Axis, e.eventName), e => e.value);
54+
_eventBuffer.Clear();
55+
56+
if (HasValueChanged(PitchName, pitch))
57+
_eventBuffer[PitchKey] = pitch;
58+
if (HasValueChanged(RollName, roll))
59+
_eventBuffer[RollKey] = roll;
60+
if (HasValueChanged(YawName, yaw))
61+
_eventBuffer[YawKey] = yaw;
5662

57-
RaiseEvent(events);
63+
RaiseEvent(_eventBuffer);
5864
}
5965

6066
private static (float Pitch, float Roll, float Yaw) QuaternionToEulerAngles(Quaternion q)
@@ -77,9 +83,9 @@ private static (float Pitch, float Roll, float Yaw) QuaternionToEulerAngles(Quat
7783
var yaw = Math.Atan2(sinYaw, cosYaw);
7884

7985
return (
80-
(float)(pitch * InversePI), // <-180; 180> => <-1; 1>
81-
(float)(roll * InverseHalfPI), // <-90; 90> => <-1; 1>
82-
(float)(yaw * InversePI) // <-180; 180> => <-1; 1>
86+
(float)Math.Round(pitch * InversePI, RoundingPrecision), // <-180; 180> => <-1; 1>
87+
(float)Math.Round(roll * InverseHalfPI, RoundingPrecision), // <-90; 90> => <-1; 1>
88+
(float)Math.Round(yaw * InversePI, RoundingPrecision) // <-180; 180> => <-1; 1>
8389
);
8490
}
8591
}

BrickController2/BrickController2/PlatformServices/InputDeviceService/IInputDeviceService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ public interface IInputDeviceService<TInputDevice> : IInputDeviceService
2828
/// Gets or sets a value whether the input device is enabled.
2929
/// </summary>
3030
bool IsEnabled { get; set; }
31-
}
31+
}

BrickController2/BrickController2/Resources/TranslationResources.de.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,4 +762,7 @@
762762
<data name="SubchannelsPortMode" xml:space="preserve">
763763
<value>Subkanäle</value>
764764
</data>
765+
<data name="OrientationSensor" xml:space="preserve">
766+
<value>Neigungssteuerung</value>
767+
</data>
765768
</root>

BrickController2/BrickController2/Resources/TranslationResources.hu.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,4 +753,7 @@
753753
<data name="SubchannelsPortMode" xml:space="preserve">
754754
<value>Alcsatorna</value>
755755
</data>
756+
<data name="OrientationSensor" xml:space="preserve">
757+
<value>Dönthető kormányzás</value>
758+
</data>
756759
</root>

BrickController2/BrickController2/Resources/TranslationResources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,6 @@
763763
<value>Subchannels</value>
764764
</data>
765765
<data name="OrientationSensor" xml:space="preserve">
766-
<value>Orientation Sensor</value>
766+
<value>Tilt Steering</value>
767767
</data>
768768
</root>

0 commit comments

Comments
 (0)