Skip to content

Commit 3dad90d

Browse files
authored
Merge pull request #152 from zsserg/master
Error handling for setting up pinmux mode in gpio.setup()
2 parents aa7ccf1 + e86acc4 commit 3dad90d

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

source/c_pinmux.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include <string.h>
3+
#include <syslog.h>
34

45
#include "c_pinmux.h"
56
#include "common.h"
@@ -34,10 +35,10 @@ BBIO_err set_pin_mode(const char *key, const char *mode)
3435
f = fopen(path, "w");
3536
if (NULL == f) {
3637
return BBIO_ACCESS;
37-
}
38-
38+
}
39+
syslog(LOG_DEBUG, "set_pin_mode() :: Pinmux file %s access OK", path);
3940
fprintf(f, "%s", mode);
4041
fclose(f);
41-
42+
syslog(LOG_DEBUG, "set_pin_mode() :: Set pinmux mode to %s for %s", mode, pin);
4243
return BBIO_OK;
4344
}

source/py_gpio.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,18 @@ static PyObject *py_setup_channel(__attribute__ ((unused)) PyObject *self, PyObj
138138
}
139139

140140
} else {
141-
if (pud == PUD_DOWN)
142-
set_pin_mode(channel, "gpio_pd");
141+
if (pud == PUD_DOWN)
142+
res = set_pin_mode(channel, "gpio_pd");
143143
else if (pud == PUD_UP)
144-
set_pin_mode(channel, "gpio_pu");
144+
res = set_pin_mode(channel, "gpio_pu");
145145
else
146-
set_pin_mode(channel, "gpio");
146+
res = set_pin_mode(channel, "gpio");
147+
}
148+
149+
//Check if set_pin_mode() returned no error
150+
if (res != BBIO_OK) {
151+
PyErr_SetString(PyExc_ValueError, "Set gpio mode failed, missing file or invalid permissions.");
152+
return NULL;
147153
}
148154

149155
gpio_direction[gpio] = direction;

0 commit comments

Comments
 (0)