SwissArmyLed - Command your LedBorg in many different ways

This is an old driver based LedBorg script
We recommend using the new driver free based scripts for LedBorg.
The new driver free examples can be found here, the installation can be found here.

Having an LedBorg can be fun, but sometimes remembering those colour codes is hard

100 200 211 110 220 221 000 111 222
010 020 121 011 022 122 120 021 210
001 002 112 101 202 212 012 102 201

It would be great if you could use something more readable, like LedBorg use blue
Well we now have something for that exact purpose, introducing the SwissArmyLed!
SwissArmyLed.pl is a Perl script which allows you to use LedBorg colour codes, colour names, red/green/blue triples, and even HTML colour codes:
Option Syntax Example
Colour name Name Azure
List usable colour names Names names
LedBorg colour code RGB 012
HTML colour code (6 digit) hRRGGBB h0079FF
HTML colour code (3 digit) hRGB h07F
RGB triple (max 255) R G B or R,G,B 0 121 255 or 000,121,255

It works by taking the value given and trying to match the text to a given name or the patterns above (for example an 'h' followed by 6 hexadecimal digits)
The script is in Perl which can be more daunting then Python to a beginner, it also uses regular expressions to match the string types.

Here's the code, you can download the SwissArmyLed script file as text here
Save the text file on your pi as SwissArmyLed.pl
Make the script executable using
chmod +x SwissArmyLed.pl
and run using
./SwissArmyLed.pl colour command

#!/usr/bin/env perl
use strict;
use warnings;

# Load library functions we want
use feature "switch";

# Named colours (based on http://piborg.org/ledborg/colours#comment-180)
my %colourMap = (
    MAROON          => '100',
    RED             => '200',
    PINK            => '211',
    OLIVE           => '110',
    YELLOW          => '220',
    LTYELLOW        => '221',
    'LIGHT YELLOW'  => '221',
    GREEN           => '010',
    LIME            => '020',
    'LIME GREEN'    => '020',
    LTGREEN         => '121',
    'LIGHT GREEN'   => '121',
    TEAL            => '011',
    CYAN            => '022',
    LTCYAN          => '122',
    'LIGHT CYAN'    => '122',
    NAVY            => '001',
    BLUE            => '002',
    LTBLUE          => '112',
    'LIGHT BLUE'    => '112',
    PURPLE          => '101',
    MAGENTA         => '202',
    FUCHSIA         => '212',
    'FUCHSIA PINK'  => '212',
    BLACK           => '000',
    OFF             => '000',
    GREY            => '111',
    HALF            => '111',
    WHITE           => '222',
    FULL            => '222',
    AZURE           => '012',
    VIOLET          => '102',
    'BLUE PURPLE'   => '102',
    'PURPLE BLUE'   => '102',
    BTPINK          => '201',
    'BRIGHT PINK'   => '201',
    'HOT PINK'      => '201',
    'RED PURPLE'    => '201',
    'PURPLE RED'    => '201',
    CHARTREUSE      => '120',
    TURQUOISE       => '021',
    'GUPPIE GREEN'  => '021',
    'SEA GREEN'     => '021',
    ORANGE          => '210'
);

# Variables for the program
my $command;
my $ledborg;
my $colour; 
my $red; 
my $green; 
my $blue; 

# Analyse the users command
$command = uc "@ARGV";                      # Take the list of all parameters and make it upper case
if (exists $colourMap{$command}) {          # If we recognise the command as a colour name then ...
    $colour = $colourMap{$command};             # Get the LedBorg colour to use from the name
} else {                                    # Otherwise ...
    # Match the command to a pattern (see https://en.wikipedia.org/wiki/Regular_expression to understand how the pattern matching works)
    given ($command) {
        when (/^[0-2][0-2][0-2]$/)  {
            # Actual colour, 3 numbers 0-2 (e.g. 211)
            $colour = $command;
        }
        when (/^H([0-9A-F])([0-9A-F])([0-9A-F])$/) {
            # HTML colour code in the form #NNN (e.g. #F77)
            # extract the 3 values as numeric (0 to 15)
            $red = hex $1;
            $green = hex $2;
            $blue = hex $3;
            # Work out red based on bottom 3rd, middle 3rd and top 3rd
            if ($red < 5) {
                $red = 0;
            } elsif ($red < 11) {
                $red = 1;
            } else {
                $red = 2;
            }
            # Work out green based on bottom 3rd, middle 3rd and top 3rd
            if ($green < 5) {
                $green = 0;
            } elsif ($green < 11) {
                $green = 1;
            } else {
                $green = 2;
            }
            # Work out blue based on bottom 3rd, middle 3rd and top 3rd
            if ($blue < 5) {
                $blue = 0;
            } elsif ($blue < 11) {
                $blue = 1;
            } else {
                $blue = 2;
            }
            # Combine to a single colour 
            $colour = "$red$green$blue";
        }
        when (/^H([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/) {
            # HTML colour code in the form #NNNNNN (e.g. #FF7979)
            # extract the 3 values as numeric (0 to 255)
            $red = hex $1;
            $green = hex $2;
            $blue = hex $3;
            # Work out red based on bottom 3rd, middle 3rd and top 3rd
            if ($red < 85) {
                $red = 0;
            } elsif ($red < 171) {
                $red = 1;
            } else {
                $red = 2;
            }
            # Work out green based on bottom 3rd, middle 3rd and top 3rd
            if ($green < 85) {
                $green = 0;
            } elsif ($green < 171) {
                $green = 1;
            } else {
                $green = 2;
            }
            # Work out blue based on bottom 3rd, middle 3rd and top 3rd
            if ($blue < 85) {
                $blue = 0;
            } elsif ($blue < 171) {
                $blue = 1;
            } else {
                $blue = 2;
            }
            # Combine to a single colour 
            $colour = "$red$green$blue";
        }
        when (/^(\d+)[\s,]\s*(\d+)[\s,]\s*(\d+)$/) {
            # RGB triple in the form r g b or r,g,b or r, g, b (e.g. 255 121 121)
            # Work out red based on bottom 3rd, middle 3rd and top 3rd
            if ($1 < 85) {
                $red = 0;
            } elsif ($1 < 171) {
                $red = 1;
            } else {
                $red = 2;
            }
            # Work out green based on bottom 3rd, middle 3rd and top 3rd
            if ($2 < 85) {
                $green = 0;
            } elsif ($2 < 171) {
                $green = 1;
            } else {
                $green = 2;
            }
            # Work out blue based on bottom 3rd, middle 3rd and top 3rd
            if ($3 < 85) {
                $blue = 0;
            } elsif ($3 < 171) {
                $blue = 1;
            } else {
                $blue = 2;
            }
            # Combine to a single colour 
            $colour = "$red$green$blue";
        }
        when (/NAMES/) {
            # User has asked for the list of colour names
            print "Available colour names\n\n";
            printf "   Name                 | Borg Code |  R   G   B  |  HTML   \n";
            printf "  ----------------------+-----------+-------------+---------\n";
            # Loop over each key, displaying the names against colour values
            foreach (sort keys %colourMap) {
                my $borgColour;
                # Read the colour code, and split into red, green and blue
                $borgColour = $colourMap{$_};
                $red = substr $borgColour, 0, 1;
                $green = substr $borgColour, 1, 1;
                $blue = substr $borgColour, 2, 1;
                # Form into 0-255 values
                if ($red == 2) {
                    $red = 255;
                } elsif ($red == 1) {
                    $red = 127;
                }
                if ($green == 2) {
                    $green = 255;
                } elsif ($green == 1) {
                    $green = 127;
                }
                if ($blue == 2) {
                    $blue = 255;
                } elsif ($blue == 1) {
                    $blue = 127;
                }
                # Form grid entry
                printf "   %-20s |    %s    | %03d %03d %03d | h%02X%02X%02X\n",
                    $_, $borgColour, $red, $green, $blue, $red, $green, $blue;
            }
            $colour = "SKIP";
        }
        default {
            # No option matched, print help
            print "Unknown colour value '@ARGV'\n\n";
            print "Colour options are:\n";
            print "Colour name              e.g. Azure\n";
            print "List usable colour names use  names\n";
            print "LedBorg colour code      e.g. 012\n";
            print "HTML colour code         e.g. h0079FF or h07F\n";
            print "RGB triple (max 255)     e.g. 0 121 255 or 000,121,255 or 0, 121, 255\n";
            $colour = "SKIP";
        }
    }
}

# Set the colour determined, ignore if set to SKIP
if ($colour ne "SKIP") {
    open($ledborg, ">", "/dev/ledborg");    # Open the LedBorg device for writing to
    print $ledborg $colour;                 # Write the colour string to the LedBorg device
    close $ledborg;                         # Close the LedBorg device
}
Subscribe to Comments for &quot;SwissArmyLed - Command your LedBorg in many different ways&quot;