#!/usr/bin/perl -w
#
# Demonstration des physiologischen Dimmens mit
# R(n+1) = Rn + k * Rn
# Es wird nur Bitschieben und Addition verwendet.

use warnings;
use strict;

my $value;

print "Aufdimmen: Faktor k = 2\n";
$value = 1;
for ( 0 .. 8 ) {
   print "$value\n";
   $value = $value << 1;
};

print "\nAbdimmen: Faktor k = 0.5\n";
$value = 255;
for ( 0 .. 8 ) {
   print "$value\n";
   $value = $value >> 1;
};

print "\nAbdimmen: Faktor k = 0.75 = (1 - 1/4)\n";
$value = 2**16-1;
for ( 1 .. 24 ) {
   my $pwm_value = $value >> 6;
   print "$pwm_value\n";
   $value = $value - ($value >> 2);
};

print "\nAbdimmen: Faktor k = 0.875 = (1 - 1/8)\n";
$value = 2**16 - 1;
for ( 1 .. 52 ) {
   my $pwm_value = $value >> 6; 
   print "$pwm_value\n";
   $value = $value - ($value >> 3);
};

# Für das Aufdimmen wurden die Zahl der Stufen und der Anfangswert
# experimentell ermittelt.

print "\nAufdimmen: Faktor k = 1.125 = (1 + 1/8)\n";
$value = 116; # Experimentell ermittelt
for ( 0 .. 54 ) {
   my $pwm_value = $value >> 6;
   print "$pwm_value\n";
   $value = $value + ($value >> 3);
};

print "\nAufdimmen: Faktor k = 1.25 = (1 + 1/4)\n";
$value = 53; # Experimentell ermittelt
for ( 0 .. 32 ) {
   my $pwm_value = $value >> 6;
   print "$pwm_value\n";
   $value = $value + ($value >> 2);
};

