//function tacho_example
clc;
clear();

CENTER = [0 0];

needle_length = 0.8
// needle = 0.8;  // use "needle" for creating varibale needle length

// define or read 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]; 
END   = max(size(speed));

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 = 8;
f.figure_size = [300,300];
f.figure_name = "tachometer"

// 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";


// 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

xarc(-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
for i=1:END;

//    needle_length = needle + speed(i)/max(speed)/10;      // possibility to create variable needle length

   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);

    xpause(50000); // define pause between two needle positions in microseconds

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


//endfunction;



