Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@onkeloki
Last active October 17, 2015 22:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onkeloki/6c72f3b21ba6fbe789ca to your computer and use it in GitHub Desktop.
Save onkeloki/6c72f3b21ba6fbe789ca to your computer and use it in GitHub Desktop.
control a LW-12 w-lan Led Module with php
<?php
/**
* Created by PhpStorm.
* User: marko.luft
* Date: 09.02.15
* Time: 10:28
*/
class LW12 {
public $fadeSteps = 10;
public $debug = false;
private $filename = "status.txt";
public $currentR = 0;
public $currentG = 0;
public $currentB = 0;
private $ip;
function __construct($ip) {
$this->ip = $ip;
$this->restore();
}
private function Min0max100($color) {
return Max(0, Min($color, 100));
}
/**
* save current color to hard drive
*/
private function store() {
$s = $this->currentR . "," . $this->currentG . "," . $this->currentB;
$myfile = fopen($this->filename, "w") or die("Unable to open file maybe the file " . $this->filename . " is not present or no premission!");
fwrite($myfile, $s);
fclose($myfile);
}
/**
* load the current colors from hard drive txt file
*/
private function restore() {
$myfile = fopen($this->filename, "r") or die("Unable to open file maybe the file " . $this->filename . " is not present or no premission!");
$x = fread($myfile, 30);
var_dump($x);
fclose($myfile);
$x = explode(",", $x);
@$this->currentR = $x[0];
@$this->currentG = $x[1];
@$this->currentB = $x[2];
}
/**
* $r - red
* $g - green
* $b - blue
* @return hex string inclunding # sign
*/
private function rgbtohex($r, $g, $b) {
$hex = "#";
$hex .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
return $hex;
}
/**
* only if debug is true
* uses current colors and draws ah html debug output
*/
private function debugRect() {
if (!$this->debug) return;
$r = $this->currentR * 2.55;
$g = $this->currentG * 2.55;
$b = $this->currentB * 2.55;
$hex = $this->rgbtohex($r, $g, $b);
echo "<div style=\"background-color:" . $hex . ";border:0px solid #000\">&nbsp;</div>";
}
/**
* $r - red 0-100
* $g - green 0-100
* $b - blue 0-100
*
*/
public function setColor($r, $g, $b) {
$this->currentR = round($this->Min0max100($r));
$this->currentG = round($this->Min0max100($g));
$this->currentB = round($this->Min0max100($b));
$sendR = round($this->currentR * 2.55);
$sendG = round($this->currentG * 2.55);
$sendB = round($this->currentB * 2.55);
fwrite(fsockopen("tcp://" . $this->ip . "", 5577), "\x56" . chr($sendR) . chr($sendG) . chr($sendB) . "\xaa");
$this->debugRect($sendR, $sendG, $sendB);
$this->store();
}
/**
* fades every color to 0
*/
public function off() {
$this->fadeTo(0, 0, 0);
}
/**
* $r - red 0-100
* $g - green 0-100
* $b - blue 0-100*
* fades to given color
*/
public function fadeTo($r, $g, $b) {
$stepsR = ($this->currentR - $r) / $this->fadeSteps;
$stepsG = ($this->currentG - $g) / $this->fadeSteps;
$stepsB = ($this->currentB - $b) / $this->fadeSteps;
//$this->setColor($r,$g,$b);
for ($i = 0; $i < $this->fadeSteps; $i++) {
$this->setColor($this->currentR - $stepsR, $this->currentG - $stepsG, $this->currentB - $stepsB);
}
}
}
<?php
include("LW12.php");
@$ip = $_GET["ip"];
@$r = $_GET["r"];
@$g = $_GET["g"];
@$b = $_GET["b"];
if (isset($_SERVER["argv"][1])) {
$ip = $_SERVER["argv"][1];
$r = $_SERVER["argv"][2];
$g = $_SERVER["argv"][3];
$b = $_SERVER["argv"][4];
$pretag = "";
}
$x = new LW12($ip);
$x->fadeTo($r, $g, $b);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment