Opens a LED device @param dev device name @see list @webref LED @webBrief Opens a LED device
(String dev)
| 63 | * @webBrief Opens a LED device |
| 64 | */ |
| 65 | public LED(String dev) { |
| 66 | NativeInterface.loadLibrary(); |
| 67 | this.dev = dev; |
| 68 | |
| 69 | if (NativeInterface.isSimulated()) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // read maximum brightness |
| 74 | try { |
| 75 | Path path = Paths.get("/sys/class/leds/" + dev + "/max_brightness"); |
| 76 | String tmp = new String(Files.readAllBytes(path)); |
| 77 | maxBrightness = Integer.parseInt(tmp.trim()); |
| 78 | } catch (Exception e) { |
| 79 | System.err.println(e.getMessage()); |
| 80 | throw new RuntimeException("Unable to read maximum brightness"); |
| 81 | } |
| 82 | |
| 83 | // read current trigger setting to be able to restore it later |
| 84 | try { |
| 85 | Path path = Paths.get("/sys/class/leds/" + dev + "/trigger"); |
| 86 | String tmp = new String(Files.readAllBytes(path)); |
| 87 | int start = tmp.indexOf('['); |
| 88 | int end = tmp.indexOf(']', start); |
| 89 | if (start != -1 && end != -1) { |
| 90 | prevTrigger = tmp.substring(start+1, end); |
| 91 | } |
| 92 | } catch (Exception e) { |
| 93 | System.err.println(e.getMessage()); |
| 94 | throw new RuntimeException("Unable to read trigger setting"); |
| 95 | } |
| 96 | |
| 97 | // read current brightness to be able to restore it later |
| 98 | try { |
| 99 | Path path = Paths.get("/sys/class/leds/" + dev + "/brightness"); |
| 100 | String tmp = new String(Files.readAllBytes(path)); |
| 101 | prevBrightness = Integer.parseInt(tmp.trim()); |
| 102 | } catch (Exception e) { |
| 103 | System.err.println(e.getMessage()); |
| 104 | throw new RuntimeException("Unable to read current brightness"); |
| 105 | } |
| 106 | |
| 107 | // disable trigger |
| 108 | String fn = "/sys/class/leds/" + dev + "/trigger"; |
| 109 | int ret = NativeInterface.writeFile(fn, "none"); |
| 110 | if (ret < 0) { |
| 111 | if (ret == -13) { // EACCES |
| 112 | System.err.println("You might need to install a custom udev rule to allow regular users to modify /sys/class/leds/*."); |
| 113 | } |
| 114 | throw new RuntimeException(NativeInterface.getError(ret)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
nothing calls this directly
no test coverage detected