Skip to content

Commit

Permalink
Add --flash command line option
Browse files Browse the repository at this point in the history
  Closes #19
  • Loading branch information
tylerszabo committed Apr 27, 2018
1 parent d013e93 commit 523275f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
11 changes: 11 additions & 0 deletions GLedApiDotNet/LedSettings/FlashLedSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,16 @@ public FlashLedSetting(Color color, byte maxBrightness, byte minBrightness, Time

CheckRanges();
}

public TimeSpan OnOffTime => TimeSpan0;
public TimeSpan IntervalTime => TimeSpan1;
public TimeSpan CycleTime => TimeSpan2;
public byte Count => CtrlValue0;

public override string ToString()
{
return string.Format("Flash: Color={0}, MaxBrightness={1}, MinBrightness={2}, OnOffTime={3}s, IntervalTime={4}s, CycleTime={5}s, Count={6}",
Color, MaxBrightness, MinBrightness, OnOffTime.TotalSeconds, IntervalTime.TotalSeconds, CycleTime.TotalSeconds, Count);
}
}
}
1 change: 1 addition & 0 deletions RGBFusionTool/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public Application(IRGBFusionMotherboard motherboardLEDs, TextWriter stdout, Tex
new StaticColorArgParser(),
new ColorCycleArgParser(),
new PulseArgParser(),
new FlashArgParser(),
new OffArgParser()
};

Expand Down
89 changes: 89 additions & 0 deletions RGBFusionTool/ArgParsers/FlashArgParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) 2018 Tyler Szabo
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

using GLedApiDotNet.LedSettings;
using Mono.Options;
using System;
using System.Collections.Generic;

namespace RGBFusionTool.ArgParsers
{
class FlashArgParser : LedSettingArgParser
{
private class FlashArgParserContext : ArgParserContext
{
public byte MaxBrightness { get; set; }
public byte MinBrightness { get; set; }
public double OnOff { get; set; }
public double Interval { get; set; }
public double Cycle { get; set; }
public byte Count { get; set; }

private string colorString;
public string ColorString
{
get => colorString; set
{
Valid = true;
colorString = value;
}
}

protected override void SetDefaults()
{
colorString = null;
MaxBrightness = 100;
MinBrightness = 0;
OnOff = 0.25;
Interval = 1;
Cycle = 5;
Count = 3;
}
}

FlashArgParserContext context;

private FlashArgParser(FlashArgParserContext context) : base(context)
{
this.context = context;
}

public FlashArgParser() : this(new FlashArgParserContext ())
{
RequiredOptions = new OptionSet
{
{ "Flash" },
{ "flash=", "flash color {COLOR}", v => context.ColorString = v },
};
ExtraOptions = new OptionSet
{
{ "maxbrightness=", "(optional) max brightness (0-100)", (byte b) => context.MaxBrightness = b },
{ "minbrightness=", "(optional) min brightness (0-100)", (byte b) => context.MinBrightness = b },
{ "time=", "(optional) {SECONDS} to flash for", (double d) => context.OnOff = d },
{ "interval=", "(optional) {SECONDS} in a flash interval", (double d) => context.Interval = d },
{ "flashcycle=", "(optional) {SECONDS} in a cycle", (double d) => context.Cycle = d },
{ "count=", "(optional) flash {COUNT} intervals in a cycle", (byte b) => context.Count = b },
{ "<>", v => throw new InvalidOperationException(string.Format("Unsupported option {0}", v)) }
};
}

public override LedSetting TryParse(IEnumerable<string> args)
{
if (!PopulateContext(args))
{
return null;
}

TimeSpan onoff = TimeSpan.FromSeconds(context.OnOff);
TimeSpan interval = TimeSpan.FromSeconds(context.Interval);
TimeSpan cycle = TimeSpan.FromSeconds(context.Cycle);

return new FlashLedSetting(GetColor(context.ColorString), context.MaxBrightness, context.MinBrightness, onoff, interval, cycle, context.Count);
}
}
}
1 change: 1 addition & 0 deletions RGBFusionTool/RGBFusionTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ You should have received a copy of the GNU General Public License along with thi
<ItemGroup>
<Compile Include="Application.cs" />
<Compile Include="ArgParsers\ColorCycleArgParser.cs" />
<Compile Include="ArgParsers\FlashArgParser.cs" />
<Compile Include="ArgParsers\LedSettingArgParser.cs" />
<Compile Include="ArgParsers\OffArgParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
36 changes: 36 additions & 0 deletions RGBFusionToolTests/RGBFusionToolExeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void Help(string[] args)
"static",
"colorcycle",
"pulse",
"flash",
"off",

"verbose",
Expand Down Expand Up @@ -381,6 +382,41 @@ public void Pulse_verbose(string[] args)
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "--flash=Blue", "--maxbrightness=100", "--minbrightness=10", "--time=0.3", "--interval=1", "--flashcycle=4", "--count=3" }, DisplayName = "--flash=Blue --maxbrightness=100 --minbrightness=10 --time=0.3 --interval=1 --flashcycle=4 --count=3")]
[DataTestMethod]
public void Flash(string[] args)
{
rgbFusionTool.Main(args);

StringAssert.DoesNotMatch(stderr.ToString(), ANY, "Expect stderr is empty");
StringAssert.DoesNotMatch(stdout.ToString(), ANY, "Expect stdout is empty");

TestHelper.AssertAllLeds(mock,
GLedApiDotNetTests.Tests.LedSettingTests.SettingByteArrays.FlashC,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "--verbose", "--flash=Blue", "--maxbrightness=100", "--minbrightness=10", "--time=0.3", "--interval=1", "--flashcycle=4", "--count=3" }, DisplayName = "--verbose --flash=Blue --maxbrightness=100 --minbrightness=10 --time=0.3 --interval=1 --flashcycle=4 --count=3")]
[DataTestMethod]
public void Flash_verbose(string[] args)
{
rgbFusionTool.Main(args);

StringAssert.DoesNotMatch(stderr.ToString(), ANY, "Expect stderr is empty");
StringAssert.Matches(stdout.ToString(), new Regex("\\bflash\\b", RegexOptions.IgnoreCase),"Expect stdout includes flash config");
StringAssert.Matches(stdout.ToString(), new Regex("\\bblue\\b", RegexOptions.IgnoreCase), "Expect stdout includes color");
StringAssert.Matches(stdout.ToString(), new Regex("\\b100\\b", RegexOptions.IgnoreCase),"Expect stdout includes max brightness");
StringAssert.Matches(stdout.ToString(), new Regex("\\b10\\b", RegexOptions.IgnoreCase),"Expect stdout includes min brightness");
StringAssert.Matches(stdout.ToString(), new Regex("\\b0?\\.3(0*)?\\s?s\\b", RegexOptions.IgnoreCase),"Expect stdout includes on/off time");
StringAssert.Matches(stdout.ToString(), new Regex("\\b1(\\.0*)?\\s?s\\b", RegexOptions.IgnoreCase),"Expect stdout includes interval time");
StringAssert.Matches(stdout.ToString(), new Regex("\\b4(\\.0*)?\\s?s\\b", RegexOptions.IgnoreCase),"Expect stdout includes cycle time");
StringAssert.Matches(stdout.ToString(), new Regex("\\b2\\b", RegexOptions.IgnoreCase),"Expect stdout includes flash count");

TestHelper.AssertAllLeds(mock,
GLedApiDotNetTests.Tests.LedSettingTests.SettingByteArrays.FlashC,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "--off" }, DisplayName = "--off")]
[DataTestMethod]
public void Off(string[] args)
Expand Down

0 comments on commit 523275f

Please sign in to comment.