def _configure_gpio(self):
if not self._hasGPIO:
self._logger.error("RPi.GPIO is required.")
return
self._logger.info("Running RPi.GPIO version %s" % GPIO.VERSION)
if GPIO.VERSION < "0.6":
self._logger.error("RPi.GPIO version 0.6.0 or greater required.")
GPIO.setwarnings(False)
for pin in self._configuredGPIOPins:
self._logger.debug("Cleaning up pin %s" % pin)
try:
GPIO.cleanup(self._gpio_get_pin(pin))
except (RuntimeError, ValueError) as e:
self._logger.error(e)
self._configuredGPIOPins = []
if GPIO.getmode() is None:
if self.GPIOMode == 'BOARD':
GPIO.setmode(GPIO.BOARD)
elif self.GPIOMode == 'BCM':
GPIO.setmode(GPIO.BCM)
else:
return
if self.sensingMethod == 'GPIO':
self._logger.info("Using GPIO sensing to determine PSU on/off state.")
self._logger.info("Configuring GPIO for pin %s" % self.senseGPIOPin)
if self.senseGPIOPinPUD == 'PULL_UP':
pudsenseGPIOPin = GPIO.PUD_UP
elif self.senseGPIOPinPUD == 'PULL_DOWN':
pudsenseGPIOPin = GPIO.PUD_DOWN
else:
pudsenseGPIOPin = GPIO.PUD_OFF
try:
GPIO.setup(self._gpio_get_pin(self.senseGPIOPin), GPIO.IN, pull_up_down=pudsenseGPIOPin)
self._configuredGPIOPins.append(self.senseGPIOPin)
except (RuntimeError, ValueError) as e:
self._logger.error(e)
if self.switchingMethod == 'GPIO':
self._logger.info("Using GPIO for On/Off")
self._logger.info("Configuring GPIO for pin %s" % self.onoffGPIOPin)
try:
if not self.invertonoffGPIOPin:
initial_pin_output=GPIO.LOW
else:
initial_pin_output=GPIO.HIGH
GPIO.setup(self._gpio_get_pin(self.onoffGPIOPin), GPIO.OUT, initial=initial_pin_output)
self._configuredGPIOPins.append(self.onoffGPIOPin)
except (RuntimeError, ValueError) as e:
self._logger.error(e)
评论列表
文章目录