Microsoft Small Basic

Program Listing: PVXC450.000
' Environment Meter
' Version 0.1
' Copyright © 2021 Nonki Takahashi. The MIT License.
' Last update 2021-04-17

title = "Environment Meter"
GraphicsWindow.Title = title
Init()
While "True"
GetLine()
If line <> "" Then
param = LDText.Split(line, ",")
c = param[1] ' [ppm]
t = param[2] ' [℃]
h = LDText.Trim(param[3]) ' [%]
If -30 < t And t < 50 Then
Shapes.SetText(txt, (t * 1) + "℃")
DrawTemp()
EndIf
Shapes.SetText(co2, c + "ppm")
Shapes.SetText(hum, h + "%")
GraphicsWindow.Title = title + " " + Clock.Date + " " + Clock.Time
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, tw / 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 = 800
tw = 200
gh = 600
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Top = 20
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.FontSize = 40
GraphicsWindow.BrushColor = "#333333"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(10, 10, tw - 20, gh - 20)
GraphicsWindow.PenColor = "Lime"
GraphicsWindow.PenWidth = 10
GraphicsWindow.BrushColor = "Transparent"
co2ring = Shapes.AddEllipse(tw, tw)
Shapes.Move(co2ring, tw, 10)
GraphicsWindow.BrushColor = "White"
co2 = Shapes.AddText("0000ppm")
Shapes.Move(co2, tw + 15, tw / 2 - 15)
GraphicsWindow.FillRectangle(2 * tw + 10, 10, gw - 2 * tw - 20, tw)
GraphicsWindow.PenWidth = 2
GraphicsWindow.BrushColor = "Gray"
hum = Shapes.AddText("50%")
Shapes.Move(hum, 2 * tw + 20, 10)
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 = tw / 2 - 40
x2 = tw / 2 + 40
If t < 0 Then
GraphicsWindow.DrawText(tw / 2 - 82, y - 40, t)
Else
GraphicsWindow.DrawText(tw / 2 + 20, y - 40, t)
EndIf
t = t - 10
Else
x1 = tw / 2 - 20
x2 = tw / 2 + 20
EndIf
GraphicsWindow.DrawLine(x1, y, x2, y)
EndFor
Shapes.Move(txt, 20, 10)
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillEllipse(tw / 2 - 15, gh - 60, 30, 30)
GraphicsWindow.FillEllipse(tw / 2 - 5, 60, 10, 10)
GraphicsWindow.FillRectangle(tw / 2 - 5, 65, 10, gh - 100)
GraphicsWindow.BrushColor = "#DD0000"
GraphicsWindow.FillEllipse(tw / 2 - 10, gh - 55, 20, 20)
GraphicsWindow.FillRectangle(tw / 2 - 2, gh - 102, 4, 50)
p = 1 ' COM3 receive buffer pointer
status = LDCommPort.OpenPort("COM4", 115200)
If status <> "SUCCESS" Then
TextWindow.WriteLine("status=" + status)
EndIf
LDCommPort.SetEncoding("Ascii")
LDCommPort.DataReceived = OnDataReceived
EndSub

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