Project

General

Profile

Download (6.52 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * led.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2009-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
$led_root = "/dev/led/led";
25

    
26
/*
27
 * Send the control string to an LED
28
 */
29
function led_ctl($led, $str) {
30
	global $led_root;
31
	if (led_exists($led)) {
32
		exec("/bin/echo " . escapeshellarg($str) . " > {$led_root}{$led}");
33
		return true;
34
	}
35
	return false;
36
}
37

    
38
/*
39
 * Blink an LED at set speed from 1-9 (1=Very Fast, 9=Very Slow)
40
 */
41
function led_blink($led, $speed = 0) {
42
	switch ($speed) {
43
		case "reallyfast":
44
		case "veryfast":
45
			$speed = 1;
46
			break;
47
		case "fast":
48
			$speed = 3;
49
			break;
50
		case "medium":
51
			$speed = 5;
52
			break;
53
		case "slow":
54
			$speed = 7;
55
			break;
56
		case "reallyslow":
57
		case "veryslow":
58
			$speed = 9;
59
			break;
60
	}
61
	if (is_numeric($speed) && ($speed > 0) && ($speed < 10)) {
62
		return led_ctl($led, "f{$speed}");
63
	}
64
	return false;
65
}
66

    
67
/*
68
 * Blink an LED in a specific pattern
69
 * Letters A-J are on from 1/10s to 1s
70
 * Letters a-j are off from 1/10s to 1s
71
 */
72
function led_pattern($led, $pattern, $repeat = true) {
73
	/*  End with a . to stop after one iteration. */
74
	$end = $repeat ? "" : ".";
75
	return led_ctl($led, "s{$pattern}{$end}");
76
}
77

    
78
/*
79
 * Encode a text message into morse code, and send it to an LED
80
 */
81
function led_morse($led, $message) {
82
	return led_ctl($led, "m" . str_to_morse($message));
83
}
84

    
85
/*
86
 * Blink digits out on LED at 1/10s intervals
87
 * e.g 1=1 blink, 8=8 blinks
88
 * 0 is 10 pulses.
89
 * One second pause between digits.
90
 */
91
function led_digit($led, $digitstring) {
92
	$i = 0;
93
	$dstring = "d";
94
	while ($i < strlen($digitstring)) {
95
		$thisdigit = substr($digitstring, $i++, 1);
96
		if (is_numeric($thisdigit)) {
97
			$dstring .= $thisdigit;
98
		}
99
	}
100
	led_ctl($led, $dstring);
101
}
102

    
103
/*
104
 * Turn an LED on
105
 */
106
function led_on($led) {
107
	led_ctl($led, "1");
108
}
109

    
110
/*
111
 * Turn an LED off
112
 */
113
function led_off($led) {
114
	led_ctl($led, "0");
115
}
116

    
117
/*
118
 * Find the number of LEDs present on the system.
119
 */
120
function led_count() {
121
	global $led_root;
122
	$count = 0;
123
	$leds = array();
124
	if (is_dir(dirname($led_root))) {
125
		$leds = glob("{$led_root}*");
126
		$count = count($leds);
127
	}
128
	return $count;
129
}
130

    
131
/*
132
 * Test to see if a given LED exists.
133
 */
134
function led_exists($led) {
135
	global $led_root;
136
	if (!is_numeric($led)) {
137
		return false;
138
	}
139
	return file_exists("{$led_root}{$led}");
140
}
141

    
142
/*
143
 * Sweep across three LEDs in a K.I.T.T.-like way.
144
 */
145
function led_kitt() {
146
	if (led_count() != 3) {
147
		/* Wrong LED count for this to work, skip. */
148
		return;
149
	}
150
	led_pattern(1, 'AaaaaA');
151
	led_pattern(2, 'aAaaAa');
152
	led_pattern(3, 'aaAAaa');
153
}
154

    
155
/*
156
 * Custom pattern for assigning interfaces
157
 */
158
function led_assigninterfaces() {
159
	if (led_count() != 3) {
160
		/* Wrong LED count for this to work, skip. */
161
		return;
162
	}
163
	led_pattern(1, 'AaaAaaaaaaaaaaaa');
164
	led_pattern(2, 'aaaaaAaaAaaaaaaa');
165
	led_pattern(3, 'aaaaaaaaaaAaaAaa');
166
}
167

    
168
/*
169
 * Return the three LEDs to a standard setup (1=on, 2 and 3 = off)
170
 */
171
function led_normalize() {
172
	if (led_count() != 3) {
173
		/* Wrong LED count for this to work, skip. */
174
		return;
175
	}
176
	led_on(1);
177
	led_off(2);
178
	led_off(3);
179
}
180

    
181
/*
182
 * Shut off ALL LEDs.
183
 */
184
function led_alloff() {
185
	if (led_count() != 3) {
186
		/* Wrong LED count for this to work, skip. */
187
		return;
188
	}
189
	led_off(1);
190
	led_off(2);
191
	led_off(3);
192
}
193

    
194
/*
195
 * Translate a string to morse code. Characters not known to have a
196
 * valid morse code representation will be ignored.
197
 */
198
function str_to_morse($string) {
199
	$i = 0;
200
	$morsestring = "";
201
	while ($i < strlen($string)) {
202
		$morsestring .= char_to_morse(substr($string, $i++, 1)) . " ";
203
	}
204
	return $morsestring . "\n";
205
}
206

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

    
381
?>
(28-28/61)