Package org.openhab.io.gpio

Examples of org.openhab.io.gpio.GPIOPin


    if (gpio != null) {
      try {
        if (registryLock.readLock().tryLock(REGISTRYLOCK_TIMEOUT, REGISTRYLOCK_TIMEOUT_UNITS)) {
          try {
            GPIOPin gpioPin = (GPIOPin) registry.get(itemName);

            if (gpioPin != null) {
              if (command == OnOffType.ON) {
                gpioPin.setValue(GPIOPin.VALUE_HIGH);
              } else {
                gpioPin.setValue(GPIOPin.VALUE_LOW);
              }
            }
          } catch (IOException e) {
            logger.error("Error occured while changing pin state for item " + itemName + ", exception: " + e.getMessage());
            return;
View Full Code Here


      /* The item configuration was changed */

      try {
        if (registryLock.writeLock().tryLock(REGISTRYLOCK_TIMEOUT, REGISTRYLOCK_TIMEOUT_UNITS)) {
          try {
            GPIOPin gpioPin = (GPIOPin) registry.get(itemName);

            /* Existing or new item */
            if (gpioPin != null) {

              /* Pin number change requires deletion of old and creation of new backend object */
              if (gpioPin.getPinNumber() != provider.getPinNumber(itemName)) {
                deleteItem(itemName);
                newItem(provider, itemName);
              } else {
                int newActiveLow = provider.getActiveLow(itemName);
                int currentDirection = gpioPin.getDirection();
                int newDirection = provider.getDirection(itemName);

                if (newActiveLow != gpioPin.getActiveLow()) {
                  gpioPin.setActiveLow(newActiveLow);
                }

                if (newDirection != currentDirection) {
                  if (currentDirection == GPIOPin.DIRECTION_IN) {
                    /* Tracking interrupts on output pins is meaningless */
                    gpioPin.removeEventHandler(this);
                  }
                  gpioPin.setDirection(newDirection);
                  if (newDirection == GPIOPin.DIRECTION_IN) {
                    gpioPin.setEdgeDetection(GPIOPin.EDGEDETECTION_BOTH);
                    gpioPin.addEventHandler(this);
                  }
                }

                /* Debouncing is valid only for input pins */
                if (newDirection == GPIOPin.DIRECTION_IN) {
                  long currentDebounceInterval = gpioPin.getDebounceInterval();
                  long defaultDebounceInterval = gpio.getDefaultDebounceInterval();
                  long newDebounceInterval = provider.getDebounceInterval(itemName);

                  /* If debounceInterval isn't configured its value is GPIOBindingProvider.DEBOUNCEINTERVAL_UNDEFINED */
                  if (newDebounceInterval != GPIOBindingProvider.DEBOUNCEINTERVAL_UNDEFINED) {
                    if (newDebounceInterval != currentDebounceInterval) {
                      gpioPin.setDebounceInterval(newDebounceInterval);
                    }
                  } else {
                    /* Revert back to default if was set before and after then deleted */
                    if (currentDebounceInterval != defaultDebounceInterval) {
                      gpioPin.setDebounceInterval(defaultDebounceInterval);
                    }
                  }                 
                }
              }
            } else {
View Full Code Here

  private void newItem(GPIOBindingProvider provider, String itemName) {

    try {
      int direction;

      GPIOPin gpioPin = gpio.reservePin(provider.getPinNumber(itemName));

      gpioPin.setActiveLow(provider.getActiveLow(itemName));

      direction = provider.getDirection(itemName);
      gpioPin.setDirection(direction);

      /* Edge detection and debouncing are valid only for input pins */
      if (direction == GPIOPin.DIRECTION_IN) {
        long debounceInterval = provider.getDebounceInterval(itemName);

        gpioPin.setEdgeDetection(GPIOPin.EDGEDETECTION_BOTH);

        /* If debounceInterval isn't configured its value is GPIOBindingProvider.DEBOUNCEINTERVAL_UNDEFINED */
        if (debounceInterval != GPIOBindingProvider.DEBOUNCEINTERVAL_UNDEFINED) {
          gpioPin.setDebounceInterval(debounceInterval);
        }
      }

      /* Register the pin */
      try {
        if (registryLock.writeLock().tryLock(REGISTRYLOCK_TIMEOUT, REGISTRYLOCK_TIMEOUT_UNITS)) {
          try {
            registry.put(itemName, gpioPin);
          } finally {
            registryLock.writeLock().unlock();
          }
        } else {
          logger.error("Item " + itemName + " hasn't been inserted into the registry, timeout expired while waiting for registry lock");
          return;
        }
      } catch (InterruptedException e) {
        logger.error("Item " + itemName + " hasn't been inserted into the registry, thread was interrupted while waiting for registry lock");
        return;
      }

      /* Set initial item state */
      if (direction == GPIOPin.DIRECTION_IN) {

        /* Item type 'Contact' */
        if (gpioPin.getValue() == GPIOPin.VALUE_HIGH) {
          eventPublisher.postUpdate(itemName, OpenClosedType.OPEN);
        } else {
          eventPublisher.postUpdate(itemName, OpenClosedType.CLOSED);
        }
      } else {

        /* Item type 'Switch' */
        if (gpioPin.getValue() == GPIOPin.VALUE_HIGH) {
          eventPublisher.postUpdate(itemName, OnOffType.ON);
        } else {
          eventPublisher.postUpdate(itemName, OnOffType.OFF);
        }
      }

      /* The item is of type 'Contact', register for state change notifications */
      if (direction == GPIOPin.DIRECTION_IN) {
        gpioPin.addEventHandler(this);
      }
    } catch (Exception e) {
      logger.error("Error occured while creating backend object for item " +itemName + ", exception: "+ e.getMessage());
    }
  }
View Full Code Here

    try {
      if (registryLock.writeLock().tryLock(REGISTRYLOCK_TIMEOUT, REGISTRYLOCK_TIMEOUT_UNITS)) {
        try {

          /* Remove the item from registry */
          GPIOPin gpioPin = (GPIOPin) registry.remove(itemName);

          /* Release the backend object */
          if (gpioPin != null) {
            try {
              gpio.releasePin(gpioPin);
View Full Code Here

TOP

Related Classes of org.openhab.io.gpio.GPIOPin

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.