Returns the value of an input pin, which is either GPIO.HIGH (1) or GPIO.LOW (0) You need to set the pin to input by calling pinMode() before calling this function. @param pin GPIO pin @return GPIO.HIGH (1) or GPIO.LOW (0) @see pinMode @see digit
(int pin)
| 197 | * @webBrief Returns the value of an input pin |
| 198 | */ |
| 199 | public static int digitalRead(int pin) { |
| 200 | checkValidPin(pin); |
| 201 | |
| 202 | if (NativeInterface.isSimulated()) { |
| 203 | return LOW; |
| 204 | } |
| 205 | |
| 206 | String fn = String.format("/sys/class/gpio/gpio%d/value", pin); |
| 207 | byte in[] = new byte[2]; |
| 208 | int ret = NativeInterface.readFile(fn, in); |
| 209 | if (ret < 0) { |
| 210 | throw new RuntimeException(NativeInterface.getError(ret)); |
| 211 | } else if (1 <= ret && in[0] == '0') { |
| 212 | return LOW; |
| 213 | } else if (1 <= ret && in[0] == '1') { |
| 214 | return HIGH; |
| 215 | } else { |
| 216 | System.err.print("Read " + ret + " bytes"); |
| 217 | if (0 < ret) { |
| 218 | System.err.format(", first byte is 0x%02x" + in[0]); |
| 219 | } |
| 220 | System.err.println(); |
| 221 | throw new RuntimeException("Unexpected value"); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** |
nothing calls this directly
no test coverage detected