Project

General

Profile

Download (7.72 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	led.inc
4

    
5
	LED control library that wraps around the functionality of led(4)
6

    
7
	part of pfSense (https://www.pfsense.org)
8
	Copyright (c) 2009-2016 Electric Sheep Fencing, LLC.
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in
19
	   the documentation and/or other materials provided with the
20
	   distribution.
21

    
22
	3. All advertising materials mentioning features or use of this software
23
	   must display the following acknowledgment:
24
	   "This product includes software developed by the pfSense Project
25
	   for use in the pfSense® software distribution. (http://www.pfsense.org/).
26

    
27
	4. The names "pfSense" and "pfSense Project" must not be used to
28
	   endorse or promote products derived from this software without
29
	   prior written permission. For written permission, please contact
30
	   coreteam@pfsense.org.
31

    
32
	5. Products derived from this software may not be called "pfSense"
33
	   nor may "pfSense" appear in their names without prior written
34
	   permission of the Electric Sheep Fencing, LLC.
35

    
36
	6. Redistributions of any form whatsoever must retain the following
37
	   acknowledgment:
38

    
39
	"This product includes software developed by the pfSense Project
40
	for use in the pfSense software distribution (http://www.pfsense.org/).
41

    
42
	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
	OF THE POSSIBILITY OF SUCH DAMAGE.
54
 */
55

    
56
$led_root = "/dev/led/led";
57

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
397
?>
(30-30/65)