Returns the value of an input pin @param pin GPIO pin @return GPIO.HIGH (1) or GPIO.LOW (0) @see pinMode @see digitalWrite @webref
(int pin)
| 175 | * @webref |
| 176 | */ |
| 177 | public static int digitalRead(int pin) { |
| 178 | checkValidPin(pin); |
| 179 | |
| 180 | if (NativeInterface.isSimulated()) { |
| 181 | return LOW; |
| 182 | } |
| 183 | |
| 184 | String fn = String.format("/sys/class/gpio/gpio%d/value", pin); |
| 185 | byte in[] = new byte[2]; |
| 186 | int ret = NativeInterface.readFile(fn, in); |
| 187 | if (ret < 0) { |
| 188 | throw new RuntimeException(NativeInterface.getError(ret)); |
| 189 | } else if (1 <= ret && in[0] == '0') { |
| 190 | return LOW; |
| 191 | } else if (1 <= ret && in[0] == '1') { |
| 192 | return HIGH; |
| 193 | } else { |
| 194 | System.err.print("Read " + ret + " bytes"); |
| 195 | if (0 < ret) { |
| 196 | System.err.format(", first byte is 0x%02x" + in[0]); |
| 197 | } |
| 198 | System.err.println(); |
| 199 | throw new RuntimeException("Unexpected value"); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
nothing calls this directly
no test coverage detected