Nfw.Linux.FrameBuffer 1.0.1

.NET 6.0
dotnet add package Nfw.Linux.FrameBuffer --version 1.0.1
NuGet\Install-Package Nfw.Linux.FrameBuffer -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nfw.Linux.FrameBuffer" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nfw.Linux.FrameBuffer --version 1.0.1
#r "nuget: Nfw.Linux.FrameBuffer, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Nfw.Linux.FrameBuffer as a Cake Addin
#addin nuget:?package=Nfw.Linux.FrameBuffer&version=1.0.1

// Install Nfw.Linux.FrameBuffer as a Cake Tool
#tool nuget:?package=Nfw.Linux.FrameBuffer&version=1.0.1

Nfw.Linux.FrameBuffer

An easy to use library for interacting with the Linux framebuffer interface (/dev/fbX).

NuGet

dotnet add package Nfw.Linux.FrameBuffer

Samples: Get Info From Display

This small snippet will open the provided device and use ioctls to discover its identifier and display parameters:

using(RawFrameBuffer fb = new RawFrameBuffer("/dev/fb0")) {
    Console.WriteLine($"Display Device => {fb.Device} Name => {fb.Id} Width => {fb.PixelWidth} Height => {fb.PixelHeight} Bpp => {fb.PixelDepth}");
}

Samples: Write Pixels

This snippet shows how you can write raw pixel data (on a 16bpp screen this shows as Black and Red hatch):

using(RawFrameBuffer fb = new RawFrameBuffer("/dev/fb0")) {                
    // For each pixel, flop between 0x00 and 0x80
    byte[] data = new byte[fb.PixelWidth * fb.PixelHeight * (fb.PixelDepth / 8)];
    byte flopMe = 0x00;
    for (int x = 0; x < data.Length; x += (fb.PixelDepth / 8)) {
        for (int pixelByte = 0; pixelByte < (fb.PixelDepth / 8); pixelByte++) {
            data[x + pixelByte] = flopMe;                    
        }
        flopMe = (flopMe == 0x00) ? (byte) 0x80 : (byte) 0x00;
    }
    fb.WriteRaw(data);    
}

Samples: Displaying an Image

This snippet uses the fantastic ImageSharp library to load an image, which must be in the same bitdepth as the framebuffer, then write it to the framebuffer:

using(RawFrameBuffer fb = new RawFrameBuffer("/dev/fb0")) {                
    byte[] pixelBytes = new byte[fb.PixelWidth * fb.PixelHeight * (fb.PixelDepth / 8)];

    // Load from source
    using(Image loadedImage = Image.Load(filename)) {
        // Resize to fit screen
        loadedImage.Mutate(x => {            
            x.Resize(fb.PixelWidth, fb.PixelHeight);            
        });

        // Write the raw pixel data into a buffer
        loadedImage.CopyPixelDataTo(pixelBytes);
        fb.WriteRaw(pixelBytes);
    }
}

More useful is converting from whatever the image format is on disk, to the framebuffer's format, see the DisplayImage Sample - which uses ImageSharp AND convert the image to the same pixel format as the display before writing the pixel data itself to the framebuffer.

Notes

  • The RawFrameBuffer constructor can optionally be told NOT to probe via ioctl on construction, instead post-construction you must call RefreshDeviceInfo() before you can render anything.
  • The RawFrameBuffer constructor can also be optionally provided an ILogger, perhaps via IoC, for logging/diagnostics.
  • In the case where your FB device can be mirrored, I suggest writing to the fastest (i.e. /dev/fb0 is often HW accelerated) then mirroring via something like the raspi2fb tool to effectively 'double buffer'.

References

Attribution

IMAGE LICENSE

The Sample Image used is an original watercolor by the author's wife: Katie Yocom. License for this file is CC BY-NC-ND 4.0.

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 65 8/18/2022