SHARE
TWEET

PHP/HTML Web controller for Arduino sample code - index.php

w0lfiesmith Dec 11th, 2012 2,016 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. error_reporting(0);
  3. if($_POST['colors'] != ''){
  4.         $colors = explode(',',$_POST['colors']);//expand color variables to an array
  5.         $textfile = "LED.txt"; // Declares the name and location of the .txt file
  6.          
  7.         $fileLocation = "$textfile";
  8.         $fh = fopen($fileLocation, 'w   ') or die("Something went wrong!"); // Opens up the .txt file for writing and replaces any previous content
  9.         fwrite($fh, $colors[0]."\n");
  10.         fwrite($fh, $colors[1]."\n");
  11.         fwrite($fh, $colors[2]."\n");
  12.         fclose($fh);
  13.          
  14.         header("HTTP/1.0 200 OK");
  15. }
  16.  
  17. ?>
  18. <html>
  19. <head>
  20.         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  21.        
  22. <script src='spectrum.js'></script>
  23. <link rel='stylesheet' href='spectrum.css' />
  24.         </head>
  25. <body>
  26.         <script>
  27.  
  28.         function getRGB(color) {
  29.             var result;
  30.             // Look for rgb(num,num,num)
  31.             if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
  32.             // Look for rgb(num%,num%,num%)
  33.             if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55];
  34.             // Look for #a0b1c2
  35.             if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)];
  36.             // Look for #fff
  37.             if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16), parseInt(result[3] + result[3], 16)];
  38. }
  39.         </script>
  40.        
  41.         <center>This is it: the most awesome button in the world. Change the color of my lights, now -> <input type='text' id="minipicker"/></center>
  42. <script>
  43. $("#minipicker").spectrum({
  44.     color: "#ECC",
  45.     showInput: true,
  46.     className: "full-spectrum",
  47.     showInitial: true,
  48.     showPalette: true,
  49.     showSelectionPalette: true,
  50.     maxPaletteSize: 10,
  51.     preferredFormat: "hex",
  52.     localStorageKey: "spectrum.demo",
  53.     move: function (color) {
  54.        
  55.     },
  56.     show: function () {
  57.    
  58.     },
  59.     beforeShow: function () {
  60.    
  61.     },
  62.     hide: function () {
  63.    
  64.     },
  65.     change: function() {
  66.        $.ajax({
  67.                                 type: "POST",
  68.                                 url: 'index.php',
  69.                                 data: {'colors':getRGB($('#minipicker').val()).join(',')},
  70.                                 success: function(data) {
  71.                                 //alert("changed, but you wont know unless technophilia is broadcasting live!");
  72.                                 }
  73.                         });
  74.     },
  75.     palette: [
  76.         ["rgb(0, 0, 0)", "rgb(67, 67, 67)", "rgb(102, 102, 102)",
  77.         "rgb(204, 204, 204)", "rgb(217, 217, 217)","rgb(255, 255, 255)"],
  78.         ["rgb(152, 0, 0)", "rgb(255, 0, 0)", "rgb(255, 153, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)",
  79.         "rgb(0, 255, 255)", "rgb(74, 134, 232)", "rgb(0, 0, 255)", "rgb(153, 0, 255)", "rgb(255, 0, 255)"],
  80.         ["rgb(230, 184, 175)", "rgb(244, 204, 204)", "rgb(252, 229, 205)", "rgb(255, 242, 204)", "rgb(217, 234, 211)",
  81.         "rgb(208, 224, 227)", "rgb(201, 218, 248)", "rgb(207, 226, 243)", "rgb(217, 210, 233)", "rgb(234, 209, 220)",
  82.         "rgb(221, 126, 107)", "rgb(234, 153, 153)", "rgb(249, 203, 156)", "rgb(255, 229, 153)", "rgb(182, 215, 168)",
  83.         "rgb(162, 196, 201)", "rgb(164, 194, 244)", "rgb(159, 197, 232)", "rgb(180, 167, 214)", "rgb(213, 166, 189)",
  84.         "rgb(204, 65, 37)", "rgb(224, 102, 102)", "rgb(246, 178, 107)", "rgb(255, 217, 102)", "rgb(147, 196, 125)",
  85.         "rgb(118, 165, 175)", "rgb(109, 158, 235)", "rgb(111, 168, 220)", "rgb(142, 124, 195)", "rgb(194, 123, 160)",
  86.         "rgb(166, 28, 0)", "rgb(204, 0, 0)", "rgb(230, 145, 56)", "rgb(241, 194, 50)", "rgb(106, 168, 79)",
  87.         "rgb(69, 129, 142)", "rgb(60, 120, 216)", "rgb(61, 133, 198)", "rgb(103, 78, 167)", "rgb(166, 77, 121)",
  88.         "rgb(91, 15, 0)", "rgb(102, 0, 0)", "rgb(120, 63, 4)", "rgb(127, 96, 0)", "rgb(39, 78, 19)",
  89.         "rgb(12, 52, 61)", "rgb(28, 69, 135)", "rgb(7, 55, 99)", "rgb(32, 18, 77)", "rgb(76, 17, 48)"]
  90.     ]
  91. });
  92. </script>
  93.         </body>
  94. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top