Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
.idea/
build/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ sudo python setup.py install
```
The library is dependent on the availability of the **gpio** and **spidev** drivers into the Linux system.

For python3,
```bash
python3 setup.py build
sudo python3 setup.py install
```
The build process require python3-dev. If necessary, run
```bash
sudo apt-get install python3-dev
```

## Basic usage
A simple **sender** program...
```python
Expand Down
85 changes: 81 additions & 4 deletions src/PyLora.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <Python.h>
#include "lora.h"

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif

int check(void)
{
if(lora_initialized()) return 1;
Expand Down Expand Up @@ -171,15 +175,15 @@ init(PyObject *self)
{
int res = lora_init();
PyEval_InitThreads(); // it seems to be necessary for using the global interpreter lock (GIL)
return PyInt_FromLong(res);
return PyLong_FromLong(res);
}

static PyObject *
packet_rssi(PyObject *self)
{
if(!check()) return NULL;
int res = lora_packet_rssi();
return PyInt_FromLong(res);
return PyLong_FromLong(res);
}

static PyObject *
Expand Down Expand Up @@ -324,10 +328,31 @@ wait_for_packet(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}

/*
* Module initialization and state
*/
struct module_state {
PyObject *error;
};

#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif

static PyObject *
error_out(PyObject *m) {
struct module_state *st = GETSTATE(m);
PyErr_SetString(st->error, "something bad happened");
return NULL;
}

/**
* Method list for PyLora module
*/
static PyMethodDef metodos[] = {
static PyMethodDef myextension_methods[] = {
{ "reset", reset, METH_NOARGS, "Physical reset of the module" },
{ "explicit_header_mode", explicit_header_mode, METH_NOARGS, "Set explicit header mode for the next messages" },
{ "implicit_header_mode", implicit_header_mode, METH_VARARGS, "Set implicit header mode for the next messages, with size bytes" },
Expand Down Expand Up @@ -356,11 +381,63 @@ static PyMethodDef metodos[] = {
{ NULL, NULL, 0, NULL }
};

#if PY_MAJOR_VERSION >= 3

static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->error);
return 0;
}

static int myextension_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->error);
return 0;
}


static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"PyLora",
NULL,
sizeof(struct module_state),
myextension_methods,
NULL,
myextension_traverse,
myextension_clear,
NULL
};

#define INITERROR return NULL

PyMODINIT_FUNC
PyInit_PyLora(void)

#else
#define INITERROR return

/**
* Initialization function for the Python interpreter.
*/
void
initPyLora(void)
#endif
{
Py_InitModule("PyLora", metodos);
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("PyLora", myextension_methods);
#endif

if (module == NULL)
INITERROR;
struct module_state *st = GETSTATE(module);

st->error = PyErr_NewException("myextension.Error", NULL, NULL);
if (st->error == NULL) {
Py_DECREF(module);
INITERROR;
}

#if PY_MAJOR_VERSION >= 3
return module;
#endif
}