Skip to content

Commit

Permalink
First draft of working library and package
Browse files Browse the repository at this point in the history
  • Loading branch information
gingters committed Mar 29, 2020
1 parent 21eee4a commit c34157e
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 21 deletions.
25 changes: 21 additions & 4 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -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
}
59 changes: 58 additions & 1 deletion src/SharpPiLed/BdfFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace SharpPiLed
{
using System;
using System.IO;
using System.Linq;
using Bindings;

/// <summary>
Expand All @@ -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);
}

///<summary>
/// 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.
/// </summary>
/// <param name="location">A path to extract the font files into.</param>
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)
Expand Down
13 changes: 0 additions & 13 deletions src/SharpPiLed/Bindings/RpiRgbLedMatrix.cs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
8 changes: 6 additions & 2 deletions src/SharpPiLed/SharpPiLed.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
</Description>
<PackageTags>Raspberry Pi;LED;</PackageTags>
<RepositoryUrl>https://github.com/gingters/SharpPiLed</RepositoryUrl>
<NoWarn>NU5125</NoWarn><!-- Next line will become deprecated, but we keep it for older nuget clients and don't warn -->
<PackageLicenseUrl>https://github.com/gingters/SharpPiLed/blob/master/LICENSE</PackageLicenseUrl>
<Copyright>Copyright © 2020 Sebastian Gingter</Copyright>
</PropertyGroup>

<ItemGroup>
<None Include="../../LICENSE" Pack="true" PackagePath="" />
<Content Include="../../rpi-rgb-led-matrix/fonts/*.bdf" PackagePath="content" />
<None Include="../../rpi-rgb-led-matrix/lib/librgbmatrix.so.1" Pack="True" PackagePath="runtimes/linux-arm/native" />
<None Include="../../rpi-rgb-led-matrix/lib/librgbmatrix.so" Pack="True" PackagePath="runtimes/linux-arm/native" />
<Content Include="../../rpi-rgb-led-matrix/fonts/*.bdf" PackageCopyToOutput="True" />
<EmbeddedResource Include="../../rpi-rgb-led-matrix/fonts/*.bdf">
<Link>fonts/%(Filename)%(Extension)</Link>
</EmbeddedResource>
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion src/examples/Matrix/Matrix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
<RestoreAdditionalProjectSources>../../../nugets</RestoreAdditionalProjectSources>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/examples/NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="Development Store" value="../../nugets" />
</packageSources>
</configuration>
30 changes: 30 additions & 0 deletions src/examples/SimpleText/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
13 changes: 13 additions & 0 deletions src/examples/SimpleText/SimpleText.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpPiLed" Version="1.0.0" />
</ItemGroup>

</Project>

0 comments on commit c34157e

Please sign in to comment.