-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expose GPIO & interrupt functions in new digital_io module
- Loading branch information
Showing
3 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/functional.h> | ||
#include <RF24.h> // The public API will pull in the backend hardware interface for GPIO | ||
|
||
namespace py = pybind11; | ||
|
||
#ifndef HIGH | ||
static const int HIGH = 1; | ||
#endif | ||
#ifndef LOW | ||
static const int LOW = 0; | ||
#endif | ||
|
||
PYBIND11_MODULE(digital_io, m) | ||
{ | ||
m.doc() = "Some Arduino-style API used to expose GPIO interface for digital input and outputs"; | ||
|
||
// ******************************************** General GPIO | ||
m.attr("HIGH") = HIGH; | ||
m.attr("LOW") = LOW; | ||
m.attr("INPUT") = INPUT; | ||
m.attr("OUTPUT") = OUTPUT; | ||
|
||
m.def("pinMode", &GPIO::open, R"docstr( | ||
pinMode(pin: int, direction: int) -> None | ||
Initialize a GPIO pin. | ||
:param pin: The pin number to initialize. | ||
:param direction: The direction of the initialized pin. | ||
This only be either `INPUT` or `OUTPUT`. | ||
)docstr", | ||
py::arg("pin"), py::arg("direction")); | ||
m.def("pinClose", &GPIO::close, R"docstr( | ||
pinClose(pin: int) -> None | ||
Deinitialize a GPIO pin. | ||
:param pin: The pin number to deinitialize. | ||
)docstr", | ||
py::arg("pin")); | ||
m.def("digitalWrite", &GPIO::write, R"docstr( | ||
digitalWrite(pin: int, value: int) -> None | ||
Write a digital output value to a GPIO pin. | ||
:param pin: The pin number to manipulate (as an output). | ||
:param value: The value set to the output pin. | ||
This can only be either `HIGH` or `LOW`. | ||
)docstr", | ||
py::arg("pin"), py::arg("value")); | ||
|
||
m.def("digitalRead", &GPIO::read, R"docstr( | ||
digitalRead(pin: int) -> int | ||
Read the current value of a given pin. | ||
:param pin: The GPIO input pin to read. | ||
:returns: | ||
``1`` if the pin is currently `HIGH`, ``0`` if the pin is currently `LOW`. | ||
)docstr", | ||
py::arg("pin")); | ||
|
||
#ifndef MRAA | ||
|
||
// ******************************************** Interrupt support | ||
|
||
#ifndef RF24_WIRINGPI | ||
m.def("detachInterrupt", &detachInterrupt, R"docstr( | ||
detachInterrupt(pin: int) | ||
Discontinue watching the given GPIO input ``pin`` for an event. This is similar to | ||
`pinClose()` but also deallocates the background thread used to monitor the pin for | ||
events. | ||
.. note:: | ||
This function is only available for the SPIDEV and RPi drivers. | ||
It is not exposed when the pyrf24 package is built (from source) with the MRAA or | ||
wiringPi drivers. | ||
:param pin: The pin number to watch for events described by ``mode``. | ||
:returns: | ||
``1`` if the pin was un-initialized and the background thread was canceled, | ||
``0`` if there was nothing to do. | ||
)docstr", | ||
py::arg("pin")); | ||
#endif // !defined(RF24_WIRINGPI) | ||
|
||
m.attr("FALLING") = INT_EDGE_FALLING; | ||
m.attr("RISING") = INT_EDGE_RISING; | ||
m.attr("CHANGE") = INT_EDGE_BOTH; | ||
m.def( | ||
"attachInterrupt", [](rf24_gpio_pin_t pin, int mode, std::function<void(void)>& func) { | ||
return attachInterrupt(pin, mode, *func.target<void (*)()>()); | ||
}, | ||
R"docstr( | ||
attachInterrupt(pin: int, mode: int, function: Callable[[], None]) -> int | ||
Attach an Interrupt Service Routine (ISR), a callback function, to a GPIO input pin. | ||
.. note:: | ||
This function is only available for the SPIDEV, RPi, and wiringPi drivers. | ||
It is not exposed when the pyrf24 package is built (from source) with the MRAA | ||
driver. | ||
:param pin: The pin number to watch for events described by ``mode``. | ||
:param mode: The event type that triggers a call to the given ``function``. | ||
This value can only be `FALLING`, `RISING`, or `CHANGE`. | ||
:param function: The function to call when an event (described by ``mode``) occurs on | ||
the specified ``pin``. This function signature expects no arguments and returns | ||
nothing. | ||
:returns: | ||
``1`` if pin is successfully setup to watch for an event, ``0`` if the given ``mode`` | ||
is malformed. If ``0`` is returned, then specified pin remains un-initialized. | ||
)docstr", | ||
py::arg("pin"), py::arg("mode"), py::arg("function")); | ||
|
||
#endif // !defined(MRAA) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Callable | ||
|
||
INPUT: int = ... | ||
OUTPUT: int = ... | ||
HIGH: int = ... | ||
LOW: int = ... | ||
|
||
FALLING: int = ... | ||
RISING: int = ... | ||
CHANGE: int = ... | ||
|
||
def pinMode(pin: int, mode: int) -> None: ... | ||
def pinClose(pin: int) -> None: ... | ||
def detachInterrupt(pin: int) -> None: ... | ||
def attachInterrupt(pin, int, mode: int, function: Callable[[], None]) -> int: ... |