My Account

Wish List (0)


The PiBorg store is closed and we are unable to ship orders.
You will not be able to checkout any orders at this time.

LEDBorg - Examples - Colour Selector

Written by in Build, LEDBorg - Build on .



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
wiringpi.wiringPiSetup()
import pygtk
pygtk.require('2.0')
import gtk

# Setup software PWMs on the GPIO pins
PIN_RED = 0
PIN_GREEN = 2
PIN_BLUE = 3
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
colourSelection.set_has_palette(True)

# 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
LedBorgOff()
Last update: Oct 27, 2017

Related Article

Related Products

Comments

Leave a Comment

Leave a Reply

The product is currently Out-of-Stock. Enter your email address below and we will notify you as soon as the product is available.