<?php

require 'config.php';

if ( !isset($_REQUEST['pattern']) ) exit();
$pattern = $_REQUEST['pattern'];

$command = 0x02;
$target = LEDTARGETID;
$unique = mt_rand(1, 2000000000);

$target = ($target > 0 ) ? 2 ** ($target - 1) : 0;
$data = pack("CQL", $command, $target, $unique);

$goodPattern = false;

if ( $pattern == "crazy" ) {
   // The first pack here sets the milliseconds duration for ramping between colors followed by the number of colors
   $data .= pack("LC", 50, 1);
   // Now we stack on the colors. R, G, B values from 0.0 to 1.0 and the final parameter being how long to rest on this color
   $data .= pack("CCCL", 0, 0, 0, 0);
   $goodPattern = true;
} elseif ( $pattern == "crazymellow" ) {
   $data .= pack("LC", 8000, 1);
   $data .= pack("CCCL", 0, 0, 0, 0);
   $goodPattern = true;
} elseif ( $pattern == "christmas" ) {
   $data .= pack("LC", 1000, 2);
   $data .= pack("CCCL", 255, 0, 0, 2000);
   $data .= pack("CCCL", 0, 255, 0, 2000);
   $goodPattern = true;
} elseif ( $pattern == "julyfourth" ) {
   $data .= pack("LC", 1000, 3);
   $data .= pack("CCCL", 255, 0, 0, 2000);
   $data .= pack("CCCL", 127, 127, 127, 2000);
   $data .= pack("CCCL", 0, 0, 255, 2000);
   $goodPattern = true;
} elseif ( $pattern == "easter" ) {
   $data .= pack("LC", 1000, 3);
   $data .= pack("CCCL", 255, 3, 192, 2000);
   $data .= pack("CCCL", 8, 255, 247, 2000);
   $data .= pack("CCCL", 255, 252, 5, 2000);
   $goodPattern = true;
} elseif ( $pattern == "halloween" ) {
   $data .= pack("LC", 1000, 2);
   $data .= pack("CCCL", 255, 24, 0, 1000);
   $data .= pack("CCCL", 0, 0, 0, 250);
   $goodPattern = true;
} elseif ( $pattern == "thanksgiving" ) {
   $data .= pack("LC", 1000, 2);
   $data .= pack("CCCL", 0x4d, 0x1e, 0, 2000);
   $data .= pack("CCCL", 0xcf, 0x53, 0, 2000);
   $goodPattern = true;
} elseif ( $pattern == "mellow" ) {
   $data .= pack("LC", 10000, 5);
   $data .= pack("CCCL", 0x00, 0x8f, 0x0d, 0);
   $data .= pack("CCCL", 0x8b, 0x00, 0x08, 0);
   $data .= pack("CCCL", 0x06, 0x01, 0x8f, 0);
   $data .= pack("CCCL", 0x8f, 0x02, 0x54, 0);
   $data .= pack("CCCL", 0x3e, 0x8f, 0x8c, 0);
   $goodPattern = true;
} elseif ( $pattern == "test" ) {
   $goodPattern = true;
   $data .= pack("LC", 1000, 2);
}

if ( $goodPattern ) {
   $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
   socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1);
   for ( $i=0; $i<3; $i++ ) {
      socket_sendto($sock, $data, strlen($data), 0, LEDADDRESS, 6565);
      if ( $i < 2 ) usleep(10000);
   }
   socket_close($sock);
}

exit();

?>
