Skip to content
Snippets Groups Projects
Commit 9ffc7ede authored by Ellis Percival's avatar Ellis Percival
Browse files

Add optional initial values for pins and make module cleanup optional. Fixes #23

parent a370abf4
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ gpio_modules: ...@@ -16,6 +16,7 @@ gpio_modules:
- name: dev - name: dev
module: stdio module: stdio
cleanup: no
digital_inputs: digital_inputs:
- name: button - name: button
...@@ -32,6 +33,7 @@ digital_outputs: ...@@ -32,6 +33,7 @@ digital_outputs:
pin: 20 pin: 20
on_payload: "ON" on_payload: "ON"
off_payload: "OFF" off_payload: "OFF"
initial: low
- name: test - name: test
module: dev module: dev
......
...@@ -67,6 +67,10 @@ gpio_modules: ...@@ -67,6 +67,10 @@ gpio_modules:
type: string type: string
required: yes required: yes
empty: no empty: no
cleanup:
type: boolean
required: no
default: yes
digital_inputs: digital_inputs:
type: list type: list
...@@ -133,3 +137,9 @@ digital_outputs: ...@@ -133,3 +137,9 @@ digital_outputs:
type: boolean type: boolean
required: no required: no
default: no default: no
initial:
type: string
required: no
allowed:
- high
- low
import sys import yaml
print("FATAL ERROR: The file at pi_mqtt_gpio/__init__.py should be replaced us"
"ing 'make schema' before packaging.") CONFIG_SCHEMA = yaml.load("""
sys.exit(1) mqtt:
type: dict
required: yes
schema:
host:
type: string
empty: no
required: no
default: localhost
port:
type: integer
min: 1
max: 65535
required: no
default: 1883
user:
type: string
required: no
default: ""
password:
type: string
required: no
default: ""
topic_prefix:
type: string
required: no
default: ""
coerce: rstrip_slash
protocol:
type: string
required: no
empty: no
coerce: tostring
default: "3.1.1"
allowed:
- "3.1"
- "3.1.1"
status_topic:
type: string
required: no
default: status
status_payload_running:
type: string
required: no
default: running
status_payload_stopped:
type: string
required: no
default: stopped
status_payload_dead:
type: string
required: no
default: dead
gpio_modules:
type: list
required: yes
schema:
type: dict
allow_unknown: yes
schema:
name:
type: string
required: yes
empty: no
module:
type: string
required: yes
empty: no
cleanup:
type: boolean
required: no
default: yes
digital_inputs:
type: list
required: no
default: []
schema:
type: dict
schema:
name:
type: string
required: yes
empty: no
module:
type: string
required: yes
empty: no
pin:
type: integer
required: yes
min: 0
on_payload:
type: string
required: yes
empty: no
off_payload:
type: string
required: yes
empty: no
pullup:
type: boolean
required: no
default: no
pulldown:
type: boolean
required: no
default: no
digital_outputs:
type: list
required: no
default: []
schema:
type: dict
schema:
name:
type: string
required: yes
module:
type: string
required: yes
pin:
type: integer
required: yes
min: 0
on_payload:
type: string
required: no
empty: no
off_payload:
type: string
required: no
empty: no
inverted:
type: boolean
required: no
default: no
initial:
type: string
required: no
allowed:
- high
- low
""")
...@@ -11,6 +11,11 @@ BASE_SCHEMA = { ...@@ -11,6 +11,11 @@ BASE_SCHEMA = {
"module": { "module": {
"required": True, "required": True,
"empty": False "empty": False
},
"cleanup": {
"required": False,
"type": "boolean",
"default": True
} }
} }
......
...@@ -36,6 +36,12 @@ class GPIO(GenericGPIO): ...@@ -36,6 +36,12 @@ class GPIO(GenericGPIO):
def setup_pin(self, pin, direction, pullup, pin_config): def setup_pin(self, pin, direction, pullup, pin_config):
if direction == PinDirection.INPUT and pullup is not None: if direction == PinDirection.INPUT and pullup is not None:
self.io.port[pin] = PULLUPS[pullup] self.io.port[pin] = PULLUPS[pullup]
initial = pin_config.get("initial")
if initial is not None:
if initial == 'high':
self.set_pin(pin, True)
elif initial == 'low':
self.set_pin(pin, False)
def set_pin(self, pin, value): def set_pin(self, pin, value):
self.io.port[pin] = value self.io.port[pin] = value
......
...@@ -36,7 +36,12 @@ class GPIO(GenericGPIO): ...@@ -36,7 +36,12 @@ class GPIO(GenericGPIO):
else: else:
pullup = PULLUPS[pullup] pullup = PULLUPS[pullup]
self.io.setup(pin, direction, pull_up_down=pullup) initial = {
None: -1,
"low": 0,
"high": 1
}[pin_config.get("initial")]
self.io.setup(pin, direction, pull_up_down=pullup, initial=initial)
def set_pin(self, pin, value): def set_pin(self, pin, value):
self.io.output(pin, value) self.io.output(pin, value)
......
...@@ -13,6 +13,12 @@ class GPIO(GenericGPIO): ...@@ -13,6 +13,12 @@ class GPIO(GenericGPIO):
def setup_pin(self, pin, direction, pullup, pin_config): def setup_pin(self, pin, direction, pullup, pin_config):
print("setup_pin(pin=%r, direction=%r, pullup=%r, pin_config=%r)" % ( print("setup_pin(pin=%r, direction=%r, pullup=%r, pin_config=%r)" % (
pin, direction, pullup, pin_config)) pin, direction, pullup, pin_config))
initial = pin_config.get("initial")
if initial is not None:
if initial == "high":
self.set_pin(pin, True)
elif initial == "low":
self.set_pin(pin, False)
def set_pin(self, pin, value): def set_pin(self, pin, value):
print("set_pin(pin=%r, value=%r)" % (pin, value)) print("set_pin(pin=%r, value=%r)" % (pin, value))
......
...@@ -23,6 +23,7 @@ LOG_LEVEL_MAP = { ...@@ -23,6 +23,7 @@ LOG_LEVEL_MAP = {
} }
RECONNECT_DELAY_SECS = 5 RECONNECT_DELAY_SECS = 5
GPIO_MODULES = {} GPIO_MODULES = {}
GPIO_CONFIGS = {}
LAST_STATES = {} LAST_STATES = {}
SET_TOPIC = "set" SET_TOPIC = "set"
SET_ON_MS_TOPIC = "set_on_ms" SET_ON_MS_TOPIC = "set_on_ms"
...@@ -431,6 +432,7 @@ if __name__ == "__main__": ...@@ -431,6 +432,7 @@ if __name__ == "__main__":
client = init_mqtt(config["mqtt"], config["digital_outputs"]) client = init_mqtt(config["mqtt"], config["digital_outputs"])
for gpio_config in config["gpio_modules"]: for gpio_config in config["gpio_modules"]:
GPIO_CONFIGS[gpio_config["name"]] = gpio_config
try: try:
GPIO_MODULES[gpio_config["name"]] = configure_gpio_module( GPIO_MODULES[gpio_config["name"]] = configure_gpio_module(
gpio_config) gpio_config)
...@@ -491,6 +493,9 @@ if __name__ == "__main__": ...@@ -491,6 +493,9 @@ if __name__ == "__main__":
# This should also quit the mqtt loop thread. # This should also quit the mqtt loop thread.
client.disconnect() client.disconnect()
for name, gpio in GPIO_MODULES.items(): for name, gpio in GPIO_MODULES.items():
if not GPIO_CONFIGS[name]["cleanup"]:
_LOG.info("Cleanup disabled for module %r.", name)
continue
try: try:
gpio.cleanup() gpio.cleanup()
except Exception: except Exception:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment