Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invert sense #39

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions octoprint_psucontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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(',')
Expand All @@ -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()

Expand Down
12 changes: 11 additions & 1 deletion octoprint_psucontrol/templates/psucontrol_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@
<div class="control-group">
<label class="control-label">Sense GPIO Pin</label>
<div class="controls">
<input type="number" min="0" class="input-mini" data-bind="value: settings.plugins.psucontrol.senseGPIOPin">
<input type="number" min="0" class="input-mini" data-bind="value: settings.plugins.psucontrol.senseGPIOPin"> <input type="checkbox" data-bind="checked: settings.plugins.psucontrol.invertsenseGPIOPin"> Invert
</div>
</div>
<div class="control-group">
<label class="control-label">Pull-Up/Down State</label>
<div class="controls">
<select data-bind="value: settings.plugins.psucontrol.senseGPIOPinPUD">
<option value="OFF">Off</option>
<option value="PULL_UP">Internal Pull-Up</option>
<option value="PULL_DOWN">Internal Pull-Down</option>
</select>
</div>
</div>
<!-- /ko -->
Expand Down