function uhr(start,ende,speed)

% input:  start  Startzeit in Sekunden
%         ende   Endzeit   in Sekunden
%         speed  Anzeige-Sek pro Zeit-Sek
%
% output: ----

    
%Eingaben kontrollieren
    switch nargin
      case 0
        start = 1; %1 nach Mitternacht
        ende  = 59+60*(59+60*(11)); %1 vor Mittag
        sek   = 60; %Laufzeit in Sekunden
        speed = 1/sek*(12*60*60); %Umrechnung Laufzeit - Geschwindigkeit
      case 1
        ende  = inf;
        speed = 1
      case 2
        speed = 1;
      case 3
      otherwise
        disp('Falsche Anzahl von Argumenten');       
    end
    
    %Initialisierungen
    handles  = [];
    handlest = [];
    
    [H,M,S] = init_window;
    zeit = start; tic;

    [PhiStart]=bewege_zeiger(H,M,S,zeit);
        
    while zeit<ende
        %analoger Durchlauf mit gewuenschter Geschwindigkeit
        zeit = min((start+speed*toc),ende);
        %Zeigerstellung plotten
        [Phi]=bewege_zeiger(H,M,S,zeit);
        %Balken plotten
        [handles,handlest]=balken([PhiStart;Phi],handles,handlest);
        %Auf Ausgabe warten
        pause(0.01);
    end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function Phi=bewege_zeiger(H,M,S,zeit)
    
    %Stundenzeiger
    Phi(1) = 2*pi/(12*60*60)*zeit; rH = 0.6;
    set(H,'XData',rH*[0 sin(Phi(1))]);
    set(H,'yData',rH*[0 cos(Phi(1))]);
    
    %Minutenzeiger
    Phi(2) = 2*pi/(60*60)*zeit; rM = 0.83;
    set(M,'XData',rM*[0 sin(Phi(2))]);
    set(M,'yData',rM*[0 cos(Phi(2))]);

    %Sekundenzeiger
    Phi(3) = 2*pi/60*zeit; rS = 0.83;     
    set(S,'XData',rS*[0 sin(Phi(3))]);
    set(S,'yData',rS*[0 cos(Phi(3))]);
     
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
function D=zaehle_zeiger(Phi)

%Differenz der Umlaeufe der Zeiger
    P=(Phi(:,[2,3,3])-Phi(:,[1,1,2]))/2/pi;
    %Zeigergleichstaende
    D=[-1 1]*floor( P );
    %Rechtwinkligkeit
    D=[D [-1 1]*(floor( P+1/4 ) + floor( P-1/4 ))];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [handles,handlest]=balken(Phi,handles,handlest)

    %Groesseneinstellungen
    xoffset  = 2;
    yoffset  = 1;
    gap      = 0.1;
    width    = 0.001;
    height   = (yoffset-2.5*gap)/3;
    fontsize = 18;
    fontweight= 'bold';
    
    %Umlaufzahlen holen
    data = zaehle_zeiger(Phi);
    
    %Text fuer Schaubild
    T={'H = M' 'H = S' 'M = S' 'H \perp M' 'H \perp S' 'M \perp S'};
    
    %Plot der Daten
    for ind=1:6
        %In String konvertieren
        datat{ind} = num2str(data(ind));   
        if length(handles)<6
            %Bisher wurden Balken und Text noch nicht geplottet
            y1=(ind-3.5)*gap+(ind-4)*height; y2=(ind-3.5)*gap+(ind-3)*height;
            handles(ind)  = fill(xoffset+width*[0 data(ind) data(ind) 0],...
                                [y1 y1 y2 y2],0.7*[1 1 1]);
            handlest(ind) = text(xoffset-0.05,(y1+y2)/2,datat{ind},...
                                 'HorizontalAlignment','right',...
                                 'FontSize',fontsize,...
                                 'FontWeight',fontweight);
            text(xoffset-0.6,(y1+y2)/2,[T{ind} ':'],...
                 'HorizontalAlignment','center',...
                 'FontSize',fontsize,...
                 'FontWeight',fontweight);
            %Refresh fuer Handles
            if ind==6
                pause(0.01);
            end
        else
            %Nur aktualisieren, nicht neu plotten
            set(handles(ind),'XData',...
                             xoffset+width*[0 data(ind) data(ind) 0]);
            set(handlest(ind),'String',datat{ind});
        end
    end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [H,M,S]=init_window
    
    %Fenster initialisieren
    figure('units','pixels','Position',[-3 317 1024 715],'Menubar','none');
    hold on;
    axis(1.2*[-1 3 -1 1])
    t=linspace(0,2*pi);
    axis equal off
    %Umgebendes Quadrat
    fill(1.3*[-1 1 1 -1],1.3*[-1 -1 1 1],0.8*[1 1 1],'LineStyle','none');
    %Rand der Uhr    
    r=1.05;
    plot(r*sin(t),r*cos(t),'k-','linewidth',8);
    %Mittelabdeckung plotten
    r=0.05;
    fill(r*cos(t),r*sin(t),'k');
    
    %Sekunden/Minutenraster plotten
    t1=linspace(0,2*pi,60+1);
    t2=linspace(0,2*pi,12+1);
    r1=0.95; r2=1.05;
    plot([r1*sin(t1); r2*sin(t1)],[r1*cos(t1); r2*cos(t1)],'k-','linewidth',3);
    r1=0.85; r2=1.05;
    plot([r1*sin(t2); r2*sin(t2)],[r1*cos(t2); r2*cos(t2)],'k-','linewidth',6);

    %Zeiger plotten
    H = plot([0 0],[0 0.6 ],'b-','linewidth',10);
    M = plot([0 0],[0 0.83],'k-','linewidth',6);
    S = plot([0 0],[0 0.83],'r-','linewidth',3);

    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

