Project

General

Profile

Download (7.76 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * led.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2009-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgment:
22
 *    "This product includes software developed by the pfSense Project
23
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
 *
25
 * 4. The names "pfSense" and "pfSense Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    coreteam@pfsense.org.
29
 *
30
 * 5. Products derived from this software may not be called "pfSense"
31
 *    nor may "pfSense" appear in their names without prior written
32
 *    permission of the Electric Sheep Fencing, LLC.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *
37
 * "This product includes software developed by the pfSense Project
38
 * for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 * OF THE POSSIBILITY OF SUCH DAMAGE.
52
 */
53

    
54
$led_root = "/dev/led/led";
55

    
56
/*
57
 * Send the control string to an LED
58
 */
59
function led_ctl($led, $str) {
60
	global $led_root;
61
	if (led_exists($led)) {
62
		exec("/bin/echo " . escapeshellarg($str) . " > {$led_root}{$led}");
63
		return true;
64
	}
65
	return false;
66
}
67

    
68
/*
69
 * Blink an LED at set speed from 1-9 (1=Very Fast, 9=Very Slow)
70
 */
71
function led_blink($led, $speed = 0) {
72
	switch ($speed) {
73
		case "reallyfast":
74
		case "veryfast":
75
			$speed = 1;
76
			break;
77
		case "fast":
78
			$speed = 3;
79
			break;
80
		case "medium":
81
			$speed = 5;
82
			break;
83
		case "slow":
84
			$speed = 7;
85
			break;
86
		case "reallyslow":
87
		case "veryslow":
88
			$speed = 9;
89
			break;
90
	}
91
	if (is_numeric($speed) && ($speed > 0) && ($speed < 10)) {
92
		return led_ctl($led, "f{$speed}");
93
	}
94
	return false;
95
}
96

    
97
/*
98
 * Blink an LED in a specific pattern
99
 * Letters A-J are on from 1/10s to 1s
100
 * Letters a-j are off from 1/10s to 1s
101
 */
102
function led_pattern($led, $pattern, $repeat = true) {
103
	/*  End with a . to stop after one iteration. */
104
	$end = $repeat ? "" : ".";
105
	return led_ctl($led, "s{$pattern}{$end}");
106
}
107

    
108
/*
109
 * Encode a text message into morse code, and send it to an LED
110
 */
111
function led_morse($led, $message) {
112
	return led_ctl($led, "m" . str_to_morse($message));
113
}
114

    
115
/*
116
 * Blink digits out on LED at 1/10s intervals
117
 * e.g 1=1 blink, 8=8 blinks
118
 * 0 is 10 pulses.
119
 * One second pause between digits.
120
 */
121
function led_digit($led, $digitstring) {
122
	$i = 0;
123
	$dstring = "d";
124
	while ($i < strlen($digitstring)) {
125
		$thisdigit = substr($digitstring, $i++, 1);
126
		if (is_numeric($thisdigit)) {
127
			$dstring .= $thisdigit;
128
		}
129
	}
130
	led_ctl($led, $dstring);
131
}
132

    
133
/*
134
 * Turn an LED on
135
 */
136
function led_on($led) {
137
	led_ctl($led, "1");
138
}
139

    
140
/*
141
 * Turn an LED off
142
 */
143
function led_off($led) {
144
	led_ctl($led, "0");
145
}
146

    
147
/*
148
 * Find the number of LEDs present on the system.
149
 */
150
function led_count() {
151
	global $led_root;
152
	$count = 0;
153
	$leds = array();
154
	if (is_dir(dirname($led_root))) {
155
		$leds = glob("{$led_root}*");
156
		$count = count($leds);
157
	}
158
	return $count;
159
}
160

    
161
/*
162
 * Test to see if a given LED exists.
163
 */
164
function led_exists($led) {
165
	global $led_root;
166
	if (!is_numeric($led)) {
167
		return false;
168
	}
169
	return file_exists("{$led_root}{$led}");
170
}
171

    
172
/*
173
 * Sweep across three LEDs in a K.I.T.T.-like way.
174
 */
175
function led_kitt() {
176
	led_pattern(1, 'AaaaaA');
177
	led_pattern(2, 'aAaaAa');
178
	led_pattern(3, 'aaAAaa');
179
}
180

    
181
/*
182
 * Custom pattern for assigning interfaces
183
 */
184
function led_assigninterfaces() {
185
	led_pattern(1, 'AaaAaaaaaaaaaaaa');
186
	led_pattern(2, 'aaaaaAaaAaaaaaaa');
187
	led_pattern(3, 'aaaaaaaaaaAaaAaa');
188
}
189

    
190
/*
191
 * Return the three LEDs to a standard setup (1=on, 2 and 3 = off)
192
 */
193
function led_normalize() {
194
	led_on(1);
195
	led_off(2);
196
	led_off(3);
197
}
198

    
199
/*
200
 * Shut off ALL LEDs.
201
 */
202
function led_alloff() {
203
	led_off(1);
204
	led_off(2);
205
	led_off(3);
206
}
207

    
208
/*
209
 * Translate a string to morse code. Characters not known to have a
210
 * valid morse code representation will be ignored.
211
 */
212
function str_to_morse($string) {
213
	$i = 0;
214
	$morsestring = "";
215
	while ($i < strlen($string)) {
216
		$morsestring .= char_to_morse(substr($string, $i++, 1)) . " ";
217
	}
218
	return $morsestring . "\n";
219
}
220

    
221
/*
222
 * Translate a single character to morse code. Characters not known
223
 * to have a valid morse code representation will be ignored.
224
 */
225
function char_to_morse($char) {
226
	switch (strtoupper($char)) {
227
		case "A":
228
			return ".-";
229
			break;
230
		case "B":
231
			return "-...";
232
			break;
233
		case "C":
234
			return "-.-.";
235
			break;
236
		case "D":
237
			return "-..";
238
			break;
239
		case "E":
240
			return ".";
241
			break;
242
		case "F":
243
			return "..-.";
244
			break;
245
		case "G":
246
			return "--.";
247
			break;
248
		case "H":
249
			return "....";
250
			break;
251
		case "I":
252
			return "..";
253
			break;
254
		case "J":
255
			return ".---";
256
			break;
257
		case "K":
258
			return "-.-";
259
			break;
260
		case "L":
261
			return ".-..";
262
			break;
263
		case "M":
264
			return "--";
265
			break;
266
		case "N":
267
			return "-.";
268
			break;
269
		case "O":
270
			return "---";
271
			break;
272
		case "P":
273
			return ".--.";
274
			break;
275
		case "Q":
276
			return "--.-";
277
			break;
278
		case "R":
279
			return ".-.";
280
			break;
281
		case "S":
282
			return "...";
283
			break;
284
		case "T":
285
			return "-";
286
			break;
287
		case "U":
288
			return "..-";
289
			break;
290
		case "V":
291
			return "...-";
292
			break;
293
		case "W":
294
			return ".--";
295
			break;
296
		case "X":
297
			return "-..-";
298
			break;
299
		case "Y":
300
			return "-.--";
301
			break;
302
		case "Z":
303
			return "--..";
304
			break;
305
		case "0":
306
			return "-----";
307
			break;
308
		case "1":
309
			return ".----";
310
			break;
311
		case "2":
312
			return "..---";
313
			break;
314
		case "3":
315
			return "...--";
316
			break;
317
		case "4":
318
			return "....-";
319
			break;
320
		case "5":
321
			return ".....";
322
			break;
323
		case "6":
324
			return "-....";
325
			break;
326
		case "7":
327
			return "--...";
328
			break;
329
		case "8":
330
			return "---..";
331
			break;
332
		case "9":
333
			return "----.";
334
			break;
335
		case ".":
336
			return ".-.-.-";
337
			break;
338
		case ",":
339
			return "--..--";
340
			break;
341
		case "?":
342
			return "..--..";
343
			break;
344
		case "'":
345
			return ".----.";
346
			break;
347
		case "!":
348
			return "-.-.--";
349
			break;
350
		case "/":
351
			return "-..-.";
352
			break;
353
		case "(":
354
			return "-.--.";
355
			break;
356
		case ")":
357
			return "-.--.-";
358
			break;
359
		case "&":
360
			return ".-...";
361
			break;
362
		case ":":
363
			return "---...";
364
			break;
365
		case ";":
366
			return "-.-.-.";
367
			break;
368
		case "=":
369
			return "-...-";
370
			break;
371
		case "+":
372
			return ".-.-.";
373
			break;
374
		case "-":
375
			return "-....-";
376
			break;
377
		case "_":
378
			return "..--.-";
379
			break;
380
		case "$":
381
			return "...-..-";
382
			break;
383
		case "@":
384
			return ".--.-.";
385
			break;
386
		case '"':
387
			return ".-..-.";
388
			break;
389
		default:
390
			return "";
391
			break;
392
	}
393
}
394

    
395
?>
(32-32/67)