-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteGenFunctions.php
More file actions
407 lines (313 loc) · 14.5 KB
/
siteGenFunctions.php
File metadata and controls
407 lines (313 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/*
Copyright 2014 Mark Briggs
This file is part of ColorRing.
ColorRing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
ColorRing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
There is a copy of the GNU General Public License in the
accompanying COPYING file
*/
define("OUT_COLORED5S_COLOR", 0);
define("IN_COLORED5S_COLOR", 1);
define("HANDCOLOR_HOUR", 2);
define("HANDCOLOR_MIN", 3);
define("HANDCOLOR_SEC", 4);
// COLOR_USAGE
define("COLOR_USAGE_NONE", 0);
define("COLOR_USAGE_OUT_ECM", 1);
define("COLOR_USAGE_IN_ECM", 2);
define("COLOR_USAGE_OUT_COLORED5S", 3);
define("COLOR_USAGE_IN_COLORED5S", 4);
define("COLOR_USAGE_HOUR_HAND", 5);
define("COLOR_USAGE_MIN_HAND", 6);
define("COLOR_USAGE_SEC_HAND", 7);
// CmdTypes
define("CMDTYPE_SSP", 0);
//define("CMDTYPE_BCG", 1);
define("CMDTYPE_SHIFT", 2);
define("CMDTYPE_FLOW", 3);
define("MAX_STRIPCMD_SIZE", 32);
function getCmdTable ($cmdBytes, $cmdPos) {
// Note: assumes $cmdBytes are all in INTEGER format!
$table = "<table border=1>";
$cmdPosPrefix = "cmdPos" . str_pad($cmdPos, 3, "0", STR_PAD_LEFT); // e.g. cmdPos001, 009, 010, etc.
$cmdType = $cmdBytes[0];
$table .= '<input type="hidden" id="' . $cmdPosPrefix . 'cmdType" value="' . $cmdType . '" />';
switch ($cmdType) {
case 0: // SetSeqPixels
$table .= getRowStartPixelNum($cmdBytes, $cmdPos, 1);
$table .= getRowNumPixelsEachColor($cmdBytes, $cmdPos, 2);
$table .= getRowColorSeriesNumIter($cmdBytes, $cmdPos, 3);
$table .= getRowNumPixelsToSkip($cmdBytes, $cmdPos, 4);
$table .= getRowNumIter($cmdBytes, $cmdPos, 5);
$table .= getRowAnimDelay($cmdBytes, $cmdPos, 7);
$table .= getRowPauseAfter($cmdBytes, $cmdPos, 9);
$table .= getRowBoolBits($cmdBytes, $cmdPos, 11, array(0,1,3,4,5,6));
$table .= getRowNumColorsInSeries($cmdBytes, $cmdPos, 12);
for ($x = 0; $x < 6; $x++) {
$table .= getRowColorSeriesArrX($cmdBytes, $cmdPos, 13+($x*3), $x);
}
break;
case 2: // Shift
$table .= getRowStartPixelNum($cmdBytes, $cmdPos, 1);
$table .= getRowEndPixelNum($cmdBytes, $cmdPos, 2);
$table .= getRowNumPixelsToSkip($cmdBytes, $cmdPos, 3);
$table .= getRowNumIter($cmdBytes, $cmdPos, 4);
$table .= getRowAnimDelay($cmdBytes, $cmdPos, 6);
$table .= getRowPauseAfter($cmdBytes, $cmdPos, 8);
$table .= getRowBoolBits($cmdBytes, $cmdPos, 10, array(1,2,3));
break;
case 3: // Flow
$table .= getRowStartPixelNum($cmdBytes, $cmdPos, 1);
$table .= getRowEndPixelNum($cmdBytes, $cmdPos, 2);
$table .= getRowNumSection($cmdBytes, $cmdPos, 3);
$table .= getRowNumPixelsEachColor($cmdBytes, $cmdPos, 4);
$table .= getRowColorSeriesNumIter($cmdBytes, $cmdPos, 5);
$table .= getRowNumPixelsToSkip($cmdBytes, $cmdPos, 6);
$table .= getRowAnimDelay($cmdBytes, $cmdPos, 7);
$table .= getRowPauseAfter($cmdBytes, $cmdPos, 9);
$table .= getRowBoolBits($cmdBytes, $cmdPos, 11, array(1,4,5,6));
$table .= getRowNumColorsInSeries($cmdBytes, $cmdPos, 12);
for ($x = 0; $x < 6; $x++) {
$table .= getRowColorSeriesArrX($cmdBytes, $cmdPos, 13+($x*3), $x);
}
break;
default:
// Overwrite 'table' and return right away
$table = "";
return $table;
break;
}
$table .= "</table>";
return $table;
}
function getRowStartPixelNum($cmdBytes, $cmdPos, $idx) {
// $idx => Index into $cmdBytes array.
$table = "<tr>";
$table .= "<td>startPixelNum: </td>";
$table .= '<td><input type="number" min="0" max="59" id="' . getCmdPosPrefix($cmdPos) . 'startPixelNum" value="' . $cmdBytes[$idx] . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowEndPixelNum($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>endPixelNum: </td>";
$table .= '<td><input type="number" min="0" max="59" id="' . getCmdPosPrefix($cmdPos) . 'endPixelNum" value="' . $cmdBytes[$idx] . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowStartPixelColor($cmdBytes, $cmdPos, $idx) {
// Note: $idx is 1st of 3 bytes (R). $idx+1 is G. $idx+2 is B.
$table = "<tr>";
$table .= "<td>startPixelColor: </td>";
$table .= '<td><input id="' . getCmdPosPrefix($cmdPos) . 'startPixelColor" value="' . sprintf("%02X", intval($cmdBytes[$idx], 0)) . sprintf("%02X", intval($cmdBytes[$idx+1], 0)) . sprintf("%02X", intval($cmdBytes[$idx+2], 0)) . '" /></td>';
$table .= "</tr>";
$table .= '<script>initStripCmdColorPicker(' . $cmdPos . ', "startPixelColor");</script>';
return $table;
}
function getRowEndPixelColor($cmdBytes, $cmdPos, $idx) {
// Note: $idx is 1st of 3 bytes (R). $idx+1 is G. $idx+2 is B.
$table = "<tr>";
$table .= "<td>endPixelColor: </td>";
$table .= '<td><input id="' . getCmdPosPrefix($cmdPos) . 'endPixelColor" value="' . sprintf("%02X", intval($cmdBytes[$idx], 0)) . sprintf("%02X", intval($cmdBytes[$idx+1], 0)) . sprintf("%02X", intval($cmdBytes[$idx+2], 0)) . '" /></td>';
$table .= "</tr>";
$table .= '<script>initStripCmdColorPicker(' . $cmdPos . ', "endPixelColor");</script>';
return $table;
}
function getRowNumPixelsToSkip($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>numPixelsToSkip: </td>";
$table .= '<td><input type="number" min="0" max="59" id="' . getCmdPosPrefix($cmdPos) . 'numPixelsToSkip" value="' . $cmdBytes[$idx] . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowNumIter($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>numIter: </td>";
$word = ($cmdBytes[$idx] << 8) + $cmdBytes[$idx+1];
$table .= '<td><input type="number" min="0" max="65535" id="' . getCmdPosPrefix($cmdPos) . 'numIter" value="' . $word . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowNumSection($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>numSections: </td>";
$table .= '<td><input type="number" min="1" max="60" id="' . getCmdPosPrefix($cmdPos) . 'numSections" value="' . $cmdBytes[$idx] . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowNumPixelsEachColor($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>numPixelsEachColor: </td>";
$table .= '<td><input type="number" min="1" max="60" id="' . getCmdPosPrefix($cmdPos) . 'numPixelsEachColor" value="' . $cmdBytes[$idx] . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowColorSeriesNumIter($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>colorSeriesNumIter: </td>";
$table .= '<td><input type="number" min="1" max="255" id="' . getCmdPosPrefix($cmdPos) . 'colorSeriesNumIter" value="' . $cmdBytes[$idx] . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowNumColorsInSeries($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>numColorsInSeries: </td>";
$table .= '<td><input type="number" min="1" max="6" id="' . getCmdPosPrefix($cmdPos) . 'numColorsInSeries" value="' . $cmdBytes[$idx] . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowColorSeriesArrX($cmdBytes, $cmdPos, $idx, $x) {
// Note: $idx is 1st of 3 bytes (R). $idx+1 is G. $idx+2 is B.
$table = "<tr>";
$table .= "<td>colorSeriesArr" . $x . ": </td>";
$table .= '<td><input id="' . getCmdPosPrefix($cmdPos) . 'colorSeriesArr' . $x . '" value="' . sprintf("%02X", intval($cmdBytes[$idx], 0)) . sprintf("%02X", intval($cmdBytes[$idx+1], 0)) . sprintf("%02X", intval($cmdBytes[$idx+2], 0)) . '" /></td>';
$table .= "</tr>";
$table .= '<script>initStripCmdColorPicker(' . $cmdPos . ', "colorSeriesArr' . $x . '");</script>';
return $table;
}
function getRowAnimDelay($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>animDelay (0-65535): </td>";
$word = ($cmdBytes[$idx] << 8) + $cmdBytes[$idx+1];
$table .= '<td><input type="number" min="0" max="65535" id="' . getCmdPosPrefix($cmdPos) . 'animDelay" value="' . $word . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowPauseAfter($cmdBytes, $cmdPos, $idx) {
$table = "<tr>";
$table .= "<td>pauseAfter (0-65535) [ms]: </td>";
$word = ($cmdBytes[$idx] << 8) + $cmdBytes[$idx+1];
$table .= '<td><input type="number" min="0" max="65535" id="' . getCmdPosPrefix($cmdPos) . 'pauseAfter" value="' . $word . '" onfocusout="updateCbArr(' . $cmdPos . ')" onkeyup="stripCmdTextInputKeyUp(' . $cmdPos . ')" /></td>';
$table .= "</tr>";
return $table;
}
function getRowBoolBits($cmdBytes, $cmdPos, $idx, $bitArr) {
$table = "<tr><td><h3>boolBits</h3></td></tr>";
$bb = $cmdBytes[$idx];
if (in_array(0, $bitArr)) {
$chkd = (($bb & 0x01) >> 0) == 1 ? "checked" : "";
$table .= '<tr><td><label><input type="checkbox" id="' . getCmdPosPrefix($cmdPos) . 'bbDestructive" value="" ' . $chkd . ' onchange="updateCbArrAndSend(' . $cmdPos . ')" /> Destructive</label></td></tr>';
}
if (in_array(1, $bitArr)) {
$chkd = (($bb & 0x02) >> 1) == 1 ? "checked" : "";
$table .= '<tr><td><label><input type="checkbox" id="' . getCmdPosPrefix($cmdPos) . 'bbDirection" value="" ' . $chkd . ' onchange="updateCbArrAndSend(' . $cmdPos . ')" /> Direction(checked=CW)</label></td></tr>';
}
if (in_array(2, $bitArr)) {
$chkd = (($bb & 0x04) >> 2) == 1 ? "checked" : "";
$table .= '<tr><td><label><input type="checkbox" id="' . getCmdPosPrefix($cmdPos) . 'bbWrap" value="" ' . $chkd . ' onchange="updateCbArrAndSend(' . $cmdPos . ')" /> Wrap</label></td></tr>';
}
if (in_array(3, $bitArr)) {
$chkd = (($bb & 0x08) >> 3) == 1 ? "checked" : "";
$table .= '<tr><td><label><input type="checkbox" id="' . getCmdPosPrefix($cmdPos) . 'bbIsAnim" value="" ' . $chkd . ' onchange="updateCbArrAndSend(' . $cmdPos . ')" /> Animated</label></td></tr>';
}
if (in_array(4, $bitArr)) {
$chkd = (($bb & 0x10) >> 4) == 1 ? "checked" : "";
$table .= '<tr><td><label><input type="checkbox" id="' . getCmdPosPrefix($cmdPos) . 'bbClearStrip" value="" ' . $chkd . ' onchange="updateCbArrAndSend(' . $cmdPos . ')" /> Clear Strip</label></td></tr>';
}
if (in_array(5, $bitArr)) {
$chkd = (($bb & 0x20) >> 5) == 1 ? "checked" : "";
$table .= '<tr><td><label><input type="checkbox" id="' . getCmdPosPrefix($cmdPos) . 'bbGradiate" value="" ' . $chkd . ' onchange="updateCbArrAndSend(' . $cmdPos . ')" /> Gradiate</label></td></tr>';
}
if (in_array(6, $bitArr)) {
$chkd = (($bb & 0x40) >> 6) == 1 ? "checked" : "";
$table .= '<tr><td><label><input type="checkbox" id="' . getCmdPosPrefix($cmdPos) . 'bbGradiateLastPixelFirstColor" value="" ' . $chkd . ' onchange="updateCbArrAndSend(' . $cmdPos . ')" /> Gradiate (Last Pixel)<br />chkd => Last Pixel is First Color<br />unchkd => Last Pixel is Last Color</label></td></tr>';
}
return $table;
}
function getCmdPosPrefix($cmdPos) {
return "cmdPos" . str_pad($cmdPos, 3, "0", STR_PAD_LEFT); // e.g. cmdPos001, 009, 010, etc.
}
function getTimestamp() {
list($usec, $sec) = explode(" ", microtime());
return date("H:i:s", $sec) . " - " . $usec;
}
function getVarFromColorRing($varRequest) {
global $colorringIP;
$target = $colorringIP;
$requestUrl = $target . '/' . $varRequest;
$curl = curl_init($requestUrl);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); // was 0.5
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($curl);
curl_close($curl);
$jsonA = json_decode($curl_response, true);
//echo "jsonA:";
//print_r($jsonA);
$val = $jsonA[$varRequest];
return $val;
}
function getByteArrayFromColorRing($fnName, $fnParamsStr) {
global $colorringIP;
$target = $colorringIP;
//$requestUrl = $target . '/' . $fnName . '?params=' . $fnParamsStr;
$requestUrl = $target . '/' . $fnName;
if ($fnParamsStr != "") {
$requestUrl .= '?params=' . $fnParamsStr;
}
$curl = curl_init($requestUrl);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); // was 0.5
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($curl);
curl_close($curl);
$jsonB = json_decode($curl_response, true);
//echo "jsonB: "; print_r($jsonB);
//$cmdBytesStr = $jsonB["name"];
$byteArrayStr = $jsonB["name"];
//echo "cmdBytesStr: " . $cmdBytesStr;
$byteArray = explode(",", $byteArrayStr);
return $byteArray;
}
function cmdBytesArr2Str($cmdBytes) {
$cmdBytesStr = "";
for ($i = 0; $i < MAX_STRIPCMD_SIZE; $i++) {
$cmdBytesStr .= $cmdBytes[$i] . ",";
}
$cmdBytesStr = rtrim($cmdBytesStr, ","); // Strip off last comma
return $cmdBytesStr;
}
function outLogFile($str) {
global $LOG_FILE;
$fh = fopen($LOG_FILE, 'a') or die("can't open file");
fwrite($fh, $str . "\n");
fclose($fh);
dbgOut($str); // Output to debug area too, if global $DEBUG is true.
}
function outDebugFile($str) {
global $DEBUG_FILE;
$fh = fopen($DEBUG_FILE, 'a') or die("can't open file");
fwrite($fh, $str . "\n");
fclose($fh);
}
function dbgOut($str) {
// Debug - prints $str to screen (or log file, in the case CRON is running this)
global $DEBUG;
global $SSH_CONNECTION;
global $USING_CRON_OR_PIPE;
if ($DEBUG) {
if ($SSH_CONNECTION) {
echo $str . "\n";
} elseif ($USING_CRON_OR_PIPE) {
outDebugFile($str); // Write to file, instead of screen
} else {
// Coming from Browser - output to screen AND outDebugFile
//outDebugFile($str);
echo $str . "<br />";
}
}
}
function convertStrToUtf8($str, $fromCharset) {
if (stripos($fromCharset, "utf-8") !== false) { return $str; }
if (stripos($fromCharset, "ascii") !== false) { $fromCharset = "ascii"; } // e.g. "us-ascii" => "ascii" - Outlook, and maybe others, sometimes send us-ascii (which is not a valid charset for mb_convert_encoding())
$str = mb_convert_encoding($str, "UTF-8", $fromCharset);
return $str;
}
?>