| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a6e38e9 commit c2cd9df
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -177,6 +177,10 @@ protected static void checkValidPin(int pin) { | |||
| 177 | 177 | public static int digitalRead(int pin) { | |
| 178 | 178 | checkValidPin(pin); | |
| 179 | 179 | ||
| 180 | + if (NativeInterface.isSimulated()) { | ||
| 181 | + return LOW; | ||
| 182 | + } | ||
| 183 | + | ||
| 180 | 184 | String fn = String.format("/sys/class/gpio/gpio%d/value", pin); | |
| 181 | 185 | byte in[] = new byte[2]; | |
| 182 | 186 | int ret = NativeInterface.readFile(fn, in); | |
@@ -222,6 +226,10 @@ public static void digitalWrite(int pin, int value) { | |||
| 222 | 226 | throw new IllegalArgumentException("Illegal value"); | |
| 223 | 227 | } | |
| 224 | 228 | ||
| 229 | + if (NativeInterface.isSimulated()) { | ||
| 230 | + return; | ||
| 231 | + } | ||
| 232 | + | ||
| 225 | 233 | String fn = String.format("/sys/class/gpio/gpio%d/value", pin); | |
| 226 | 234 | int ret = NativeInterface.writeFile(fn, out); | |
| 227 | 235 | if (ret < 0) { | |
@@ -278,6 +286,10 @@ protected static void enableInterrupt(int pin, int mode) { | |||
| 278 | 286 | throw new IllegalArgumentException("Unknown mode"); | |
| 279 | 287 | } | |
| 280 | 288 | ||
| 289 | + if (NativeInterface.isSimulated()) { | ||
| 290 | + return; | ||
| 291 | + } | ||
| 292 | + | ||
| 281 | 293 | String fn = String.format("/sys/class/gpio/gpio%d/edge", pin); | |
| 282 | 294 | int ret = NativeInterface.writeFile(fn, out); | |
| 283 | 295 | if (ret < 0) { | |
@@ -325,6 +337,10 @@ public static void noInterrupts() { | |||
| 325 | 337 | public static void pinMode(int pin, int mode) { | |
| 326 | 338 | checkValidPin(pin); | |
| 327 | 339 | ||
| 340 | + if (NativeInterface.isSimulated()) { | ||
| 341 | + return; | ||
| 342 | + } | ||
| 343 | + | ||
| 328 | 344 | // export pin through sysfs | |
| 329 | 345 | String fn = "/sys/class/gpio/export"; | |
| 330 | 346 | int ret = NativeInterface.writeFile(fn, Integer.toString(pin)); | |
@@ -409,6 +425,10 @@ public static void releaseInterrupt(int pin) { | |||
| 409 | 425 | public static void releasePin(int pin) { | |
| 410 | 426 | checkValidPin(pin); | |
| 411 | 427 | ||
| 428 | + if (NativeInterface.isSimulated()) { | ||
| 429 | + return; | ||
| 430 | + } | ||
| 431 | + | ||
| 412 | 432 | String fn = "/sys/class/gpio/unexport"; | |
| 413 | 433 | int ret = NativeInterface.writeFile(fn, Integer.toString(pin)); | |
| 414 | 434 | if (ret < 0) { | |
@@ -451,6 +471,14 @@ public static boolean waitForInterrupt(int pin, int mode, int timeout) { | |||
| 451 | 471 | protected static boolean waitForInterrupt(int pin, int timeout) { | |
| 452 | 472 | checkValidPin(pin); | |
| 453 | 473 | ||
| 474 | + if (NativeInterface.isSimulated()) { | ||
| 475 | + // pretend the interrupt happens after 200ms | ||
| 476 | + try { | ||
| 477 | + Thread.sleep(200); | ||
| 478 | + } catch (InterruptedException e) {} | ||
| 479 | + return true; | ||
| 480 | + } | ||
| 481 | + | ||
| 454 | 482 | String fn = String.format("/sys/class/gpio/gpio%d/value", pin); | |
| 455 | 483 | int ret = NativeInterface.pollDevice(fn, timeout); | |
| 456 | 484 | if (ret < 0) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,6 +50,11 @@ public class I2C { | |||
| 50 | 50 | public I2C(String dev) { | |
| 51 | 51 | NativeInterface.loadLibrary(); | |
| 52 | 52 | this.dev = dev; | |
| 53 | + | ||
| 54 | + if (NativeInterface.isSimulated()) { | ||
| 55 | + return; | ||
| 56 | + } | ||
| 57 | + | ||
| 53 | 58 | handle = NativeInterface.openDevice("/dev/" + dev); | |
| 54 | 59 | if (handle < 0) { | |
| 55 | 60 | throw new RuntimeException(NativeInterface.getError(handle)); | |
@@ -81,6 +86,10 @@ public void beginTransmission(int slave) { | |||
| 81 | 86 | * @webref | |
| 82 | 87 | */ | |
| 83 | 88 | public void close() { | |
| 89 | + if (NativeInterface.isSimulated()) { | ||
| 90 | + return; | ||
| 91 | + } | ||
| 92 | + | ||
| 84 | 93 | NativeInterface.closeDevice(handle); | |
| 85 | 94 | handle = 0; | |
| 86 | 95 | } | |
@@ -107,6 +116,10 @@ public void endTransmission() { | |||
| 107 | 116 | return; | |
| 108 | 117 | } | |
| 109 | 118 | ||
| 119 | + if (NativeInterface.isSimulated()) { | ||
| 120 | + return; | ||
| 121 | + } | ||
| 122 | + | ||
| 110 | 123 | // implement these flags if needed: https://github.com/raspberrypi/linux/blob/rpi-patches/Documentation/i2c/i2c-protocol | |
| 111 | 124 | int ret = NativeInterface.transferI2c(handle, slave, out, null); | |
| 112 | 125 | transmitting = false; | |
@@ -126,6 +139,11 @@ public void endTransmission() { | |||
| 126 | 139 | * @webref | |
| 127 | 140 | */ | |
| 128 | 141 | public static String[] list() { | |
| 142 | + if (NativeInterface.isSimulated()) { | ||
| 143 | + // as on the Raspberry Pi | ||
| 144 | + return new String[]{ "i2c-1" }; | ||
| 145 | + } | ||
| 146 | + | ||
| 129 | 147 | ArrayList<String> devs = new ArrayList<String>(); | |
| 130 | 148 | File dir = new File("/dev"); | |
| 131 | 149 | File[] files = dir.listFiles(); | |
@@ -159,6 +177,10 @@ public byte[] read(int len) { | |||
| 159 | 177 | ||
| 160 | 178 | byte[] in = new byte[len]; | |
| 161 | 179 | ||
| 180 | + if (NativeInterface.isSimulated()) { | ||
| 181 | + return in; | ||
| 182 | + } | ||
| 183 | + | ||
| 162 | 184 | int ret = NativeInterface.transferI2c(handle, slave, out, in); | |
| 163 | 185 | transmitting = false; | |
| 164 | 186 | out = null; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,6 +53,10 @@ public LED(String dev) { | |||
| 53 | 53 | NativeInterface.loadLibrary(); | |
| 54 | 54 | this.dev = dev; | |
| 55 | 55 | ||
| 56 | + if (NativeInterface.isSimulated()) { | ||
| 57 | + return; | ||
| 58 | + } | ||
| 59 | + | ||
| 56 | 60 | // read maximum brightness | |
| 57 | 61 | try { | |
| 58 | 62 | Path path = Paths.get("/sys/class/leds/" + dev + "/max_brightness"); | |
@@ -105,11 +109,16 @@ public LED(String dev) { | |||
| 105 | 109 | * @webref | |
| 106 | 110 | */ | |
| 107 | 111 | public void brightness(float bright) { | |
| 108 | - String fn = "/sys/class/leds/" + dev + "/brightness"; | ||
| 109 | 112 | if (bright < 0.0 || 1.0 < bright) { | |
| 110 | 113 | System.err.println("Brightness must be between 0.0 and 1.0."); | |
| 111 | 114 | throw new IllegalArgumentException("Illegal argument"); | |
| 112 | 115 | } | |
| 116 | + | ||
| 117 | + if (NativeInterface.isSimulated()) { | ||
| 118 | + return; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + String fn = "/sys/class/leds/" + dev + "/brightness"; | ||
| 113 | 122 | int ret = NativeInterface.writeFile(fn, Integer.toString((int)(bright * maxBrightness))); | |
| 114 | 123 | if (ret < 0) { | |
| 115 | 124 | throw new RuntimeException(fn + ": " + NativeInterface.getError(ret)); | |
@@ -122,6 +131,10 @@ public void brightness(float bright) { | |||
| 122 | 131 | * @webref | |
| 123 | 132 | */ | |
| 124 | 133 | public void close() { | |
| 134 | + if (NativeInterface.isSimulated()) { | ||
| 135 | + return; | ||
| 136 | + } | ||
| 137 | + | ||
| 125 | 138 | // restore previous settings | |
| 126 | 139 | String fn = "/sys/class/leds/" + dev + "/brightness"; | |
| 127 | 140 | int ret = NativeInterface.writeFile(fn, Integer.toString(prevBrightness)); | |
@@ -143,6 +156,11 @@ public void close() { | |||
| 143 | 156 | * @webref | |
| 144 | 157 | */ | |
| 145 | 158 | public static String[] list() { | |
| 159 | + if (NativeInterface.isSimulated()) { | ||
| 160 | + // as on the Raspberry Pi | ||
| 161 | + return new String[]{ "led0", "led1" }; | ||
| 162 | + } | ||
| 163 | + | ||
| 146 | 164 | ArrayList<String> devs = new ArrayList<String>(); | |
| 147 | 165 | File dir = new File("/sys/class/leds"); | |
| 148 | 166 | File[] files = dir.listFiles(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,17 +26,28 @@ | |||
| 26 | 26 | public class NativeInterface { | |
| 27 | 27 | ||
| 28 | 28 | protected static boolean loaded = false; | |
| 29 | + protected static boolean alwaysSimulate = false; | ||
| 29 | 30 | ||
| 30 | 31 | public static void loadLibrary() { | |
| 31 | 32 | if (!loaded) { | |
| 32 | - if (!"Linux".equals(System.getProperty("os.name"))) { | ||
| 33 | - throw new RuntimeException("The Processing I/O library is only supported on Linux"); | ||
| 33 | + if (isSimulated()) { | ||
| 34 | + System.err.println("The Processing I/O library is not supported on this platform. Instead of values from actual hardware ports, your sketch will only receive stand-in values that allow you to test the remainder of its functionality."); | ||
| 35 | + } else { | ||
| 36 | + System.loadLibrary("processing-io"); | ||
| 34 | 37 | } | |
| 35 | - System.loadLibrary("processing-io"); | ||
| 36 | 38 | loaded = true; | |
| 37 | 39 | } | |
| 38 | 40 | } | |
| 39 | 41 | ||
| 42 | + public static void alwaysSimulate() { | ||
| 43 | + alwaysSimulate = true; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + public static boolean isSimulated() { | ||
| 47 | + return alwaysSimulate || | ||
| 48 | + !"Linux".equals(System.getProperty("os.name")); | ||
| 49 | + } | ||
| 50 | + | ||
| 40 | 51 | ||
| 41 | 52 | public static native int openDevice(String fn); | |
| 42 | 53 | public static native String getError(int errno); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,6 +57,10 @@ public PWM(String channel) { | |||
| 57 | 57 | chip = channel.substring(0, pos); | |
| 58 | 58 | this.channel = Integer.parseInt(channel.substring(pos+4)); | |
| 59 | 59 | ||
| 60 | + if (NativeInterface.isSimulated()) { | ||
| 61 | + return; | ||
| 62 | + } | ||
| 63 | + | ||
| 60 | 64 | // export channel through sysfs | |
| 61 | 65 | String fn = "/sys/class/pwm/" + chip + "/export"; | |
| 62 | 66 | int ret = NativeInterface.writeFile(fn, Integer.toString(this.channel)); | |
@@ -89,6 +93,10 @@ public PWM(String channel) { | |||
| 89 | 93 | * @webref | |
| 90 | 94 | */ | |
| 91 | 95 | public void clear() { | |
| 96 | + if (NativeInterface.isSimulated()) { | ||
| 97 | + return; | ||
| 98 | + } | ||
| 99 | + | ||
| 92 | 100 | String fn = String.format("/sys/class/pwm/%s/pwm%d/enable", chip, channel); | |
| 93 | 101 | int ret = NativeInterface.writeFile(fn, "0"); | |
| 94 | 102 | if (ret < 0) { | |
@@ -102,6 +110,10 @@ public void clear() { | |||
| 102 | 110 | * @webref | |
| 103 | 111 | */ | |
| 104 | 112 | public void close() { | |
| 113 | + if (NativeInterface.isSimulated()) { | ||
| 114 | + return; | ||
| 115 | + } | ||
| 116 | + | ||
| 105 | 117 | // XXX: implicit clear()? | |
| 106 | 118 | // XXX: also check GPIO | |
| 107 | 119 | ||
@@ -124,6 +136,10 @@ public void close() { | |||
| 124 | 136 | * @webref | |
| 125 | 137 | */ | |
| 126 | 138 | public static String[] list() { | |
| 139 | + if (NativeInterface.isSimulated()) { | ||
| 140 | + return new String[]{ "pwmchip0/pwm0", "pwmchip0/pwm1" }; | ||
| 141 | + } | ||
| 142 | + | ||
| 127 | 143 | ArrayList<String> devs = new ArrayList<String>(); | |
| 128 | 144 | File dir = new File("/sys/class/pwm"); | |
| 129 | 145 | File[] chips = dir.listFiles(); | |
@@ -155,6 +171,10 @@ public static String[] list() { | |||
| 155 | 171 | * @webref | |
| 156 | 172 | */ | |
| 157 | 173 | public void set(int period, float duty) { | |
| 174 | + if (NativeInterface.isSimulated()) { | ||
| 175 | + return; | ||
| 176 | + } | ||
| 177 | + | ||
| 158 | 178 | // set period | |
| 159 | 179 | String fn = fn = String.format("/sys/class/pwm/%s/pwm%d/period", chip, channel); | |
| 160 | 180 | // convert to nanoseconds | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -78,6 +78,11 @@ public class SPI { | |||
| 78 | 78 | public SPI(String dev) { | |
| 79 | 79 | NativeInterface.loadLibrary(); | |
| 80 | 80 | this.dev = dev; | |
| 81 | + | ||
| 82 | + if (NativeInterface.isSimulated()) { | ||
| 83 | + return; | ||
| 84 | + } | ||
| 85 | + | ||
| 81 | 86 | handle = NativeInterface.openDevice("/dev/" + dev); | |
| 82 | 87 | if (handle < 0) { | |
| 83 | 88 | throw new RuntimeException(NativeInterface.getError(handle)); | |
@@ -90,6 +95,10 @@ public SPI(String dev) { | |||
| 90 | 95 | * @webref | |
| 91 | 96 | */ | |
| 92 | 97 | public void close() { | |
| 98 | + if (NativeInterface.isSimulated()) { | ||
| 99 | + return; | ||
| 100 | + } | ||
| 101 | + | ||
| 93 | 102 | NativeInterface.closeDevice(handle); | |
| 94 | 103 | handle = 0; | |
| 95 | 104 | } | |
@@ -110,6 +119,11 @@ protected void finalize() throws Throwable { | |||
| 110 | 119 | * @webref | |
| 111 | 120 | */ | |
| 112 | 121 | public static String[] list() { | |
| 122 | + if (NativeInterface.isSimulated()) { | ||
| 123 | + // as on the Raspberry Pi | ||
| 124 | + return new String[]{ "spidev0.0", "spidev0.1" }; | ||
| 125 | + } | ||
| 126 | + | ||
| 113 | 127 | ArrayList<String> devs = new ArrayList<String>(); | |
| 114 | 128 | File dir = new File("/dev"); | |
| 115 | 129 | File[] files = dir.listFiles(); | |
@@ -148,6 +162,10 @@ public void settings(int maxSpeed, int dataOrder, int mode) { | |||
| 148 | 162 | * @webref | |
| 149 | 163 | */ | |
| 150 | 164 | public byte[] transfer(byte[] out) { | |
| 165 | + if (NativeInterface.isSimulated()) { | ||
| 166 | + return new byte[out.length]; | ||
| 167 | + } | ||
| 168 | + | ||
| 151 | 169 | // track the current setting per device across multiple instances | |
| 152 | 170 | String curSettings = maxSpeed + "-" + dataOrder + "-" + mode; | |
| 153 | 171 | if (!curSettings.equals(settings.get(dev))) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments