From c94c6dc878e68bfb1258ebcb2d9d7034a20cb9ef Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Wed, 4 Dec 2019 22:02:38 +0100 Subject: [PATCH] dinge --- Testconsole/DispDriver.cs | 216 +++++++++++++++++++++++++++++++++ Testconsole/Program.cs | 14 ++- Testconsole/Testconsole.csproj | 1 + 3 files changed, 229 insertions(+), 2 deletions(-) create mode 100644 Testconsole/DispDriver.cs diff --git a/Testconsole/DispDriver.cs b/Testconsole/DispDriver.cs new file mode 100644 index 0000000..b3cb968 --- /dev/null +++ b/Testconsole/DispDriver.cs @@ -0,0 +1,216 @@ +using System; +using System.Collections.Generic; +using System.IO.Ports; + +namespace Testconsole { + class DispDriver { + private readonly SerialPort serial; + private readonly UInt32[,] zbuffer = new UInt32[8, 16]; + + private struct Digets { + public static readonly Boolean[][] Null = { + new Boolean[]{ false, true, false }, + new Boolean[] { true, false, true }, + new Boolean[] { true, false, true }, + new Boolean[] { true, false, true }, + new Boolean[]{ false, true, false } + }; + public static readonly Boolean[][] One = { + new Boolean[] { false, false, true }, + new Boolean[] { false, true, true }, + new Boolean[] { false, false, true }, + new Boolean[] { false, false, true }, + new Boolean[] { false, false, true } + }; + public static readonly Boolean[][] Two = { + new Boolean[] { false, true, false }, + new Boolean[] { true, false, true }, + new Boolean[] { false, false, true }, + new Boolean[] { false, true, false }, + new Boolean[] { true, true, true } + }; + public static readonly Boolean[][] Three = { + new Boolean[] { true, true, false }, + new Boolean[] { false, false, true }, + new Boolean[] { false, true, false }, + new Boolean[] { false, false, true }, + new Boolean[] { true, true, false } + }; + public static readonly Boolean[][] Four = { + new Boolean[] { false, false, true }, + new Boolean[] { false, true, true }, + new Boolean[] { true, false, true }, + new Boolean[] { true, true, true }, + new Boolean[] { false, false, true } + }; + public static readonly Boolean[][] Five = { + new Boolean[] { true, true, true }, + new Boolean[] { true, false, false }, + new Boolean[] { true, true, false }, + new Boolean[] { false, false, true }, + new Boolean[] { true, true, false } + }; + public static readonly Boolean[][] Six = { + new Boolean[] { false, true, true }, + new Boolean[] { true, false, false }, + new Boolean[] { true, true, true }, + new Boolean[] { true, false, true }, + new Boolean[] { false, true, false } + }; + public static readonly Boolean[][] Seven = { + new Boolean[] { true, true, true }, + new Boolean[] { false, false, true }, + new Boolean[] { false, true, false }, + new Boolean[] { true, false, false }, + new Boolean[] { true, false, false } + }; + public static readonly Boolean[][] Eight = { + new Boolean[] { false, true, false }, + new Boolean[] { true, false, true }, + new Boolean[] { false, true, false }, + new Boolean[] { true, false, true }, + new Boolean[] { false, true, false } + }; + public static readonly Boolean[][] Nine = { + new Boolean[] { false, true, false }, + new Boolean[] { true, false, true }, + new Boolean[] { true, true, true }, + new Boolean[] { false, false, true }, + new Boolean[] { true, true, false } + }; + } + + public DispDriver(String com) { + this.serial = new SerialPort(com, 19200); + this.serial.Open(); + } + + public void DrawDigets(String number, UInt32 color) { + Int32 sub = number.Length; + if(number.Length > 4 && number.Substring(0, 5).Contains(".")) { + sub = 5; + } else if(number.Length >= 4) { + sub = 4; + } + String a = number.Substring(0, sub); + Int32 y = 0; + foreach(Char item in a) { + if(item != '.') { + this.CopyDigetToZbuffer(this.GetDiget(item), 3, y, color); + y += 4; + } else { + this.DrawDot(y-1, 7, color); + } + } + } + + public void DrawDot(Int32 x, Int32 y, UInt32 color) { + try { + this.zbuffer[y, x] = color; + } catch { + Console.WriteLine("out of bound"); + } + } + + public void DrawLineX(Int32 x, Int32 y, Int32 l, UInt32 color) { + for(Int32 i = x; i < x+l; i++) { + this.DrawDot(i, y, color); + } + } + + public void DrawLineY(Int32 x, Int32 y, Int32 l, UInt32 color) { + for(Int32 i = y; i < y + l; i++) { + this.DrawDot(x, i, color); + } + } + + public void Write() { + this.Clear(); + List colors = this.GetAllColors(); + foreach(UInt32 item in colors) { + this.serial.Write(String.Join(" ", this.GetZbufferPage(0, item)) + " " + String.Join(" ", this.GetZbufferPage(1, item)) + " " + String.Join(" ", this.GetAllColors(item)) + "\n"); + } + this.ClearZbuffer(); + } + + public void Clear() => this.serial.Write("clear\n"); + + private void ClearZbuffer() { + for(Int32 i = 0; i < 8; i++) { + for(Int32 j = 0; j < 16; j++) { + this.zbuffer[i, j] = 0; + } + } + } + + private String[] GetAllColors(UInt32 color) { + String[] ret = new String[3]; + ret[0] = ((Byte)((color & 0x00FF0000) >> 16)).ToString().PadLeft(3, '0'); + ret[1] = ((Byte)((color & 0x0000FF00) >> 8)).ToString().PadLeft(3, '0'); + ret[2] = ((Byte)((color & 0x000000FF) >> 0)).ToString().PadLeft(3, '0'); + return ret; + } + + private String[] GetZbufferPage(Int32 page, UInt32 color) { + String[] ret = new String[8]; + for(Int32 i = 0; i < 8; i++) { + Byte p = 0; + for(Int32 j = 0; j < 8; j++) { + if(this.zbuffer[i,j+(page*8)] == color) { + p |= (Byte)(1 << (7 - j)); + } + } + ret[i] = p.ToString().PadLeft(3,'0'); + } + return ret; + } + + private List GetAllColors() { + List colors = new List(); + foreach(UInt32 item in this.zbuffer) { + if(!colors.Contains(item) && item != 0) { + colors.Add(item); + } + } + return colors; + } + + private Boolean[][] GetDiget(Char item) { + switch(item) { + case '1': + return Digets.One; + case '2': + return Digets.Two; + case '3': + return Digets.Three; + case '4': + return Digets.Four; + case '5': + return Digets.Five; + case '6': + return Digets.Six; + case '7': + return Digets.Seven; + case '8': + return Digets.Eight; + case '9': + return Digets.Nine; + default: + return Digets.Null; + } + } + + private void CopyDigetToZbuffer(Boolean[][] diget, Int32 x, Int32 y, UInt32 color) { + try { + for(Int32 i = 0; i < diget.Length; i++) { + for(Int32 j = 0; j < diget[i].Length; j++) { + this.zbuffer[i + x, j + y] = diget[i][j] ? color : this.zbuffer[i + x, j + y]; + } + } + } catch { + Console.WriteLine("out of bound"); + } + } + + } +} diff --git a/Testconsole/Program.cs b/Testconsole/Program.cs index 2eea56a..7ca1d1e 100644 --- a/Testconsole/Program.cs +++ b/Testconsole/Program.cs @@ -9,9 +9,19 @@ namespace Testconsole { Program() { Console.WriteLine("Start"); + + DispDriver a = new DispDriver("COM3"); + for(Double i = 0; i < 1000; i = i + 0.5) { + a.DrawDigets(i.ToString().Replace(",", "."), 0x110000); + a.Write(); + System.Threading.Thread.Sleep(200); + } + + //a.DrawLineX(0, 0, 16, 0xFF0000); + //a.DrawLineY(0, 1, 2, 0x00FF00); + this.WaitForShutdown(); - Console.WriteLine("Shutdown"); - Console.ReadLine(); + Console.WriteLine("Shutdown"); } private void WaitForShutdown() { diff --git a/Testconsole/Testconsole.csproj b/Testconsole/Testconsole.csproj index 64de50f..b281bc8 100644 --- a/Testconsole/Testconsole.csproj +++ b/Testconsole/Testconsole.csproj @@ -44,6 +44,7 @@ +