#! /usr/bin/perl
#
# BlueBrightnessControl4 - brightness control module for 4 230V AC lamps
# version 0.6.2 date 2007-03-23
# Copyright (C) 2006 Stefan Schuermans <stefan@blinkenarea.org>
# a BlinkenArea project - http://www.blinkenarea.org/
#
# calculate the mapping from grayscale values to PWM values

use strict;

my $preheat = 0.0;
my $gamma = 1.4;

print <<EOF;
; BlueBrightnessControl4 - brightness control module for 4 230V AC lamps
; version 0.6.2 date 2007-03-23
; Copyright (C) 2006 Stefan Schuermans <stefan\@blinkenarea.org>
; a BlinkenArea project - http://www.blinkenarea.org/

; table for mapping grayscale values to PWM values
EOF
printf( ";  - preheat: %f\n", $preheat );
printf( ";  - gamma: %f\n", $gamma );
printf( "MAPPING_TABLE:\n" );

my ($last_gray, $last_pwm );
for( my $gray = 0; $gray < 128; $gray++ ) {

	my $x = $gray / 127;
	$x = $x * (1 - $preheat) + $preheat;
	$x = ($x ** $gamma);
	my $pwm = int( $x * 255 + 0.5 );

	if( $gray & 1 ) {
		printf( "        ; grayscale %d -> PWM %d, grayscale %d -> PWM %d\n", $last_gray, $last_pwm, $gray, $pwm );
		printf( "        .db     0x%02X, 0x%02X\n", $last_pwm, $pwm );
	} else {
		$last_gray = $gray;
		$last_pwm = $pwm;
	}
}
printf( "\n" );

