2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-05-14 22:39:14 +02:00
|
|
|
"""Inverting pin wrapper
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
"""
|
|
|
|
|
2020-01-30 22:46:35 +01:00
|
|
|
class Signal(object):
|
2020-05-14 22:39:14 +02:00
|
|
|
"""Simplified Signal class
|
|
|
|
|
|
|
|
.. note::
|
2020-01-30 22:46:35 +01:00
|
|
|
|
2020-05-14 22:39:14 +02: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 22:46:35 +01:00
|
|
|
|
|
|
|
def __init__(self, pin, invert=False):
|
2020-05-14 22:39:14 +02:00
|
|
|
"""Create a Signal object by wrapping a pin."""
|
2020-01-30 22:46:35 +01:00
|
|
|
self.pin = pin
|
|
|
|
self.invert = invert
|
|
|
|
|
|
|
|
def __call__(self, v=None):
|
2020-05-14 22:39:14 +02:00
|
|
|
"""Shortcut for :py:meth:`.value`"""
|
2020-01-30 22:46:35 +01:00
|
|
|
return self.value(v)
|
|
|
|
|
|
|
|
def value(self, v=None):
|
2020-05-14 22:39:14 +02: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 22:46:35 +01:00
|
|
|
if v == None:
|
|
|
|
return self.invert ^ self.pin.value()
|
|
|
|
self.pin.value(self.invert ^ bool(v))
|
|
|
|
|
|
|
|
def on(self):
|
2020-05-14 22:39:14 +02:00
|
|
|
"""Activate the signal."""
|
2020-01-30 22:46:35 +01:00
|
|
|
self.value(1)
|
|
|
|
|
|
|
|
def off(self):
|
2020-05-14 22:39:14 +02:00
|
|
|
"""Deactivate the signal."""
|
2020-01-30 22:46:35 +01:00
|
|
|
self.value(0)
|