Skip to content

Commit

Permalink
First tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davepl committed Dec 9, 2023
1 parent 00f5a3c commit b4f75d2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 50 deletions.
12 changes: 9 additions & 3 deletions NightDriverServer.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30406.217
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NightDriverServer", "NightDriverServer.csproj", "{8EB8AFE9-C498-4960-B880-D66EB72148C0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NightDriverServer", "NightDriverServer.csproj", "{8EB8AFE9-C498-4960-B880-D66EB72148C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NightDriverDesktop", "..\NightDriverDesktop\NightDriverDesktop.csproj", "{ECF1EECD-18CF-4815-9216-31D0BA5B026D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{8EB8AFE9-C498-4960-B880-D66EB72148C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EB8AFE9-C498-4960-B880-D66EB72148C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EB8AFE9-C498-4960-B880-D66EB72148C0}.Release|Any CPU.Build.0 = Release|Any CPU
{ECF1EECD-18CF-4815-9216-31D0BA5B026D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ECF1EECD-18CF-4815-9216-31D0BA5B026D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ECF1EECD-18CF-4815-9216-31D0BA5B026D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ECF1EECD-18CF-4815-9216-31D0BA5B026D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 0 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ public class Program
{
public static void Main(string[] args)
{
Thread ledControlThread = new Thread(NightDriver.ConsoleApp.Start);
ledControlThread.IsBackground = true;

var host = CreateHostBuilder(args).Build();
var hostTask = host.StartAsync();

ledControlThread.Start();

while (ledControlThread.IsAlive)
Thread.Sleep(1000);

host.StopAsync();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Expand Down
17 changes: 0 additions & 17 deletions Server/LEDControllerChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,6 @@ protected virtual byte[] GetDataFrame(CRGB[] MainLEDs, DateTime timeStart)
throw new ApplicationException("Should never hit base class GetDataFrame");
}

/*
protected virtual byte[] GetClockFrame(DateTime timeStart)
{
// The timeOffset is how far in the future frames are generated for. If the chips have a 2 second buffer, you could
// go up to 2 seconds, but I shoot for the middle of the buffer depth.
double epoch = (DateTime.UtcNow.Ticks - DateTime.UnixEpoch.Ticks) / (double)TimeSpan.TicksPerSecond;
ulong seconds = (ulong)epoch; // Whole part of time number (left of the decimal point)
ulong uSeconds = (ulong)((epoch - (int)epoch) * 1000000); // Fractional part of time (right of the decimal point)
return LEDInterop.CombineByteArrays(LEDInterop.WORDToBytes((UInt16)2), // Command, which is 2 for us
LEDInterop.WORDToBytes((UInt16)0), // LED channel on ESP32
LEDInterop.ULONGToBytes((UInt64)seconds), // Number of LEDs
LEDInterop.ULONGToBytes((UInt64)uSeconds) // Timestamp seconds
); // Color Data
}
*/
byte[] CompressFrame(byte[] data)
{
const int COMPRESSED_HEADER_TAG = 0x44415645; // Magic "DAVE" tag for compressed data - replaces size field
Expand Down
2 changes: 1 addition & 1 deletion Server/LightStrip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public double TimeOffset
get
{
if (0 == Location.FramesPerSecond) // No speed indication yet, can't guess at offset, assume 1 second for now
return 1.0;
return 0.0;

double offset = (FramesPerBuffer * PercentBufferUse) / Location.FramesPerSecond;
return offset;
Expand Down
12 changes: 6 additions & 6 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace NightDriver
{
internal class ConsoleApp
{
private static bool bShouldExit = false;
CancellationToken _token;
public static Statistics Stats = new Statistics();

// REVIEW - Best was I can find at the moment to conirm whether a console is present.
Expand Down Expand Up @@ -58,7 +58,7 @@ internal static bool SystemHasConsole

internal static Location [] g_AllSites =
{
new ChristmasPresents { FramesPerSecond = 30 },
new CeilingStrip { FramesPerSecond = 30 },
new ChristmasTruck { FramesPerSecond = 45 },
new Pillars() { FramesPerSecond = 30 },

Expand Down Expand Up @@ -91,11 +91,11 @@ protected static void myCancelKeyPressHandler(object sender, ConsoleCancelEventA

// Set the Cancel property to true to prevent the process from terminating.
Console.WriteLine("Setting the bShouldExit property to true...");
bShouldExit = true;

args.Cancel = false;
}

internal static void Start()
internal void Start(CancellationToken token)
{
// Establish an event handler to process key press events.
if (SystemHasConsole)
Expand All @@ -104,9 +104,9 @@ internal static void Start()
DateTime lastStats = DateTime.UtcNow - TimeSpan.FromDays(1);

foreach (var site in g_AllSites)
site.StartWorkerThread();
site.StartWorkerThread(token);

while (!bShouldExit)
while (!token.IsCancellationRequested)
{
double d = (DateTime.UtcNow - lastStats).TotalMilliseconds;
// Don't update the stats more than ever 100ms
Expand Down
23 changes: 12 additions & 11 deletions Server/SiteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,20 @@ public LEDEffect LEDEffect

// Location
//
// A "site" is a set of one or more LED strip controllers and the effects that will run on them. It
// A "location" is a set of one or more LED strip controllers and the effects that will run on them. It
// implements the "GraphicsBase" interface so that the effects can draw upon the "site" as a whole,
// and it is later divied up to the various controllers. So if you have 4000 LEDs, you might have
// four strips with 1000 LEDs each, for example. Combined with a list of effects, they consitute a site.

public class Location : GraphicsBase
{
protected CancellationToken _token;
protected DateTime StartTime;
protected System.Threading.Thread _Thread;
protected virtual CRGB[] LEDs { get; }
public virtual LightStrip[] LightStrips { get; }
public virtual ScheduledEffect[] LEDEffects { get; }

public const int PIXELS_PER_METER144 = 144;

public Location()
Expand Down Expand Up @@ -125,11 +126,11 @@ public uint SpareTime
void WorkerDrawAndSendLoop()
{
DateTime lastSpareTimeReset = DateTime.UtcNow;
DateTime timeLastFrame = DateTime.UtcNow - TimeSpan.FromSeconds(1.0 / FramesPerSecond);
DateTime timeLastFrame = DateTime.UtcNow -TimeSpan.FromSeconds((FramesPerSecond == 0 ? 0 : 1.0 / FramesPerSecond));

for (;;)
while (!_token.IsCancellationRequested)
{
DateTime timeNext = timeLastFrame + TimeSpan.FromSeconds(1.0 / FramesPerSecond);
DateTime timeNext = timeLastFrame + TimeSpan.FromSeconds(1.0 / (FramesPerSecond > 0 ? FramesPerSecond : 30));
DrawAndEnqueueAll(timeNext);
timeLastFrame = timeNext;

Expand Down Expand Up @@ -157,15 +158,15 @@ void WorkerDrawAndSendLoop()
}
}

public void StartWorkerThread()
public void StartWorkerThread(CancellationToken token)
{
foreach (var strip in LightStrips)
strip.Location = this;


_token = token;
_Thread = new Thread(WorkerDrawAndSendLoop);
_Thread.IsBackground = true;
_Thread.Priority = ThreadPriority.BelowNormal;
_Thread.Priority = ThreadPriority.Normal;
_Thread.Start();
}

Expand Down Expand Up @@ -1079,16 +1080,16 @@ public class Bench : Location
//
// Location definition for the test rig on the workbench

public class ChristmasPresents : Location
public class CeilingStrip : Location
{
const int START = 0;
const int LENGTH = 3*32 + 50;
const int LENGTH = 5*144 + 38;

private CRGB[] _LEDs = InitializePixels<CRGB>(LENGTH);

private LightStrip[] _StripControllers =
{
new LightStrip("192.168.8.67", "FireTruck", true, LENGTH, 1, START, true, 0, false) { FramesPerBuffer = 500, BatchSize = 10 } // 216
new LightStrip("192.168.8.196", "Ceiling", true, LENGTH, 1, START, true, 0, false) { FramesPerBuffer = 10, BatchSize = 1 } // 216
};

public ScheduledEffect[] _LEDEffects =
Expand Down

0 comments on commit b4f75d2

Please sign in to comment.