Your shopping cart is empty!

LEDBorg - Examples - Colour Selector

Posted by in Build, LEDBorg - Build on October 17, 2017 . 0 Comments.

This example uses the standard GTK colour selection dialog to provide a user-friendly control for selecting LedBorg colours.
The colour of the LedBorg changes as you change the selection on the dialog, as a bonus most of the GUI code is done for us in this example ^_^

You can download the code directly to your Raspberry Pi using:
cd ~/ledborg
wget -O ColourSelector.py https://piborg.org/downloads/ColourSelector.py.txt
Or save the text file from the link on your Raspberry Pi as ColourSelector.py

Make the script executable using
chmod +x ColourSelector.py
ensure you have pygtk installed using
sudo apt-get -y install python-gtk2
and run using
gksudo ./ColourSelector.py

You will need to run this script in a graphical environment, otherwise you will get an error message.

Full code listing

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

# Import library functions we need
import wiringpi2 as wiringpi
import pygtk
import gtk

# Setup software PWMs on the GPIO pins
LED_MAX = 100
wiringpi.softPwmCreate(PIN_RED,   0, LED_MAX)
wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX)
wiringpi.softPwmCreate(PIN_BLUE,  0, LED_MAX)
wiringpi.softPwmWrite(PIN_RED,   0)
wiringpi.softPwmWrite(PIN_GREEN, 0)
wiringpi.softPwmWrite(PIN_BLUE,  0)

# A function to set the LedBorg colours
def SetLedBorg(red, green, blue):
    wiringpi.softPwmWrite(PIN_RED,   int(red   * LED_MAX))
    wiringpi.softPwmWrite(PIN_GREEN, int(green * LED_MAX))
    wiringpi.softPwmWrite(PIN_BLUE,  int(blue  * LED_MAX))

# A function to turn the LedBorg off
def LedBorgOff():
    SetLedBorg(0, 0, 0)
# A function to handle the colour selection dialog change event
def ColourChangedEvent(widget):
    global colourSelectionDialog

    # Get current colour
    colour = colourSelectionDialog.colorsel.get_current_color()

    # Read the red, green, and blue levels to use
    red = colour.red_float
    green = colour.green_float
    blue = colour.blue_float

    # Set the LedBorg colours
    SetLedBorg(red, green, blue)

# Create colour selection dialog
global colourSelectionDialog
colourSelectionDialog = gtk.ColorSelectionDialog("Select LedBorg colour")

# Get the ColorSelection widget
colourSelection = colourSelectionDialog.colorsel

# Connect to the "color_changed" signal to our function
colourSelection.connect("color_changed", ColourChangedEvent)

# Show the dialog
response = colourSelectionDialog.run()

# Turn LedBorg off after we are finished
Last update: October 27, 2017


Leave a Comment

Leave a Reply

* Name:
* E-mail: (Not Published)
   Website: (Site url with http://)
* Comment:
Verification code: