Skip to content

Commit

Permalink
♻️ Razer rename lighting features into V1 and V2.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexawyz committed Sep 20, 2024
1 parent dc9d69f commit 1ee406e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 79 deletions.
16 changes: 8 additions & 8 deletions src/Exo/Devices/Exo.Devices.Razer/IRazerProtocolTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ internal interface IRazerProtocolTransport : IDisposable
ValueTask<byte> GetPollingFrequencyDivider(CancellationToken cancellationToken);
Task SetPollingFrequencyDivider(byte divider, CancellationToken cancellationToken);

ValueTask<ILightingEffect?> GetSavedLegacyEffectAsync(CancellationToken cancellationToken);
Task SetLegacyEffectAsync(RazerLegacyLightingEffect effect, byte parameter, RgbColor color1, RgbColor color2, CancellationToken cancellationToken);
Task SetLegacyBrightnessAsync(byte value, CancellationToken cancellationToken);
ValueTask<byte> GetLegacyBrightnessAsync(CancellationToken cancellationToken);
ValueTask<ILightingEffect?> GetSavedEffectV1Async(CancellationToken cancellationToken);
Task SetEffectV1Async(RazerLegacyLightingEffect effect, byte parameter, RgbColor color1, RgbColor color2, CancellationToken cancellationToken);
Task SetBrightnessV1Async(byte value, CancellationToken cancellationToken);
ValueTask<byte> GetBrightnessV1Async(CancellationToken cancellationToken);

ValueTask<byte> GetDeviceInformationXxxxxAsync(CancellationToken cancellationToken);
ValueTask<byte> GetBrightnessAsync(bool persisted, byte flag, CancellationToken cancellationToken);
Task SetBrightnessAsync(bool persist, byte value, CancellationToken cancellationToken);
ValueTask<ILightingEffect?> GetSavedEffectAsync(byte flag, CancellationToken cancellationToken);
Task SetEffectAsync(bool persist, RazerLightingEffect effect, byte colorCount, RgbColor color1, RgbColor color2, CancellationToken cancellationToken);
ValueTask<byte> GetBrightnessV2Async(bool persisted, byte flag, CancellationToken cancellationToken);
Task SetBrightnessV2Async(bool persist, byte value, CancellationToken cancellationToken);
ValueTask<ILightingEffect?> GetSavedEffectV2Async(byte flag, CancellationToken cancellationToken);
Task SetEffectV2Async(bool persist, RazerLightingEffect effect, byte colorCount, RgbColor color1, RgbColor color2, CancellationToken cancellationToken);
ValueTask SetDynamicColorAsync(RgbColor color, CancellationToken cancellationToken);

ValueTask<ushort> GetIdleTimerAsync(CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private sealed class LightingEffectWaitState : WaitState<ILightingEffect?>
{
public LightingEffectWaitState(byte[] buffer, byte commandId) : base(buffer, commandId) { }

protected override ILightingEffect? ProcessData(ReadOnlySpan<byte> data) => RazerProtocolTransport.ParseEffect(data);
protected override ILightingEffect? ProcessData(ReadOnlySpan<byte> data) => RazerProtocolTransport.ParseV2Effect(data);
}

private sealed class DpiWaitState : WaitState<DotsPerInch>
Expand Down Expand Up @@ -287,7 +287,7 @@ static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharact
// TODO: Should find a way to make this return the information that we nneed. The value is currently hardcoded.
public ValueTask<byte> GetDeviceInformationXxxxxAsync(CancellationToken cancellationToken) => ValueTask.FromResult<byte>(4);

public async ValueTask<ILightingEffect?> GetSavedEffectAsync(byte flag, CancellationToken cancellationToken)
public async ValueTask<ILightingEffect?> GetSavedEffectV2Async(byte flag, CancellationToken cancellationToken)
{
static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharacteristicInformation writeCharacteristic, bool persisted, byte flag)
{
Expand Down Expand Up @@ -327,7 +327,7 @@ static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharact
}


public async Task SetEffectAsync(bool persist, RazerLightingEffect effect, byte colorCount, RgbColor color1, RgbColor color2, CancellationToken cancellationToken)
public async Task SetEffectV2Async(bool persist, RazerLightingEffect effect, byte colorCount, RgbColor color1, RgbColor color2, CancellationToken cancellationToken)
{
static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharacteristicInformation writeCharacteristic, bool persist, RazerLightingEffect effect, byte colorCount, RgbColor color1, RgbColor color2)
{
Expand All @@ -342,7 +342,7 @@ static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharact
*(ushort*)&((uint*)buffer)[2] = 0x_03_10;
buffer[4 + 6] = persist ? (byte)1 : (byte)0;
buffer[4 + 7] = 0;
uint length = (uint)RazerProtocolTransport.WriteEffect(new Span<byte>(&buffer[4 + 8 + 4], EffectBufferLength), effect, colorCount, color1, color2);
uint length = (uint)RazerProtocolTransport.WriteV2Effect(new Span<byte>(&buffer[4 + 8 + 4], EffectBufferLength), effect, colorCount, color1, color2);
if (length > EffectBufferLength) throw new InvalidOperationException("Unsupported effect length");
buffer[4 + 1] = (byte)length;
((uint*)buffer)[3] = length;
Expand Down Expand Up @@ -378,7 +378,7 @@ static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharact
// TODO
public ValueTask SetDynamicColorAsync(RgbColor color, CancellationToken cancellationToken) => throw new NotImplementedException();

public async ValueTask<byte> GetBrightnessAsync(bool persisted, byte flag, CancellationToken cancellationToken)
public async ValueTask<byte> GetBrightnessV2Async(bool persisted, byte flag, CancellationToken cancellationToken)
{
static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharacteristicInformation writeCharacteristic, bool persisted, byte flag)
{
Expand Down Expand Up @@ -417,7 +417,7 @@ static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharact
}
}

public async Task SetBrightnessAsync(bool persist, byte value, CancellationToken cancellationToken)
public async Task SetBrightnessV2Async(bool persist, byte value, CancellationToken cancellationToken)
{
static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharacteristicInformation writeCharacteristic, bool persist, byte value)
{
Expand Down Expand Up @@ -879,8 +879,8 @@ static unsafe void WriteData(SafeFileHandle serviceHandle, in BluetoothLeCharact
ValueTask<PairedDeviceInformation> IRazerProtocolTransport.GetDeviceInformationAsync(CancellationToken cancellationToken) => throw new NotSupportedException();
ValueTask<PairedDeviceInformation[]> IRazerProtocolTransport.GetDevicePairingInformationAsync(CancellationToken cancellationToken) => throw new NotSupportedException();

ValueTask<ILightingEffect?> IRazerProtocolTransport.GetSavedLegacyEffectAsync(CancellationToken cancellationToken) => throw new NotSupportedException();
Task IRazerProtocolTransport.SetLegacyEffectAsync(RazerLegacyLightingEffect effect, byte parameter, RgbColor color1, RgbColor color2, CancellationToken cancellationToken) => throw new NotSupportedException();
Task IRazerProtocolTransport.SetLegacyBrightnessAsync(byte value, CancellationToken cancellationToken) => throw new NotSupportedException();
ValueTask<byte> IRazerProtocolTransport.GetLegacyBrightnessAsync(CancellationToken cancellationToken) => throw new NotSupportedException();
ValueTask<ILightingEffect?> IRazerProtocolTransport.GetSavedEffectV1Async(CancellationToken cancellationToken) => throw new NotSupportedException();
Task IRazerProtocolTransport.SetEffectV1Async(RazerLegacyLightingEffect effect, byte parameter, RgbColor color1, RgbColor color2, CancellationToken cancellationToken) => throw new NotSupportedException();
Task IRazerProtocolTransport.SetBrightnessV1Async(byte value, CancellationToken cancellationToken) => throw new NotSupportedException();
ValueTask<byte> IRazerProtocolTransport.GetBrightnessV1Async(CancellationToken cancellationToken) => throw new NotSupportedException();
}
48 changes: 24 additions & 24 deletions src/Exo/Devices/Exo.Devices.Razer/RazerDeviceDriver.BaseDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ await _transport.IsConnectedToExternalPowerAsync(cancellationToken).ConfigureAwa
{
if (!HasLightingV2)
{
_currentBrightness = await _transport.GetLegacyBrightnessAsync(cancellationToken).ConfigureAwait(false);
_currentBrightness = await _transport.GetBrightnessV1Async(cancellationToken).ConfigureAwait(false);
//_appliedEffect = await _transport.GetSavedLegacyEffectAsync(cancellationToken).ConfigureAwait(false) ?? DisabledEffect.SharedInstance;
_appliedEffect = DisabledEffect.SharedInstance;
}
else
{
byte flag = await _transport.GetDeviceInformationXxxxxAsync(cancellationToken).ConfigureAwait(false);
_currentBrightness = await _transport.GetBrightnessAsync(true, flag, cancellationToken).ConfigureAwait(false);
_appliedEffect = await _transport.GetSavedEffectAsync(flag, cancellationToken).ConfigureAwait(false) ?? DisabledEffect.SharedInstance;
_currentBrightness = await _transport.GetBrightnessV2Async(true, flag, cancellationToken).ConfigureAwait(false);
_appliedEffect = await _transport.GetSavedEffectV2Async(flag, cancellationToken).ConfigureAwait(false) ?? DisabledEffect.SharedInstance;

// Reapply the persisted effect. (In case it was overridden by a temporary effect)
await ApplyEffectAsync(_appliedEffect, _currentBrightness, false, true, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -297,7 +297,7 @@ private async ValueTask ApplyChangesAsync(bool shouldPersist, CancellationToken
}
else if (!ReferenceEquals(_currentEffect, DisabledEffect.SharedInstance) && _appliedBrightness != _currentBrightness)
{
await _transport.SetBrightnessAsync(shouldPersist, _currentBrightness, cancellationToken).ConfigureAwait(false);
await _transport.SetBrightnessV2Async(shouldPersist, _currentBrightness, cancellationToken).ConfigureAwait(false);
}
_appliedBrightness = _currentBrightness;
}
Expand All @@ -307,40 +307,40 @@ private async ValueTask ApplyLegacyEffectAsync(ILightingEffect effect, byte brig
{
if (ReferenceEquals(effect, DisabledEffect.SharedInstance))
{
await _transport.SetLegacyEffectAsync(0, 0, default, default, cancellationToken).ConfigureAwait(false);
await _transport.SetLegacyBrightnessAsync(0, cancellationToken);
await _transport.SetEffectV1Async(0, 0, default, default, cancellationToken).ConfigureAwait(false);
await _transport.SetBrightnessV1Async(0, cancellationToken);
return;
}

// It seems brightness must be restored from zero first before setting a color effect.
// Otherwise, the device might restore to its saved effect. (e.g. Color Cycle)
if (forceBrightnessUpdate)
{
await _transport.SetLegacyBrightnessAsync(brightness, cancellationToken);
await _transport.SetBrightnessV1Async(brightness, cancellationToken);
}

switch (effect)
{
case StaticColorEffect staticColorEffect:
await _transport.SetLegacyEffectAsync(RazerLegacyLightingEffect.Static, 1, staticColorEffect.Color, staticColorEffect.Color, cancellationToken);
await _transport.SetEffectV1Async(RazerLegacyLightingEffect.Static, 1, staticColorEffect.Color, staticColorEffect.Color, cancellationToken);
break;
case RandomColorPulseEffect:
await _transport.SetLegacyEffectAsync(RazerLegacyLightingEffect.Breathing, 3, default, default, cancellationToken);
await _transport.SetEffectV1Async(RazerLegacyLightingEffect.Breathing, 3, default, default, cancellationToken);
break;
case ColorPulseEffect colorPulseEffect:
await _transport.SetLegacyEffectAsync(RazerLegacyLightingEffect.Breathing, 1, colorPulseEffect.Color, default, cancellationToken);
await _transport.SetEffectV1Async(RazerLegacyLightingEffect.Breathing, 1, colorPulseEffect.Color, default, cancellationToken);
break;
case TwoColorPulseEffect twoColorPulseEffect:
await _transport.SetLegacyEffectAsync(RazerLegacyLightingEffect.Breathing, 2, twoColorPulseEffect.Color, twoColorPulseEffect.SecondColor, cancellationToken);
await _transport.SetEffectV1Async(RazerLegacyLightingEffect.Breathing, 2, twoColorPulseEffect.Color, twoColorPulseEffect.SecondColor, cancellationToken);
break;
case SpectrumCycleEffect:
await _transport.SetLegacyEffectAsync(RazerLegacyLightingEffect.SpectrumCycle, 0, default, default, cancellationToken);
await _transport.SetEffectV1Async(RazerLegacyLightingEffect.SpectrumCycle, 0, default, default, cancellationToken);
break;
case SpectrumWaveEffect:
await _transport.SetLegacyEffectAsync(RazerLegacyLightingEffect.Wave, 1, default, default, cancellationToken);
await _transport.SetEffectV1Async(RazerLegacyLightingEffect.Wave, 1, default, default, cancellationToken);
break;
case ReactiveEffect reactiveEffect:
await _transport.SetLegacyEffectAsync(RazerLegacyLightingEffect.Reactive, 1, reactiveEffect.Color, default, cancellationToken);
await _transport.SetEffectV1Async(RazerLegacyLightingEffect.Reactive, 1, reactiveEffect.Color, default, cancellationToken);
break;
}
}
Expand All @@ -349,40 +349,40 @@ private async ValueTask ApplyEffectAsync(ILightingEffect effect, byte brightness
{
if (ReferenceEquals(effect, DisabledEffect.SharedInstance))
{
await _transport.SetEffectAsync(shouldPersist, 0, 0, default, default, cancellationToken).ConfigureAwait(false);
await _transport.SetBrightnessAsync(shouldPersist, 0, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, 0, 0, default, default, cancellationToken).ConfigureAwait(false);
await _transport.SetBrightnessV2Async(shouldPersist, 0, cancellationToken);
return;
}

// It seems brightness must be restored from zero first before setting a color effect.
// Otherwise, the device might restore to its saved effect. (e.g. Color Cycle)
if (forceBrightnessUpdate)
{
await _transport.SetBrightnessAsync(shouldPersist, brightness, cancellationToken);
await _transport.SetBrightnessV2Async(shouldPersist, brightness, cancellationToken);
}

switch (effect)
{
case StaticColorEffect staticColorEffect:
await _transport.SetEffectAsync(shouldPersist, RazerLightingEffect.Static, 1, staticColorEffect.Color, staticColorEffect.Color, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, RazerLightingEffect.Static, 1, staticColorEffect.Color, staticColorEffect.Color, cancellationToken);
break;
case RandomColorPulseEffect:
await _transport.SetEffectAsync(shouldPersist, RazerLightingEffect.Breathing, 0, default, default, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, RazerLightingEffect.Breathing, 0, default, default, cancellationToken);
break;
case ColorPulseEffect colorPulseEffect:
await _transport.SetEffectAsync(shouldPersist, RazerLightingEffect.Breathing, 1, colorPulseEffect.Color, default, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, RazerLightingEffect.Breathing, 1, colorPulseEffect.Color, default, cancellationToken);
break;
case TwoColorPulseEffect twoColorPulseEffect:
await _transport.SetEffectAsync(shouldPersist, RazerLightingEffect.Breathing, 2, twoColorPulseEffect.Color, twoColorPulseEffect.SecondColor, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, RazerLightingEffect.Breathing, 2, twoColorPulseEffect.Color, twoColorPulseEffect.SecondColor, cancellationToken);
break;
case SpectrumCycleEffect:
await _transport.SetEffectAsync(shouldPersist, RazerLightingEffect.SpectrumCycle, 0, default, default, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, RazerLightingEffect.SpectrumCycle, 0, default, default, cancellationToken);
break;
case SpectrumWaveEffect:
await _transport.SetEffectAsync(shouldPersist, RazerLightingEffect.Wave, 0, default, default, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, RazerLightingEffect.Wave, 0, default, default, cancellationToken);
break;
case ReactiveEffect reactiveEffect:
await _transport.SetEffectAsync(shouldPersist, RazerLightingEffect.Reactive, 1, reactiveEffect.Color, default, cancellationToken);
await _transport.SetEffectV2Async(shouldPersist, RazerLightingEffect.Reactive, 1, reactiveEffect.Color, default, cancellationToken);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Exo/Devices/Exo.Devices.Razer/RazerDeviceFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace Exo.Devices.Razer;
public enum RazerDeviceFeature : byte
{
General = 0x00,
LightingLegacy = 0x03,
LightingV1 = 0x03,
Mouse = 0x04,
Power = 0x07,
Lighting = 0x0F,
LightingV2 = 0x0F,
}
Loading

0 comments on commit 1ee406e

Please sign in to comment.