Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
- fixing idle timeout continuously looping
- tweaked default hold timings to flicker less
- added framebuffer test that screen captures from the mouse position
- enabled xml docs... still filling these out
  • Loading branch information
Effix committed Dec 9, 2015
1 parent 8cf0944 commit 6d4135e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 17 deletions.
36 changes: 27 additions & 9 deletions LedBadge/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ enum DisplayMode
Nothing,
Messages,
TestFill,
TestCopy
TestCopy,
TestFrame
}

class MainViewModel: INotifyPropertyChanged
Expand All @@ -34,7 +35,7 @@ public MainViewModel(Dispatcher dispatcher, Action<UIElement> logFunc)
m_frameTimer.Start();

HoldTimingA = 1;
HoldTimingB = 4;
HoldTimingB = 3;
HoldTimingC = 4;
IdleFade = true;
IdleResetToBootImage = true;
Expand Down Expand Up @@ -89,7 +90,7 @@ public DisplayMode DisplayMode
set
{
m_displayMode = value;
m_badgePump.UseFrameBuffer = m_displayMode == DisplayMode.Messages;
m_badgePump.UseFrameBuffer = m_displayMode == DisplayMode.Messages || m_displayMode == DisplayMode.TestFrame;
}
}

Expand Down Expand Up @@ -147,10 +148,22 @@ void InitScene()

void OnRenderFrame(object sender, LedBadgeLib.BadgeFrameEventArgs args)
{
LedBadgeLib.BadgePump pump = (LedBadgeLib.BadgePump)sender;
m_messageScene.Update(1.0f / pump.FrameRate);
m_messageScene.Render(args.Frame, 0, 0);
Frame++;
switch(DisplayMode)
{
case DisplayMode.Messages:
{
LedBadgeLib.BadgePump pump = (LedBadgeLib.BadgePump)sender;
m_messageScene.Update(1.0f / pump.FrameRate);
m_messageScene.Render(args.Frame, 0, 0);
Frame++;
break;
}
case DisplayMode.TestFrame:
{
TestFrame(args.Frame);
break;
}
}
}

void OnFrameReady(object sender, LedBadgeLib.BadgeFrameEventArgs args)
Expand Down Expand Up @@ -181,12 +194,12 @@ void OnGenerateCommands(object sender, LedBadgeLib.BadgeCommandEventArgs args)
{
switch(DisplayMode)
{
case LedBadge.DisplayMode.TestFill:
case DisplayMode.TestFill:
{
TestPattern(args.CommandStream);
break;
}
case LedBadge.DisplayMode.TestCopy:
case DisplayMode.TestCopy:
{
TestScroll(args.CommandStream);
break;
Expand Down Expand Up @@ -223,6 +236,11 @@ void TestScroll(MemoryStream commands)
LedBadgeLib.BadgeCommands.Swap(commands);
}

void TestFrame(LedBadgeLib.BadgeRenderTarget frame)
{
LedBadgeLib.ScreenCapture.ReadScreenAtMousePosition(frame.IntermediateImage, LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height);
}

void OnBadgeResponse(object sender, LedBadgeLib.BadgeResponseEventArgs args)
{
Dispatcher.InvokeAsync(() => LogMessage(args.Code, args.Response));
Expand Down
6 changes: 3 additions & 3 deletions LedBadge/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@
<Button Content="Set Startup Image" Click="SetBootImage" Style="{StaticResource EnableWhenConnected}"/>
<DockPanel Margin="0,5,0,1" ToolTip="Hold for the darker gray value">
<TextBlock DockPanel.Dock="Left" Text="Hold Timing A" Margin="0,0,5,0"/>
<Slider Minimum="0" Maximum="15" Value="{Binding HoldTimingA}"/>
<Slider Minimum="1" Maximum="15" Value="{Binding HoldTimingA}"/>
</DockPanel>
<DockPanel Margin="0,0,0,1" ToolTip="Hold for the lighter gray value (additive with A)">
<TextBlock DockPanel.Dock="Left" Text="Hold Timing B" Margin="0,0,5,0"/>
<Slider Minimum="0" Maximum="15" Value="{Binding HoldTimingB}"/>
<Slider Minimum="1" Maximum="15" Value="{Binding HoldTimingB}"/>
</DockPanel>
<DockPanel Margin="0,0,0,1" ToolTip="Hold for the brightest value (additive with A and B)">
<TextBlock DockPanel.Dock="Left" Text="Hold Timing C" Margin="0,0,5,0"/>
<Slider Minimum="0" Maximum="15" Value="{Binding HoldTimingC}"/>
<Slider Minimum="1" Maximum="15" Value="{Binding HoldTimingC}"/>
</DockPanel>
<Button Content="Set Hold Timings" Click="SetHoldTimings" Style="{StaticResource EnableWhenConnected}"/>
<CheckBox Margin="0,5,0,0" ToolTip="Enable fading when the idle reset kicks in" Content="Idle Fade out" IsChecked="{Binding IdleFade}"/>
Expand Down
2 changes: 1 addition & 1 deletion LedBadge/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MainWindow()

void LogMessage(UIElement messageElement)
{
int maxMessages = 512;
int maxMessages = 128;
if(Log.Children.Count >= maxMessages)
{
Log.Children.RemoveRange(0, Log.Children.Count - maxMessages + 1);
Expand Down
7 changes: 5 additions & 2 deletions LedBadgeFirmware/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ void SetIdleTimeout(unsigned char fade, unsigned char resetToBootImage, unsigned
void ResetIdleTime()
{
g_DisplayReg.TimeoutCounter = 0;
g_DisplayReg.TimeoutAllowUpdate = true;
}

// Stops and clears any in progress fade
Expand Down Expand Up @@ -324,7 +325,7 @@ static inline void LatchInBrightness()
// Updates the timeout state machine
static inline void PumpTimeout()
{
if(g_DisplayReg.TimeoutTrigger < 255 && g_DisplayReg.FadeState == FadingAction::None)
if(g_DisplayReg.TimeoutTrigger < 255 && g_DisplayReg.FadeState == FadingAction::None && g_DisplayReg.TimeoutAllowUpdate)
{
if(g_DisplayReg.TimeoutCounter >= g_DisplayReg.TimeoutTrigger)
{
Expand All @@ -345,6 +346,8 @@ static inline void PumpTimeout()

g_DisplayReg.TimeoutCounter = 0;
}

g_DisplayReg.TimeoutAllowUpdate = false;
}
else
{
Expand Down Expand Up @@ -422,7 +425,7 @@ void ConfigureDisplay()
g_DisplayReg.BackBuffer = g_Buffer1;
g_DisplayReg.BrightnessLevel = BrightnessLevels / 2;
g_DisplayReg.GammaTable[0] = 1;
g_DisplayReg.GammaTable[1] = 4;
g_DisplayReg.GammaTable[1] = 3;
g_DisplayReg.GammaTable[2] = 4;
g_DisplayReg.Y = BufferHeight - 1;
g_DisplayReg.Half = 1;
Expand Down
1 change: 1 addition & 0 deletions LedBadgeFirmware/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct DisplayState
unsigned char BitPlaneHold; // remaining count on current bit-plane
const unsigned char *BufferP; // points at the next 8 pixels to go out
volatile bool FrameChanged; // true if frame just changed
volatile bool TimeoutAllowUpdate; // true if timeout counter can change
unsigned char TimeoutTrigger; // idle frame count threshold
unsigned char TimeoutCounter; // idle frames so far...
unsigned char FadeState; // current action for the fade state machine
Expand Down
72 changes: 72 additions & 0 deletions LedBadgeLib/BadgeCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,98 @@

namespace LedBadgeLib
{
/// <summary>
/// Raw command code values that begin the command packets for the badge.
/// </summary>
public enum CommandCodes: byte
{
/// <summary>No action.</summary>
Nop,
/// <summary>Asks the badge to return the given cookie.</summary>
Ping,
/// <summary>Requests the version of the running firmware.</summary>
Version,
/// <summary>Swaps the front/back render targets.</summary>
Swap,
/// <summary>Requests the button state.</summary>
PollInputs,
/// <summary>Adjusts the overall output brightness of the leds.</summary>
SetBrightness,
/// <summary>Writes a single pixel value to a buffer.</summary>
SetPix,
/// <summary>Requests a single pixel value from a buffer.</summary>
GetPix,
/// <summary>Requests a block of pixels from a buffer.</summary>
GetPixRect,
/// <summary>Writes a single value to a block of pixels in a buffer.</summary>
SolidFill,
/// <summary>Sets a block of pixels in a buffer to the given data (2bpp packed).</summary>
Fill,
/// <summary>Copies a block of pixels from a location in a buffer to another.</summary>
Copy,
/// <summary>Sets the initial frame when first powered up (saves the front buffer to non-volatile memory).</summary>
SetPowerOnImage,
/// <summary>Controls the gray scale levels by setting the hold levels between the bit-planes.</summary>
SetHoldTimings,
/// <summary>Sets the idle timeout duration and behavior.</summary>
SetIdleTimeout,
/// <summary>Queries the state of the input buffer.</summary>
GetBufferFullness
}

/// <summary>
/// Identifiers for the command read and write locations.
/// </summary>
public enum Target: byte
{
/// <summary>The buffer not being displayed. This can be modified without seeing flicker.</summary>
BackBuffer,
/// <summary>The buffer being scanned out to the display.</summary>
FrontBuffer
}

/// <summary>
/// Constant metrics describing different aspects of the badge.
/// </summary>
public static class BadgeCaps
{
/// <summary>Pixels across.</summary>
public const int Width = 48;
/// <summary>Pixels tall.</summary>
public const int Height = 12;
/// <summary>Number of bits per pixel in a packed image buffer.</summary>
public const int BitsPerPixel = 2;
/// <summary>Number of colors per pixel in a packed image buffer.</summary>
public const int ColorValues = 4;
/// <summary>Number of pixels per byte in a packed image buffer.</summary>
public const int PixelsPerByte = 8 / BitsPerPixel;
/// <summary>Number of bytes for an entire row of pixels for a full screen packed image buffer.</summary>
public const int FrameStride = Width * BitsPerPixel / 8;
/// <summary>Number of bytes for a full screen packed image buffer.</summary>
public const int FrameSize = FrameStride * Height;
/// <summary>Number of bytes for an entire row of pixels for a full screen unpacked image buffer (i.e., one byte per pixel).</summary>
public const int IntermediateFrameStride = Width;
/// <summary>Number of bytes for a full screen unpacked image buffer (i.e., one byte per pixel).</summary>
public const int IntermediateFrameSize = IntermediateFrameStride * Height;
}

/// <summary>
/// Methods to construct badge commands into a stream of data.
/// </summary>
public static class BadgeCommands
{
/// <summary>
/// No action.
/// </summary>
public static void Nop(Stream stream)
{
stream.WriteByte((byte)CommandCodes.Nop << 4);
}

/// <summary>
/// Asks the badge to return the given cookie.
/// <param name="cookie">A 4-bit value ranging from 0 to 15.</param>
/// </summary>
public static void Ping(Stream stream, int cookie)
{
System.Diagnostics.Debug.Assert(cookie >= 0);
Expand All @@ -61,27 +107,47 @@ public static void Ping(Stream stream, int cookie)
stream.WriteByte((byte)(((byte)CommandCodes.Ping << 4) | cookie));
}

/// <summary>
/// Requests the version of the running firmware.
/// </summary>
public static void GetVersion(Stream stream)
{
stream.WriteByte((byte)CommandCodes.Version << 4);
}

/// <summary>
/// Swaps the front/back render targets.
/// </summary>
public static void Swap(Stream stream)
{
stream.WriteByte((byte)CommandCodes.Swap << 4);
}

/// <summary>
/// Requests the button state.
/// </summary>
public static void PollInputs(Stream stream)
{
stream.WriteByte((byte)CommandCodes.PollInputs << 4);
}

/// <summary>
/// Adjusts the overall output brightness of the leds.
/// <param name="brightness">A value ranging from 0 to 255.</param>
/// </summary>
public static void SetBrightness(Stream stream, byte brightness)
{
stream.WriteByte((byte)CommandCodes.SetBrightness << 4);
stream.WriteByte(brightness);
}

/// <summary>
/// Writes a single pixel value to a buffer.
/// <param name="x">The horizontal location ranging from 0 to 41.</param>
/// <param name="y">The vertical location ranging from 0 to 11.</param>
/// <param name="target">The buffer to modify.</param>
/// <param name="color">The value (0-3) to send.</param>
/// </summary>
public static void SetPixel(Stream stream, int x, int y, Target target, int color)
{
System.Diagnostics.Debug.Assert(x >= 0);
Expand All @@ -95,6 +161,12 @@ public static void SetPixel(Stream stream, int x, int y, Target target, int colo
stream.WriteByte((byte)(((byte)y << 4) | ((byte)target << 2) | color));
}

/// <summary>
/// Requests a single pixel value from a buffer.
/// <param name="x">The horizontal location ranging from 0 to 41.</param>
/// <param name="y">The vertical location ranging from 0 to 11.</param>
/// <param name="target">The buffer to query.</param>
/// </summary>
public static void GetPixel(Stream stream, int x, int y, Target target)
{
System.Diagnostics.Debug.Assert(x >= 0);
Expand Down
2 changes: 2 additions & 0 deletions LedBadgeLib/LedBadgeLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\LedBadgeLib.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,6 +29,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\LedBadgeLib.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
4 changes: 2 additions & 2 deletions LedBadgeUtilLib/Image/ScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
static extern IntPtr GetWindowDC(IntPtr ptr);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
Expand Down
2 changes: 2 additions & 0 deletions LedBadgeUtilLib/LedBadgeUtilLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\LedBadgeUtilLib.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,6 +29,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\LedBadgeUtilLib.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
Expand Down

0 comments on commit 6d4135e

Please sign in to comment.