Τρίτη 11 Οκτωβρίου 2022

LED CLOCK published in Rosetta Code

This small program draw a digital LED like clock, on desktop, in any monitor type (1280X1024, 1920X1080 etc), using transparent background (so we can click enything behind).

Also there is low demand for CPU usage.

You can find this programm as LED in INFO (the Info.gsb file included in M2000 Setup File)

The main loop is a block of type Every ms_number { }. We use 1000/2 (so 500ms, or 1/2 second) for refreshing the screen. Because M2000 use double buffering, we use Refresh 1000 statement to do two things: To redraw everything (from double buffer) and to suspend redrawing for 1000ms (1 second), where the auto redrawing happen. So because of the Every block we do a redraw each time the Every block run. The idle time goes to system by the Every Block.

About Arrays in M2000. We see here that we have an array D() which defined by the Dim statement. Also we see here that every item of D() array get a tuple (an auto array) using the parenthesis style. An empty tuple is this (,) and a tuple with one item say "Alfa" is this ("Alfa",). Array items can be anything including other arrays. From a "named" array like D() we get numeric values or objects (like arrays). If we have strings a Link D() to D$() make an interface for reading strings (so D$() is a reference of D()). In the call of subrutine LED() we passs the D(n) item (the n item of D() using n as index from low bound to upper bound). Auto arrays or tuple have always index low bound 0. So D(n) is an auto array which pass to a local to subroutine name S(). The S() is a copy of the tuple. If we use S without parenthesis, in LED() subrutine we pass the pointer of the object to that S. A S#val(0)  read the first item of array which pointed by S. Here we use S() a copy of an array (a tuple in D()).

For drawing the polygons, we use polygon statement. We use move statement to move the graphic cursor. The coordinates are in Twips (1440 for a inch, real for printers, but not real for monitors). The origin is always in top left corner (0,0). We draw on M2000 Console, a layer above the Back (Background) a layer which wecan draw also, but here we use a black color.  M2000 console has also 32 layers above the console layer, and also we can use Use Forms, and you can draw in Form Layer or on the layer of an Image control.





\\ if you have two monitors:
\\ Window mode, 1     \\ mode is a read only variable return the size of current font
// Window mode, 2    // selecet monitor 2
cls, 0
window 6, window


Module Led_Clock{
Escape Off
Smooth off
Dim D(-1 to 9)
D(-1)=(0,0,0,0,0,0,0)
D(0)=(1,1,1,0,1,1,1)
D(1)=(0,0,1,0,0,1,0)
D(2)=(1,0,1,1,1,0,1)
D(3)=(1,0,1,1,0,1,1)
D(4)=(0,1,1,1,0,1,0)
D(5)=(1,1,0,1,0,1,1)
D(6)=(1,1,0,1,1,1,1)
D(7)=(1,0,1,0,0,1,0)
D(8)=(1,1,1,1,1,1,1)
D(9)=(1,1,1,1,0,1,1)
N=240
XX=(scale.x-N*75) div 2
YY=scale.y-N*22
NN=N
BackColor=0
CLS BackColor, 0
Back {CLS BackColor,0}
desktop 255, BackColor
Forecolor=12
C=BackColor-Forecolor
pen BackColor
for i=0 to 9: cc=d(i): cc*=c:next
m=1
move XX+N*23.2, YY+N*5.2
polygon BackColor-C, N,-N, N,N, -N, N, -N, -N
move XX+N*23.2,YY+N*13.2
polygon BackColor-C, N,-N, N,N, -N, N, -N, -N
move XX+N*49.2,YY+N*5.2
polygon BackColor-C, N,-N, N,N, -N, N, -N, -N
move XX+N*49.2,YY+N*13.2
polygon BackColor-C, N,-N, N,N, -N, N, -N, -N
dsk=True
every 1000/2 {
k=now
k1=val(str$(k, "hh"))
k2=val(str$(k, "nn"))
k3=val(str$(k, "ss"))
LED(XX, D(k1 div 10))
LED(XX+N*12, D(k1 mod 10))
LED(XX+N*26, D(k2 div 10))
LED(XX+N*38, D(k2 mod 10))
LED(XX+N*52, D(k3 div 10))
LED(XX+N*64, D(k3 mod 10))
refresh 1000
if keypress(32) then
dsk~
if dsk then desktop 255 else desktop 255, BackColor
end if
if keypress(27) or mouse=2 then exit
}
desktop 255
pen 14
refresh 50
mode 16
wait 1000
Escape On


sub LED(XX, S())
move XX+N*1.2, YY+NN
\\ LED  - UPPER
polygon BackColor-S(0), N,-N,N*6,0, N,N, -N, N,-N*6,0, -N, -N
\\ LED | LEFT UPPER
move XX+N*1.2-N*1.2, YY+N*1.2+NN
polygon BackColor-S(1), N,-N,N,N,0,N*6,-N, N, -N, -N, 0, -N*6
move XX+N*1.2+N*7.2, YY+N*1.2+NN
\\ LED | RIGHT UPPER
polygon BackColor-S(2), N,-N,N,N,0,N*6,-N, N, -N, -N, 0, -N*6
move XX+N*1.2, YY+N*8.4+NN
\\ LED - MIDDLE
polygon BackColor-S(3), N,-N,N*6,0, N,N, -N, N,-N*6,0, -N, -N
\\ LED | LEFT BOTTOM
move XX+N*1.2-N*1.2, YY+N*9.6+NN
polygon BackColor-S(4), N,-N,N,N,0,N*6,-N, N, -N, -N, 0, -N*6
\\ LED | RIGHT BOTTOM
move XX+N*1.2+N*7.2, YY+N*9.6+NN
polygon BackColor-S(5), N,-N,N,N,0,N*6,-N, N, -N, -N, 0, -N*6
\\ LED - BOTTOM
move XX+N*1.2, YY+N*16.8+NN
polygon BackColor-S(6), N,-N,N*6,0, N,N, -N, N,-N*6,0, -N, -N
end sub
}
Led_Clock


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.