diff --git a/octoprint_psucontrol/__init__.py b/octoprint_psucontrol/__init__.py index ae99a27..3607f0d 100644 --- a/octoprint_psucontrol/__init__.py +++ b/octoprint_psucontrol/__init__.py @@ -46,6 +46,8 @@ def __init__(self): self.enableSensing = False self.disconnectOnPowerOff = False self.senseGPIOPin = 0 + self.invertsenseGPIOPin = False + self.senseGPIOPinPUD = '' self.isPSUOn = False self._noSensing_isPSUOn = False self._checkPSUTimer = None @@ -91,6 +93,12 @@ def on_settings_initialized(self): self.senseGPIOPin = self._settings.get_int(["senseGPIOPin"]) self._logger.debug("senseGPIOPin: %s" % self.senseGPIOPin) + self.invertsenseGPIOPin = self._settings.get_boolean(["invertsenseGPIOPin"]) + self._logger.debug("invertsenseGPIOPin: %s" % self.invertsenseGPIOPin) + + self.senseGPIOPinPUD = self._settings.get(["senseGPIOPinPUD"]) + self._logger.debug("senseGPIOPinPUD: %s" % self.senseGPIOPinPUD) + self.autoOn = self._settings.get_boolean(["autoOn"]) self._logger.debug("autoOn: %s" % self.autoOn) @@ -177,8 +185,16 @@ def _configure_gpio(self): if self.enableSensing: self._logger.info("Using 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) + 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) @@ -201,6 +217,7 @@ def check_psu_state(self): if self.enableSensing: self._logger.debug("Polling PSU state...") + new_isPSUOn = False r = 0 try: r = GPIO.input(self._gpio_get_pin(self.senseGPIOPin)) @@ -209,9 +226,14 @@ def check_psu_state(self): self._logger.debug("Result: %s" % r) if r==1: - self.isPSUOn = True + new_isPSUOn = True elif r==0: - self.isPSUOn = False + new_isPSUOn = False + + if self.invertsenseGPIOPin: + new_isPSUOn = not new_isPSUOn + + self.isPSUOn = new_isPSUOn else: self.isPSUOn = self._noSensing_isPSUOn @@ -392,6 +414,8 @@ def get_settings_defaults(self): enableSensing = False, disconnectOnPowerOff = False, senseGPIOPin = 0, + invertsenseGPIOPin = False, + senseGPIOPinPUD = 'OFF', autoOn = False, autoOnTriggerGCodeCommands = "G0,G1,G2,G3,G10,G11,G28,G29,G32,M104,M109,M140,M190", enablePowerOffWarningDialog = True, @@ -406,6 +430,8 @@ def on_settings_save(self, data): old_onoffGPIOPin = self.onoffGPIOPin old_enableSensing = self.enableSensing old_senseGPIOPin = self.senseGPIOPin + old_invertsenseGPIOPin = self.invertsenseGPIOPin + old_senseGPIOPinPUD = self.senseGPIOPinPUD old_switchingMethod = self.switchingMethod octoprint.plugin.SettingsPlugin.on_settings_save(self, data) @@ -422,6 +448,8 @@ def on_settings_save(self, data): self.enableSensing = self._settings.get_boolean(["enableSensing"]) self.disconnectOnPowerOff = self._settings.get_boolean(["disconnectOnPowerOff"]) self.senseGPIOPin = self._settings.get_int(["senseGPIOPin"]) + self.invertsenseGPIOPin = self._settings.get_boolean(["invertsenseGPIOPin"]) + self.senseGPIOPinPUD = self._settings.get(["senseGPIOPinPUD"]) self.autoOn = self._settings.get_boolean(["autoOn"]) self.autoOnTriggerGCodeCommands = self._settings.get(["autoOnTriggerGCodeCommands"]) self._autoOnTriggerGCodeCommandsArray = self.autoOnTriggerGCodeCommands.split(',') @@ -436,6 +464,8 @@ def on_settings_save(self, data): old_onoffGPIOPin != self.onoffGPIOPin or old_senseGPIOPin != self.senseGPIOPin or old_enableSensing != self.enableSensing or + old_invertsenseGPIOPin != self.invertsenseGPIOPin or + old_senseGPIOPinPUD != self.senseGPIOPinPUD or old_switchingMethod != self.switchingMethod): self._configure_gpio() diff --git a/octoprint_psucontrol/templates/psucontrol_settings.jinja2 b/octoprint_psucontrol/templates/psucontrol_settings.jinja2 index b34ef7b..e8b98f7 100644 --- a/octoprint_psucontrol/templates/psucontrol_settings.jinja2 +++ b/octoprint_psucontrol/templates/psucontrol_settings.jinja2 @@ -83,7 +83,17 @@
- + Invert +
+
+
+ +
+