//THIS FUNCTION WILL CREATE A ANALOGUE DISPLAY OF THE INPUT DATA
//
//var1: 1xn vector             --> data to be displayd
//var2: integer                --> maximum value to be displayed
//var3: integer between 0-1    --> needle length
//var4: integer                --> needle thickness
//var5: integer                --> needle line style
//var6: [1x2] matrix           --> figure size
//var7: string                 --> figure name
//var8: [1x2] matrix           --> figure position
//var9: integer                --> figure color
//var10: integer               --> display color
//var11: integer               --> needle color
//var12: integer               --> the time pause [in milliseconds] after that the
//                                 needle will move to its next position

function tachometer(data, scale, needle_length, needle_thickness,needle_style, figsize, figname, figpos, color_fig, color_disp, color_needle,tstep)

CENTER = [0 0];
END   = max(size(data));

scale = 180;                // scale gives the maximum value for the tachometer 
start_pos = scale / 2;      // this is to have the needle start at left position

// create a new figure and delete all the menues and the toolbox                
f = figure(1);
delmenu(1,'Datei');
delmenu(1,'Zusatzprogramme');
delmenu(1,'Editieren');
delmenu(1,'?');
toolbar(1,'off') 
f.background = color_fig;
f.figure_size = figsize;
f.figure_name = figname;
f.figure_position = figpos;

// define a symmetric box, so that arc is a circle
rect=[-1,-1,1,1];
plot2d(0,0,0,rect=rect,frameflag=3,axesflag=5);     
a = gca()  
a.visible = "on"
a.box = "off"
a.data_bounds = [-1.2,0;1.2,1.2];       // play with these parameters to maybe see the full circle
a.axes_bounds = [0,0,1,1];
a.margins = [0,0,0,0];
a.axes_visible = ["off","off","off"];
a.x_location = "bottom";
a.axes_reverse = ["off","off","off"];   // play with this around, so you can change the orientation of the tachometer

// plot the arc
// NOTE: without the arc the design is more like a clock
//          --> than - by use of 3 needles - one could create a watch

xset("color",color_disp)
xfarc(-1,1,2,2,0,1000*scale);                           // draw the arc


// define the grid of the arc
GRID = linspace(1,scale,scale/10+1);
// plot the arc and define its properties
plot ([CENTER(1) CENTER(1)+sin((GRID-start_pos)*%pi/scale)],[CENTER(2) CENTER(2)+cos((GRID-start_pos)*%pi/scale)],'o');
a = gca();
a.axes_visible = ["off","off","off"];
e=gce();
p=e.children(1);

n = length (GRID);
// define GRID labels (datatips)
function str=myfmt(curve, pt, index)
    if i == 1 then
        str = msprintf('%d', 0);
    else
        str = msprintf('%d', GRID(i));
    end
endfunction  
datatipSetDisplay(p,myfmt) 
for i = 1:n
    if i == 1 then
        t = datatipCreate(p,i+1);
        datatipSetOrientation(t,"upper left");  // set to "lower right" otherwise
    else
        t = datatipCreate(p,i+1);
        datatipSetOrientation(t,"automatic");        
    end
end

// create the tachometer needle
xset("color",color_needle);
for i=1:END;

    xpoly([CENTER(1) CENTER(1)+sin((speed(i)-start_pos)*%pi/scale)*needle_length],[CENTER(2) CENTER(2)+cos((speed(i)-start_pos)*%pi/scale)*needle_length],'lines',0);
    e = gce()
    e.line_style = 1
    e.thickness = 3

    xpause(tstep*1000);  // xpause counts in microseconds, tstep is for milliseconds, thatswhy factor 1000

    if  i < END
        delete(gce());
    end
end


endfunction;


// define the data values
speed = [0 10 20 40 60 80 100 120 130 140 145 147 150 153 155 157 160 140 120 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 18 16 14 12 10 8 7 6 5 4.5 3 3.5 2 2.5 2 1.5 1 0]; 

tachometer(speed,240,0.8,3,1,[300;300],'tachometer',[200;300],1,1,7,50);

