1
0
mirror of https://github.com/xtacocorex/CHIP_IO synced 2025-07-20 04:43:21 +00:00

More work on #32, Checks for gpio being invalid on specific hardware, pwm invalid checks done. Need to do proper export of PWM1 on CHIP Pro

This commit is contained in:
Robert Wolterman
2017-02-20 05:03:04 +00:00
parent e179e52b5e
commit ca7d221b33
7 changed files with 613 additions and 114 deletions

View File

@ -152,6 +152,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs)
{
int gpio;
int allowed = -1;
char *channel;
int direction;
int pud = PUD_OFF;
@ -190,6 +191,21 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (gpio_export(gpio) < 0) {
char err[2000];
@ -241,6 +257,7 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
int gpio;
int value;
char *channel;
int allowed = -1;
clear_error_msg();
@ -252,6 +269,21 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (!module_setup || dyn_int_array_get(&gpio_direction, gpio, -1) != OUTPUT)
{
char err[2000];
@ -279,6 +311,7 @@ static PyObject *py_input_gpio(PyObject *self, PyObject *args)
char *channel;
unsigned int value;
PyObject *py_value;
int allowed = -1;
clear_error_msg();
@ -290,6 +323,21 @@ static PyObject *py_input_gpio(PyObject *self, PyObject *args)
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// check channel is set up as an input or output
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
{
@ -317,6 +365,7 @@ static PyObject *py_read_byte_gpio(PyObject *self, PyObject *args)
char *channel;
unsigned int value = 0;
PyObject *py_value;
int allowed = -1;
clear_error_msg();
@ -328,6 +377,21 @@ static PyObject *py_read_byte_gpio(PyObject *self, PyObject *args)
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// check channel is set up as an input or output
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
{
@ -355,6 +419,7 @@ static PyObject *py_read_word_gpio(PyObject *self, PyObject *args)
char *channel;
unsigned int value = 0;
PyObject *py_value;
int allowed = -1;
clear_error_msg();
@ -365,8 +430,23 @@ static PyObject *py_read_word_gpio(PyObject *self, PyObject *args)
PyErr_SetString(PyExc_ValueError, "Invalid channel");
return NULL;
}
// check channel is set up as an input or output
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// check channel is set up as an input or output
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
{
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel first");
@ -464,6 +544,7 @@ static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject
{
int gpio;
char *channel;
int allowed = -1;
unsigned int bouncetime = 0;
PyObject *cb_func;
char *kwlist[] = {"gpio", "callback", "bouncetime", NULL};
@ -484,6 +565,21 @@ static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// check to ensure gpio is one of the allowed pins
if (gpio != lookup_gpio_by_name("AP-EINT3")
&& gpio != lookup_gpio_by_name("AP-EINT1")
@ -518,6 +614,7 @@ static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *k
char *channel;
int edge, result;
unsigned int bouncetime = 0;
int allowed = -1;
PyObject *cb_func = NULL;
char *kwlist[] = {"gpio", "edge", "callback", "bouncetime", NULL};
@ -537,6 +634,21 @@ static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *k
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// check to ensure gpio is one of the allowed pins
if (gpio != lookup_gpio_by_name("AP-EINT3")
&& gpio != lookup_gpio_by_name("AP-EINT1")
@ -586,6 +698,7 @@ static PyObject *py_remove_event_detect(PyObject *self, PyObject *args)
struct py_callback *cb = py_callbacks;
struct py_callback *temp;
struct py_callback *prev = NULL;
int allowed = -1;
clear_error_msg();
@ -597,6 +710,21 @@ static PyObject *py_remove_event_detect(PyObject *self, PyObject *args)
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// check to ensure gpio is one of the allowed pins
if (gpio != lookup_gpio_by_name("AP-EINT3")
&& gpio != lookup_gpio_by_name("AP-EINT1")
@ -634,6 +762,7 @@ static PyObject *py_event_detected(PyObject *self, PyObject *args)
{
int gpio;
char *channel;
int allowed = -1;
clear_error_msg();
@ -645,6 +774,21 @@ static PyObject *py_event_detected(PyObject *self, PyObject *args)
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (event_detected(gpio))
Py_RETURN_TRUE;
else
@ -658,6 +802,7 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
int edge, result;
char *channel;
char error[81];
int allowed = -1;
clear_error_msg();
@ -669,6 +814,21 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// check to ensure gpio is one of the allowed pins
if (gpio != lookup_gpio_by_name("AP-EINT3")
&& gpio != lookup_gpio_by_name("AP-EINT1")
@ -717,6 +877,7 @@ static PyObject *py_gpio_function(PyObject *self, PyObject *args)
unsigned int value;
PyObject *func;
char *channel;
int allowed = -1;
clear_error_msg();
@ -728,6 +889,21 @@ static PyObject *py_gpio_function(PyObject *self, PyObject *args)
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (setup_error)
{
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
@ -915,6 +1091,7 @@ static PyObject *py_set_direction(PyObject *self, PyObject *args, PyObject *kwar
int gpio;
char *channel;
int direction;
int allowed = -1;
static char *kwlist[] = { "channel", "direction", NULL };
clear_error_msg();
@ -939,6 +1116,21 @@ static PyObject *py_set_direction(PyObject *self, PyObject *args, PyObject *kwar
return NULL;
}
// Check to see if GPIO is allowed on the hardware
// A 1 means we're good to go
allowed = gpio_allowed(gpio);
if (allowed == -1) {
char err[2000];
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
PyErr_SetString(PyExc_ValueError, err);
return NULL;
} else if (allowed == 0) {
char err[2000];
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (gpio_set_direction(gpio, direction) < 0) {
char err[2000];
snprintf(err, sizeof(err), "Error setting direction %d on channel %s. (%s)", direction, channel, get_error_msg());