#!/usr/bin/perl -W

# used to make C-array of charset (also rotate chars 90 deg).

if (@ARGV != 2) {
    print "usage:\n";
    print "\t make_charset.pl in-lcdcc-file out-commands\n\n";
    exit;
}

open(GLOBAL, "<$ARGV[0]") or die "can't open $ARGV[0]";
open(OF, ">$ARGV[1]");

print OF '#include <inttypes.h>' . "\n" . '#include <avr/pgmspace.h>' 
	. "\nuint8_t PROGMEM charset_r[] = {\n";

while($l = <GLOBAL>) {
    chomp $l;

    if ($l =~ /^\s*($|\/\/)/) {  # skip lines consisting of comments only
	next;
    }
    
    if ($l =~ /(\w)\s+(.+)/) {
	@values = split /\,/, $2;
	# $table_width=@values;
	$char_name = $1;
	
	# we have to rotate the characters by 90 degrees:
	for ($mod=16; $mod > 0.9; $mod /= 2){	# every column
		my $bitmap = 0;
		my $line;
		# print "mod=$mod, tab width=$table_width\n";
		for($line = 0; $line < @values; $line ++) {
			$bitmap += 2**(7-$line) if((hex($values[$line]) & $mod) > 0);
		}
		printf(OF "0x%02X, ", $bitmap );
	} # for mod
	print OF "// $char_name\n";

	} # if
} # while

close GLOBAL;

print OF "0 };\n"; 
close OF;
