From c34157e1ee7f8f15461b0db4e1b8c4e3e5f89a8a Mon Sep 17 00:00:00 2001 From: Sebastian Gingter Date: Sun, 29 Mar 2020 16:55:26 +0100 Subject: [PATCH] First draft of working library and package --- build.ps1 | 25 +++++++-- src/SharpPiLed/BdfFont.cs | 59 +++++++++++++++++++++- src/SharpPiLed/Bindings/RpiRgbLedMatrix.cs | 13 ----- src/SharpPiLed/SharpPiLed.csproj | 8 ++- src/examples/Matrix/Matrix.csproj | 1 - src/examples/NuGet.config | 6 +++ src/examples/SimpleText/Program.cs | 30 +++++++++++ src/examples/SimpleText/SimpleText.csproj | 13 +++++ 8 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 src/examples/NuGet.config create mode 100644 src/examples/SimpleText/Program.cs create mode 100644 src/examples/SimpleText/SimpleText.csproj diff --git a/build.ps1 b/build.ps1 index 2d964c9..298075a 100644 --- a/build.ps1 +++ b/build.ps1 @@ -1,8 +1,25 @@ # We require the C++ toolchain to build the native lib # as well as .NET core to build our library and samples. -make --directory ./rpi-rgb-led-matrix all -New-Item -Path ./rpi-rgb-led-matrix/lib/librgbmatrix.so -ItemType SymbolicLink -Value librgbmatrix.so.1 -ErrorAction SilentlyContinue +Write-Host "Building the native C++ library" +make --directory ./rpi-rgb-led-matrix | Out-Null +New-Item -Path ./rpi-rgb-led-matrix/lib/librgbmatrix.so -ItemType SymbolicLink -Value librgbmatrix.so.1 -Force | Out-Null -dotnet pack ./src/SharpPiLed/SharpPiLed.csproj -c Release -o ./nugets -dotnet build ./src/examples/Matrix/Matrix.csproj -c Release +Write-Host "Removing existing build artifacts and clearing local nuget cache" +Get-ChildItem .\ -include nugets,bin,obj -Recurse | + Foreach-Object { + Remove-Item $_.fullname -Force -Recurse + } + +dotnet nuget locals all --clear | Out-Null + +Write-Host "Build SharpPiLed library" +dotnet pack ./src/SharpPiLed -c Release -o ./nugets + +Write-Host "Build all example projects" +Get-ChildItem ./src/examples/ *.csproj -Recurse | + Foreach-Object { + $project = $_.fullname + Write-Host "Building $project" + dotnet publish $project -c Release + } diff --git a/src/SharpPiLed/BdfFont.cs b/src/SharpPiLed/BdfFont.cs index 2ddf2f1..b994cd9 100644 --- a/src/SharpPiLed/BdfFont.cs +++ b/src/SharpPiLed/BdfFont.cs @@ -2,6 +2,7 @@ namespace SharpPiLed { using System; using System.IO; + using System.Linq; using Bindings; /// @@ -25,12 +26,68 @@ public BdfFont(string bdfFontFile) if (!File.Exists(bdfFontFile)) { - throw new ArgumentException($"The provided file '{bdfFontFile}' needs to exist."); + // try local folder as an alternative + var alternateFontPath = Path.Combine( + Path.GetDirectoryName(GetType().Assembly.Location), + Path.GetFileName(bdfFontFile) + ); + + if (!File.Exists(alternateFontPath)) + { + throw new ArgumentException($"The provided file '{bdfFontFile}' needs to exist."); + } + + bdfFontFile = alternateFontPath; } _font = RpiRgbLedMatrix.load_font(bdfFontFile); } + /// + /// This assembly contains some bdf font files as embedded resources. This method will + /// write the files into a specified folder, so that they can be loaded from there. + /// + /// A path to extract the font files into. + public static bool ExtractFontFiles(string location) + { + if (String.IsNullOrWhiteSpace(location)) + { + throw new ArgumentNullException(nameof(location)); + } + + try + { + // make sure directory is available + if (!Directory.Exists(location)) + { + Directory.CreateDirectory(location); + } + + // try to extract all fonts + var assembly = typeof(BdfFont).Assembly; + foreach (var font in assembly.GetManifestResourceNames() + .Where(r => r.EndsWith(".bdf"))) + { + var fileName = Path.Combine(location, font.Replace("SharpPiLed.fonts.", String.Empty)); + + if (!File.Exists(fileName)) + { + using (var fileStream = File.Create(fileName)) + using (var assemblyStream = assembly.GetManifestResourceStream(font)) + { + assemblyStream.CopyTo(fileStream); + } + } + } + } + catch + { + return false; + } + + return true; + } + internal int DrawText(IntPtr canvas, int x, int y, Color color, string text, int spacing = 0, bool vertical = false) { return (vertical) diff --git a/src/SharpPiLed/Bindings/RpiRgbLedMatrix.cs b/src/SharpPiLed/Bindings/RpiRgbLedMatrix.cs index 62be34c..125d4ac 100644 --- a/src/SharpPiLed/Bindings/RpiRgbLedMatrix.cs +++ b/src/SharpPiLed/Bindings/RpiRgbLedMatrix.cs @@ -1,25 +1,12 @@ namespace SharpPiLed.Bindings { using System; - using System.IO; using System.Runtime.InteropServices; internal static class RpiRgbLedMatrix { private const string LIBRARY_NAME = "librgbmatrix"; - static RpiRgbLedMatrix() - { - var path = Path.GetDirectoryName(typeof(RpiRgbLedMatrix).Assembly.Location); - var libraryPath = Path.Combine(path, $"{LIBRARY_NAME}.so"); - - // if we have a .so.1 file, then copy that to .so - if (File.Exists(libraryPath + ".1")) - { - File.Copy(libraryPath + ".1", libraryPath, true); - } - } - #region Bindings for Canvas [DllImport(LIBRARY_NAME)] diff --git a/src/SharpPiLed/SharpPiLed.csproj b/src/SharpPiLed/SharpPiLed.csproj index e810c11..859a51e 100644 --- a/src/SharpPiLed/SharpPiLed.csproj +++ b/src/SharpPiLed/SharpPiLed.csproj @@ -14,14 +14,18 @@ Raspberry Pi;LED; https://github.com/gingters/SharpPiLed + NU5125 https://github.com/gingters/SharpPiLed/blob/master/LICENSE Copyright © 2020 Sebastian Gingter - - + + + + fonts/%(Filename)%(Extension) + diff --git a/src/examples/Matrix/Matrix.csproj b/src/examples/Matrix/Matrix.csproj index 6ad8f21..33800b6 100644 --- a/src/examples/Matrix/Matrix.csproj +++ b/src/examples/Matrix/Matrix.csproj @@ -4,7 +4,6 @@ Exe netcoreapp3.1 linux-arm - ../../../nugets diff --git a/src/examples/NuGet.config b/src/examples/NuGet.config new file mode 100644 index 0000000..7f91555 --- /dev/null +++ b/src/examples/NuGet.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/examples/SimpleText/Program.cs b/src/examples/SimpleText/Program.cs new file mode 100644 index 0000000..df37b42 --- /dev/null +++ b/src/examples/SimpleText/Program.cs @@ -0,0 +1,30 @@ +using System; +using System.IO; +using System.Threading; +using SharpPiLed; + +namespace SimpleText +{ + public class Program + { + public static int Main(string[] args) + { + BdfFont.ExtractFontFiles("./fonts"); + + var matrix = new LedMatrix(new LedMatrixOptions(), args); + var canvas = matrix.CreateOffscreenCanvas(); + + var font = new BdfFont("./fonts/6x13.bdf"); + + canvas.DrawText(font, 1, 20, new Color(0, 255, 0), "Thank you!"); + matrix.SwapOnVsync(canvas); + + while (!Console.KeyAvailable) + { + Thread.Sleep(250); + } + + return 0; + } + } +} diff --git a/src/examples/SimpleText/SimpleText.csproj b/src/examples/SimpleText/SimpleText.csproj new file mode 100644 index 0000000..33800b6 --- /dev/null +++ b/src/examples/SimpleText/SimpleText.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.1 + linux-arm + + + + + + +