Steiner´s Script angepasst. CRC Berechnung HJH.
Sollte jemand an weiteren Daten interessiert sein, stelle ich diese soweit es möglich ist gerne zur Verfügung.
<?
/*
*******************************
IP-SYMCON Event Scripting
*******************************
File : Chromoflex.ips.php
Trigger : OnUpDate
Interval : Dateneingang
*/
$Xbee = 15562; // InstanceID Xbee/Comport
$Chromoflex = GetValueString("Dateneingang");
// Daten aus empfangenem String extrahieren, Zuweisung der einzelnen Bytes
$Graph = String2Value($Chromoflex,3,1);
$DatenGraph = String2Value($Chromoflex,4,1);
$Startbyte = "\xCA"; // StartByte (Muss nicht verändert werden)
$Adresse = "\x00\x00\x00"; // Adresse 0,0,0 = Broadcast
$GraphRot = 1; // BarGraph 1 Rot
$GraphGruen = 2; // BarGraph 2 Gruen
$GraphBlau = 3; // BarGraph 3 Blau
$Helligkeit = 4; // BarGraph 4 Dimmer
// Werte BarGraph setzen 0-100%
if ($GraphRot == $Graph)
{
SetValueInteger("Rot", $DatenGraph);
}
if ($GraphGruen == $Graph)
{
SetValueInteger("Gruen", $DatenGraph);
}
if ($GraphBlau == $Graph)
{
SetValueInteger("Blau", $DatenGraph);
}
if($Helligkeit == $Graph)
{
SetValueInteger("Dimmer", $DatenGraph);
}
// Werte holen und umrechnen auf max 255
$Rot = GetValueInteger("Rot");
if ($Rot == 1) {
SetValueInteger("Rot", 0); }
else {
$Rot = $Rot*2.55; }
$Gruen = GetValueInteger("Gruen");
if ($Gruen == 1) {
SetValueInteger("Gruen", 0); }
else {
$Gruen = $Gruen*2.55; }
$Blau = GetValueInteger("Blau");
if ($Blau == 1) {
SetValueInteger("Blau", 0); }
else {
$Blau = $Blau*2.55; }
$Dimmer = GetValueInteger("Dimmer");
switch ($Chromoflex)
{
// Nach Reset startet das Default Programm
case "Reset": // Reset
Reseten(); // Funktion aufrufen
return;
break;
// Programmnummer aufrufen 0-7 und 9. Es erfolgt erst ein Reset,
// danach wird das Programm mit einer kleinen Verzögerung aufgerufen, diese muss sein, sonst startet das Programm nicht.
// Programm = 200+3*Programmnummer. Hier als Beispiel Nr.3, 200 + 3 * 3 = 209 entspricht 0xD1
case "PrgAus":
Reseten(); // Funktion aufrufen
IPS_Sleep(700);
$cmd = chr(0).chr(3).chr(0x7D).chr(0x15).chr(0).chr(0xD1); //Programm. 0xD1 startet Programm 3
$txt = $Adresse.$cmd;
$crc = CRC16r($Startbyte.$txt);
$txt = str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt = str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText($Xbee, $Startbyte.$txt.$crc);
return;
break;
}
// Status setzen
$cmd = chr(0).chr(2).chr(0x7E).chr(0x12).chr(0x01);
$txt = $Adresse.$cmd;
$crc = CRC16r($Startbyte.$txt);
$txt = str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt = str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText($Xbee, $Startbyte.$txt.$crc);
/*
// Track setzen
$cmd = chr(0).chr(3).chr(0x7E).chr(0x11).chr(0x01).chr(0x01);
$txt = $Adresse.$cmd;
$crc = CRC16r($Startbyte.$txt);
$txt = str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt = str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText($Xbee, $Startbyte.$txt.$crc);
/*
//INKREMENTIERUNG FESTLEGEN -> 255 = sofort, 1 = 2,55ms für Änderungen von 0 auf 255
define('Rot', 255);
define('Gruen', 255);
define('Blau', 255);
$cmd = chr(0).chr(5).chr(0x7E).chr(8).chr(Rot).chr(Gruen).chr(Blau).chr(0x64);
$txt = $Adresse.$cmd;
$crc = CRC16r($Startbyte.$txt);
$txt = str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt = str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText($Xbee, $Startbyte.$txt.$crc);
*/
//Farben setzen ==> Set
$cmd = chr(0).chr(5).chr(0x7E).chr(4).chr($Rot).chr($Gruen).chr($Blau).chr($Dimmer);
$txt = $Adresse.$cmd;
$crc = CRC16r($Startbyte.$txt);
$txt = str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt = str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText($Xbee, $Startbyte.$txt.$crc);
//Level setzen
$cmd = chr(0).chr(5).chr(0x7E).chr(0).chr($Rot).chr($Gruen).chr($Blau).chr($Dimmer);
$txt = $Adresse.$cmd;
$crc = CRC16r($Startbyte.$txt);
$txt = str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt = str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText($Xbee, $Startbyte.$txt.$crc);
// Funktion zur Checksummenberechnung
function CRC16r($s) // CRC16 reverse
{
$CRCreg = 0xffff << 1; // Startwert
$PLYreg = 0xA001 << 1; // Polynom
$len = strlen($s);
for ($cp=0; $cp<$len; $cp++)
{
$CRCreg ^= ord($s[$cp]) << 1;
for ($bp=0; $bp<8; $bp++)
{
$CRCreg >>= 1;
if ($CRCreg & 0x01) $CRCreg ^= $PLYreg;
}
}
$crc = $CRCreg >> 1;
$crcLo = $crc & 0xff;
$crcHi = $crc >> 8;
return chr($crcHi). chr($crcLo);
}
// Funktion zur Umwandlung eines Teil eines String in eine Zahl, Zählung beginnt bei Null
function String2Value($str, $p, $c) // String, Position, Anzahl Characters
{
$val = 0;
for ($i=0; $i<$c; $i++)
{
$val <<= 8;
$val += ord($str[$p+$i]);
}
return $val; // Rückgabe: Zahl
}
// Funktion Reseten
function Reseten()
{
SetValueInteger("Rot", 0);
SetValueInteger("Gruen", 0);
SetValueInteger("Blau", 0);
$cmd = chr(0).chr(0).chr(0xFE);
$txt = "\x00\x00\x00".$cmd;
$crc = CRC16r("\xCA".$txt);
$txt = str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt = str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText(15562, "\xCA".$txt.$crc);
}
?>