Microsoft Small Basic

Program Listing: KKK886-0
' Thermometer
' Version 0.2
' Copyright © 2016-2017 Nonki Takahashi. The MIT License.
' Last update 2017-08-24
' Program ID KKK886-0

title = "Thermometer"
GraphicsWindow.Title = title
Init()
While "True"
GetLine()
If line <> "" Then
t = line
If -30 < t And t < 50 Then
Shapes.SetText(txt, t + "℃")
DrawTemp()
EndIf
EndIf
Program.Delay(200)
EndWhile

Sub DrawTemp
If temp <> "" Then
Shapes.Remove(temp)
EndIf
GraphicsWindow.BrushColor = "#DD0000"
GraphicsWindow.PenWidth = 0
temp = Shapes.AddRectangle(4, (t + 30) * 4.5)
Shapes.Move(temp, gw / 2 - 2, y1 + (50 - t) * 4.5)
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 = 200
gh = 600
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Top = 20
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.FontSize = 40
GraphicsWindow.BrushColor = "Gray"
txt = Shapes.AddText("")
y1 = 138
y2 = gh - 100
t = 50
GraphicsWindow.PenColor = "Gray"
For y = y1 To y2 Step 9
If Math.Remainder(y, 45) = 3 Then
x1 = gw / 2 - 40
x2 = gw / 2 + 40
If t < 0 Then
GraphicsWindow.DrawText(gw / 2 - 82, y - 40, t)
Else
GraphicsWindow.DrawText(gw / 2 + 20, y - 40, t)
EndIf
t = t - 10
Else
x1 = gw / 2 - 20
x2 = gw / 2 + 20
EndIf
GraphicsWindow.DrawLine(x1, y, x2, y)
EndFor
Shapes.Move(txt, 20, 10)
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillEllipse(gw / 2 - 15, gh - 60, 30, 30)
GraphicsWindow.FillEllipse(gw / 2 - 5, 60, 10, 10)
GraphicsWindow.FillRectangle(gw / 2 - 5, 65, 10, gh - 100)
GraphicsWindow.BrushColor = "#DD0000"
GraphicsWindow.FillEllipse(gw / 2 - 10, gh - 55, 20, 20)
GraphicsWindow.FillRectangle(gw / 2 - 2, gh - 102, 4, 50)
p = 1 ' COM3 receive buffer pointer
status = LDCommPort.OpenPort("COM3", 115200)
LDCommPort.SetEncoding("Ascii")
LDCommPort.DataReceived = OnDataReceived
EndSub

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