Skip to content

Commit

Permalink
Reorder declarations to avoid function prototype.
Browse files Browse the repository at this point in the history
Prep for adding open_path Python method in doceme#129
  • Loading branch information
tim-seoss committed Apr 24, 2023
1 parent 9575842 commit 5cb6405
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions spidev_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1335,30 +1335,6 @@ static PyGetSetDef SpiDev_getset[] = {
{NULL},
};

static PyObject *
SpiDev_open_dev(SpiDevObject *self, char *dev_path);

PyDoc_STRVAR(SpiDev_open_doc,
"open(bus, device)\n\n"
"Connects the object to the specified SPI device.\n"
"open(X,Y) will open /dev/spidev<X>.<Y>\n");

static PyObject *
SpiDev_open(SpiDevObject *self, PyObject *args, PyObject *kwds)
{
int bus, device;
char path[SPIDEV_MAXPATH];
static char *kwlist[] = {"bus", "device", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ii:open", kwlist, &bus, &device))
return NULL;
if (snprintf(path, SPIDEV_MAXPATH, "/dev/spidev%d.%d", bus, device) >= SPIDEV_MAXPATH) {
PyErr_SetString(PyExc_OverflowError,
"Bus and/or device number is invalid.");
return NULL;
}
return SpiDev_open_dev(self, path);
}

static PyObject *
SpiDev_open_dev(SpiDevObject *self, char *dev_path)
{
Expand Down Expand Up @@ -1388,6 +1364,28 @@ SpiDev_open_dev(SpiDevObject *self, char *dev_path)
return Py_None;
}


PyDoc_STRVAR(SpiDev_open_doc,
"open(bus, device)\n\n"
"Connects the object to the specified SPI device.\n"
"open(X,Y) will open /dev/spidev<X>.<Y>\n");

static PyObject *
SpiDev_open(SpiDevObject *self, PyObject *args, PyObject *kwds)
{
int bus, device;
char path[SPIDEV_MAXPATH];
static char *kwlist[] = {"bus", "device", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ii:open", kwlist, &bus, &device))
return NULL;
if (snprintf(path, SPIDEV_MAXPATH, "/dev/spidev%d.%d", bus, device) >= SPIDEV_MAXPATH) {
PyErr_SetString(PyExc_OverflowError,
"Bus and/or device number is invalid.");
return NULL;
}
return SpiDev_open_dev(self, path);
}

static int
SpiDev_init(SpiDevObject *self, PyObject *args, PyObject *kwds)
{
Expand Down

0 comments on commit 5cb6405

Please sign in to comment.