## Meter object

Henko
Posts: 793
Joined: Tue Apr 09, 2013 12:23 pm
Windows
Location: Groningen, Netherlands
Flag:

### Meter object

option angle degrees
graphics
fill color .8,.8,.8
graphics clear .8,.8,.8
rmeter("RPM","b",500,200,130,20,60,0,1)
for t=0 to 2000 step 3
speed=70+60*sin(t) ! rpm=40+20*sin(-.7*t)
rmeter("RPM","b",500,200,130,10,60,rpm,0)
pause .1
next t
rmeter("RPM","b",500,200,130,10,60,0,-1)
end

' rmeter function, where:
' tit\$ = text, printed on the meter
' col\$ = color of meter, "r", "g" , "b" for red, green and blue
' xc and yc = centre of meter on the screen
' rad = size (diameter) of the meter
' minval and maxval = lowest and highest value on the scale
' val = the value to be displayed by the meter
' mode=1 : creation of one meter
' mode=0 : use meter to display value
' mode=-1: delete meter from screen
'
if mode=-1 then
return
end if
graphics lock
if mode=0 then mode_0
set_col("n")
draw size 3 ! draw circle xc,yc size rad
draw size 4 ! draw circle xc,yc size rad-6
set_col(col\$)
draw size 3 ! draw circle xc,yc size rad-3
set_col("n")
draw size 3
for a=130 to 410 step 28
draw line xs,ys to xe,ye
next a
draw size 1
for a=130 to 410 step 7
draw line xs,ys to xe,ye
next a
delta=(maxval-minval)/10 ! sval=minval
draw font size 15
for a=130 to 410 step 28
draw text n2a\$(sval,3,0) at xs,ys
sval=sval+delta
next a
draw font size 20
mode_0:
set_col(col\$)
fill circle xc,yc size 10 ! draw text tit\$ at xs,ys
set_col("n")
if val<minval then val=minval
if val>maxval then val=maxval
beta=130+280*(val-minval)/(maxval-minval)
xs=xc-30*cos(beta) ! ys=yc-30*sin(beta)
draw size 6 ! draw line xs,ys to xe,ye
graphics unlock
end def

def n2a\$(num,lang,dec)
b\$="               "
fac=10^dec
num\$=int(fac*num+.5)/fac
tot=lang-len(num\$)
if tot<1 then tot=1
a\$=substr\$(b\$,1,tot) & num\$
n2a\$=a\$
end def

def set_col(col\$)
if col\$="r" or col\$="R" then
draw color 1,0,0 ! fill color 1,0,0
end if
if col\$="g" or col\$="G" then
draw color 0,1,0 ! fill color 0,1,0
end if
if col\$="b" or col\$="B" then
draw color 0,0,1 ! fill color 0,0,1
end if
if col\$="n" or col\$="N" then
draw color 0,0,0 ! fill color .8,.8,.8
end if
enddef