MCPcopy Index your code
hub / github.com/benfry/processing4 / digitalRead

Method digitalRead

java/libraries/io/src/processing/io/GPIO.java:199–223  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 /**

Callers

nothing calls this directly

Calls 7

checkValidPinMethod · 0.95
isSimulatedMethod · 0.95
readFileMethod · 0.95
getErrorMethod · 0.95
formatMethod · 0.65
printMethod · 0.65
printlnMethod · 0.45

Tested by

no test coverage detected