Microsoft Small Basic
Program Listing: PVXC450.000-0
List Program
' Environment Meter
' Version 0.2.0
' Copyright © 2021 Nonki Takahashi. The MIT License.
' Last update 2021-05-07
' Program ID PVXC450.001
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")
If c < 400 Then
hue = 120
ElseIf 1000 < c Then
hue = 0
Else
hue = (1000 - c) * 120 / 600
EndIf
rc = LDColours.HSLtoRGB(hue, 1, 0.5)
LDShapes.PenColour(co2ring, rc)
Shapes.SetText(hum, h + "%")
a = h * 180 / 100
LDShapes.RotateAbout(pointer, cx, cy, a)
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.Left = 20
bg = "#333333"
GraphicsWindow.BackgroundColor = bg
' CO2 meter
GraphicsWindow.PenColor = "Lime"
GraphicsWindow.PenWidth = 10
GraphicsWindow.BrushColor = "Transparent"
co2ring = Shapes.AddEllipse(tw, tw)
Shapes.Move(co2ring, tw, 10)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.FontSize = 40
co2 = Shapes.AddText("0000ppm")
Shapes.Move(co2, tw + 15, tw / 2 - 15)
' humidity meter
hx = 2 * tw + 10
hy = 10
hw = gw - 2 * tw - 20
hh = tw
GraphicsWindow.FillRectangle(hx, hy, hw, hh)
GraphicsWindow.PenWidth = 2
GraphicsWindow.BrushColor = "Gray"
hum = Shapes.AddText("50%")
Shapes.Move(hum, 2 * tw + 20, 10)
GraphicsWindow.BrushColor = bg
cs = 20
cx = hx + hw / 2
cy = hy + hh - 1.5 * cs
GraphicsWindow.FillEllipse(cx - cs / 2, cy - cs / 2, cs, cs)
pl = 100
GraphicsWindow.PenColor = "Gray"
r1 = 1.02
r3 = 1.4
r4 = 0.98
r5 = 0.7
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FontSize = 20
i = 0
For h = 0 To 100 Step 2
a = h * 180 / 100
_a = Math.GetRadians(a)
If (40 <= h) And (h <= 60) Then
i = i + 1
x4[i] = cx - r4 * pl * Math.Cos(_a)
y4[i] = cy - r4 * pl * Math.Sin(_a)
x5[i] = cx - r5 * pl * Math.Cos(_a)
y5[i] = cy - r5 * pl * Math.Sin(_a)
EndIf
If Math.Remainder(h, 10) = 0 Then
x3 = cx - r3 * pl * Math.Cos(_a)
y3 = cy - r3 * pl * Math.Sin(_a)
GraphicsWindow.DrawText(x3 - 10, y3 - 10, h)
r2 = 1.2
Else
r2 = 1.1
EndIf
x1 = cx - r1 * pl * Math.Cos(_a)
y1 = cy - r1 * pl * Math.Sin(_a)
x2 = cx - r2 * pl * Math.Cos(_a)
y2 = cy - r2 * pl * Math.Sin(_a)
GraphicsWindow.DrawLine(x1, y1, x2, y2)
EndFor
GraphicsWindow.BrushColor = "Lime"
GraphicsWindow.PenWidth = 0
n = Array.GetItemCount(x4)
For i = 1 To n
points[i][1] = x4[i]
points[i][2] = y4[i]
EndFor
For j = n To 1 Step -1
points[i][1] = x5[j]
points[i][2] = y5[j]
i = i + 1
EndFor
arc = LDShapes.AddPolygon(points)
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = bg
pointer = Shapes.AddLine(cx, cy, cx - pl, cy)
LDShapes.RotateAbout(pointer, cx, cy, 90)
' thermometer
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(10, 10, tw - 20, gh - 20)
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FontSize = 40
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