Page 1 of 1

Meter object

Posted: Wed May 08, 2013 4:18 pm
by Henko
option angle degrees
graphics
fill color .8,.8,.8
graphics clear .8,.8,.8
rad=150
rmeter("km/hr","R",200,200,rad,0,200,0,1)
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("km/hr","R",200,200,rad,0,200,speed,0)
  rmeter("RPM","b",500,200,130,10,60,rpm,0)
  pause .1
  next t
rmeter("km/hr","R",200,200,rad,0,180,0,-1)
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
'
def rmeter(tit$,col$,xc,yc,rad,minval,maxval,val,mode)
if mode=-1 then
  fill circle xc,yc size rad+2
  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 arc xc,yc,rad-50,130,410
  draw size 3
  for a=130 to 410 step 28
    xs=xc+(rad-50)*cos(a) ! ys=yc+(rad-50)*sin(a)
    xe=xc+(rad-42)*cos(a) ! ye=yc+(rad-42)*sin(a)
    draw line xs,ys to xe,ye
    next a
  draw size 1
  for a=130 to 410 step 7
    xs=xc+(rad-50)*cos(a) ! ys=yc+(rad-50)*sin(a)
    xe=xc+(rad-44)*cos(a) ! ye=yc+(rad-44)*sin(a)
    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
    xs=xc+(rad-30)*cos(a)-18 ! ys=yc+(rad-30)*sin(a)-10
    draw text n2a$(sval,3,0) at xs,ys
    sval=sval+delta
    next a
  draw font size 20
mode_0:
  fill circle xc,yc size rad-52
  xs=xc+(rad-80)*cos(112) ! ys=yc+(rad-80)*sin(112)
  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)
  xe=xc+(rad-54)*cos(beta) ! ye=yc+(rad-54)*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