SwissArmyPy - Command your LedBorg in many different ways using Python

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.

SwissArmyLed was a great script for controlling your LedBorg in an easier way, but what if you want to learn it's secrets?
Well Perl can be a daunting language for a beginner, so we now have a more learner friendly example in Python, SwissArmyPy!

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

Like the SwissArmyLed it was based on, SwissArmyPy can take in easier to read commands such as blue or off
It 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)
This version of the script is in Python, which should be better suited to a beginner, it does however still use regular expressions to match the string types.
If you want to learn about regular expressions, the interactive guide here is a good place to start, but be warned that regular expressions are an advanced topic!

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

#!/usr/bin/env python
# coding: latin-1

# Import library functions we need
import sys
import re

# Named colours (based on http://piborg.org/ledborg/colours#comment-180)
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'
 }

# Make a function to set the LedBorg colour
def SetColour(colour):
    LedBorg=open('/dev/ledborg','w')
    LedBorg.write(colour)
    LedBorg.close()

# Analyse the users command
command = ''
for arg in sys.argv[1:]:                    # The [1:] strips the first value, our own name
    command += arg + ' '
command = command.strip()                   # Remove trailing space
command = command.upper()                   # Make the command upper case
if colourMap.has_key(command):              # If we recognise the command as a colour name then ...
    colour = colourMap[command]
else:
    # Match the command to a pattern (see https://en.wikipedia.org/wiki/Regular_expression to understand how the pattern matching works)
    if re.match('[0-2][0-2][0-2]$', command):
        # Actual colour, 3 numbers 0-2 (e.g. 211)
        colour = command
    elif re.match('H([0-9A-F])([0-9A-F])([0-9A-F])$', command):
        # HTML colour code in the form #NNN (e.g. #F77)
        # extract the 3 values as numeric (0 to 15)
        red = int(command[1], 16)
        green = int(command[2], 16)
        blue = int(command[3], 16)
        # Work out red based on bottom 3rd, middle 3rd and top 3rd
        if red < 5:
            red = '0'
        elif red < 11:
            red = '1'
        else:
            red = '2'
        # Work out green based on bottom 3rd, middle 3rd and top 3rd
        if green < 5:
            green = '0'
        elif green < 11:
            green = '1'
        else:
            green = '2'
        # Work out blue based on bottom 3rd, middle 3rd and top 3rd
        if blue < 5:
            blue = '0'
        elif blue < 11:
            blue = '1'
        else:
            blue = '2'
        # Combine to a single colour 
        colour = red + green + blue
    elif re.match('H([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$', command):
        # HTML colour code in the form #NNNNNN (e.g. #FF7979)
        # extract the 3 values as numeric (0 to 255)
        red = int(command[1:3], 16)
        green = int(command[3:5], 16)
        blue = int(command[5:7], 16)
        # Work out red based on bottom 3rd, middle 3rd and top 3rd
        if red < 85:
            red = '0'
        elif red < 171:
            red = '1'
        else:
            red = '2'
        # Work out green based on bottom 3rd, middle 3rd and top 3rd
        if green < 85:
            green = '0'
        elif green < 171:
            green = '1'
        else:
            green = '2'
        # Work out blue based on bottom 3rd, middle 3rd and top 3rd
        if blue < 85:
            blue = '0'
        elif blue < 171:
            blue = '1'
        else:
            blue = '2'
        # Combine to a single colour 
        colour = red + green + blue
    elif re.match('(\d+)[\s,]\s*(\d+)[\s,]\s*(\d+)$', command):
        # 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
        # The next line takes the command, converts any whitespace that may start with a comma into just a comma,
        #  then splits it by those commas (which should be 3 parts)
        [red, green, blue] = re.sub('[\s,]\s*', ',', command.strip()).split(',')
        red = int(red)
        green = int(green)
        blue = int(blue)
        if red < 85:
            red = '0'
        elif red < 171:
            red = '1'
        else:
            red = '2'
        # Work out green based on bottom 3rd, middle 3rd and top 3rd
        if green < 85:
            green = '0'
        elif green < 171:
            green = '1'
        else:
            green = '2'
        # Work out blue based on bottom 3rd, middle 3rd and top 3rd
        if blue < 85:
            blue = '0'
        elif blue < 171:
            blue = '1'
        else:
            blue = '2'
        # Combine to a single colour 
        colour = red + green + blue
    elif command == 'NAMES':
        # User has asked for the list of colour names
        print 'Available colour names\n'
        print '   Name                 | Borg Code |  R   G   B  |  HTML   '
        print '  ----------------------+-----------+-------------+---------'
        # Loop over each key, displaying the names against colour values
        keys = colourMap.keys()
        keys.sort()
        for key in keys:
            # Read the colour code, and split into red, green and blue
            borgColour = colourMap[key]
            red = borgColour[0]
            green = borgColour[1]
            blue = borgColour[2]
            # Form into 0-255 values
            if red == '2':
                red = 255
            elif red == '1':
                red = 127
            else:
                red = 0
            if green == '2':
                green = 255
            elif green == '1':
                green = 127
            else:
                green = 0
            if blue == '2':
                blue = 255
            elif blue == '1':
                blue = 127
            else:
                blue = 0
            # Form grid entry
            print '   %-20s |    %s    | %03d %03d %03d | h%02X%02X%02X' % (
                key, borgColour, red, green, blue, red, green, blue)
        colour = 'SKIP'
    else:
        # No option matched, print help
        print "Unknown colour value '%s'\n" % (command)
        print 'Colour options are:'
        print 'Colour name              e.g. Azure'
        print 'List usable colour names use  names'
        print 'LedBorg colour code      e.g. 012'
        print 'HTML colour code         e.g. h0079FF or h07F'
        print 'RGB triple (max 255)     e.g. 0 121 255 or 000,121,255 or 0, 121, 255'
        colour = 'SKIP'

if colour != 'SKIP':
    SetColour(colour)
Subscribe to Comments for &quot;SwissArmyPy - Command your LedBorg in many different ways using Python&quot;