daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Sep 16th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //autowatch = 1;
  2. outlets = 1;
  3. inlets = 5;
  4.  
  5. var ajaxreq;
  6. var ip = "";
  7. var key = ""; // username
  8.  
  9. var tran = 0;
  10.  
  11. var red;
  12. var green;
  13. var blue;
  14.  
  15. var tone;
  16. var velocity;
  17. var bulb_id = 1;
  18. var brightness = 100;
  19. var color = "100 100 100";
  20. var strobe;
  21.  
  22. var i = 0;
  23.  
  24.  
  25. function msg_int(v){
  26.    
  27.     //assign inlets with if statements
  28.    
  29.     if(inlet==0){
  30.         bulb_id = v;
  31.     }
  32.    
  33.  
  34.     if(inlet==1){
  35.         brightness = v;
  36.         set_light('bri',brightness);
  37.     }  
  38.    
  39.     if(inlet==2){
  40.         if(i == 0){
  41.             red = v;
  42.             i++;
  43.         }else if(i == 1){
  44.             green = v;
  45.             i++;
  46.         }else if(i == 2){
  47.             blue = v;
  48.             i = 0;
  49.             set_light('xy','[' + toXY(red, green, blue)[0] + ',' + toXY(red, green, blue)[1] + ']');
  50.             post(red + ":" + green + ":" + blue);
  51.         }
  52.     }
  53.  
  54.     //register_user();
  55.  
  56. }
  57.  
  58. function bang(v){
  59.    
  60. }
  61.  
  62.  
  63. function register_user(){
  64.     ajaxreq = new XMLHttpRequest();
  65.     ajaxreq.open("POST",'http://' + ip + '/api');
  66.     ajaxreq.onreadystatechange = readystatechange_parsejson;
  67.     ajaxreq.send('{"devicetype":"'+ key +'","username":"'+ key +'"}');
  68. }
  69.  
  70. function get_lights(){
  71.     ajaxreq = new XMLHttpRequest();
  72.     ajaxreq.open("GET",'http://' + ip + '/api/' + key +'/lights');
  73.     ajaxreq.onreadystatechange = readystatechange_parsejson;
  74.     ajaxreq.send();
  75. }
  76.    
  77.  
  78. function on(state)
  79. {
  80.     ajaxreq = new XMLHttpRequest();
  81.     ajaxreq.open("PUT",'http://' + ip + '/api/' + key +'/lights/' + bulb_id + '/state');
  82.     ajaxreq.onreadystatechange = readystatechange_parsejson;
  83.     if (state == 1)
  84.         ajaxreq.send('{"on": true}');
  85.     if (state == 0)
  86.         ajaxreq.send('{"on": false}');
  87. }
  88.  
  89. function set_light(cmd, value){
  90.    
  91.     ajaxreq = new XMLHttpRequest();
  92.     ajaxreq.open("PUT",'http://' + ip + '/api/' + key +'/lights/' + bulb_id + '/state');
  93.     ajaxreq.onreadystatechange = readystatechange_parsejson;
  94.     // {"on":true, "sat":255, "bri":255,"hue":10000}
  95.    
  96.    
  97.     ajaxreq.send('{"'+cmd+'": '+value+', "transitiontime": 0}');
  98.     //post('{"'+cmd+'": '+value+', "transitiontime": 0}');
  99. }
  100.  
  101. function readystatechange_parsejson()
  102. {
  103.     if (this.readyState ==4){
  104.         //post(this.responseText);
  105.     }
  106. }
  107.  
  108. function strobe(){
  109.     set_light('alert','select');
  110. }
  111.  
  112. function toXY(red,green,blue){
  113.     //Gamma correctie
  114.     red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
  115.     green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
  116.     blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);
  117.                    
  118.     //Apply wide gamut conversion D65
  119.     var X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
  120.     var Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
  121.     var Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
  122.                    
  123.     var fx = X / (X + Y + Z);
  124.     var fy = Y / (X + Y + Z);
  125.     if (isNaN(fx)) {
  126.         fx = 0.0;
  127.     }
  128.     if (isNaN(fy)) {
  129.         fy = 0.0;
  130.     }
  131.                    
  132.     return [fx.toPrecision(4),fy.toPrecision(4)];
  133. }
  134.  
  135. function list(v){
  136.         post(v);
  137. }
RAW Paste Data
Top