public DigitalMotor(GpioDeviceFactoryInterface deviceFactory, int forwardGpio, int backwardGpio) throws RuntimeIOException { forward = new DigitalOutputDevice(deviceFactory, forwardGpio, true, false); backward = new DigitalOutputDevice(deviceFactory, backwardGpio, true, false); }
/** * Turn the device on or off. * * @param on * New on/off value. * @throws RuntimeIOException * If an I/O error occurred. */ public void setOn(boolean on) throws RuntimeIOException { stopOnOffLoop(); setValueUnsafe(activeHigh & on); }
/** * Toggle the device on-off. * * @param onTime * On time in seconds. * @param offTime * Off time in seconds. * @param n * Number of iterations. Set to <0 to blink indefinitely. * @param background * If true start a background thread to control the blink and * return immediately. If false, only return once the blink * iterations have finished. * @param stopAction * Action to perform when the loop finishes * @throws RuntimeIOException * If an I/O error occurs */ public void onOffLoop(float onTime, float offTime, int n, boolean background, Action stopAction) throws RuntimeIOException { stopOnOffLoop(); if (background) { DioZeroScheduler.getDaemonInstance().execute(() -> onOffLoop(onTime, offTime, n, stopAction)); } else { onOffLoop(onTime, offTime, n, stopAction); } } }
@Override public void setValue(float value) { setValueUnsafe(value != 0); }
private void onOffLoop(float onTime, float offTime, int n, Action stopAction) throws RuntimeIOException { running = true; if (n > 0) { for (int i = 0; i < n && running; i++) { onOff(onTime, offTime); } } else if (n == INFINITE_ITERATIONS) { while (running) { onOff(onTime, offTime); } } if (stopAction != null) { stopAction.action(); } }
/** * Turn on the device. * * @throws RuntimeIOException * If an I/O error occurred. */ public void on() throws RuntimeIOException { stopOnOffLoop(); setValueUnsafe(activeHigh); }
@Override public void close() { Logger.debug("close()"); if (motorForwardControlPin != null) { try { motorForwardControlPin.close(); } catch (Exception e) { } } if (motorBackwardControlPin != null) { try { motorBackwardControlPin.close(); } catch (Exception e) { } } if (motorPwmControl != null) { try { motorPwmControl.close(); } catch (Exception e) { } } }
private void onOff(float onTime, float offTime) throws RuntimeIOException { if (!running) { return; } setValueUnsafe(activeHigh); SleepUtil.sleepSeconds(onTime); if (!running) { return; } setValueUnsafe(!activeHigh); SleepUtil.sleepSeconds(offTime); }
/** * Set the colour to be sampled. * f S2 S3 Photodiode * 0 L L Red * 1 H H Green * 2 L H Blue * 3 H L Clear (no filter) * @param f Filter */ public void setFilter(Filter f) { switch (f) { case RED: s2.off(); s3.off(); break; case GREEN: s2.on(); s3.on(); break; case BLUE: s2.off(); s3.on(); break; default: // Clear s2.on(); s3.off(); } }
/** * Turn off the device. * * @throws RuntimeIOException * If an I/O error occurred. */ public void off() throws RuntimeIOException { stopOnOffLoop(); setValueUnsafe(!activeHigh); }