示例#1
0
 public void setShutdownOptions(GpioPinShutdown options, GpioPin... pin) {
   for (GpioPin p : pin) {
     if (!pins.contains(p)) {
       throw new GpioPinNotProvisionedException(p.getPin());
     }
     p.setShutdownOptions(options);
   }
 }
示例#2
0
 public void setShutdownOptions(Boolean unexport, PinState state, GpioPin... pin) {
   for (GpioPin p : pin) {
     if (!pins.contains(p)) {
       throw new GpioPinNotProvisionedException(p.getPin());
     }
     p.setShutdownOptions(unexport, state);
   }
 }
 private void unprovision(Pin pin) {
   for (GpioPin e : gpioContoller.getProvisionedPins()) {
     if (e.getPin().equals(pin)) {
       gpioContoller.unprovisionPin(e);
       break;
     }
   }
 }
示例#4
0
 @Override
 public void unexportAll() {
   // un-export all GPIO pins that are currently exported
   for (GpioPin pin : pins) {
     if (pin.isExported()) {
       pin.unexport();
     }
   }
 }
示例#5
0
 @Override
 public PinPullResistance getPullResistance(GpioPin pin) {
   // ensure the requested pin has been provisioned
   if (!pins.contains(pin)) {
     throw new GpioPinNotProvisionedException(pin.getPin());
   }
   // get pin pull resistance
   return pin.getPullResistance();
 }
示例#6
0
 @Override
 public PinMode getMode(GpioPin pin) {
   // ensure the requested pin has been provisioned
   if (!pins.contains(pin)) {
     throw new GpioPinNotProvisionedException(pin.getPin());
   }
   // return pin edge setting
   return pin.getMode();
 }
示例#7
0
 @Override
 public boolean isMode(PinMode mode, GpioPin... pin) {
   if (pin == null || pin.length == 0) {
     throw new IllegalArgumentException("Missing pin argument.");
   }
   for (GpioPin p : pin) {
     if (!p.isMode(mode)) {
       return false;
     }
   }
   return true;
 }
示例#8
0
 @Override
 public void export(PinMode mode, GpioPin... pin) {
   if (pin == null || pin.length == 0) {
     throw new IllegalArgumentException("Missing pin argument.");
   }
   for (GpioPin p : pin) {
     // ensure the requested pin has been provisioned
     if (!pins.contains(p)) {
       throw new GpioPinNotProvisionedException(p.getPin());
     }
     // export the pin
     p.export(mode);
   }
 }
示例#9
0
 @Override
 public void setPullResistance(PinPullResistance resistance, GpioPin... pin) {
   if (pin == null || pin.length == 0) {
     throw new IllegalArgumentException("Missing pin argument.");
   }
   for (GpioPin p : pin) {
     // ensure the requested pin has been provisioned
     if (!pins.contains(p)) {
       throw new GpioPinNotProvisionedException(p.getPin());
     }
     // set pin pull resistance
     p.setPullResistance(resistance);
   }
 }
示例#10
0
  /**
   * @param pin
   * @return
   *     <p>A value of 'true' is returned if the requested pin is exported.
   */
  @Override
  public boolean isExported(GpioPin... pin) {
    if (pin == null || pin.length == 0) {
      throw new IllegalArgumentException("Missing pin argument.");
    }
    for (GpioPin p : pin) {
      // ensure the requested pin has been provisioned
      if (!pins.contains(pin)) {
        throw new GpioPinNotProvisionedException(p.getPin());
      }
      if (!p.isExported()) {
        return false;
      }
    }

    // return the pin exported state
    return true;
  }
示例#11
0
  @Override
  public GpioPin provisionPin(GpioProvider provider, Pin pin, String name, PinMode mode) {
    // if an existing pin has been previously created, then throw an error
    for (GpioPin p : pins) {
      if (p.getProvider().equals(provider) && p.getPin().equals(pin)) {
        throw new GpioPinExistsException(pin);
      }
    }

    // create new GPIO pin instance
    GpioPin gpioPin = new GpioPinImpl(this, provider, pin);

    // set the gpio pin name
    if (name != null) {
      gpioPin.setName(name);
    }

    // export this pin
    gpioPin.export(mode);

    // add this new pin instance to the managed collection
    pins.add(gpioPin);

    // return new new pin instance
    return gpioPin;
  }
示例#12
0
  @Override
  public void unprovisionPin(GpioPin... pin) {
    if (pin == null || pin.length == 0) {
      throw new IllegalArgumentException("Missing pin argument.");
    }
    for (int index = (pin.length - 1); index >= 0; index--) {
      GpioPin p = pin[index];

      // ensure the requested pin has been provisioned
      if (!pins.contains(p)) {
        throw new GpioPinNotProvisionedException(p.getPin());
      }
      // remove all listeners and triggers
      if (p instanceof GpioPinInput) {
        ((GpioPinInput) p).removeAllListeners();
        ((GpioPinInput) p).removeAllTriggers();
      }

      // remove this pin instance from the managed collection
      pins.remove(p);
    }
  }
示例#13
0
  /**
   * This method can be called to forcefully shutdown all GPIO controller monitoring, listening, and
   * task threads/executors.
   */
  @Override
  public synchronized void shutdown() {
    // prevent reentrant invocation
    if (isShutdown()) return;

    // shutdown all executor services
    //
    // NOTE: we are not permitted to access the shutdown() method of the individual
    // executor services, we must perform the shutdown with the factory
    GpioFactory.getExecutorServiceFactory().shutdown();

    // shutdown explicit configured GPIO pins
    for (GpioPin pin : pins) {

      // perform a shutdown on the GPIO provider for this pin
      if (!pin.getProvider().isShutdown()) {
        pin.getProvider().shutdown();
      }

      // perform the shutdown options if configured for the pin
      GpioPinShutdown shutdownOptions = pin.getShutdownOptions();
      if (shutdownOptions != null) {
        // get shutdown option configuration
        PinState state = shutdownOptions.getState();
        PinMode mode = shutdownOptions.getMode();
        PinPullResistance resistance = shutdownOptions.getPullResistor();
        Boolean unexport = shutdownOptions.getUnexport();

        // perform shutdown actions
        if ((state != null) && (pin instanceof GpioPinDigitalOutput)) {
          ((GpioPinDigitalOutput) pin).setState(state);
        }
        if (resistance != null) {
          pin.setPullResistance(resistance);
        }
        if (mode != null) {
          pin.setMode(mode);
        }
        if (unexport != null && unexport == Boolean.TRUE) {
          pin.unexport();
        }
      }
    }

    // set is shutdown tracking variable
    isshutdown = true;
  }
示例#14
0
  private void output(Exchange exchange, Object value) {

    //
    PinMode mode = pin.getMode();
    log.debug("Mode > " + mode + " for " + pin);

    // Check mode
    switch (mode) {
      case DIGITAL_OUTPUT:
        Boolean outputBoolean =
            exchange.getContext().getTypeConverter().convertTo(Boolean.class, value);
        ((GpioPinDigitalOutput) pin).setState(outputBoolean);
        break;

      case ANALOG_OUTPUT:
        Double outputDouble =
            exchange.getContext().getTypeConverter().convertTo(Double.class, value);
        ((GpioPinAnalogOutput) pin).setValue(outputDouble);
        break;

      case PWM_OUTPUT:
        Integer outputInt =
            exchange.getContext().getTypeConverter().convertTo(Integer.class, value);
        ((GpioPinPwmOutput) pin).setPwm(outputInt);

        break;

      case ANALOG_INPUT:
      case DIGITAL_INPUT:
        log.error("Cannot output with INPUT PinMode");
        break;

      default:
        log.error("Any PinMode found");
        break;
    }
  }
示例#15
0
 @Override
 public void invoke(GpioPin pin, PinState state) {
   if (targetPin != null) {
     targetPin.setState(targetPinState);
   }
 }
示例#16
0
  /** Process the message */
  @Override
  public void process(Exchange exchange) throws Exception {
    if (log.isTraceEnabled()) {
      log.trace(exchange.toString());
    }

    GPIOAction messageAction =
        exchange.getIn().getHeader(Pi4jConstants.CAMEL_RBPI_PIN_ACTION, action, GPIOAction.class);

    if (messageAction == null) {
      log.trace("No action pick up body");
      this.output(exchange, exchange.getIn().getBody());
    } else {
      log.trace("action= {} ", action);
      switch (messageAction) {
        case TOGGLE:
          if (pin.getMode() == PinMode.DIGITAL_OUTPUT) {
            ((GpioPinDigitalOutput) pin).toggle();
          }
          break;

        case LOW:
          if (pin.getMode() == PinMode.DIGITAL_OUTPUT) {
            ((GpioPinDigitalOutput) pin).low();
          }
          break;

        case HIGH:
          if (pin.getMode() == PinMode.DIGITAL_OUTPUT) {
            ((GpioPinDigitalOutput) pin).high();
          }
          break;

        case BLINK:
          if (pin.getMode() == PinMode.DIGITAL_OUTPUT) {

            pool.submit(
                new Runnable() {

                  @Override
                  public void run() {
                    try {
                      Thread.sleep(getEndpoint().getDelay());
                      ((GpioPinDigitalOutput) pin).toggle();
                      Thread.sleep(getEndpoint().getDuration());
                      ((GpioPinDigitalOutput) pin).toggle();
                    } catch (InterruptedException e) {
                      log.error("Thread interruption into BLINK sequence", e);
                    }
                  }
                });
          }

          break;

        default:
          log.error("Any action set found");
          break;
      }
    }
  }