Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private static DeviceImageRegistry CreateRegistry()
var registry = new DeviceImageRegistry();
registry.Register(DeviceType.BuWizz2, "buwizz_image.png", "buwizz_image_small.png");
registry.Register(DeviceType.RemoteControl, "remotecontrol_image_small.png", "remotecontrol_image_small.png");
registry.Register(DeviceType.Infrared, "powerfunctions_image.png", "powerfunctions_image_small.png");
return registry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BrickController2.DeviceManagement
/// <summary>
/// Baseclass of Bluetooth LE Advertising devices
/// </summary>
internal abstract class BluetoothAdvertisingDevice : Device
internal abstract class BluetoothAdvertisingDevice : Device, IBluetoothDevice
{
/// <summary>
/// BluetoothAdvertisingDeviceHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace BrickController2.DeviceManagement
{
internal abstract class BluetoothDevice : Device
internal abstract class BluetoothDevice : Device, IBluetoothDevice
{
protected readonly IBluetoothLEService _bleService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ public class DeviceManagementModule : Module
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<BluetoothDeviceManager>().As<IBluetoothDeviceManager>().SingleInstance();
builder.RegisterType<InfraredDeviceManager>().As<IInfraredDeviceManager>().SingleInstance();

builder.RegisterType<DeviceRepository>().As<IDeviceRepository>().SingleInstance();
builder.RegisterType<DeviceManager>().As<IDeviceManager>().SingleInstance();
builder.RegisterType<ManualDeviceManager>().As<IManualDeviceManager>().SingleInstance();

builder.RegisterType<InfraredDevice>().Keyed<Device>(DeviceType.Infrared);
builder.RegisterType<CircuitCubeDevice>().Keyed<Device>(DeviceType.CircuitCubes);
builder.RegisterType<PfxBrickDevice>().Keyed<Device>(DeviceType.PfxBrick);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public DeviceFactoryData(TVendor vendor, string name, string address, byte[] dev

public DeviceType DeviceType => TDevice.Type;

public bool IsAvailable => Vendor.IsAvailable;
public TVendor Vendor { get; }
public string Name { get; }
public string Address { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace BrickController2.DeviceManagement
internal class DeviceManager : NotifyPropertyChangedSource, IDeviceManager
{
private readonly IBluetoothDeviceManager _bluetoothDeviceManager;
private readonly IInfraredDeviceManager _infraredDeviceManager;
private readonly IDeviceRepository _deviceRepository;
private readonly DeviceFactory _deviceFactory;
private readonly IMainThreadService _uiThreadService;
Expand All @@ -25,14 +24,12 @@ internal class DeviceManager : NotifyPropertyChangedSource, IDeviceManager

public DeviceManager(
IBluetoothDeviceManager bluetoothDeviceManager,
IInfraredDeviceManager infraredDeviceManager,
IDeviceRepository deviceRepository,
DeviceFactory deviceFactory,
IMainThreadService uiThreadService,
ILogger<DeviceManager> logger)
{
_bluetoothDeviceManager = bluetoothDeviceManager;
_infraredDeviceManager = infraredDeviceManager;
_deviceRepository = deviceRepository;
_deviceFactory = deviceFactory;
_uiThreadService = uiThreadService;
Expand Down Expand Up @@ -88,12 +85,7 @@ public async Task<bool> ScanAsync(CancellationToken token)

try
{
var infraScan = _infraredDeviceManager.ScanAsync(CreateDeviceAsync!, token);
var bluetoothScan = _bluetoothDeviceManager.ScanAsync(CreateDeviceAsync!, token);

await Task.WhenAll(infraScan, bluetoothScan);

return infraScan.Result && bluetoothScan.Result;
return await _bluetoothDeviceManager.ScanAsync(CreateDeviceAsync!, token);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BrickController2.DeviceManagement
{
internal interface IBluetoothDevice
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BrickController2.DeviceManagement
{
public interface IDeviceFactoryData
{
bool IsAvailable { get; }
DeviceType DeviceType { get; }
string Name { get; }
string Address { get; }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading.Tasks;

namespace BrickController2.DeviceManagement.PowerFunctions
{
internal interface IPowerFunctionsManager
{
Task<DeviceConnectionResult> ConnectDevice(PowerFunctionsDevice device);
Task DisconnectDevice(PowerFunctionsDevice device);

void SetOutput(PowerFunctionsDevice device, int channel, int value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Autofac;
using BrickController2.DeviceManagement.DI;
using BrickController2.DeviceManagement.Vendors;
using BrickController2.PlatformServices.Infrared;

namespace BrickController2.DeviceManagement.PowerFunctions;

/// <summary>
/// Vendor for Power Functions devices
/// </summary>
internal class PowerFunctions : Vendor<PowerFunctions>
{
private IInfraredService? _infraredService;

public override string VendorName => "LEGO";

public override bool IsAvailable => _infraredService != null && _infraredService.IsInfraredSupported && _infraredService.IsCarrierFrequencySupported(PowerFunctionsManager.IR_FREQUENCY);

protected override void Register(VendorBuilder<PowerFunctions> builder)
{
// device manager
builder.ContainerBuilder.RegisterType<PowerFunctionsManager>().As<IPowerFunctionsManager>().SingleInstance();

builder.ContainerBuilder.RegisterBuildCallback(scope =>
{
_infraredService = scope.Resolve<IInfraredService>();
});

// manually added devices
builder.RegisterDevice<PowerFunctionsDevice>()
.WithImages("powerfunctions_image.png", "powerfunctions_image_small.png")
.WithDeviceFactory("0", "PF Infra 1")
.WithDeviceFactory("1", "PF Infra 2")
.WithDeviceFactory("2", "PF Infra 3")
.WithDeviceFactory("3", "PF Infra 4");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
using System.Threading;
using System.Threading.Tasks;

namespace BrickController2.DeviceManagement
namespace BrickController2.DeviceManagement.PowerFunctions
{
internal class InfraredDevice : Device
internal class PowerFunctionsDevice : Device, IDeviceType<PowerFunctionsDevice>
{
private readonly IInfraredDeviceManager _infraredDeviceManager;
private readonly IPowerFunctionsManager _powerFunctionsManager;

public InfraredDevice(string name, string address, byte[] deviceData, IInfraredDeviceManager infraredDeviceManager, IDeviceRepository deviceRepository)
public PowerFunctionsDevice(string name, string address, byte[] deviceData, IPowerFunctionsManager powerFunctionsManager, IDeviceRepository deviceRepository)
: base(name, address, deviceRepository)
{
_infraredDeviceManager = infraredDeviceManager;
_powerFunctionsManager = powerFunctionsManager;
}

public override DeviceType DeviceType => DeviceType.Infrared;
public static DeviceType Type => DeviceType.Infrared;

public static string TypeName => "Power Functions";
public override DeviceType DeviceType => Type;
public override int NumberOfChannels => 2;

public override async Task<DeviceConnectionResult> ConnectAsync(
Expand All @@ -28,7 +31,7 @@ public override async Task<DeviceConnectionResult> ConnectAsync(
{
DeviceState = DeviceState.Connecting;

var result = await _infraredDeviceManager.ConnectDevice(this);
var result = await _powerFunctionsManager.ConnectDevice(this);

DeviceState = result == DeviceConnectionResult.Ok ? DeviceState.Connected : DeviceState.Disconnected;
return result;
Expand All @@ -38,7 +41,7 @@ public override async Task DisconnectAsync()
{
DeviceState = DeviceState.Disconnecting;

await _infraredDeviceManager.DisconnectDevice(this);
await _powerFunctionsManager.DisconnectDevice(this);

DeviceState = DeviceState.Disconnected;
}
Expand All @@ -49,7 +52,7 @@ public override void SetOutput(int channel, float value)
value = CutOutputValue(value);

var intValue = (int)(7 * value);
_infraredDeviceManager.SetOutput(this, channel, intValue);
_powerFunctionsManager.SetOutput(this, channel, intValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using BrickController2.PlatformServices.Infrared;
using BrickController2.Helpers;

namespace BrickController2.DeviceManagement
namespace BrickController2.DeviceManagement.PowerFunctions
{
internal class InfraredDeviceManager : IInfraredDeviceManager
internal class PowerFunctionsManager : IPowerFunctionsManager
{
private const int IR_FREQUENCY = 38000;
public const int IR_FREQUENCY = 38000;

private const int IR_MARK_US = 158;
private const int IR_START_GAP_US = 1026;
Expand All @@ -29,33 +29,12 @@ internal class InfraredDeviceManager : IInfraredDeviceManager
private Task? _irTask;
private CancellationTokenSource? _irTaskCancelationTokenSource;

public InfraredDeviceManager(IInfraredService infraredService)
public PowerFunctionsManager(IInfraredService infraredService)
{
_infraredService = infraredService;
}

public async Task<bool> ScanAsync(Func<DeviceType, string, string, byte[]?, Task> deviceFoundCallback, CancellationToken token)
{
using (await _asyncLock.LockAsync())
{
if (_infraredService.IsInfraredSupported && _infraredService.IsCarrierFrequencySupported(IR_FREQUENCY))
{
for (int i = 0; i < 4; i++)
{
if (token.IsCancellationRequested)
{
break;
}

await deviceFoundCallback(DeviceType.Infrared, $"PF Infra {i + 1}", $"{i}", null);
}
}
}

return true;
}

public async Task<DeviceConnectionResult> ConnectDevice(InfraredDevice device)
public async Task<DeviceConnectionResult> ConnectDevice(PowerFunctionsDevice device)
{
using (await _asyncLock.LockAsync())
{
Expand All @@ -74,7 +53,7 @@ public async Task<DeviceConnectionResult> ConnectDevice(InfraredDevice device)
}
}

public async Task DisconnectDevice(InfraredDevice device)
public async Task DisconnectDevice(PowerFunctionsDevice device)
{
using (await _asyncLock.LockAsync())
{
Expand All @@ -91,7 +70,7 @@ public async Task DisconnectDevice(InfraredDevice device)
}
}

public void SetOutput(InfraredDevice device, int channel, int value)
public void SetOutput(PowerFunctionsDevice device, int channel, int value)
{
if (int.TryParse(device.Address, out int address))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public abstract class Vendor<TVendor> : Module, IVendorModule
{
public abstract string VendorName { get; }

public virtual bool IsAvailable => true;

protected abstract void Register(VendorBuilder<TVendor> builder);

protected sealed override void Load(ContainerBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@
</Grid>
</Grid>

<!-- Infrared -->
<Grid x:Name="InfraredSection">
<!-- Power Functions -->
<Grid x:Name="PowerFunctionsSection">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<Grid Grid.Column="0">
<controls:ChannelSelectorRadioButton x:Name="InfraredChannel0" DeviceType="Infrared" Channel="0" HorizontalOptions="End" VerticalOptions="End"/>
<controls:ChannelSelectorRadioButton x:Name="PowerFunctionsChannel0" DeviceType="Infrared" Channel="0" HorizontalOptions="End" VerticalOptions="End"/>
</Grid>

<Image Grid.Column="1" Source="{extensions:ImageResource Source=infrared_image.png}" WidthRequest="150" HeightRequest="150"/>
<Image Grid.Column="1" Source="{extensions:ImageResource Source=powerfunctions_image.png}" WidthRequest="150" HeightRequest="150"/>

<Grid Grid.Column="2">
<controls:ChannelSelectorRadioButton x:Name="InfraredChannel1" DeviceType="Infrared" Channel="1" HorizontalOptions="Start" VerticalOptions="End"/>
<controls:ChannelSelectorRadioButton x:Name="PowerFunctionsChannel1" DeviceType="Infrared" Channel="1" HorizontalOptions="Start" VerticalOptions="End"/>
</Grid>
</Grid>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public DeviceChannelSelector()
BuWizz3Channel3.Command = new SafeCommand(() => SelectedChannel = 3);
BuWizz3Channel4.Command = new SafeCommand(() => SelectedChannel = 4);
BuWizz3Channel5.Command = new SafeCommand(() => SelectedChannel = 5);
InfraredChannel0.Command = new SafeCommand(() => SelectedChannel = 0);
InfraredChannel1.Command = new SafeCommand(() => SelectedChannel = 1);
PowerFunctionsChannel0.Command = new SafeCommand(() => SelectedChannel = 0);
PowerFunctionsChannel1.Command = new SafeCommand(() => SelectedChannel = 1);
PoweredUpChannel0.Command = new SafeCommand(() => SelectedChannel = 0);
PoweredUpChannel1.Command = new SafeCommand(() => SelectedChannel = 1);
BoostChannelA.Command = new SafeCommand(() => SelectedChannel = 0);
Expand Down Expand Up @@ -173,7 +173,7 @@ private void OnDeviceChanged(Device device)
SbrickLightSection.IsVisible = deviceType == DeviceType.SBrickLight;
BuWizzSection.IsVisible = deviceType == DeviceType.BuWizz || deviceType == DeviceType.BuWizz2;
BuWizz3Section.IsVisible = deviceType == DeviceType.BuWizz3;
InfraredSection.IsVisible = deviceType == DeviceType.Infrared;
PowerFunctionsSection.IsVisible = deviceType == DeviceType.Infrared;
PoweredUpSection.IsVisible = deviceType == DeviceType.PoweredUp;
BoostSection.IsVisible = deviceType == DeviceType.Boost;
TechnicHubSection.IsVisible = deviceType == DeviceType.TechnicHub;
Expand Down Expand Up @@ -222,8 +222,8 @@ private void OnSelectedChannelChanged(int selectedChannel)
BuWizz3Channel3.SelectedChannel = selectedChannel;
BuWizz3Channel4.SelectedChannel = selectedChannel;
BuWizz3Channel5.SelectedChannel = selectedChannel;
InfraredChannel0.SelectedChannel = selectedChannel;
InfraredChannel1.SelectedChannel = selectedChannel;
PowerFunctionsChannel0.SelectedChannel = selectedChannel;
PowerFunctionsChannel1.SelectedChannel = selectedChannel;
PoweredUpChannel0.SelectedChannel = selectedChannel;
PoweredUpChannel1.SelectedChannel = selectedChannel;
BoostChannelA.SelectedChannel = selectedChannel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override async void OnAppearing()
_isDisappearing = false;
base.OnAppearing();

if (Device.DeviceType != DeviceType.Infrared)
if (Device is IBluetoothDevice)
{
if (!await _deviceManager.IsBluetoothOnAsync())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override async void OnAppearing()
_isDisappearing = false;
base.OnAppearing();

if (Device.DeviceType != DeviceType.Infrared)
if (Device is IBluetoothDevice)
{
if (!await _deviceManager.IsBluetoothOnAsync())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public ManualDeviceListPageViewModel(

var groups = manualDeviceManager.FactoryDataList
// apply ordering per vendor and device type
.Where(o => o.IsAvailable)
.OrderBy(o => o.VendorName)
.ThenBy(o => o.DeviceTypeName)
.GroupBy(o => (o.VendorName, o.DeviceType, o.DeviceTypeName), x => new DeviceEntry(x, GetDeviceInstance(x)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override async void OnAppearing()
_isDisappearing = false;
base.OnAppearing();

if (_devices.Any(d => d.DeviceType != DeviceType.Infrared) && !await _deviceManager.IsBluetoothOnAsync())
if (_devices.Any(d => d is IBluetoothDevice) && !await _deviceManager.IsBluetoothOnAsync())
{
await _dialogService.ShowMessageBoxAsync(
Translate("Warning"),
Expand Down
Loading