led.inc 5.99 KB
Newer Older
1 2
<?php

Ad Schellevis's avatar
Ad Schellevis committed
3 4 5 6 7 8 9 10 11 12 13
/*
 * (C) 2009 Jim Pingle <jimp@pfsense.org>
 *
 * LED control library that wraps around the functionality of led(4)
 */

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

/*
 * Send the control string to an LED
 */
14 15 16 17 18 19 20 21
function led_ctl($led, $str)
{
    global $led_root;
    if (led_exists($led)) {
        exec("/bin/echo " . escapeshellarg($str) . " > {$led_root}{$led}");
        return true;
    }
    return false;
Ad Schellevis's avatar
Ad Schellevis committed
22 23 24 25 26 27 28
}

/*
 * Blink an LED in a specific pattern
 * Letters A-J are on from 1/10s to 1s
 * Letters a-j are off from 1/10s to 1s
 */
29 30 31 32 33
function led_pattern($led, $pattern, $repeat=true)
{
    /*  End with a . to stop after one iteration. */
    $end = $repeat ? "" : ".";
    return led_ctl($led, "s{$pattern}{$end}");
Ad Schellevis's avatar
Ad Schellevis committed
34 35 36 37 38
}

/*
 * Encode a text message into morse code, and send it to an LED
 */
39 40 41
function led_morse($led, $message)
{
    return led_ctl($led, "m" . str_to_morse($message));
Ad Schellevis's avatar
Ad Schellevis committed
42 43 44 45 46 47
}


/*
 * Turn an LED on
 */
48 49 50
function led_on($led)
{
    led_ctl($led, "1");
Ad Schellevis's avatar
Ad Schellevis committed
51 52 53 54 55
}

/*
 * Turn an LED off
 */
56 57 58
function led_off($led)
{
    led_ctl($led, "0");
Ad Schellevis's avatar
Ad Schellevis committed
59 60 61 62 63 64
}

/*
 * Find the number of LEDs present on the system.
 */
function led_count() {
65 66 67 68 69 70 71 72
    global $led_root;
    $count = 0;
    $leds = array();
    if (is_dir(dirname($led_root))) {
        $leds = glob("{$led_root}*");
        $count = count($leds);
    }
    return $count;
Ad Schellevis's avatar
Ad Schellevis committed
73 74 75 76 77 78
}

/*
 * Test to see if a given LED exists.
 */
function led_exists($led) {
79 80 81 82 83
    global $led_root;
    if (!is_numeric($led)) {
        return false;
    }
    return file_exists("{$led_root}{$led}");
Ad Schellevis's avatar
Ad Schellevis committed
84 85 86 87 88
}

/*
 * Sweep across three LEDs in a K.I.T.T.-like way.
 */
89 90 91 92 93
function led_kitt()
{
    led_pattern(1, 'AaaaaA');
    led_pattern(2, 'aAaaAa');
    led_pattern(3, 'aaAAaa');
Ad Schellevis's avatar
Ad Schellevis committed
94 95 96 97 98
}

/*
 * Custom pattern for assigning interfaces
 */
99 100 101 102 103
function led_assigninterfaces()
{
    led_pattern(1, 'AaaAaaaaaaaaaaaa');
    led_pattern(2, 'aaaaaAaaAaaaaaaa');
    led_pattern(3, 'aaaaaaaaaaAaaAaa');
Ad Schellevis's avatar
Ad Schellevis committed
104 105 106 107 108
}

/*
 * Return the three LEDs to a standard setup (1=on, 2 and 3 = off)
 */
109 110 111 112 113
function led_normalize()
{
    led_on(1);
    led_off(2);
    led_off(3);
Ad Schellevis's avatar
Ad Schellevis committed
114 115 116 117 118 119
}

/*
 * Translate a string to morse code. Characters not known to have a
 * valid morse code representation will be ignored.
 */
120 121 122 123 124 125 126 127
function str_to_morse($string)
{
    $i = 0;
    $morsestring = "";
    while ($i < strlen($string)) {
        $morsestring .= char_to_morse(substr($string, $i++, 1)) . " ";
    }
    return $morsestring . "\n";
Ad Schellevis's avatar
Ad Schellevis committed
128 129 130 131 132 133
}

/*
 * Translate a single character to morse code. Characters not known
 * to have a valid morse code representation will be ignored.
 */
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
function char_to_morse($char)
{
    switch (strtoupper($char)) {
        case "A":
            return ".-";
            break;
        case "B":
            return "-...";
            break;
        case "C":
            return "-.-.";
            break;
        case "D":
            return "-..";
            break;
        case "E":
            return ".";
            break;
        case "F":
            return "..-.";
            break;
        case "G":
            return "--.";
            break;
        case "H":
            return "....";
            break;
        case "I":
            return "..";
            break;
        case "J":
            return ".---";
            break;
        case "K":
            return "-.-";
            break;
        case "L":
            return ".-..";
            break;
        case "M":
            return "--";
            break;
        case "N":
            return "-.";
            break;
        case "O":
            return "---";
            break;
        case "P":
            return ".--.";
            break;
        case "Q":
            return "--.-";
            break;
        case "R":
            return ".-.";
            break;
        case "S":
            return "...";
            break;
        case "T":
            return "-";
            break;
        case "U":
            return "..-";
            break;
        case "V":
            return "...-";
            break;
        case "W":
            return ".--";
            break;
        case "X":
            return "-..-";
            break;
        case "Y":
            return "-.--";
            break;
        case "Z":
            return "--..";
            break;
        case "0":
            return "-----";
            break;
        case "1":
            return ".----";
            break;
        case "2":
            return "..---";
            break;
        case "3":
            return "...--";
            break;
        case "4":
            return "....-";
            break;
        case "5":
            return ".....";
            break;
        case "6":
            return "-....";
            break;
        case "7":
            return "--...";
            break;
        case "8":
            return "---..";
            break;
        case "9":
            return "----.";
            break;
        case ".":
            return ".-.-.-";
            break;
        case ",":
            return "--..--";
            break;
        case "?":
            return "..--..";
            break;
        case "'":
            return ".----.";
            break;
        case "!":
            return "-.-.--";
            break;
        case "/":
            return "-..-.";
            break;
        case "(":
            return "-.--.";
            break;
        case ")":
            return "-.--.-";
            break;
        case "&":
            return ".-...";
            break;
        case ":":
            return "---...";
            break;
        case ";":
            return "-.-.-.";
            break;
        case "=":
            return "-...-";
            break;
        case "+":
            return ".-.-.";
            break;
        case "-":
            return "-....-";
            break;
        case "_":
            return "..--.-";
            break;
        case "$":
            return "...-..-";
            break;
        case "@":
            return ".--.-.";
            break;
        case '"':
            return ".-..-.";
            break;
        default:
            return "";
            break;
    }
Ad Schellevis's avatar
Ad Schellevis committed
303 304
}

305
?>