You use the WindowLibrary to gather a collection of windows type objects on a page. You can call up and display the pages at any time while you update the data contents of the fields in the background.
It is my first go at a C# project so it is not perfect but useable. Please comment and advise.
This class is to drive the Sparkfun with.
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using System.IO.Ports; namespace MC.Hardware.SerialGraphicLcd { public sealed class SGLcd { public enum DisplayType { H12864, H240320 } // will expand later public enum Status { On, Off } private readonly SerialPort _serialPort; private readonly DisplayType _displayType; public SGLcd(string portName) : this(portName, DisplayType.H12864) { } public SGLcd(string portName, DisplayType displayType) { // Defaults for SerialPort are the same as the settings for the LCD, but I'll set them explicitly _serialPort = new SerialPort(portName, 115200, Parity.None, 8, StopBits.One); _displayType = displayType; } public void DemoDisplay() { Write(new byte[] { 0x7C, 0x04 }); } public void ClearDisplay() { Write(new byte[] { 0x7C, 0x00 }); } // Reverse Mode public void Reverse() { Write(new byte[] { 0x7C, 0x12 }); } //Splash Screen public void SplashScreen() { Write(new byte[] { 0x7C, 0x13 }); } //Set Backlight Duty Cycle public void BackLightDutyCycle(byte dutycycle) { byte[] buffer = new byte[3]; buffer[0] = 0x7c; buffer[1] = 0x02; buffer[2] = dutycycle; Write(buffer); } //Change Baud Rate /* “1” = 4800bps “2” = 9600bps “3” = 19,200bps “4” = 38,400bps “5” = 57,600bps “6” = 115,200bps */ public void NewBaudRate(string baud) { byte[] temp = new byte[3]; temp[0] = 0x7c; temp[1] = 0x07; temp[2] = (byte)baud[0]; Write(temp); } //Set X or Y Coordinates public void GotoXY(byte x, byte y) { //set x first byte[] temp = new byte[3]; temp[0] = 0x7c; temp[1] = 0x18; temp[2] = x; Write(temp); //set y temp[0] = 0x7c; temp[1] = 0x19; temp[2] = y; Write(temp); } //Set/Reset Pixel - x =0 to xmax, y = 0 to ymax, state = 0 or 1 public void SetPixel(byte x, byte y, byte state) { byte[] temp = new byte[5]; temp[0] = 0x7c; temp[1] = 0x10; temp[2] = x; temp[3] = y; temp[4] = state; Write(temp); } //Draw Line x1 y1 first coords, x2 y2 second coords, state 0 = erase 1 = draw public void DrawLine(byte x1, byte y1, byte x2, byte y2, byte state) { byte[] temp = new byte[7]; temp[0] = 0x7c; temp[1] = 0x0c; temp[2] = x1; temp[3] = y1; temp[4] = x2; temp[5] = y2; temp[6] = state; Write(temp); } //Draw Circle public void DrawCircle(byte x, byte y, byte r, byte state) { byte[] temp = new byte[6]; temp[0] = 0x7c; temp[1] = 0x03; temp[2] = x; temp[3] = y; temp[4] = r; temp[5] = state; Write(temp); } //Draw Box public void DrawBox(byte x1, byte y1, byte x2, byte y2, byte state) { byte[] temp = new byte[7]; temp[0] = 0x7c; temp[1] = 0x0f; temp[2] = x1; temp[3] = y1; temp[4] = x2; temp[5] = y2; temp[6] = state; Write(temp); } //Erase Block public void EraseBlock(byte x1, byte y1, byte x2, byte y2) { byte[] temp = new byte[6]; temp[0] = 0x7c; temp[1] = 0x05; temp[2] = x1; temp[3] = y1; temp[4] = x2; temp[5] = y2; Write(temp); } public void Open() { if (!_serialPort.IsOpen) _serialPort.Open(); } public void Write(byte buffer) { Write(new[] { buffer }); } public void Write(byte[] buffer) { Open(); _serialPort.Write(buffer, 0, buffer.Length); } public void Write(char character) { Write((byte)character); } public void Write(string text) { byte[] buffer = new byte[text.Length]; for (int i = 0; i < text.Length; i++) { buffer[i] = (byte)text[i]; } Write(buffer); } public void WriteXY(byte x, byte y, string text) { byte[] buffer = new byte[text.Length]; for (int i = 0; i < text.Length; i++) { buffer[i] = (byte)text[i]; } this.GotoXY(x, y); Write(buffer); } } }
And this one is used to create and use the screen objects on pages.
using System; using Microsoft.SPOT; using MC.Hardware.SerialGraphicLcd; using System.Collections; namespace MC.Hardware.WindowLibrary { public class Window { private readonly ArrayList widgets = new ArrayList(); private SGLcd lcd; public Window(SGLcd Lcd) { lcd = Lcd; } public void Add(Widget widget) { widgets.Add(widget); } public void Delete(string name) { foreach (Widget widget in widgets) { if (widget != null && widget.Name == name) { widgets.Remove(widget); lcd.ClearDisplay(); this.DrawAll(); } } } public void DrawAll() { this.ForEach(w => w.Draw(lcd)); } public Widget Search(string id) { Widget wid = null; foreach (Widget widget in widgets) { if (widget != null && widget.Name == id) { wid = widget; } } return (wid); } public delegate void WidgetAction(Widget widget); public void ForEach(WidgetAction action) { foreach (Widget widget in widgets) { action(widget); } } } public abstract class Widget { public string Name { get; set; } public byte X { get; set; } public byte Y { get; set; } public byte D { get; set; } public string Text { get; set; } protected Widget(string name, byte x, byte y, byte d, byte state, string text) { Name = name; X = x; Y = y; D = d; Text = text; } public abstract void Draw(SGLcd lcd); } public sealed class Circle : Widget { public byte Radius { get; set; } public Circle(string name, byte x, byte y, byte radius, byte state, string text) : base(name, x, y, radius, state, text) { Radius = radius; } public override void Draw(SGLcd lcd) { lcd.DrawCircle(X, Y, Radius, 1); } } public sealed class Rectangle : Widget { public byte Width { get; set; } public byte Height { get; set; } public Rectangle(string name, byte x, byte y, byte width, byte height, string state) : base(name, x, y, width, height, state) { Width = width; Height = height; } public override void Draw(SGLcd lcd) { lcd.DrawBox(X, Y, Width, Height, 1); } } public sealed class TextArea : Widget { public string Text { get; set; } public TextArea(string name, byte x, byte y, byte d1, byte d2, string text) : base(name, x, y, d1, d2, text) { Text = text; } public override void Draw(SGLcd lcd) { lcd.GotoXY(X, Y); lcd.Write(Text); } } }
And this is some silly test code.
using System.Collections; using MC.Hardware.WindowLibrary; using Microsoft.SPOT; using MC.Hardware.SerialGraphicLcd; using System.Threading; namespace DisplayTest { public class Program { public static void Main() { SGLcd Screen = new SGLcd("COM1", 0); var display = new Window(Screen); display.Add(new Circle("circle1", 10, 10, 10,1,"text")); display.Add(new Rectangle("rect1", 30, 40, 50, 60,"text")); display.Add(new TextArea("text", 22, 33,0,0, "hello World")); Screen.ClearDisplay(); display.DrawAll(); Thread.Sleep(1000); var pageOne = new Window(Screen); pageOne.Add(new TextArea("heading",10,10,0,0,"Page One")); //Screen.ClearDisplay(); pageOne.DrawAll(); Thread.Sleep(1000); var pageTwo = new Window(Screen); pageTwo.Add(new TextArea("heading", 10, 56, 0, 0, "Page TWO")); //Screen.ClearDisplay(); pageTwo.DrawAll(); Thread.Sleep(1000); // Calling the display driver class directly to do some tricks Screen.WriteXY(1, 8, pageOne.Search("heading").Text.ToString()); // retreiving some of the object information and using it to write some data from another object var p = display.Search("rect1"); Screen.WriteXY(p.X , p.Y , pageOne.Search("heading").Text.ToString()); //advanced section I: move every widget by (20,20) /* display.ForEach(w => { w.X += 5; w.Y += 5; }); */ //advanced section II: find all circles and double their radius /* display.ForEach(w => { var c = w as Circle; if (c != null && c.Name == "circle1") { c.Radius += 5; } }); */ } } }
- scardinale likes this