use warnings;
use Tk;
use strict;

my $value;           # Wert fuer den Zeigerausschlag
my $MIN = 0;         # Minimalwert der Variablen $v
my $MAX = 100;       # Maximalwert der Variablen $v
my $PI = atan2(1,1) * 4;

# Das mw erzeugen
my $mw = MainWindow->new();

$mw->Label(-text => '    Tachometer    ')->pack();


my $canvas = $mw->Canvas(-width  => 200, 
                    -height => 110,
                    -background => 'white',
                    -bd     => 2,
                    -relief => 'sunken')->pack();
# Der 'Zeiger' ist eine Linie mit Pfeilspitze                    
my $zeiger = $canvas->createLine(100, 100, 10, 100, 
                    -arrow => 'last', -width => 3);

# noch eine kleine Skala drumrum
scale($canvas);

# Zum Einstellen des Tachowerts
my $s = $mw->Scale(-orient => 'horizontal',
                   -from   => 0,
                   -to     => 100,
                   -variable => \$value)->pack();

# Regelmaessig aktualisieren
$mw->repeat(100, [ \&update, $canvas, $zeiger ]);

MainLoop;

# zeichnet einen neuen Zeiger entsprechend $value
sub update 
  {
  my $c = shift;
  my $z = shift;
  my $pos = $value / abs($MAX - $MIN);
  my $x = 100.0 - 90.0 * (cos($pos * $PI));
  my $y = 100.0 - 90.0 * (sin($pos * $PI));
  $c->coords($z, 100, 100, $x, $y);
  }

# zeichnet eine Skala: Halbkreis mit 10er-Teilung
sub scale 
  {
  my $c = shift;
  $c->createArc(10,190,190,10, 
                 -extent => 180, 
                 -style => 'arc');
  for my $i (0..10)
    {
    my $pos = $i * 10 / abs($MAX - $MIN);
    my $x1 = 100.0 - 90.0 * (cos($pos * $PI));
    my $y1 = 100.0 - 90.0 * (sin($pos * $PI));
    my $x2 = 100.0 - 95.0 * (cos($pos * $PI));
    my $y2 = 100.0 - 95.0 * (sin($pos * $PI));
    $c->createLine($x1, $y1, $x2, $y2);
    }
  }
