Skip to content

Commit

Permalink
Add 80% battery charge limit for Legion Go (#1074)
Browse files Browse the repository at this point in the history
* Add 80% battery charge limit for Legion Go

* removed UpdateSettings()

not needed, SettingsManager will iterate through all settings on startup anyway

---------

Co-authored-by: Lesueur Benjamin <[email protected]>
  • Loading branch information
moyogii and Valkirie authored Jun 4, 2024
1 parent 9655ffd commit 9022c50
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 5 deletions.
3 changes: 3 additions & 0 deletions HandheldCompanion/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@
<setting name="LegionControllerGyroIndex" serializeAs="String">
<value>0</value>
</setting>
<setting name="LegionBatteryChargeLimit" serializeAs="String">
<value>False</value>
</setting>
</HandheldCompanion.Properties.Settings>
</userSettings>
</configuration>
27 changes: 23 additions & 4 deletions HandheldCompanion/Devices/Lenovo/LegionGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public Task SetCPUPowerLimit(CapabilityID capabilityID, int limit) =>
{ "value", limit },
});

// InstantBootAc (0x03010001) controls the 80% power charge limit.
// https://github.com/aarron-lee/LegionGoRemapper/blob/ab823f2042fc857cca856687a385a033d68c58bf/py_modules/legion_space.py#L138
public Task SetBatteryChargeLimit(bool enabled) =>
WMI.CallAsync("root\\WMI",
$"SELECT * FROM LENOVO_OTHER_METHOD",
"SetFeatureValue",
new()
{
{ "IDs", (int)CapabilityID.InstantBootAc },
{ "value", enabled ? 1 : 0 },
});

public const byte INPUT_HID_ID = 0x04;

public override bool IsOpen => hidDevices.ContainsKey(INPUT_HID_ID) && hidDevices[INPUT_HID_ID].IsOpen;
Expand Down Expand Up @@ -211,10 +223,7 @@ public LegionGo()
DefaultLayout.ButtonLayout[ButtonFlags.B7] = new List<IActions>() { new MouseActions { MouseType = MouseActionsType.ScrollUp } };
DefaultLayout.ButtonLayout[ButtonFlags.B8] = new List<IActions>() { new MouseActions { MouseType = MouseActionsType.ScrollDown } };

/*
Task<bool> task = Task.Run(async () => await GetFanFullSpeedAsync());
bool FanFullSpeed = task.Result;
*/
SettingsManager.SettingValueChanged += SettingsManager_SettingValueChanged;
}

private void PowerProfileManager_Applied(PowerProfile profile, UpdateSource source)
Expand Down Expand Up @@ -427,4 +436,14 @@ public override string GetGlyph(ButtonFlags button)

return defaultGlyph;
}

private void SettingsManager_SettingValueChanged(string name, object value)
{
switch (name)
{
case "LegionBatteryChargeLimit":
SetBatteryChargeLimit(Convert.ToBoolean(value));
break;
}
}
}
18 changes: 18 additions & 0 deletions HandheldCompanion/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions HandheldCompanion/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2994,4 +2994,10 @@ Motion input toggle: press selected button(s) to switch motion state.</value>
<data name="PowerProfileMSIClawBetterPerformanceDesc" xml:space="preserve">
<value>The best system settings that MSI recommends.</value>
</data>
<data name="DevicePage_LegionBatteryChargeLimit" xml:space="preserve">
<value>80% Battery Charge Limit</value>
</data>
<data name="DevicePage_LegionBatteryChargeLimit_Desc" xml:space="preserve">
<value>Sets the maximum charge level of the battery to 80%</value>
</data>
</root>
15 changes: 15 additions & 0 deletions HandheldCompanion/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,8 @@
<Setting Name="LegionControllerGyroIndex" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="LegionBatteryChargeLimit" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
2 changes: 1 addition & 1 deletion HandheldCompanion/Views/Pages/AboutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
Margin="0,0,0,5"
Foreground="{DynamicResource SystemControlForegroundBaseMediumBrush}"
Style="{StaticResource BodyTextBlockStyle}"
Text="Nefarius, CasperH2O, B-Core, Frank东, Toomy, Havner, CoryManson, MSeys, ShadowFlare, trippyone, MiguelLedesmaC, Cheng77777, thororen1234, fighterguard, micdah, Geckon01, Bagboii, MeikoMenmaHonma, cerahmed, indiesaudi, Radther, Staubgeborener, twjmy, m33ts4k0z, howanghk, Creaous, xerootg, quangmach, MrCivsteR, 0SkillAllLuck, DevL0rd, romracer, JT" />
Text="Nefarius, CasperH2O, B-Core, Frank东, Toomy, Havner, CoryManson, MSeys, ShadowFlare, trippyone, MiguelLedesmaC, Cheng77777, thororen1234, fighterguard, micdah, Geckon01, Bagboii, MeikoMenmaHonma, cerahmed, indiesaudi, Radther, Staubgeborener, twjmy, m33ts4k0z, howanghk, Creaous, xerootg, quangmach, MrCivsteR, 0SkillAllLuck, DevL0rd, romracer, JT, PlasmidEve" />

<!-- Description -->
<TextBlock
Expand Down
38 changes: 38 additions & 0 deletions HandheldCompanion/Views/Pages/DevicePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,44 @@
</Grid>
</Border>

<Border
Name="BatteryChargeLimit"
Padding="15,12,12,12"
Background="{DynamicResource ExpanderHeaderBackground}"
CornerRadius="{DynamicResource ControlCornerRadius}">

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" MinWidth="200" />
<ColumnDefinition Width="2*" MinWidth="200" />
</Grid.ColumnDefinitions>

<DockPanel>
<ui:FontIcon
Height="40"
HorizontalAlignment="Center"
FontFamily="{DynamicResource SymbolThemeFontFamily}"
Glyph="&#xE862;" />

<ui:SimpleStackPanel Margin="12,0,0,0" VerticalAlignment="Center">
<TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{x:Static resx:Resources.DevicePage_LegionBatteryChargeLimit}" />
<TextBlock
Foreground="{DynamicResource SystemControlForegroundBaseMediumBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{x:Static resx:Resources.DevicePage_LegionBatteryChargeLimit_Desc}"
TextWrapping="Wrap" />
</ui:SimpleStackPanel>
</DockPanel>

<ui:ToggleSwitch
Name="Toggle_LegionBatteryChargeLimit"
Grid.Column="1"
HorizontalAlignment="Right"
Style="{DynamicResource InvertedToggleSwitchStyle}"
Toggled="Toggle_LegionBatteryChargeLimit_Toggled" />
</Grid>
</Border>

<Border
Name="GyroController"
Padding="15,12,12,12"
Expand Down
8 changes: 8 additions & 0 deletions HandheldCompanion/Views/Pages/DevicePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@ private void Toggle_TouchpadPassthrough_Toggled(object sender, RoutedEventArgs e
SettingsManager.SetProperty("LegionControllerPassthrough", Toggle_TouchpadPassthrough.IsOn);
}

private void Toggle_LegionBatteryChargeLimit_Toggled(object sender, RoutedEventArgs e)
{
if (!IsLoaded)
return;

SettingsManager.SetProperty("LegionBatteryChargeLimit", Toggle_LegionBatteryChargeLimit.IsOn);
}

private void ComboBox_GyroController_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!IsLoaded)
Expand Down

0 comments on commit 9022c50

Please sign in to comment.