Commit 3299878a authored by Doc's avatar Doc 💀
Browse files

implementing brightness + fixing bugs

parent 41a3f15e
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -3,3 +3,4 @@
/testing/bin
/testing/obj/Debug
/key
/.vscode
 No newline at end of file
+2.74 MiB

File added.

No diff preview for this file type.

+106 −3
Original line number Diff line number Diff line
using System;
using System.IO.Ports;
using System.Text;

/*
    Straight ported and slightly tweaked from a Python library on the internet
@@ -86,15 +87,99 @@ public static class Device {
}

public static class Display {
    public static void WriteText(this SerialPort? device, string text) {
    public static void WriteText(this SerialPort? device, string text, int id = 0) {
        /* MatrixOrbital.Display.WriteText(device, text) 
            Clears the contents of the display.
            device  (required) device name of the controller
            text    (required) text to write
            id      (optional) id of a TextBox to update. 0 is default and is the entire display.
        */
        byte[] bytepayload = new byte[] { 254,42,(byte)id};
        SerialControl.WritePayload(device, bytepayload);
        device.Write(text);
    }

    public static void NewTextBox(this SerialPort? device, int id, int left, int top, int right, int bot, int scroll = 8, int cspace = 0, int lspace = 1, int font = 1) {
        /* MatrixOrbital.Display.NewTextBox(device, id, left, top, right, bot, <scroll>, <cspace>, <lspace>, <font>) 
            Create a text box on screen to be manipulated with WriteText().
            device  (required) device name of the controller
            id      (required) id of the textbox (1-15)
            left    (required) left edge as coord (1-192)
            top     (required) top edge as coord (1-64)
            right   (required) right edge as coord (1-192)
            bot     (required) bottom edge as coord (1-64)
            scroll  (optional) no. of pixel-rows before scrolling text (default 8 (one line))
            cspace  (optional) character spacing (default 0)
            lspace  (optional) line spacing (default 0)
            font    (optional) font to use (0-1023, default 1)
        */
        byte[] fontasbytes = BitConverter.GetBytes((short)font);
        byte[] bytepayload = new byte[] { 254,43,(byte)id,(byte)left,(byte)top,(byte)right,(byte)bot,fontasbytes[0],fontasbytes[1],(byte)cspace,(byte)lspace,(byte)scroll};
        SerialControl.WritePayload(device, bytepayload);
    }

    public static void NewTextLabel(this SerialPort? device, int id, bool scroll, int left, int top, int right, int bot, int d = 50, bool bg = false, int jusv = 0, int jush = 0, int cspace = 0, int font = 1) {
        /* MatrixOrbital.Display.NewTextLabel(device, id, scrolling, left, top, right, bot, <jusv>, <jush>, <cspace>, <font>)
            Create a text label on screen to be easily edited with .EditTextLabel()
            device  (required) device name of the controller
            id      (required) id of the label (0-15)
            scroll  (required) is this a scrolling label?
            left    (required) left edge as coord (1-192)
            top     (required) top edge as coord (1-64)
            right   (required) right edge as coord (1-192)
            bot     (required) bottom edge as coord (1-64)
            d       (optional) scrolling delay in ms. only applicable when (scroll) (default 50)
            bg      (optional) turn non-text pixels on (default false/off)
            jusv    (optional) vertical justification: 0 top, 1 middle, 2 bottom (default 0/top)
            jush    (optional) horizontal justification: 0 left, 1 center, 2 right (default 0/left)
                    if scroll is true, jush is scroll direction: 0 left, 1 right, 2 back-and-forth
            cspace  (optional) character spacing (default 6)
            lspace  (optional) line spacing (default 0)
            font    (optional) font to use (0-1023, default 1)

            difference between label and scrolling label
             label: 254 45 ID L T R B V H F BG CS
            slabel: 254 47 ID L T R B V D F BG CS D
        */
        byte[] fontasbytes = BitConverter.GetBytes((short)font);
        byte[] delayasbytes = BitConverter.GetBytes((short)d);

        byte labelraw = 45;
        if (scroll) labelraw = 47;
        byte bgraw = 0;
        if (bg) bgraw = 1;

        byte[] initbytepayload = new byte[] { 254,labelraw,(byte)id,(byte)left,(byte)top,(byte)right,(byte)bot,(byte)jusv,(byte)jush,fontasbytes[0],fontasbytes[1],bgraw,(byte)cspace};

        var t = new MemoryStream();
        t.Write(initbytepayload, 0, initbytepayload.Length);
        if (scroll) t.Write(delayasbytes, 0, delayasbytes.Length);
        var bytepayload = t.ToArray();
        SerialControl.WritePayload(device, bytepayload);
    }

    public static void EditTextLabel(this SerialPort? device, int id, string text) {
        /* MatrixOrbital.Display.EditTextLabel(device, id, text)
            Edit the text of an existing TextLabel.
            device  (required) device name of the controller
            id      (required) id of the label (0-15)
            text    (required) text to send
        */
        byte[] textbytes = Encoding.ASCII.GetBytes(text);
        foreach (byte b in textbytes)
        {
            // Console.Write(b);
        }
        byte[] bytepayload = new byte[] { 254,46,(byte)id};
        byte[] terminator = new byte[1] { 0 };
        var t = new MemoryStream();
        t.Write(bytepayload, 0, bytepayload.Length);
        t.Write(textbytes, 0, textbytes.Length);
        t.Write(terminator, 0, terminator.Length);
        byte[] tb = t.ToArray();
        SerialControl.WritePayload(device, tb);
    }

    public static void DrawBitmap(this SerialPort? device, int x, int y, int w, int h, byte[] bitmap) {
        /* MatrixOrbital.Display.DrawBitmap(device, bitmap) 
            Draws a bitmap to the display
@@ -122,6 +207,16 @@ public static class Display {
        SerialControl.WritePayload(device, bytepayload);
    }

    public static void ClearTextBox(this SerialPort? device, int id) {
        /* MatrixOrbital.Display.ClearTextBox(device, id) 
            Clears the contents of a text box
            device  (required) device name of the controller
            id      (required) id of the textbox
        */
        byte[] bytepayload = new byte[3] { 254,44,(byte)id };
        SerialControl.WritePayload(device, bytepayload);
    }

    public static void ResetCursorPos(this SerialPort? device) {
        /* MatrixOrbital.Display.ResetCursorPos(device) 
            Sets cursor position to top left of display.
@@ -131,30 +226,38 @@ public static class Display {
        SerialControl.WritePayload(device, bytepayload);
    }

    public static void PositionCursor(this SerialPort? device, int x, int y) {
    public static void PositionCursor(this SerialPort? device, int x, int y, int id = 0) {
        /* MatrixOrbital.Display.PositionCursor(device, x, y) 
            Positions the text cursor arbitrarily.
            device  (required) device name of the controller
            x       (required) x position from 1 to 27
            y       (required) y position from 1 to 8
            id      (optional) display id 0-15, 0 is default and is the whole display
                               may not be completely functional
        */
        if (x < 1 || x > 27 || y < 1 || y > 8) {
            throw new ArgumentOutOfRangeException("Cursor position is invalid. Make sure x is a positive value less than 28 or y is a positive value less than 9.");
        }
        byte[] sbytepayload = new byte[] { 254,42,(byte)id };
        SerialControl.WritePayload(device, sbytepayload);
        byte[] bytepayload = new byte[4] { 254,71,(byte)x,(byte)y };
        SerialControl.WritePayload(device, bytepayload);
    }

    public static void PositionCoord(this SerialPort? device, int x, int y) {
    public static void PositionCoord(this SerialPort? device, int x, int y, int id = 0) {
        /* MatrixOrbital.Display.PositionCoord(device, x, y) 
            Positions the graphics cursor arbitrarily.
            device  (required) device name of the controller
            x       (required) x position from 1 to 192
            y       (required) y position from 1 to 64
            id      (optional) display id 0-15, 0 is default and is the whole display
                               may not be completely functional
        */
        if (x < 1 || x > 192 || y < 1 || y > 64) {
            throw new ArgumentOutOfRangeException("Coordinate position is invalid. Make sure x is a positive value less than 193 or y is a positive value less than 65.");
        }
        byte[] sbytepayload = new byte[] { 254,42,(byte)id };
        SerialControl.WritePayload(device, sbytepayload);
        byte[] bytepayload = new byte[4] { 254,121,(byte)x,(byte)y };
        SerialControl.WritePayload(device, bytepayload);
    }
+16 −1
Original line number Diff line number Diff line
@@ -32,6 +32,11 @@
          "warnAsError": [
            "NU1605"
          ]
        },
        "restoreAuditProperties": {
          "enableAudit": "true",
          "auditLevel": "low",
          "auditMode": "direct"
        }
      },
      "frameworks": {
@@ -54,12 +59,22 @@
          ],
          "assetTargetFallback": true,
          "warn": true,
          "downloadDependencies": [
            {
              "name": "Microsoft.AspNetCore.App.Ref",
              "version": "[7.0.17, 7.0.17]"
            },
            {
              "name": "Microsoft.NETCore.App.Ref",
              "version": "[7.0.17, 7.0.17]"
            }
          ],
          "frameworkReferences": {
            "Microsoft.NETCore.App": {
              "privateAssets": "all"
            }
          },
          "runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/7.0.203/RuntimeIdentifierGraph.json"
          "runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.203/RuntimeIdentifierGraph.json"
        }
      }
    }
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@
    <NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/moona/.nuget/packages/</NuGetPackageRoot>
    <NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/moona/.nuget/packages/</NuGetPackageFolders>
    <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
    <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.5.0</NuGetToolVersion>
    <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.9.1</NuGetToolVersion>
  </PropertyGroup>
  <ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
    <SourceRoot Include="/home/moona/.nuget/packages/" />
Loading