remove empty lines at beginng/end and draw last line/cut mark
[linux-gpio-pinout] / gpio.pl
diff --git a/gpio.pl b/gpio.pl
index 4491529..d9fffeb 100755 (executable)
--- a/gpio.pl
+++ b/gpio.pl
@@ -7,15 +7,17 @@ use Getopt::Long;
 
 my $opt_svg = $ENV{SVG} || 0;
 my $opt_alt = $ENV{ALT} || 0;
-my $opt_invert = $ENV{INVERT} = 0;
-my $opt_vertical = $ENV{VERTICAL} = 0;
-my $opt_kernel = $ENV{kernel} = 1;
+my $opt_invert = $ENV{INVERT} || 0;
+my $opt_vertical = $ENV{VERTICAL} || 0;
+my $opt_zebra = $ENV{ZEBRA} || 0;
+my $opt_lines = $ENV{LINES} || 0;
 GetOptions(
        'svg!' => \$opt_svg,
        'alt!' => \$opt_alt,
        'invert!' => \$opt_invert,
        'vertical!' => \$opt_vertical,
-       'kernel!' => \$opt_kernel,
+       'zebra!' => \$opt_zebra,
+       'lines!' => \$opt_lines,
 );
 
 # svg font hints
@@ -47,8 +49,6 @@ while(<DATA>) {
                } else {
                        $include = 0;
                }
-       } elsif ( m/^#\s+/ ) {
-               $include = 1;
        } elsif ( $include ) {
                push @{ $pins->{$1} }, $line_i while ( m/\t(\w+\d+)/g );
 
@@ -60,29 +60,33 @@ while(<DATA>) {
        }
 }
 
+shift(@lines) while ( ! $lines[0] );   # remove empty at beginning
+pop(@lines) while ( ! $lines[-1] );    # remove empty at end
+
 die "add pin definition for # $model" unless $pins;
 
 warn "# pins ",dump($pins);
 
 my $pin_function;
+my $device;
 
 open(my $fh, '<', '/sys/kernel/debug/pinctrl/pinctrl-handles');
 while(<$fh>) {
        chomp;
-       if ( m/group: (\w+\d+)\s.+function: (\S+)/ ) {
+       if ( m/device: [0-9a-f]+\.(\w+)/ ) {
+               $device = $1;
+       } elsif ( m/group: (\w+\d+)\s.+function: (\S+)/ ) {
                my ($pin, $function) = ($1,$2);
-               $pin_function->{$pin} = $function;
-
-               next unless $opt_kernel;
+               $pin_function->{$pin} = "$device $function";
 
                if ( $pins->{$pin} ) {
                        foreach my $line ( @{$pins->{$pin}} ) {
 warn "XXX $pin $line";
                                my $t = $lines[$line];
                                if ( $opt_svg ) {
-                                       $t =~ s/$pin/[$function]/;
+                                       $t =~ s/$pin/[$device $function]/;
                                } else {
-                                       $t =~ s/$pin/$pin [$function]/ || die "can't find $pin in [$t]";
+                                       $t =~ s/$pin/$pin [$device $function]/ || die "can't find $pin in [$t]";
                                }
                                $lines[$line] = $t;
                                warn "# $line: $lines[$line]\n";
@@ -102,11 +106,15 @@ foreach my $line (@lines) {
                push @line_parts, [ $line ] unless $opt_svg;
                next;
        }
-       $line =~ s/(\[uart\d)(\]\s[^\t]*(rx|tx))/$1 $3$2/gi;
-       $line =~ s/(\[i2c\d)(\]\s[^\t]*(sck|sda))/$1 $3$2/gi;
-       $line =~ s/(\[spi\d)(\]\s[^\t]*(miso|mosi|clk|cs))/$1 $3$2/gi;
+       $line =~ s/(\[(?:uart|serial))([^\t]*\]\s[^\t]*(rx|tx)d?)/$1 $3$2/gi;
+       $line =~ s/(\[i2c)([^\t]*\]\s[^\t]*(scl?k?|sda))/$1 $3$2/gi;
+       $line =~ s/(\[spi)([^\t]*\]\s[^\t]*(miso|mosi|s?clk|c[se]\d*))/$1 $3$2/gi;
        $line =~ s/\s*\([^\)]+\)//g if ! $opt_alt;
 
+       # shorten duplicate kernel device/function
+       $line =~ s/\[serial (\w+) (uart\d+)\]/[$2 $1]/g;
+       $line =~ s/\[(\w+) (\w+) \1(\d+)\]/[$1$3 $2]/g;
+
        my @v = split(/\s*\t+\s*/,$line,4);
        push @line_parts, [ @v ];
        foreach my $i ( 0 .. 3 ) {
@@ -158,7 +166,7 @@ my $cols = {        # foreground background
        vcc  => [ '#ff0000', '#ffff00' ],
        gnd  => [ '#000000', '#00ffff' ],
        i2c  => [ '#008800', '#ffcccc' ],
-       uart => [ '#000088', '#ccffcc' ],
+       serial=>[ '#000088', '#ccffcc' ],
        spi  => [ '#880000', '#ccccff' ],
 };
 
@@ -178,7 +186,7 @@ sub svg_style {
 
        sub rect {
                my ($x,$y,$col,$fill) = @_;
-               print qq{<rect x="$x" y="$y" height="2.54" width="}, $max_len[$col] * $font_w, qq{" style="opacity:1;fill:$fill;stroke:#ffffff;stroke-width:0.10" />\n};
+               print qq{<rect x="$x" y="$y" height="2.54" width="}, $max_len[$col] * $font_w, qq{" style="fill:$fill;stroke:#ffffff;stroke-width:0.10" />\n};
 
        }
 
@@ -200,20 +208,20 @@ sub svg_style {
                return qq{ style="fill:$bg"};
        }
 
-       if ( $name =~ m/(VCC|3V3|3.3V)/i ) {
+       if ( $name =~ m/(VCC|3V3|3.3V|5v)/i ) {
                my ($fg,$bg) = @{ $cols->{vcc} };
                rect $x,$y,$col,$bg;
                return qq{ style="fill:$fg"};
-       } elsif ( $name =~ m/(G(ND|Round)|VSS)/i ) {
+       } elsif ( $name =~ m/(G(ND|Round)|VSS|0v)/i ) {
                my ($fg,$bg) = @{ $cols->{gnd} };
                rect $x,$y,$col,$bg;
                return qq{ style="fill:$fg"};
-       } elsif ( $name =~ m/\[(\w+)\d/ ) { # kernel
+       } elsif ( $name =~ m/\[(\w+)/ ) { # kernel
                my $dev = $1;
-               if ( my ($fg,$bg) = @{ $cols->{$dev} } ) {
-                       rect $x,$y,$col,$bg;
-                       return qq{ style="fill:$fg"};
-               }
+               my ($fg,$bg) = @{ $cols->{txt} };
+               ($fg,$bg) = @{ $cols->{$dev} } if exists $cols->{$dev};
+               rect $x,$y,$col,$bg;
+               return qq{ style="fill:$fg"};
        } else {
                my ( $fg, $bg ) = @{ $cols->{txt} };
                rect $x,$y,$col,$bg;
@@ -239,14 +247,20 @@ my @cut_marks;
 sub cut_mark {
        my ($x,$y) = @_;
        return unless $opt_svg;
-       push @cut_marks, printf($line_fmt, $x-5, $y-$font_b,   $x+5, $y-$font_b);
-       push @cut_marks, printf($line_fmt, $x,   $y-$font_b-5, $x,   $y-$font_b+5);
+       push @cut_marks, sprintf($line_fmt, $x-5, $y-$font_b,   $x+5, $y-$font_b);
+       push @cut_marks, sprintf($line_fmt, $x,   $y-$font_b-5, $x,   $y-$font_b+5);
 }
 cut_mark $x, $y;
 my $max_x = $x;
 $max_x += $max_len[$_] * $font_w foreach ( 0 .. 3 );
 cut_mark $max_x, $y;
 
+sub line {
+       my ($x,$y,$max_x) = @_;
+       push @cut_marks, sprintf($line_fmt, $x, $y-$font_b, $max_x, $y-$font_b);
+}
+
+
 my $last_cut_mark = 0;
 
 foreach my $i ( 0 .. $#line_parts ) {
@@ -261,6 +275,7 @@ foreach my $i ( 0 .. $#line_parts ) {
                                cut_mark $x, $y;
                                cut_mark $max_x, $y;
                                $last_cut_mark = 1;
+                               line $x, $y, $max_x if $opt_lines;
                                $y += 15; # make spacing between pinouts
                        }
                } elsif ( $last_cut_mark ) {
@@ -269,11 +284,13 @@ foreach my $i ( 0 .. $#line_parts ) {
                        cut_mark $max_x, $y;
                        $last_cut_mark = 0;
                } else {
-                       warn "CUTMARK no magic";
+                       #warn "CUTMARK no magic";
                }
 
+               line $x, $y, $max_x if $opt_lines && exists $line->[1];
+
                my ($fg,$bg) = @{ $cols->{txt} };
-               my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill-opacity:1;fill:$fg;stroke:none;">\n};
+               my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill:$fg;stroke:none;">\n};
 
                my $x_pos = $x;
                foreach my $i ( @cols_order ) {
@@ -288,8 +305,10 @@ foreach my $i ( 0 .. $#line_parts ) {
                push @later,sprintf $tspan, @$line;
                $y += 2.54;
 
-               # swap pin colors for line stripes
-               swap_cols $_ foreach qw( pins txt );
+               # swap pin colors for line stripe
+               if ( $opt_zebra ) {
+                       swap_cols $_ foreach qw( pins txt );
+               }
 
        } else {
 
@@ -304,26 +323,26 @@ foreach my $i ( 0 .. $#line_parts ) {
 }
 
 if ( $opt_svg ) {
+       cut_mark $x,$y;
+       cut_mark $max_x,$y;
+       line $x, $y, $max_x if $opt_lines;
+
        print qq{
     <text
        id="text4506"
        y="$x"
        x="$y"
-       style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';fill-opacity:1;stroke:none;stroke-opacity:1"
+       style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';stroke:none"
        xml:space="preserve">
 
        }; #svg
 
-       print @later, @cut_marks, qq{
-</text>
-</g>
-</svg>
-       }; #svg
+       print @later, qq{</text>\n}, @cut_marks, qq{</g>\n</svg>};
 
 }
 
 __DATA__
-# Cubietech Cubieboard2
+# Cubietech Cubieboard
 ## U14 (Next to SATA connector)
 ###    SPI0
 48     PI13 (SPI0-MISO/UART6-RX/EINT25)        47      PI11 (SPI0-CLK/UART5-RX/EINT23)
@@ -352,7 +371,6 @@ __DATA__
 4      PD1 (LCDD1/LVDS0N0)                     3       PD2 (LCDD2/LVDS0P1)
 2      Ground                                  1       PD0 (LCDD0/LVDSP0)
 
-# Cubietech Cubieboard2
 ## U15 (Between Ethernet port and USB ports)
 ### CSI1/TS
 1      VCC-5V                                  2       PH15 (CSI1-PWR/EINT15)
@@ -436,3 +454,4 @@ __DATA__
 35     gpio19 (GPIO.24)        36      gpio16  (GPIO.27)
 37     gpio26 (GPIO.25)        38      gpio20  (GPIO.28)
 39     0v                      40      gpio21  (GPIO.29)
+