Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Digole
- Implements IDisposable
- Private Shared ReadOnly Enc As Text.Encoding = Text.Encoding.GetEncoding("ISO-8859-1")
- Private Port As IO.Ports.SerialPort
- Private PortName As String
- ''' <summary>Creates a new instance of this class and opens the com port with the default settings</summary>
- ''' <param name="PortName">The name of the com port to open, eg. "COM2"</param>
- Public Sub New(ByVal PortName As String)
- Me.New(PortName, 9600)
- End Sub
- ''' <summary>Creates a new instance of this class and opens the com port with the default settings</summary>
- ''' <param name="PortName">The name of the com port to open, eg. "COM2"</param>
- ''' <param name="Baud">The baud rate to use. Normally 9600</param>
- Public Sub New(ByVal PortName As String, ByVal Baud As Integer)
- Me.PortName = PortName
- Port = New IO.Ports.SerialPort(PortName, Baud, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
- Port.Encoding = Enc
- Port.Open()
- End Sub
- ''' <summary>Releases all used resources by this class and closes the com port</summary>
- Public Sub Dispose() Implements IDisposable.Dispose
- If Port IsNot Nothing Then
- If Port.IsOpen Then Port.Close()
- Port.Dispose()
- Port = Nothing
- End If
- GC.SuppressFinalize(Me)
- GC.Collect()
- End Sub
- ''' <summary>Writes raw data to the com port</summary>
- ''' <param name="Data">The data to write</param>
- Public Sub WriteRaw(ByVal Data As Byte())
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Writes raw data to the com port</summary>
- ''' <param name="Data">The data to write</param>
- Public Sub WriteRaw(ByVal Data As String)
- Port.Write(Data)
- End Sub
- ''' <summary>Clears the screen</summary>
- Public Sub ClearScreen()
- Port.Write("CL")
- End Sub
- ''' <summary>Enables the text cursor</summary>
- Public Sub EnableCorsor()
- Port.Write("CS1")
- End Sub
- ''' <summary>Disables the text cursor</summary>
- Public Sub DisableCursor()
- Port.Write("CS0")
- End Sub
- ''' <summary>Enables the backlight</summary>
- Public Sub BacklightOn()
- Port.Write("BL1")
- End Sub
- ''' <summary>Disables the backlight</summary>
- Public Sub BacklightOff()
- Port.Write("BL0")
- End Sub
- ''' <summary>Sets the backlight (0..100%)</summary>
- Public Sub SetBacklight(ByVal Percent As Byte)
- If Percent > 100 Then Throw New ArgumentException("Value must be in range of 0..100.", "Percent")
- Dim Data As Byte() = Enc.GetBytes("BL ")
- Data(2) = Percent
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the screen on (True) or off (False)</summary>
- ''' <param name="OnOff">True for turn on the screen, False for turn off</param>
- Public Sub ScreenPower(ByVal OnOff As Boolean)
- If OnOff Then
- Port.Write("SOO1")
- Else
- Port.Write("SOO0")
- End If
- End Sub
- ''' <summary>Sets if the configuration is displayed (True) or not (False)</summary>
- ''' <param name="OnOff">True to display the information, False for not</param>
- Public Sub DisplayConfig(ByVal OnOff As Boolean)
- If OnOff Then
- Port.Write("DC1")
- Else
- Port.Write("DC0")
- End If
- End Sub
- ''' <summary>Sets the baud rate for the device.</summary>
- ''' <param name="Rate">The new baud rate. Must be one of the following: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200</param>
- ''' <remarks>Device always starts up with 9600 baud</remarks>
- Public Sub SetBaudrate(ByVal Rate As Integer)
- Select Case Rate
- Case 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200 ' anything ok
- Case Else : Throw New ArgumentException("Rate invalid.", "Rate")
- End Select
- Port.Write("SB" & Rate.ToString)
- Port.Write(New Byte() {0}, 0, 1)
- Do While Port.BytesToWrite > 0
- Threading.Thread.Sleep(100)
- Loop
- Port.Close()
- Port = Nothing
- GC.Collect()
- Port = New IO.Ports.SerialPort(PortName, Rate, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
- Port.Encoding = Enc
- Port.Open()
- End Sub
- ''' <summary>Sets the I2C address</summary>
- Public Sub SetI2CAddress(ByVal Address As Byte)
- Dim Data As Byte() = Enc.GetBytes("SI2CA ")
- Data(5) = Address
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the displays columns and rows</summary>
- Public Sub SetLCDColRow(ByVal Columns As Byte, ByVal Rows As Byte)
- Dim Data As Byte() = Enc.GetBytes("STCR ")
- Data(4) = Columns
- Data(5) = Rows
- Data(6) = &H80
- Data(7) = &HC0
- Data(8) = &H94
- Data(9) = &HD4
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the position of the text cursor</summary>
- ''' <param name="x">The vertical position</param>
- ''' <param name="y">The horizontal position</param>
- Public Sub SetPrintPos(ByVal x As Byte, ByVal y As Byte)
- Dim Data As Byte() = Enc.GetBytes("TP ")
- Data(2) = x
- Data(3) = y
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Writes a text on the display</summary>
- ''' <param name="Text">The text to write</param>
- Public Sub Print(ByVal Text As String)
- Dim Data As Byte() = Enc.GetBytes("TT" & Text & " ")
- Data(Data.Length - 1) = 0
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Writes a number on the display</summary>
- ''' <param name="Value">The number to write</param>
- Public Sub Print(ByVal Value As Double)
- Dim Data As Byte() = Enc.GetBytes("TT" & Value.ToString & " ")
- Data(Data.Length - 1) = 0
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sends a raw command to the display, bypassing the controller</summary>
- ''' <param name="Command">The command to write</param>
- Public Sub DirectCommand(ByVal Command As Byte)
- Dim Data As Byte() = Enc.GetBytes("MCD ")
- Data(3) = Command
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sends a raw command to the display, bypassing the controller</summary>
- ''' <param name="Command">The command to write</param>
- Public Sub DirectData(ByVal Command As Byte)
- Dim Data As Byte() = Enc.GetBytes("MDT ")
- Data(3) = Command
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the graplic position</summary>
- ''' <param name="x">The horizontal position</param>
- ''' <param name="y">The vertical position</param>
- Public Sub SetPointPos(ByVal x As Byte, ByVal y As Byte)
- Dim Data As Byte() = Enc.GetBytes("GP ")
- Data(2) = x
- Data(3) = y
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the drawing mode</summary>
- ''' <param name="Mode">C = Copy, ! or ~ = NOT, | = OR, ^ = XOR, & = AND</param>
- Public Sub SetMode(ByVal Mode As Char)
- Select Case Mode
- Case "C"c, "!"c, "~"c, "|"c, "^"c, "&"c ' all ok
- Case Else : Throw New ArgumentException("Invalid mode.", "Mode")
- End Select
- Port.Write("DM" & Mode)
- End Sub
- ''' <summary>Draws a monochrome image on the display</summary>
- ''' <param name="x">The horizontal position</param>
- ''' <param name="y">The vertical position</param>
- ''' <param name="Width">The width of the image</param>
- ''' <param name="Height">The height of the image</param>
- ''' <param name="Image">The raw image data</param>
- Public Sub DrawBitmap(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte, ByVal Height As Byte, ByVal Image As Byte())
- Dim Data(Image.Length + 6) As Byte
- Array.Copy(Enc.GetBytes("DIM"), Data, 3)
- Data(3) = x
- Data(4) = y
- Data(5) = Width
- Data(6) = Height
- Array.Copy(Image, 0, Data, 7, Image.Length)
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the graphic rotation</summary>
- ''' <param name="Value">0 for 0°, 1 for 90°, 2 for 180°, 3 for 270°</param>
- Public Sub SetRotation(ByVal Value As Byte)
- If Value > 3 Then Throw New ArgumentException("Value must be in range of 0..3", "Value")
- Port.Write("SD" & Value.ToString)
- End Sub
- ''' <summary>Same as SetRotation(0)</summary>
- Public Sub UndoRotation()
- Call SetRotation(0)
- End Sub
- ''' <summary>Same as SetRotation(1)</summary>
- Public Sub SetRot90()
- Call SetRotation(1)
- End Sub
- ''' <summary>Same as SetRotation(2)</summary>
- Public Sub SetRot180()
- Call SetRotation(2)
- End Sub
- ''' <summary>Same as SetRotation(3)</summary>
- Public Sub SetRot270()
- Call SetRotation(3)
- End Sub
- ''' <summary>Sets the display contrast</summary>
- ''' <param name="Value">The contrast value</param>
- Public Sub SetContrast(ByVal Value As Byte)
- Dim Data As Byte() = Enc.GetBytes("CT ")
- Data(2) = Value
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws a filled rectangle</summary>
- ''' <param name="x">Horizontal position</param>
- ''' <param name="y">Vertical position</param>
- ''' <param name="Width">The width</param>
- ''' <param name="Height">The height</param>
- Public Sub DrawBox(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte, ByVal Height As Byte)
- Dim Data As Byte() = Enc.GetBytes("FR ")
- Data(2) = x
- Data(3) = y
- Data(4) = x + Width
- Data(5) = y + Height
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws a rectangle</summary>
- ''' <param name="x">Horizontal position</param>
- ''' <param name="y">Vertical position</param>
- ''' <param name="Width">The width</param>
- ''' <param name="Height">The height</param>
- Public Sub DrawFrame(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte, ByVal Height As Byte)
- Dim Data As Byte() = Enc.GetBytes("DR ")
- Data(2) = x
- Data(3) = y
- Data(4) = x + Width
- Data(5) = y + Height
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws a circle</summary>
- ''' <param name="x">Horizontal position</param>
- ''' <param name="y">Vertical position</param>
- ''' <param name="Radius">The radius</param>
- ''' <param name="Filled">True to fill it, False to not fill</param>
- Public Sub DrawCircle(ByVal x As Byte, ByVal y As Byte, ByVal Radius As Byte, ByVal Filled As Boolean)
- Dim Data As Byte() = Enc.GetBytes("CC ")
- Data(2) = x
- Data(3) = y
- Data(4) = Radius
- Data(5) = CByte(If(Filled, Asc("1"c), Asc("0"c)))
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws a filled circle</summary>
- ''' <param name="x">Horizontal position</param>
- ''' <param name="y">Vertical position</param>
- ''' <param name="Radius">The radius</param>
- Public Sub DrawDisc(ByVal x As Byte, ByVal y As Byte, ByVal Radius As Byte)
- Call DrawCircle(x, y, Radius, True)
- End Sub
- ''' <summary>Draws a single pixel</summary>
- ''' <param name="x">Horizontal position</param>
- ''' <param name="y">Vertical position</param>
- Public Sub DrawPixel(ByVal x As Byte, ByVal y As Byte)
- Dim Data As Byte() = Enc.GetBytes("DP ")
- Data(2) = x
- Data(3) = y
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws a line on the screen</summary>
- ''' <param name="x1">The horizontal start position</param>
- ''' <param name="y1">The vertical start position</param>
- ''' <param name="x2">The horizontal end position</param>
- ''' <param name="y2">The vertical end position</param>
- Public Sub DrawLine(ByVal x1 As Byte, ByVal y1 As Byte, ByVal x2 As Byte, ByVal y2 As Byte)
- Dim Data As Byte() = Enc.GetBytes("LN ")
- Data(2) = x1
- Data(3) = y1
- Data(4) = x2
- Data(5) = y2
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws a horizontal line on the screen</summary>
- ''' <param name="x">The horizontal start position</param>
- ''' <param name="y">The vertical start position</param>
- ''' <param name="Width">The width for the line</param>
- Public Sub DrawHLine(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte)
- Call DrawLine(x, y, x + Width, y)
- End Sub
- ''' <summary>Draws a vertical line on the screen</summary>
- ''' <param name="x">The horizontal start position</param>
- ''' <param name="y">The vertical start position</param>
- ''' <param name="Height">The height for the line</param>
- Public Sub DrawVLine(ByVal x As Byte, ByVal y As Byte, ByVal Height As Byte)
- Call DrawLine(x, y, x, y + Height)
- End Sub
- ''' <summary>Draws a line from the last position to the new position</summary>
- ''' <param name="x">The new horizontal position</param>
- ''' <param name="y">The new vertical position</param>
- Public Sub DrawLineTo(ByVal x As Byte, ByVal y As Byte)
- Dim Data As Byte() = Enc.GetBytes("LT ")
- Data(2) = x
- Data(3) = y
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Moves the text cursor to the next line</summary>
- Public Sub NextTextLine()
- Port.Write("TRT")
- End Sub
- ''' <summary>Sets the font which will be used</summary>
- ''' <param name="Font">The font to use. Valid: 0, 6, 10, 18, 51, 120, 123, 200, 201, 202, 203</param>
- Public Sub SetFont(ByVal Font As Byte)
- Select Case Font
- Case 0, 6, 10, 18, 51, 120, 123, 200, 201, 202, 203 ' Anything ok
- Case Else : Throw New ArgumentException("Invalid font.", "Font")
- End Select
- Dim Data As Byte() = Enc.GetBytes("SF ")
- Data(2) = Font
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the color which will be used</summary>
- ''' <param name="Color">The color to use. Valid: 0..255 for color displays, 0..1 for monochrome</param>
- Public Sub SetColor(ByVal Color As Byte)
- Dim Data As Byte() = Enc.GetBytes("SC ")
- Data(2) = Color
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Moves an area to another location</summary>
- ''' <param name="x">The horizontal position of the rectangle to move</param>
- ''' <param name="y">The vertical position of the rectangle to move</param>
- ''' <param name="Width">The width of the rectangle to move</param>
- ''' <param name="Height">The height of the rectangle to move</param>
- ''' <param name="XOffset">The horizontal offset to move to</param>
- ''' <param name="YOffset">The vertical offset to move to</param>
- Public Sub MoveArea(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte, ByVal Height As Byte, ByVal XOffset As Byte, ByVal YOffset As Byte)
- Dim Data As Byte() = Enc.GetBytes("MA ")
- Data(2) = x
- Data(3) = y
- Data(4) = Width
- Data(5) = Height
- Data(6) = XOffset
- Data(7) = YOffset
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Moves the text cursor back to the last position</summary>
- Public Sub SetTextPosBack()
- Port.Write("ETB")
- End Sub
- ''' <summary>Moves the text cursor to another relative position (in pixel)</summary>
- ''' <param name="XOffset">The horizontal offset to move to</param>
- ''' <param name="YOffset">The vertical offset to move to</param>
- Public Sub SetTextPosOffset(ByVal XOffset As Byte, ByVal YOffset As Byte)
- Dim Data As Byte() = Enc.GetBytes("ETO ")
- Data(3) = XOffset
- Data(4) = YOffset
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Moves the text cursor to another absolute position (in pixel)</summary>
- ''' <param name="x">The horizontal position to move to</param>
- ''' <param name="y">The vertical position to move to</param>
- Public Sub SetTextPosAbs(ByVal x As Byte, ByVal y As Byte)
- Dim Data As Byte() = Enc.GetBytes("ETP ")
- Data(3) = x
- Data(4) = y
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Uploads the splash screen data</summary>
- ''' <param name="Image">The splash screen data</param>
- Public Sub UploadStartScreen(ByVal Image As Byte())
- Dim Data(4 + Image.Length) As Byte
- Array.Copy(Enc.GetBytes("SSS"), Data, 3)
- Data(3) = CByte(Image.Length And &HFF)
- Data(4) = CByte(Image.Length >> 8)
- Array.Copy(Image, 0, Data, 5, Data.Length)
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Uploads a user font data</summary>
- ''' <param name="Index">The section for the data</param>
- ''' <param name="Font">The font data</param>
- Public Sub UploadUserFont(ByVal Index As Byte, ByVal Font As Byte())
- Dim Data(5 + Font.Length) As Byte
- Array.Copy(Enc.GetBytes("SUF"), Data, 3)
- Data(3) = Index
- Data(4) = CByte(Font.Length And &HFF)
- Data(5) = CByte(Font.Length >> 8)
- Array.Copy(Font, 0, Data, 5, Data.Length)
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets if the start screen should be displayed or not</summary>
- ''' <param name="Value">True will show the splash screen on startup, False disables it</param>
- Public Sub DisplayStartScreen(ByVal Value As Boolean)
- Port.Write("DSS" & If(Value, "1", "0"))
- End Sub
- ''' <summary>Sends a byte to the output head of the display</summary>
- ''' <param name="Value">The byte to send</param>
- Public Sub DigitalOutput(ByVal Value As Byte)
- Dim Data As Byte() = Enc.GetBytes("DOUT ")
- Data(4) = Value
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the line pattern when drawing a line</summary>
- ''' <param name="Value">The pattern to use. eg.: 0xAA = dotted, 0xFA = dash</param>
- Public Sub SetLinePattern(ByVal Value As Byte)
- Dim Data As Byte() = Enc.GetBytes("SLP ")
- Data(3) = Value
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws an image on the device which has up to 256 colors.</summary>
- ''' <param name="x">The horizontal position for the image</param>
- ''' <param name="y">The vertical position of the image</param>
- ''' <param name="Width">The width of the image</param>
- ''' <param name="Height">The height of the image</param>
- ''' <param name="Image">The raw image data</param>
- ''' <remarks>Color format: RRRGGGBB</remarks>
- Public Sub DrawBitmap256(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte, ByVal Height As Byte, ByVal Image As Byte())
- Dim Data(9 + Image.Length) As Byte
- Array.Copy(Enc.GetBytes("EDIM1"), Data, 5)
- Data(5) = x
- Data(6) = y
- Data(7) = Width
- Data(8) = Height
- Array.Copy(Image, 0, Data, 9, Image.Length)
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws an image on the device which has up to 65k colors.</summary>
- ''' <param name="x">The horizontal position for the image</param>
- ''' <param name="y">The vertical position of the image</param>
- ''' <param name="Width">The width of the image</param>
- ''' <param name="Height">The height of the image</param>
- ''' <param name="Image">The raw image data</param>
- ''' <remarks>Color format: RRRRRGGG GGGBBBBB</remarks>
- Public Sub DrawBitmap64K(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte, ByVal Height As Byte, ByVal Image As Byte())
- Dim Data(9 + Image.Length) As Byte
- Array.Copy(Enc.GetBytes("EDIM2"), Data, 5)
- Data(5) = x
- Data(6) = y
- Data(7) = Width
- Data(8) = Height
- Array.Copy(Image, 0, Data, 9, Image.Length)
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Draws an image on the device which has up to 262k colors.</summary>
- ''' <param name="x">The horizontal position for the image</param>
- ''' <param name="y">The vertical position of the image</param>
- ''' <param name="Width">The width of the image</param>
- ''' <param name="Height">The height of the image</param>
- ''' <param name="Image">The raw image data</param>
- ''' <remarks>Color format: 00RRRRRR 00GGGGGG 00BBBBBB</remarks>
- Public Sub DrawBitmap262K(ByVal x As Byte, ByVal y As Byte, ByVal Width As Byte, ByVal Height As Byte, ByVal Image As Byte())
- Dim Data(9 + Image.Length) As Byte
- Array.Copy(Enc.GetBytes("EDIM3"), Data, 5)
- Data(5) = x
- Data(6) = y
- Data(7) = Width
- Data(8) = Height
- Array.Copy(Image, 0, Data, 9, Image.Length)
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the color for ongoing graphic operations.</summary>
- ''' <param name="Red">The intensity for red color</param>
- ''' <param name="Green">The intensity for green color</param>
- ''' <param name="Blue">The intensity for blue color</param>
- Public Sub SetTrueColor(ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte)
- Dim Data As Byte() = Enc.GetBytes("ESC ")
- Data(3) = Red
- Data(4) = Green
- Data(5) = Blue
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Sets the color for ongoing graphic operations.</summary>
- ''' <param name="Color">The .Net Framework color</param>
- Public Sub SetTrueColor(ByVal Color As Drawing.Color)
- Call SetTrueColor(Color.R >> 2, Color.G >> 2, Color.B >> 2)
- End Sub
- ''' <summary>Sets the LCD chip used. See user manual for details.</summary>
- ''' <param name="Chip">The chip to use.</param>
- Public Sub SetLCDChip(ByVal Chip As Byte)
- Dim Data As Byte() = Enc.GetBytes("SLCD ")
- Data(4) = Chip
- Port.Write(Data, 0, Data.Length)
- End Sub
- ''' <summary>Shuts down the MCU to save energy. Send a dummy byte (0x00) for wake up and wait a few msec.</summary>
- Public Sub ShutdownMCU()
- Port.Write("DNMCU")
- End Sub
- ''' <summary>Shuts down the MCU and the display to save energy. Send a dummy byte (0x00) for wake up and wait a few msec.</summary>
- Public Sub ShutdownDevice()
- Port.Write("DNALL")
- End Sub
- ''' <summary>Dummy function to wakeup the device after shutdown command</summary>
- Public Sub WakeupDevice()
- Dim Data(0) As Byte
- Data(0) = 0
- Port.Write(Data, 0, Data.Length)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement