Microsoft Small Basic

Program Listing: HLT038
' Turtle Controller
' Version 0.1
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Last update 2016-10-17

GraphicsWindow.Title = "Turtle Controller 0.1"
Init()
While "True"
GetCmd()
tx = Turtle.X
ty = Turtle.Y
If cmd = "0" Then
GraphicsWindow.Clear()
tx = gw / 2
ty = gh / 2
Turtle.Show()
Turtle.X = tx
Turtle.Y = ty
Turtle.Angle = 0
ElseIf cmd = "2" Then
Turtle.MoveTo(tx, ty - 50)
ElseIf cmd = "4" Then
Turtle.MoveTo(tx - 50, ty)
ElseIf cmd = "6" Then
Turtle.MoveTo(tx + 50, ty)
ElseIf cmd = "8" Then
Turtle.MoveTo(tx, ty + 50)
EndIf
Program.Delay(200)
EndWhile

Sub GetCmd
GetLine()
If line <> "" Then
If line = "FFFFFFFF" Then
code = lastLine
n = n + 1
Else
code = line
n = 1
EndIf
If n <> 2 Then
cmd = ctrl[code]
Else
cmd = ""
EndIf
If line <> "FFFFFFFF" Then
lastLine = line
EndIf
Else
cmd = ""
EndIf
EndSub

Sub GetLine
len = Text.GetLength(buf)
If p <= len Then
line = Text.GetSubText(buf, p, len - p + 1)
nl = Text.GetIndexOf(line, CRLF)
If 0 < nl Then
line = Text.GetSubText(line, 1, nl - 1)
p = p + nl + 1
Else
line = ""
EndIf
Else
line = ""
EndIf
EndSub

Sub Init
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
ctrl = "FFA25D=CH-;FF629D=CH;FFE21D=CH+;"
ctrl = ctrl + "FF22DD=PREV;FF02FD=NEXT;FFC23D=PLAY;"
ctrl = ctrl + "FFE01F=-;FFA857=+;FF906F=EQ;"
ctrl = ctrl + "FF6897=0;FF9867=100+;FFB04F=200+;"
ctrl = ctrl + "FF30CF=1;FF18E7=2;FF7A85=3;"
ctrl = ctrl + "FF10EF=4;FF38C7=5;FF5AA5=6;"
ctrl = ctrl + "FF42BD=7;FF4AB5=8;FF52AD=9;"
Turtle.Speed = 8
Turtle.X = gw / 2
Turtle.Y = gh / 2
Turtle.Show()
p = 1 ' COM3 receive buffer pointer
status = LDCommPort.OpenPort("COM3",9600)
LDCommPort.SetEncoding("Ascii")
LDCommPort.DataReceived = OnDataReceived
EndSub

Sub OnDataReceived
buf = Text.Append(buf, LDCommPort.RXAll())
EndSub