Microsoft Small Basic

Program Listing: GZN399-2
' OSOYOO UNO R3 - LED Signal
' Version 0.41
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Program ID GZN399-2

GraphicsWindow.Title = "LED Signal"
Init()
InitBreadboard()
Draw()
DrawBreadboardHoles()
InitBoard()
Draw()
param = "x=52;y=11.5;width=12;height=5.5;pc=White;"
DrawRoundRect()
param = "points=101.8 27.3 101.8 19 40.3 19 40.3 26;pw=1;pc=Black;"
DrawWire()
param = "points=110.4 35.5 110.4 16 53 16 53 26;pw=1;pc=Red;"
DrawWire()
param = "points=126.95 35.5 126.95 13 64.5 13 64.5 26;pw=1;pc=Yellow;"
DrawWire()
param = "points=143.5 35.5 143.5 10 69.8 10 69.8 26;pw=1;pc=DarkBlue;"
DrawWire()
param = "points=99.3 35.5 99.3 27.5;pw=1;pc=Black;"
DrawWire()
param = "points=115.85 35.5 115.85 27.5;pw=1;pc=Black;"
DrawWire()
param = "points=132.4 35.5 132.4 27.5;pw=1;pc=Black;"
DrawWire()
InitRegister()
For j = 1 To 3
Draw()
ox = ox + 16.55
EndFor
color = "#CC0000"
InitLED()
Draw()
color = "#CCCC00"
InitLED()
ox = ox + 16.59
Draw()
color = "#00CC00"
InitLED()
ox = ox + 33.18
Draw()

Sub Draw
GraphicsWindow.FontName = "OCRB"
GraphicsWindow.FontSize = 8
GraphicsWindow.FontBold = "False"
n = Array.GetItemCount(shape)
For i = 1 To n
shp = shape[i]
x = (ox + shp["x"]) * scale
y = (oy + shp["y"]) * scale
width = shp["width"] * scale
height = shp["height"] * scale
GraphicsWindow.BrushColor = shp["bc"]
If shp["func"] = "rect" Then
GraphicsWindow.FillRectangle(x, y, width, height)
If shp["bc"] = "#333333" Then
DrawHoles()
EndIf
ElseIf shp["func"] = "ell" Then
GraphicsWindow.FillEllipse(x, y, width, height)
ElseIf shp["func"] = "line" Then
GraphicsWindow.PenColor = shp["pc"]
GraphicsWindow.PenWidth = shp["pw"] * scale
x1 = (ox + shp["x1"]) * scale
y1 = (oy + shp["y1"]) * scale
x2 = (ox + shp["x2"]) * scale
y2 = (oy + shp["y2"]) * scale
GraphicsWindow.DrawLine(x1, y1, x2, y2)
ElseIf shp["func"] = "tri" Then
x1 = (ox + shp["x1"]) * scale
y1 = (oy + shp["y1"]) * scale
x2 = (ox + shp["x2"]) * scale
y2 = (oy + shp["y2"]) * scale
x3 = (ox + shp["x3"]) * scale
y3 = (oy + shp["y3"]) * scale
GraphicsWindow.FillTriangle(x1, y1, x2, y2, x3, y3)
ElseIf shp["func"] = "text" Then
txt = shp["text"]
GraphicsWindow.FontName = shp["fn"]
GraphicsWindow.FontSize = shp["fs"] * scale
GraphicsWindow.FontItalic = shp["fi"]
angle= shp["angle"]
If angle <> 0 Then
obj = Shapes.AddText(txt)
Shapes.Move(obj, x, y)
Shapes.Rotate(obj, angle)
Else
GraphicsWindow.DrawText(x, y, txt)
EndIf
EndIf
EndFor
EndSub

Sub DrawBreadboardHoles
GraphicsWindow.BrushColor = "Black"
size = 1
width = size * scale
height = size * scale
dx = 2.8 * scale
dy = 2.8 * scale
For row = 1 To 18
y = (oy + 3) * scale + (row - 1) * dy
If row = 3 Or row = 9 Or row = 10 Or row = 16 Then
Else
For col = 1 To 63
If row = 1 Or row = 2 Or row = 17 Or row = 18 Then
If 3 <= col And col <= 61 And Math.Remainder(col, 6) <> 2 Then
skip = "False"
Else
skip = "True"
EndIf
Else
skip = "False"
EndIf
If Not[skip] Then
x = (ox + 3) * scale + (col - 1) * dx
GraphicsWindow.FillRectangle(x, y, width, height)
EndIf
EndFor
EndIf
EndFor
EndSub

Sub DrawHoles
' holes for connector
If i = 11 Then
holes = 10
s = symbol[1]
ElseIf i = 12 Then
holes = 8
s = symbol[2]
ElseIf i = 13 Then
holes = 8
s = symbol[3]
ElseIf i = 14 Then
holes = 6
s = symbol[4]
Else
holes = 0
EndIf
If 0 < holes Then
x = (ox + shp["x"]) * scale
y = (oy + shp["y"]) * scale
width = shp["width"] * scale
height = shp["height"] * scale
hw = (width - (holes + 1) * 3) / holes
hy = y + 3
hh = height - 6
pin = 0
For hx = x + 3 To x + width - hw - 2 Step hw + 3
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillRectangle(hx, hy, hw, hh)
pin = pin + 1
GraphicsWindow.BrushColor = "White"
len = Text.GetLength(s[pin])
txt = Shapes.AddText(s[pin])
If i < 13 Then
Shapes.Move(txt, hx + (1.8 - len) * 2.8 - 2, hy + len * 2.4 + 8)
Else
Shapes.Move(txt, hx + (1.8 - len) * 2.8 - 2, hy - len * 2.4 - 10)
EndIf
Shapes.Rotate(txt, -90)
EndFor
EndIf
EndSub

Sub DrawRoundRect
pc = param["pc"]
x = ox + param["x"]
y = oy + param["y"]
r = param["height"] / 2
cx = x + r
cy = y + r
For a = 90 To 270 Step 10
_a = Math.GetRadians(a)
x = cx + r * Math.Cos(_a)
y = cy + r * Math.Sin(_a)
GraphicsWindow.SetPixel(x * scale, y * scale, pc)
EndFor
x1 = ox + param["x"] + r
x2 = ox + param["x"] + param["width"] - r
y1 = oy + param["y"]
y2 = oy + param["y"] + param["height"]
For x = x1 + 0.4 To x2 - 0.5 Step 0.5
GraphicsWindow.SetPixel(x * scale, y1 * scale, pc)
GraphicsWindow.SetPixel(x * scale, y2 * scale, pc)
EndFor
cx = cx + param["width"] - 2 * r
For a = -90 To 90 Step 10
_a = Math.GetRadians(a)
x = cx + r * Math.Cos(_a)
y = cy + r * Math.Sin(_a)
GraphicsWindow.SetPixel(x * scale, y * scale, pc)
EndFor
EndSub

Sub DrawWire
size = param["pw"] * scale
GraphicsWindow.PenWidth = size
GraphicsWindow.PenColor = param["pc"]
GraphicsWindow.BrushColor = param["pc"]
p = 1
points = param["points"]
len = Text.GetLength(points)
n = 0
While p <= len
sp = Text.GetIndexOf(Text.GetSubTextToEnd(points, p), " ")
If 0 < sp Then
n = n + 1
x[n] = Text.GetSubText(points, p, sp - 1) * scale
p = p + sp
Else
x[n] = Text.GetSubTextToEnd(points, p) * scale
p = len + 1
EndIf
If p <= len Then
sp = Text.GetIndexOf(Text.GetSubTextToEnd(points, p), " ")
If 0 < sp Then
y[n] = Text.GetSubText(points, p, sp - 1) * scale
p = p + sp
Else
y[n] = Text.GetSubTextToEnd(points, p) * scale
p = len + 1
EndIf
EndIf
EndWhile
GraphicsWindow.FillEllipse(x[1] - size / 2, y[1] - size / 2, size, size)
For i = 2 To n
GraphicsWindow.DrawLine(x[i - 1], y[i - 1], x[i], y[i])
GraphicsWindow.FillEllipse(x[i] - size / 2, y[i] - size / 2, size, size)
EndFor
EndSub

Sub Init
Not = "False=True;True=False;"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
url = Program.Directory + "\IMG_0045.JPG"
url = ""
img = ImageList.LoadImage(url)
iw = ImageList.GetWidthOfImage(img)
ih = ImageList.GetHeightOfImage(img)
x = -92.5
y = 0
scale = 0.135
width = iw * scale
height = ih * scale
photo = Shapes.AddImage(img)
Shapes.Move(photo, x - iw / 2 + width / 2, y - ih / 2 + height / 2)
Shapes.Zoom(photo, scale, scale)
Shapes.SetOpacity(photo, 50)
EndSub

Sub InitBoard
scale = 4
ox = 6
oy = 24
shape = ""
shape[1] = "func=rect;x=7;y=0;width=65;height=53;bc=#003366;"
shape[2] = "func=tri;x1=72;y1=0;x2=72;y2=1;x3=73;y3=1;bc=#003366;"
shape[3] = "func=rect;x=72;y=1;width=1;height=52;bc=#003366;"
shape[4] = "func=tri;x1=73;y1=13;x2=73;y2=16;x3=76;y3=16;bc=#003366;"
shape[5] = "func=rect;x=73;y=16;width=3;height=32;bc=#003366;"
shape[6] = "func=tri;x1=73;y1=48;x2=73;y2=51;x3=76;y3=48;bc=#003366;"
shape[7] = "func=rect;x=0;y=9;width=16;height=12;bc=Silver;"
shape[8] = "func=rect;x=10;y=1;width=6;height=6;bc=Silver;"
shape[9] = "func=ell;x=11.5;y=2.5;width=3;height=3;bc=Red;"
shape[10] = "func=rect;x=5;y=40;width=14;height=9;bc=#333333;"
shape[11] = "func=rect;x=25;y=1;width=26;height=3;bc=#333333;"
shape[12] = "func=rect;x=52;y=1;width=21;height=3;bc=#333333;"
shape[13] = "func=rect;x=35;y=49;width=21;height=3;bc=#333333;"
shape[14] = "func=rect;x=57;y=49;width=16;height=3;bc=#333333;"
shape[15] = "func=rect;x=36;y=32;width=37;height=10;bc=#333333;"
shape[16] = "func=text;x=38;y=12;text=OSOYOO;fn=Impact;fs=4;fi=True;bc=White;"
shape[17] = "func=text;x=53.5;y=12;text=UNO;fn=Arial;fs=4.2;bc=White;"
shape[18] = "func=ell;x=21;y=1;width=3.2;height=3.2;bc=White;"
shape[19] = "func=ell;x=20;y=49;width=3.2;height=3.2;bc=White;"
shape[20] = "func=ell;x=71.5;y=16;width=3.2;height=3.2;bc=White;"
shape[21] = "func=ell;x=71.5;y=44;width=3.2;height=3.2;bc=White;"
shape[22] = "func=ell;x=22;y=42;width=6;height=6;bc=Silver;"
shape[23] = "func=ell;x=29;y=42;width=6;height=6;bc=Silver;"
shape[24] = "func=rect;x=24;y=16.5;width=5.5;height=5.5;bc=#333333;"
shape[25] = "func=rect;x=13;y=33;width=3;height=6;bc=#333333;"
shape[26] = "func=ell;x=21;y=25;width=4;height=4;bc=Silver;"
shape[27] = "func=ell;x=27;y=25;width=4;height=4;bc=Silver;"
shape[28] = "func=rect;x=23;y=25;width=6;height=4;bc=Silver;"
shape[29] = "func=rect;x=34;y=11;width=2;height=1;bc=Silver;"
shape[30] = "func=rect;x=34.5;y=11;width=1;height=1;bc=Yellow;"
shape[31] = "func=text;x=31;y=10.5;text=L;fn=OCRB;fs=2;bc=White;"
shape[32] = "func=rect;x=34;y=16;width=2;height=1;bc=Silver;"
shape[33] = "func=rect;x=34.5;y=16;width=1;height=1;bc=Yellow;"
shape[34] = "func=text;x=31;y=15;text=TX;fn=OCRB;fs=2;bc=White;"
shape[35] = "func=rect;x=34;y=18.5;width=2;height=1;bc=Silver;"
shape[36] = "func=rect;x=34.5;y=18.5;width=1;height=1;bc=Yellow;"
shape[37] = "func=text;x=31;y=17.5;text=RX;fn=OCRB;fs=2;bc=White;"
shape[38] = "func=rect;x=64.5;y=16;width=2;height=1;bc=Silver;"
shape[39] = "func=rect;x=65;y=16;width=1;height=1;bc=Lime;"
shape[40] = "func=text;x=67;y=15;text=ON;fn=OCRB;fs=2;bc=White;"
shape[41] = "func=text;x=45;y=34;text=ATMEGA328P-PU;fn=OCRB;fs=2.5;angle=180;bc=#666666;"
symbol[1] = "3=AREF;4=GND;5=D13;6=D12;7=~D11;8=~D10;9=~D9;10=D8;"
symbol[2] = "1=D7;2=~D6;3=~D5;4=D4;5=~D3;6=D2;7=RX→D1;8=TX←D0;"
symbol[3] = "2=IOREF;3=RESET;4=3.3V;5=5V;6=GND;7=GND;8=Vin;"
symbol[4] = "1=A0;2=A1;3=A2;4=A3;5=A4;6=A5;"
EndSub

Sub InitBreadboard
scale = 4
ox = 90
oy = 24
shape = ""
shape[1] = "func=rect;x=0;y=0;width=166;height=54;bc=#F8FFCC;"
shape[2] = "func=rect;x=0;y=26;width=166;height=2;bc=#E8EEBB;"
shape[3] = "func=text;x=3;y=0;text=-;fn=Courier New;fs=4;bc=DodgerBlue;"
shape[4] = "func=rect;x=6;y=1;width=152;height=0.8;bc=DodgerBlue;"
shape[5] = "func=text;x=3;y=5;text=+;fn=Courier New;fs=4;bc=Red;"
shape[6] = "func=rect;x=6;y=7.6;width=152;height=0.8;bc=Red;"
shape[7] = "func=text;x=3;y=44.8;text=-;fn=Courier New;fs=4;bc=DodgerBlue;"
shape[8] = "func=rect;x=6;y=45.8;width=152;height=0.8;bc=DodgerBlue;"
shape[9] = "func=text;x=3;y=49.4;text=+;fn=Courier New;fs=4;bc=Red;"
shape[10] = "func=rect;x=6;y=52.2;width=152;height=0.8;bc=Red;"
EndSub

Sub InitLED
scale = 4
ox = 106.5
oy = 36
shape = ""
shape[1] = "func=line;x1=1.5;y1=7;x2=1.4;y2=11;pc=Silver;pw=0.5;"
shape[2] = "func=line;x1=4;y1=7;x2=4;y2=11;pc=Silver;pw=0.5;"
shape[3] = "func=rect;x=0;y=7.5;width=5;height=1;bc=" + color + ";"
shape[4] = "func=rect;x=0.5;y=2;width=4;height=5.5;bc=" + color + ";"
shape[5] = "func=ell;x=0.5;y=0;width=4;height=4;bc=" + color + ";"
EndSub

Sub InitRegister
scale = 4
ox = 98
oy = 31
shape = ""
shape[1] = "func=line;x1=1.5;y1=3;x2=1.4;y2=13.5;pc=Silver;pw=0.5;"
shape[2] = "func=line;x1=9.5;y1=3;x2=9.6;y2=13.5;pc=Silver;pw=0.5;"
shape[3] = "func=line;x1=1.5;y1=3;x2=2;y2=2.5;pc=Silver;pw=0.5;"
shape[4] = "func=line;x1=9.5;y1=3;x2=9;y2=2.5;pc=Silver;pw=0.5;"
shape[5] = "func=ell;x=2;y=1;width=2;height=3;bc=SandyBrown;"
shape[6] = "func=rect;x=2;y=1.5;width=7;height=2;bc=SandyBrown;"
shape[7] = "func=ell;x=7;y=1;width=2;height=3;bc=SandyBrown;"
shape[8] = "func=rect;x=3;y=1.5;width=0.7;height=2;bc=Red;"
shape[9] = "func=rect;x=4.5;y=1.5;width=0.7;height=2;bc=Black;"
shape[10] = "func=rect;x=6;y=1.5;width=0.7;height=2;bc=SaddleBrown;"
shape[11] = "func=rect;x=7.5;y=1.5;width=0.7;height=2;bc=Gold;"
EndSub