Update 24.1.2011: Dieses Script funktioniert nur mit Chromoflex für RS232. NICHT mit Chromoflex RC
Hallo zusammen
Wie vor einigen Tagen angekündigt hab ich mein Chromoflex Script für den allgemeinen Gebrauch umgebaut.
Das möche ich nun an euch weitergeben.
Vorweg gleich mal ein Dank an einige andere Script Schreiber, vieles hab ich mir nur aus im Forum vorhandenen Ideen und Codeschnipseln zusammengesucht.
Will mich keinesfalls mit fremden Federn schmücken.
Meine Variante besteht aus 3 Scripten.
„setup“ … das Installationsscript um alle Variablen und Profile anzulegen
„update“… dies wird von den Webfrontelementen aufgerufen und steuert seinerseits andere Webfrontelemente
„set_Chromoflex“ … dies steuert die Chromoflex Hardware
Vorbereitung der Installation:
- Korrekte Funktion des Chromoflex mit der Original Software überprüfen
- Chromoflex Sofware wieder beenden. Diese darf nicht mit IPS zusammen aktiv sein.
- In IPS eine I/O Instanz fürs entsprechende Serielle Port einrichten,
Parameter 9600,8,N,1 - Port öffnen
Optional:
Bei mir war auch die Verwendung eines USB/RS232 Wandlers problemlos möglich (muß aber nicht immer so sein)
Ich verwende zzt. zwei XBee Module um das Chromoflex per Funk anzubinden.
Das funktioniert hier ebenfalls einwandfrei.
Installation:
1. eine Kategorie beliebigen Namens anlegen
2. die drei Scripte in diese Kategorie kopieren ( Namen müssen exakt geschrieben sein, und dürfen sich nicht ändern)
3. die beiliegende Datei: color_bar.png nach „…IP-Symcon/Webfront/User“ kopieren
4. das „setup“ Script ausführen
5. im Script „set_Chromoflex“ die instance Id des Seriellen Ports eintragen an dem das Chromoflex angeschlossen ist.
5. Bitte keinesfalls irgendwelche Variablennamen ändern, dann funktioniert nämlich nix mehr.
Funktion:
Ich denke mal es ist soweit selbsterklärend, einfach klicken und sich freuen.
Es ist auch möglich die Variablen durch eigene Scripte zu beschreiben, und so eigene Sequenzen zu erstellen.
Das würde dann zb. so aussehen:
.....
SetValueInteger(58697 /*[Räume\Flur\Weinregal\SettlingTime & Brightness\Helligkeit]*/,100);
SetValueInteger(43676 /*[Räume\Flur\Weinregal\SettlingTime & Brightness\DimTime]*/,20);
IPS_RunScript(12817 /*[Räume\Flur\Weinregal\Scripte\random]*/);
IPS_RunScriptEx(28613 /*[Räume\Flur\Weinregal\Scripte\set_Chromoflex]*/,Array("ChromoflexMode" => 0.0)); //Chromoflex setzen
}
.....
Warnung: Die Scripte wurden nach bestem Wissen und Gewissen erstellt, und bei mir ausgiebig getestet.
Trotzdem kann ich natürlich für Funktion, oder evtl. gar Schäden an anderen Systemen keine Garantie übernehmen.
Tja und wenn mir jemand einen Gefallen tun möchte, so möge er bitte ein paar paar passende Icons für Webfront basteln.
Ich würds dann noch in die Installationsroutine dazubauen.
Das „setup“ Script:
# Variablenprofile anlegen
if (IPS_VariableProfileExists("my_Chromoflex_Prog")==False) {
IPS_CreateVariableProfile('my_Chromoflex_Prog',1);
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 1, "Normal","");
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 2, "Slow","");
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 3, "Blob","");
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 4, "User","");
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 5, "LSD","");
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 6, "Fire","");
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 7, "Flash","");
IPS_SetVariableProfileAssociation("my_Chromoflex_Prog", 8, "Burst","");
}
if (IPS_VariableProfileExists("my_ColorSlider")==False) {
IPS_CreateVariableProfile('my_ColorSlider',1);
IPS_SetVariableProfileValues("my_ColorSlider", 0, 100, 1);
IPS_SetVariableProfileText("my_ColorSlider", "", "%");
}
if (IPS_VariableProfileExists("my_RGBSlider")==False) {
IPS_CreateVariableProfile('my_RGBSlider',1);
IPS_SetVariableProfileValues("my_RGBSlider", 0, 1000, 1);
IPS_SetVariableProfileText("my_RGBSlider", "", "%");
}
if (IPS_VariableProfileExists("my_RGBPresets")==False) {
IPS_CreateVariableProfile('my_RGBPresets',1);
IPS_SetVariableProfileAssociation("my_RGBPresets", 1, "White","");
IPS_SetVariableProfileAssociation("my_RGBPresets", 2, "Random","");
}
$name_RGB = "Color";
$name_T_B = "Time & Brightness";
$ParentID = IPS_GetObject($IPS_SELF);
$ParentID = $ParentID['ParentID'];
IPS_SetHidden($IPS_SELF,true);
# Scripte definieren
$id_update = @IPS_GetScriptIDByName("update", $ParentID);
IPS_SetHidden($id_update,true);
$id_setChromoflex = @IPS_GetScriptIDByName("set_Chromoflex", $ParentID);
IPS_SetHidden($id_setChromoflex,true);
# DummyInstanzen anlegen
$id_RGB = getId($name_RGB);
IPS_SetPosition($id_RGB, 5);
$id_T_B = getId($name_T_B);
IPS_SetPosition($id_T_B, 10);
#Varianblen anlegen
$id = CreateVariableByName($ParentID, "Program_Selection", 1);
IPS_SetVariableCustomProfile($id, "my_Chromoflex_Prog");
IPS_SetPosition($id, 15);
IPS_SetVariableCustomAction($id, $id_update);
$id = CreateVariableByName($id_RGB, "Red", 1);
IPS_SetVariableCustomProfile($id, "my_ColorSlider");
IPS_SetPosition($id, 20);
IPS_SetVariableCustomAction($id, $id_update);
$id = CreateVariableByName($id_RGB, "Green", 1);
IPS_SetVariableCustomProfile($id, "my_ColorSlider");
IPS_SetPosition($id, 25);
IPS_SetVariableCustomAction($id, $id_update);
$id = CreateVariableByName($id_RGB, "Blue", 1);
IPS_SetVariableCustomProfile($id, "my_ColorSlider");
IPS_SetPosition($id, 30);
IPS_SetVariableCustomAction($id, $id_update);
$id = CreateVariableByName($id_RGB, "Presets", 1);
IPS_SetVariableCustomProfile($id, "my_RGBPresets");
IPS_SetPosition($id, 15);
IPS_SetVariableCustomAction($id, $id_update);
$id = CreateVariableByName($id_RGB, "RGB", 1);
IPS_SetVariableCustomProfile($id, "my_RGBSlider");
IPS_SetPosition($id, 10);
IPS_SetVariableCustomAction($id, $id_update);
$id = CreateVariableByName($id_RGB, "RGB-Table", 3);
IPS_SetVariableCustomProfile($id, "~HTMLBox");
IPS_SetPosition($id, 5);
SetvalueString($id, '<TABLE width="100%"><TR><TD width="65px"></TD><TD><img src="user/color_bar.png" width="100%" height="35" align="middle" ></TD><TD width="65px"></TD></TR></TABLE>');
$id = CreateVariableByName($id_T_B, "Brightness", 1);
IPS_SetVariableCustomProfile($id, "my_ColorSlider");
IPS_SetVariableCustomAction($id, $id_update);
$id = CreateVariableByName($id_T_B, "Dim-Time", 1);
IPS_SetVariableCustomProfile($id, "my_ColorSlider");
IPS_SetVariableCustomAction($id, $id_update);
echo " Installation finished";
function CreateVariableByName($id, $name, $type)
{
global $IPS_SELF;
$vid = @IPS_GetVariableIDByName($name, $id);
if($vid===false) {
$vid = IPS_CreateVariable($type);
IPS_SetParent($vid, $id);
IPS_SetName($vid, $name);
IPS_SetInfo($vid, "This Variable was created by Script #$IPS_SELF");
}
return $vid;
}
function getId($name) {
global $IPS_SELF;
$ParentID = IPS_GetObject($IPS_SELF);
$ParentID = $ParentID['ParentID'];
$ID = @IPS_GetObjectIDByName($name, $ParentID);
if ($ID == 0) {
$dummy=IPS_CreateInstance("{485D0419-BE97-4548-AA9C-C083EB82E61E}"); #Dummy Instance
IPS_ApplyChanges($dummy);
IPS_SetName($dummy,$name);
IPS_SetParent($dummy,$ParentID);
$ID = @IPS_GetObjectIDByName($name, $ParentID);
}
return $ID;
}
?>
Das „update“ Script
<?
// This Script is called by Sliders and Button in Webfront
// it updates the separate R-G-B Sliders and forward necessary parameters to
// set_Chromoflex Script
{
// Get categorie ID
$parentID = IPS_GetObject($IPS_SELF);
$parentID = $parentID['ParentID'];
// Get all variable IDs
$id_Color = @IPS_GetInstanceIDByName("Color", $parentID);
$id_Red = @IPS_GetVariableIDByName("Red", $id_Color);
$id_Green = @IPS_GetVariableIDByName("Green", $id_Color);
$id_Blue = @IPS_GetVariableIDByName("Blue", $id_Color);
$id_RGB = @IPS_GetVariableIDByName("RGB", $id_Color);
$id_Presets = @IPS_GetVariableIDByName("Presets", $id_Color);
$id_setChromoflex = @IPS_GetScriptIDByName("set_Chromoflex", $parentID);
$id_Programwahl = @IPS_GetVariableIDByName("Program_Selection", $parentID);
SetValueinteger($IPS_VARIABLE, $IPS_VALUE);
Switch ($IPS_VARIABLE) {
// suppport fixed Color Presets
case $id_Presets: //Presets
if ($IPS_VALUE == 2) {
setvalueinteger($id_Red /*..Color\Rot]*/,rand(0,100));
setvalueinteger($id_Green /*..Color\Grün]*/,rand(0,100));
setvalueinteger($id_Blue /*../Color\Blau]*/,rand(0,100));
setvalueinteger($id_RGB /*../Color\RGB*/,0);
}
if ($IPS_VALUE == 1) {
setvalueinteger($id_Red /*..Color\Rot]*/,100);
setvalueinteger($id_Green /*..Color\Grün]*/,100);
setvalueinteger($id_Blue /*../Color\Blau]*/,100);
setvalueinteger($id_RGB /*../Color\RGB*/,0);
}
setvalueinteger($IPS_VARIABLE,0);
break;
case $id_Red:
setvalueinteger($id_RGB /*../Color\RGB*/,0);
break;
case $id_Green:
setvalueinteger($id_RGB /*../Color\RGB*/,0);
break;
case $id_Blue:
setvalueinteger($id_RGB /*../Color\RGB*/,0);
break;
// Combined RGB Slider
case $id_RGB: //Farbband Slider
$colorVal = $IPS_VALUE;
// RED > ORANGE > YELLOW
if ($colorVal >= 0 && $colorVal <= 170) {
$redPwr = 255;
$bluePwr = 0;
$greenPwr = mape($colorVal, 0, 170, 0, 255);
}
// YELLOW > LIME?? > GREEN
if ($colorVal > 170 && $colorVal <= 341) {
$greenPwr = 255;
$bluePwr = 0;
$redPwr = mape($colorVal, 341, 170, 0, 255);
}
// GREEN > TURQOUISE
if ($colorVal > 341 && $colorVal <= 511) {
$greenPwr = 255;
$redPwr = 0;
$bluePwr = mape($colorVal, 341, 511, 0, 255);
}
// TURQOUISE > BLUE
if ($colorVal > 511 && $colorVal <= 682) {
$bluePwr = 255;
$redPwr = 0;
$greenPwr = mape($colorVal, 682, 511, 0, 255);
}
// BLUE > PURPLE
if ($colorVal > 682 && $colorVal <= 852) {
$bluePwr = 255;
$greenPwr = 0;
$redPwr = mape($colorVal, 682, 852, 0, 255);
}
// PURPLE > RED
if ($colorVal > 852 && $colorVal <= 1023) {
$redPwr = 255;
$greenPwr = 0;
$bluePwr = mape($colorVal, 1023, 852, 0, 255);
}
// update seperate RGB Sliders
setvalueinteger($id_Red /*..Color\Rot]*/,intval($redPwr/2.5));
setvalueinteger($id_Green /*..Color\Grün]*/,intval($greenPwr/2.5));
setvalueinteger($id_Blue /*../Color\Blau]*/,intval($bluePwr/2.5));
IPS_RunScriptEx($id_setChromoflex /*..\/Set_Chromoflex*/,Array("ChromoflexMode" => 0.0)); //Chromoflex setzen
break;
}
}
// call set_Chromoflex Script and call build in Scene
if ($IPS_VARIABLE ==$id_Programwahl /*../Programauswahl]*/){
IPS_RunScriptEx($id_setChromoflex /*..\/Set_Chromoflex*/,Array("ChromoflexMode" => getvalue($id_Programwahl /*../Programauswahl]*/))); //Chromoflex setzen
}
else {
// call set_Chromoflex Script and call static RGB Color
setvalueinteger($id_Programwahl /*../Programauswahl]*/,0);
IPS_RunScriptEx($id_setChromoflex /*..\/Set_Chromoflex*/,Array("ChromoflexMode" => 0.0)); //Chromoflex setzen
}
// support function for combiend RGB Slider
function mape($Value, $min, $max, $new_min, $new_max)
{
$range = $max - $min;
$new_range = $new_max - $new_min;
$scaling = $new_range / $range;
$new_Value = round(($Value - $min)*$scaling);
if ($range < 0)
{
$new_Value = round(($Value - $max)*$scaling);
$new_Value = $new_max + $new_Value;
}
return $new_Value;
}
?>
Das „set_Chromoflex“ Script
<?
// This Script is called by "update" Script.
// it reads settings from Webfront and forward to Chromoflex Module
// Configuration:
// SET Chromoflex Module Adress, check Chromoflex Dokumentation for further Info
// If only one Module is connected default setting will be fine.
$addr=chr(0).chr(0).chr(0); //Adresse from Chromoflex Moduls 0,0,0 = Broadcast = all connected Modules
//IPS InstanceID of SerialPort where your Chromoflex is connected
define ("ID_CommPort",55219);
// make shure your SerialPort is set to 9600,8,N,1
// Test correct Hardware Function and settings with Original Chromoflex Software
// Note its not posible to have IPS & Chromoflex Software talking to same ComPort at the same time
//______________________________________________________________________________________________
// NO more User settings below this line
//______________________________________________________________________________________________
// get Kategory ID
$parentID = IPS_GetObject($IPS_SELF);
$parentID = $parentID['ParentID'];
// Get Variable IDs
$id_Color = @IPS_GetInstanceIDByName("Color", $parentID);
$id_Red = @IPS_GetVariableIDByName("Red", $id_Color);
$id_Green = @IPS_GetVariableIDByName("Green", $id_Color);
$id_Blue = @IPS_GetVariableIDByName("Blue", $id_Color);
$id_B_T = @IPS_GetInstanceIDByName("Time & Brightness", $parentID);
$id_Brightness = @IPS_GetVariableIDByName("Brightness", $id_B_T);
$id_Dim_Time = @IPS_GetVariableIDByName("Dim-Time", $id_B_T);
//_________________________________________________________________________________________________
// Check If static RGB Mode or Chromoflex interenal Scenes should be used
IF ($ChromoflexMode > 0) {
// Code for Chromoflex internal Scenes
$cmd=chr(0).chr(0).chr(0xFE); //RESET
send_command($addr.$cmd);
IPS_Sleep(700);
$ProgNr=200+($ChromoflexMode*3)-3; // $IPS_Value = 1 bis 8 für Chromoflex interen Modes,
$cmd = chr(0).chr(3).chr(0x7D).chr(0x15).chr(0).chr($ProgNr);
send_command($addr.$cmd);
}
// Code for static RGB Mode
else {
//Switch to manual Mode
$cmd=chr(0).chr(3).chr(0x7E).chr(0x11).chr(0x01).chr(0x01);
send_command($addr.$cmd);
$dimtime=GetValue($id_Dim_Time /*.../ DimTime]*/)*2.55;
echo $dimtime;
$cmd=chr(0).chr(5).chr(0x7E).chr(17).chr(1).chr(0x00);
send_command($addr.$cmd);
//set increments -> 255 = immediate, 1 = 2,55ms for step from 0 to 255
$cmd=chr(0).chr(5).chr(0x7E).chr(8).chr($dimtime).chr($dimtime).chr($dimtime).chr(0x00);
send_command($addr.$cmd);
//Set colors
$bright = getvalue($id_Brightness /*../Brightness*/);
$red=(getvalue($id_Red /*../Color\Red]*/)*2.5 / 100)*$bright;
$green=(getvalue($id_Green /*../Color\Green]*/)*2.5 / 100)*$bright;
$blue=(getvalue($id_Blue /*../Color\Blue]*/)*2.5 / 100)*$bright;
$cmd=chr(0).chr(5).chr(0x7E).chr(4).chr($red).chr($green).chr($blue).chr(0x00);
send_command($addr.$cmd);
}
//_________________________________________________________________________________________________
// Code to send Commands to Chromoflex
//_________________________________________________________________________________________________
function send_command($txt) {
$head=chr(0xCA); //StartByte
$crc=crc16($head.$txt);
$txt=str_replace(chr(0xCB), chr(0xCB).chr(0x01), $txt);
$txt=str_replace(chr(0xCA), chr(0xCB).chr(0x00), $txt);
COMPort_SendText(ID_CommPort/*[Serial Port]*/, $head.$txt.$crc);
}
//_________________________________________________________________________________________________
// Calc Checksumm
//___________________________________________________________________________________________________
function CRC16($s) // CRC16 reverse
{
$CRCreg = 0xffff << 1; // Start
$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);
}
?>