Last active
December 16, 2015 00:19
-
-
Save atierant/5346791 to your computer and use it in GitHub Desktop.
LedBorg examples ported to PHP. All examples are ported from the Python orginals @ http://piborg.org/ledborg/examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* Script to make your LedBorg pick random colours at a regular interval | |
* | |
* PHP version 5 | |
* | |
* @category PiBorg | |
* @package PiBorg | |
* @author Arnaud TIERANT <at@synap.fr> | |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License | |
* @version SVN: $Id$ | |
* @link http://www.piborg.com/ledborg | |
*/ | |
// Setup parameters | |
$cheerlightsUrl = 'http://api.thingspeak.com/channels/1417/field/1/last.txt'; | |
$colourMap = [ | |
"red" => "200", | |
"green" => "020", | |
"blue" => "002", | |
"cyan" => "022", | |
"white" => "111", | |
"warmwhite" => "222", | |
"purple" => "102", | |
"magenta" => "202", | |
"yellow" => "220", | |
"orange" => "210", | |
]; | |
// | |
/** | |
* Function who gets the Cheerlights mood | |
* | |
* @param string &$string The url to follow to get the web value | |
* | |
* @return string $buffer | |
* | |
*/ | |
function getURLdata(&$string) | |
{ | |
return file_get_contents($string); | |
} | |
// Loop indefinitely | |
while (true) { | |
// Read the last cheerlights colour via its URL | |
$colourName = getURLdata($cheerlightsUrl); | |
// If we recognise this colour name then ... | |
if (isset($colourMap[$colourName])) { | |
// Get the LedBorg colour to use from the name | |
$colour = $colourMap[$colourName]; | |
// Otherwise ... | |
} else { | |
// Display the name we did not recognise | |
print "Unexpected colour '$colourName'\n"; | |
// Use the colour of black / off | |
$colour = "000"; | |
} | |
// Open the LedBorg device for writing to | |
$ledborg = fopen("/dev/ledborg", "w"); | |
// Write the colour string to the LedBorg device | |
fwrite($ledborg, $colour); | |
// Close the LedBorg device | |
fclose($ledborg); | |
// Reset the colour | |
unset($colour); | |
// Wait for 1 second | |
sleep(1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* Script to make your LedBorg pick random colours at a regular interval | |
* | |
* PHP version 5 | |
* | |
* @category PiBorg | |
* @package PiBorg | |
* @author Arnaud TIERANT <at@synap.fr> | |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License | |
* @version SVN: $Id$ | |
* @link http://www.piborg.com/ledborg | |
*/ | |
// Loop indefinitely | |
while (true) { | |
// Generate a value for red between 0 and 2 inclusive | |
$red = rand()&2; | |
// Generate a value for green between 0 and 2 inclusive | |
$green = rand()&2; | |
// Generate a value for blue between 0 and 2 inclusive | |
$blue = rand()&2; | |
// Create a text string of the form "RGB" from our random values | |
$colour = "$red$green$blue"; | |
// Open the LedBorg device for writing to | |
$ledborg = fopen("/dev/ledborg", "w"); | |
// Write the colour string to the LedBorg device | |
fwrite($ledborg, $colour); | |
// Close the LedBorg device | |
fclose($ledborg); | |
// Wait for 1 second | |
sleep(1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* Script to make your LedBorg pick random colours at a regular interval | |
* | |
* PHP version 5 | |
* | |
* @category PiBorg | |
* @package PiBorg | |
* @author Arnaud TIERANT <at@synap.fr> | |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License | |
* @version SVN: $Id$ | |
* @link http://www.piborg.com/ledborg | |
*/ | |
// Setup our sqeuence list | |
$colours = [ | |
"200", | |
"210", | |
"110", | |
"120", | |
"020", | |
"021", | |
"011", | |
"012", | |
"002", | |
"102", | |
"101", | |
"201", | |
]; | |
// Loop indefinitely | |
while (true) { | |
// For each colour in the list in order... | |
foreach ($colours as &$colour) { | |
// Open the LedBorg device for writing to | |
$ledborg = fopen("/dev/ledborg", "w"); | |
// Write the colour string to the LedBorg device | |
fwrite($ledborg, $colour); | |
// Close the LedBorg device | |
fclose($ledborg); | |
// Wait for 1 seconds | |
sleep(1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* More advanced script which provides a user menu and responds to key presses. | |
* | |
* Demonstrates both reading and writing with the device. | |
* | |
* PHP version 5 | |
* | |
* @category PiBorg | |
* @package PiBorg | |
* @author Arnaud TIERANT <at@synap.fr> | |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License | |
* @version SVN: $Id$ | |
* @link http://www.piborg.org/downloads/ledborg/random-colours.php.txt | |
*/ | |
// Display the options to the user as a menu | |
print "+-------------------------------+\n"; | |
print "| LedBorg |\n"; | |
print "+-------------------------------+\n"; | |
print "| |\n"; | |
print "| R Cycle red |\n"; | |
print "| G Cycle green |\n"; | |
print "| B Cycle blue |\n"; | |
print "| 0 Set all off |\n"; | |
print "| 1 Set all 50% |\n"; | |
print "| 2 Set all 100% |\n"; | |
print "| I Invert all channels |\n"; | |
print "| |\n"; | |
print "| Q Quit |\n"; | |
print "| |\n"; | |
print "+-------------------------------+\n"; | |
print "\n"; | |
/** | |
* Function to get a keypress without showing text on screen | |
* | |
* This function is a bit more complex then the rest of the | |
* example, understanding it is a more advanced topic | |
* | |
* @return string $c Pressed Key | |
* | |
*/ | |
function getKey() | |
{ | |
system("stty -icanon"); | |
while ($c = fread(STDIN, 1)) { | |
return $c; | |
} | |
} | |
// Loop indefinitely | |
while (true) { | |
// Define some variables | |
$red = 0; | |
$green = 0; | |
$blue = 0; | |
// Read the next keypress | |
$command = getKey(); | |
// Read the current colour from the LedBorg device | |
$ledborg = fopen("/dev/ledborg", "r"); | |
// Read the entire contents of the LedBorg file | |
// (should only be 3 characters long) | |
$oldColour = fgets($ledborg); | |
// Close the LedBorg device | |
fclose($ledborg); | |
// If oldColour matches 3 digits of 0, 1, or 2 (storing each matched digit)... | |
if ( preg_match('/^([0-2])([0-2])([0-2])$/', $oldColour, $matches) ) { | |
$red = $matches[1]; // Get the red setting from the colour string | |
$green = $matches[2]; // Get the green setting from the colour string | |
$blue = $matches[3]; // Get the blue setting from the colour string | |
} | |
// Convert the command to upper case (we do not care what case it is) | |
$command = strtoupper($command); | |
switch($command){ | |
case "R": // If the user pressed R | |
$red += 1; // Increase red by 1 | |
if ($red > 2) { // Set back to 0 if red is now above 100% | |
$red = 0; | |
} | |
break; | |
case "G": // If the user pressed G | |
$green += 1; // Increase green by 1 | |
if ($green > 2) { // Set back to 0 if green is now above 100% | |
$green = 0; | |
} | |
break; | |
case "B": // If the user pressed B | |
$blue += 1; // Increase blue by 1 | |
if ($blue > 2) { // Set back to 0 if blue is now above 100% | |
$blue = 0; | |
} | |
break; | |
case "1": // If the user pressed 1... | |
$red = 0; // Set red to off | |
$green = 0; // Set green to off | |
$blue = 0; // Set blue to off | |
break; | |
case "2": // If the user pressed 2... | |
$red = 1; // Set red to 50% | |
$green = 1; // Set green to 50% | |
$blue = 1; // Set blue to 50% | |
break; | |
case "3": // If the user pressed 3... | |
$red = 2; // Set red to 100% | |
$green = 2; // Set green to 100% | |
$blue = 2; // Set blue to 100% | |
break; | |
case "I": // If the user pressed I... | |
$red = 2 - $red; // Set red to its inverse | |
$green = 2 - $green; // Set green to its inverse | |
$blue = 2 - $blue; // Set blue to its inverse | |
break; | |
case "Q": // If the user pressed Q... | |
echo "\nBye ;)\n"; | |
exit(); // Jump out of the while loop | |
case "\3": // If the user pressed CTRL+C... | |
echo "\nBye ;)\n"; | |
exit(); // Jump out of the while loop | |
default; | |
echo "\nUnknown option '$command'\n"; | |
} | |
// Create a text string of the form "RGB" from our new values | |
$newColour = "$red$green$blue"; | |
// Open the LedBorg device for writing to | |
$ledborg = fopen("/dev/ledborg", "w"); | |
// Write the colour string to the LedBorg device | |
fwrite($ledborg, $newColour); | |
// Close the LedBorg device | |
fclose($ledborg); | |
// Reset the colour | |
unset($newColour); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment