Commit 34a0418e authored by Doc's avatar Doc 💀
Browse files

Straight ported. Probably works.

parent 106a3e9a
Loading
Loading
Loading
Loading
+166 −0
Original line number Diff line number Diff line
using System;
using System.IO.Ports;
using System.Threading;

/*
    Straight ported and slightly tweaked from a Python library on the internet
    https://git.en0.io/alyx/matrixorbital-serial-display-library/-/blob/master/morbital.py
*/

namespace MatrixOrbital;

internal static class SerialControl {
    public static bool CheckSerialPermissionAndOpen(this SerialPort? device) {
        try {
            // user may not have permission to use serial
            // in this case, die
            device.Open();
        }
        catch (UnauthorizedAccessException) {
            Console.WriteLine("Failed to access serial port. Make sure you have the correct permissions and that the port exists.");
            return true;
        }
        return false;
    }

    public static void WriteAndClose(this SerialPort? device, byte[] bytepayload) {
        device.Write(bytepayload, 0, bytepayload.Length);
        device.Close();
    }
    
}

public static class Display {
    public static void ClearDisplay(this SerialPort? device) {
        /* MatrixOrbital.Display.ClearDisplay(device) 
            Clears the contents of the display.
            device  (required) device name of the controller
        */
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload = new byte[2] { 254,88 };
        SerialControl.WriteAndClose(device, bytepayload);
    }

    public static void ResetCursorPos(this SerialPort? device) {
        /* MatrixOrbital.Display.ResetCursorPos(device) 
            Sets cursor position to top left of display.
            device  (required) device name of the controller
        */
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload = new byte[2] { 254,72 };
        SerialControl.WriteAndClose(device, bytepayload);
    }

    public static void SoftReset(this SerialPort? device) {
        /* MatrixOrbital.Display.SoftReset(device) 
            Soft restarts the device.
            device  (required) device name of the controller
        */
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload = new byte[6] { 254,253,77,79,117,110 };
        SerialControl.WriteAndClose(device, bytepayload);
        Thread.Sleep(4000);
    }

    public static void PositionCursor(this SerialPort? device, int x, int y) {
        /* 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
        */
        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.");
        }
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload = new byte[4] { 254,71,(byte)x,(byte)y };
        SerialControl.WriteAndClose(device, bytepayload);
    }

    public static void PositionCoord(this SerialPort? device, int x, int y) {
        /* 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
        */
        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.");
        }
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload = new byte[4] { 254,121,(byte)x,(byte)y };
        SerialControl.WriteAndClose(device, bytepayload);
    }

    public static void BacklightControl(this SerialPort? device, int b, int c = -1 ) {
        /* MatrixOrbital.Display.BacklightControl(device, b) 
            Sets the display backlight brightness or turns it off.
            device  (required) device name of the controller
            b       (required) display brightness from 0 - 255
            c       (optional) display contrast from 0 - 255
                               specifying -1 here or omitting it
                               will skip setting contrast

            The python library I referenced has distinct backlight on
            or off controls and separate contrast but I figure that
            integrating it into one func is just better.
        */
        if (b < 0 || b > 255 || c < -1 || c > 255) {
            throw new ArgumentOutOfRangeException("Display settings are invalid. Make sure brightness and contrast are between 0 (off) and 255 (full on).");
        }
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload;
        if (c == -1) {
            bytepayload = new byte[3] { 254,153,(byte)b };
        } else {
            bytepayload = new byte[6] { 254,153,(byte)b,254,80,(byte)c };
        }
        SerialControl.WriteAndClose(device, bytepayload);
    }
}

public static class LED {
    public static void SetColor(this SerialPort? device, int led, int color) {
        /* MatrixOrbital.LED.SetColor(device, led, color) 
            Sets the LED colors to the left of the display
            device  (required) device name of the controller
            led     (required) led from top to bottom (0-2)
            color   (required) color from 0-3:
                               0: yellow, 1: green, 2: red, 3: off
        */
        if (led < 0 || led > 2 || color < 0 || color > 3) {
            throw new ArgumentOutOfRangeException("LED configuration is invalid. Make sure led is a positive value less than 3 or color is a positive value less than 4.");
        }
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload = new byte[4] { 254,90,(byte)led,(byte)color };
        SerialControl.WriteAndClose(device, bytepayload);
    }
}

public static class Keypad {
    public static void BacklightControl(this SerialPort? device, int b ) {
        /* MatrixOrbital.Keypad.BacklightControl(device, b) 
            Sets the keypad backlight brightness or turns it off.
            device  (required) device name of the controller
            b       (required) keypad brightness from 0 - 255

            The python library I referenced has distinct backlight on
            or off controls but I figure that integrating it into one 
            func is just better.
        */
        if (b < 0 || b > 255) {
            throw new ArgumentOutOfRangeException("Display settings are invalid. Make sure brightness and contrast are between 0 (off) and 255 (full on).");
        }
        bool sanity = SerialControl.CheckSerialPermissionAndOpen(device);
        if (sanity) return;
        byte[] bytepayload = new byte[3] { 254,156,(byte)b };
        SerialControl.WriteAndClose(device, bytepayload);
    }
}
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.IO.Ports" Version="7.0.0" />
  </ItemGroup>

</Project>
+154 −0
Original line number Diff line number Diff line
{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v7.0",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v7.0": {
      "MatrixOrbital/1.0.0": {
        "dependencies": {
          "System.IO.Ports": "7.0.0"
        },
        "runtime": {
          "MatrixOrbital.dll": {}
        }
      },
      "runtime.linux-arm.runtime.native.System.IO.Ports/7.0.0": {
        "runtimeTargets": {
          "runtimes/linux-arm/native/libSystem.IO.Ports.Native.so": {
            "rid": "linux-arm",
            "assetType": "native",
            "fileVersion": "0.0.0.0"
          }
        }
      },
      "runtime.linux-arm64.runtime.native.System.IO.Ports/7.0.0": {
        "runtimeTargets": {
          "runtimes/linux-arm64/native/libSystem.IO.Ports.Native.so": {
            "rid": "linux-arm64",
            "assetType": "native",
            "fileVersion": "0.0.0.0"
          }
        }
      },
      "runtime.linux-x64.runtime.native.System.IO.Ports/7.0.0": {
        "runtimeTargets": {
          "runtimes/linux-x64/native/libSystem.IO.Ports.Native.so": {
            "rid": "linux-x64",
            "assetType": "native",
            "fileVersion": "0.0.0.0"
          }
        }
      },
      "runtime.native.System.IO.Ports/7.0.0": {
        "dependencies": {
          "runtime.linux-arm.runtime.native.System.IO.Ports": "7.0.0",
          "runtime.linux-arm64.runtime.native.System.IO.Ports": "7.0.0",
          "runtime.linux-x64.runtime.native.System.IO.Ports": "7.0.0",
          "runtime.osx-arm64.runtime.native.System.IO.Ports": "7.0.0",
          "runtime.osx-x64.runtime.native.System.IO.Ports": "7.0.0"
        }
      },
      "runtime.osx-arm64.runtime.native.System.IO.Ports/7.0.0": {
        "runtimeTargets": {
          "runtimes/osx-arm64/native/libSystem.IO.Ports.Native.dylib": {
            "rid": "osx-arm64",
            "assetType": "native",
            "fileVersion": "0.0.0.0"
          }
        }
      },
      "runtime.osx-x64.runtime.native.System.IO.Ports/7.0.0": {
        "runtimeTargets": {
          "runtimes/osx-x64/native/libSystem.IO.Ports.Native.dylib": {
            "rid": "osx-x64",
            "assetType": "native",
            "fileVersion": "0.0.0.0"
          }
        }
      },
      "System.IO.Ports/7.0.0": {
        "dependencies": {
          "runtime.native.System.IO.Ports": "7.0.0"
        },
        "runtime": {
          "lib/net7.0/System.IO.Ports.dll": {
            "assemblyVersion": "7.0.0.0",
            "fileVersion": "7.0.22.51805"
          }
        },
        "runtimeTargets": {
          "runtimes/unix/lib/net7.0/System.IO.Ports.dll": {
            "rid": "unix",
            "assetType": "runtime",
            "assemblyVersion": "7.0.0.0",
            "fileVersion": "7.0.22.51805"
          },
          "runtimes/win/lib/net7.0/System.IO.Ports.dll": {
            "rid": "win",
            "assetType": "runtime",
            "assemblyVersion": "7.0.0.0",
            "fileVersion": "7.0.22.51805"
          }
        }
      }
    }
  },
  "libraries": {
    "MatrixOrbital/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    },
    "runtime.linux-arm.runtime.native.System.IO.Ports/7.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-CBvgRaF+M0xGLDv2Geb/0v0LEADheH8aK72GRAUJdnqnJVsQO60ki1XO8M3keEhnjm+T5NvLm41pNXAVYAPiSg==",
      "path": "runtime.linux-arm.runtime.native.system.io.ports/7.0.0",
      "hashPath": "runtime.linux-arm.runtime.native.system.io.ports.7.0.0.nupkg.sha512"
    },
    "runtime.linux-arm64.runtime.native.System.IO.Ports/7.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-5VCyRCtCIYU8FR/W8oo7ouFuJ8tmAg9ddsuXhfCKZfZrbaVZSKxkmNBa6fxkfYPueD0jQfOvwFBmE5c6zalCSw==",
      "path": "runtime.linux-arm64.runtime.native.system.io.ports/7.0.0",
      "hashPath": "runtime.linux-arm64.runtime.native.system.io.ports.7.0.0.nupkg.sha512"
    },
    "runtime.linux-x64.runtime.native.System.IO.Ports/7.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-DV9dWDUs23OoZqMWl5IhLr3D+b9koDiSHQxFKdYgWnQbnthv8c/yDjrlrI8nMrDc71RAKCO8jlUojzuPMX04gg==",
      "path": "runtime.linux-x64.runtime.native.system.io.ports/7.0.0",
      "hashPath": "runtime.linux-x64.runtime.native.system.io.ports.7.0.0.nupkg.sha512"
    },
    "runtime.native.System.IO.Ports/7.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-L4Ivegqc3B0Fee7VifFy2JST9nndm+uvJ0viLIZUaImDfnr+JmRin9Tbqd56KuMtm0eVxHpNOWZBPtKrA/1h5Q==",
      "path": "runtime.native.system.io.ports/7.0.0",
      "hashPath": "runtime.native.system.io.ports.7.0.0.nupkg.sha512"
    },
    "runtime.osx-arm64.runtime.native.System.IO.Ports/7.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-jFwh4sKSXZ7al5XrItEO4GdGWa6XNxvNx+LhEHjrSzOwawO1znwJ+Dy+VjnrkySX9Qi4bnHNLoiqOXbqMuka4g==",
      "path": "runtime.osx-arm64.runtime.native.system.io.ports/7.0.0",
      "hashPath": "runtime.osx-arm64.runtime.native.system.io.ports.7.0.0.nupkg.sha512"
    },
    "runtime.osx-x64.runtime.native.System.IO.Ports/7.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-X4LrHEfke/z9+z+iuVr35NlkhdZldY8JGNMYUN+sfPK/U/6TcE+vP44I0Yv0ir1v0bqIzq3v6Qdv1c1vmp8s4g==",
      "path": "runtime.osx-x64.runtime.native.system.io.ports/7.0.0",
      "hashPath": "runtime.osx-x64.runtime.native.system.io.ports.7.0.0.nupkg.sha512"
    },
    "System.IO.Ports/7.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-0nWQjM5IofaIGpvkifN+LLuYwBG6BHlpmphLhhOJepcW12G8qToGuNDRgBzeTVBZzp33wVsESSZ8hUOCfq+8QA==",
      "path": "system.io.ports/7.0.0",
      "hashPath": "system.io.ports.7.0.0.nupkg.sha512"
    }
  }
}
 No newline at end of file
+5 KiB

File added.

No diff preview for this file type.

+10.5 KiB

File added.

No diff preview for this file type.

Loading