1
0
Fork 0
jebbatime/wasp/drivers/signal.py

47 lines
1.2 KiB
Python
Raw Permalink Normal View History

# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2020 Daniel Thompson
2020-05-14 14:39:14 -06:00
"""Inverting pin wrapper
~~~~~~~~~~~~~~~~~~~~~~~~
"""
2020-01-30 14:46:35 -07:00
class Signal(object):
2020-05-14 14:39:14 -06:00
"""Simplified Signal class
.. note::
2020-01-30 14:46:35 -07:00
2020-05-14 14:39:14 -06:00
The normal C implementation of the Signal class used by MicroPython
doesn't work on the nRF family. This class provides a temporary
workaround until that can be addressed.
.. automethod:: __init__
"""
2020-01-30 14:46:35 -07:00
def __init__(self, pin, invert=False):
2020-05-14 14:39:14 -06:00
"""Create a Signal object by wrapping a pin."""
2020-01-30 14:46:35 -07:00
self.pin = pin
self.invert = invert
def __call__(self, v=None):
2020-05-14 14:39:14 -06:00
"""Shortcut for :py:meth:`.value`"""
2020-01-30 14:46:35 -07:00
return self.value(v)
def value(self, v=None):
2020-05-14 14:39:14 -06:00
"""Get or set the state of the signal.
:param v: Value to set, defaults to None (which means get the signal
state instead.
:returns: The state of the signal if v is None, otherwise None.
"""
2020-01-30 14:46:35 -07:00
if v == None:
return self.invert ^ self.pin.value()
self.pin.value(self.invert ^ bool(v))
def on(self):
2020-05-14 14:39:14 -06:00
"""Activate the signal."""
2020-01-30 14:46:35 -07:00
self.value(1)
def off(self):
2020-05-14 14:39:14 -06:00
"""Deactivate the signal."""
2020-01-30 14:46:35 -07:00
self.value(0)