Skip to content

Commit 6ab0c0d

Browse files
committed
Merge pull request adafruit#15 from amscins/master
Pass the name of the GPIO to callbacks rather than the GPIO number.
2 parents 2047922 + b412e17 commit 6ab0c0d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

source/py_gpio.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int gpio_warnings = 1;
3838

3939
struct py_callback
4040
{
41+
char channel[32];
4142
unsigned int gpio;
4243
PyObject *py_cb;
4344
unsigned long long lastcall;
@@ -183,8 +184,7 @@ static void run_py_callbacks(unsigned int gpio)
183184
if (cb->bouncetime == 0 || timenow - cb->lastcall > cb->bouncetime*1000 || cb->lastcall == 0 || cb->lastcall > timenow) {
184185
// run callback
185186
gstate = PyGILState_Ensure();
186-
//result = PyObject_CallFunction(cb->py_cb, "i", chan_from_gpio(gpio));
187-
result = PyObject_CallFunction(cb->py_cb, "i", gpio);
187+
result = PyObject_CallFunction(cb->py_cb, "s", cb->channel);
188188

189189
if (result == NULL && PyErr_Occurred())
190190
{
@@ -200,7 +200,7 @@ static void run_py_callbacks(unsigned int gpio)
200200
}
201201
}
202202

203-
static int add_py_callback(unsigned int gpio, unsigned int bouncetime, PyObject *cb_func)
203+
static int add_py_callback(char *channel, unsigned int gpio, unsigned int bouncetime, PyObject *cb_func)
204204
{
205205
struct py_callback *new_py_cb;
206206
struct py_callback *cb = py_callbacks;
@@ -214,6 +214,8 @@ static int add_py_callback(unsigned int gpio, unsigned int bouncetime, PyObject
214214
}
215215
new_py_cb->py_cb = cb_func;
216216
Py_XINCREF(cb_func); // Add a reference to new callback
217+
memset(new_py_cb->channel, 0, sizeof(new_py_cb->channel));
218+
strncpy(new_py_cb->channel, channel, sizeof(new_py_cb->channel) - 1);
217219
new_py_cb->gpio = gpio;
218220
new_py_cb->lastcall = 0;
219221
new_py_cb->bouncetime = bouncetime;
@@ -264,7 +266,7 @@ static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject
264266
return NULL;
265267
}
266268

267-
if (add_py_callback(gpio, bouncetime, cb_func) != 0)
269+
if (add_py_callback(channel, gpio, bouncetime, cb_func) != 0)
268270
return NULL;
269271

270272
Py_RETURN_NONE;
@@ -319,7 +321,7 @@ static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *k
319321
}
320322

321323
if (cb_func != NULL)
322-
if (add_py_callback(gpio, bouncetime, cb_func) != 0)
324+
if (add_py_callback(channel, gpio, bouncetime, cb_func) != 0)
323325
return NULL;
324326

325327
Py_RETURN_NONE;
@@ -529,4 +531,4 @@ PyMODINIT_FUNC initGPIO(void)
529531
#else
530532
return;
531533
#endif
532-
}
534+
}

0 commit comments

Comments
 (0)