Skip to content

Commit

Permalink
Add feature to YAMDCC service to copy fan curve from EC to loaded config
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparronator9999 committed Sep 28, 2024
1 parent c0249d1 commit 50cee9e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
16 changes: 16 additions & 0 deletions YAMDCC.IPC/ServiceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ public enum Command
/// maximum brightness value (minus offset).
/// </remarks>
SetKeyLightBright,
/// <summary>
/// Attempts to write the default fan curves
/// of each fan from the EC to the config.
/// </summary>
/// <remarks>
/// <para>This command expects no arguments.</para>
/// <para>
/// If the loaded config is a template config, it
/// will be made into a full (non-template) config.
/// </para>
/// <para>
/// If there is no config loaded, this command will issue a
/// <see cref="Response.Error"/> to the requesting client.
/// </para>
/// </remarks>
FanCurveECToConf,
}

/// <summary>
Expand Down
67 changes: 67 additions & 0 deletions YAMDCC.Service/FanControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ private void IPCClientMessage(object sender, PipeMessageEventArgs<ServiceCommand
case Command.SetKeyLightBright:
error = SetKeyLightBright(e.Connection.ID, e.Message.Arguments);
break;
case Command.FanCurveECToConf:
error = FanCurveECToConf(e.Connection.ID);
break;
default: // Unknown command
Log.Error(Strings.GetString("errBadCmd"), e.Message);
break;
Expand Down Expand Up @@ -755,5 +758,69 @@ private int SetKeyLightBright(int clientId, string args)
}
return 2;
}

private int FanCurveECToConf(int clientId)
{
if (!ConfigLoaded)
{
return 0;
}

Log.Debug("Getting fan curve from EC...");
if (EC.AcquireLock(500))
{
for (int i = 0; i < Config.FanConfs.Length; i++)
{
FanConf cfg = Config.FanConfs[i];

if (cfg.FanCurveConfs is null || cfg.FanCurveConfs.Length == 0)
{
cfg.FanCurveConfs = new FanCurveConf[1];
cfg.FanCurveConfs[0] = new()
{
Name = "Default",
Desc = "Default fan curve (auto-generated by YAMDCC)",
TempThresholds = new TempThreshold[cfg.FanCurveRegs.Length],
};
}

for (int j = 0; j < cfg.FanCurveRegs.Length; j++)
{
FanCurveConf curveCfg = cfg.FanCurveConfs[0];
if (curveCfg.TempThresholds[j] is null)
{
curveCfg.TempThresholds[j] = new();
}

if (_EC.ReadByte(cfg.FanCurveRegs[j], out byte value))
{
curveCfg.TempThresholds[j].FanSpeed = value;
}

if (j == 0)
{
curveCfg.TempThresholds[j].UpThreshold = 0;
curveCfg.TempThresholds[j].DownThreshold = 0;
}
else
{
if (_EC.ReadByte(cfg.UpThresholdRegs[j], out value))
{
curveCfg.TempThresholds[j].UpThreshold = value;
}
if (_EC.ReadByte(cfg.DownThresholdRegs[j], out value))
{
curveCfg.TempThresholds[j].DownThreshold = value;
}
}
}
}
}

Log.Debug("Saving config...");
Config.Save(Path.Combine(DataPath, "CurrentConfig.xml"));

return 0;
}
}
}

0 comments on commit 50cee9e

Please sign in to comment.