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

@ -95,6 +95,7 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
float frequency = 2000.0;
float duty_cycle = 0.0;
int polarity = 0;
int allowed = -1;
static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL};
clear_error_msg();
@ -112,6 +113,21 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
return NULL;
}
// Check to see if PWM is allowed on the hardware
// A 1 means we're good to go
allowed = pwm_allowed(key);
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), "PWM %s not available on current Hardware", key);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (duty_cycle < 0.0 || duty_cycle > 100.0) {
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
return NULL;
@ -142,6 +158,7 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
{
char key[8];
char *channel;
int allowed = -1;
clear_error_msg();
@ -153,6 +170,21 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
return NULL;
}
// Check to see if PWM is allowed on the hardware
// A 1 means we're good to go
allowed = pwm_allowed(key);
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), "PWM %s not available on current Hardware", key);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (pwm_disable(key) < 0) {
char err[2000];
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
@ -168,6 +200,7 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
{
char key[8];
char *channel;
int allowed = -1;
float duty_cycle = 0.0;
static char *kwlist[] = {"channel", "duty_cycle", NULL};
@ -186,6 +219,21 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
return NULL;
}
// Check to see if PWM is allowed on the hardware
// A 1 means we're good to go
allowed = pwm_allowed(key);
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), "PWM %s not available on current Hardware", key);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (pwm_set_duty_cycle(key, duty_cycle) == -1) {
char err[2000];
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
@ -201,6 +249,7 @@ static PyObject *py_set_pulse_width_ns(PyObject *self, PyObject *args, PyObject
{
char key[8];
char *channel;
int allowed = -1;
unsigned long pulse_width_ns = 0.0;
unsigned long period_ns;
static char *kwlist[] = {"channel", "pulse_width_ns", NULL};
@ -215,6 +264,21 @@ static PyObject *py_set_pulse_width_ns(PyObject *self, PyObject *args, PyObject
return NULL;
}
// Check to see if PWM is allowed on the hardware
// A 1 means we're good to go
allowed = pwm_allowed(key);
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), "PWM %s not available on current Hardware", key);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
// Get the period out of the data struct
int rtn = pwm_get_period_ns(key, &period_ns);
if (rtn == -1) {
@ -242,6 +306,7 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
{
char key[8];
char *channel;
int allowed = -1;
float frequency = 1.0;
static char *kwlist[] = {"channel", "frequency", NULL};
@ -260,6 +325,21 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
return NULL;
}
// Check to see if PWM is allowed on the hardware
// A 1 means we're good to go
allowed = pwm_allowed(key);
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), "PWM %s not available on current Hardware", key);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (pwm_set_frequency(key, frequency) < 0) {
char err[2000];
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
@ -275,6 +355,7 @@ static PyObject *py_set_period_ns(PyObject *self, PyObject *args, PyObject *kwar
{
char key[8];
char *channel;
int allowed = -1;
unsigned long period_ns = 2e6;
static char *kwlist[] = {"channel", "period_ns", NULL};
@ -293,6 +374,21 @@ static PyObject *py_set_period_ns(PyObject *self, PyObject *args, PyObject *kwar
return NULL;
}
// Check to see if PWM is allowed on the hardware
// A 1 means we're good to go
allowed = pwm_allowed(key);
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), "PWM %s not available on current Hardware", key);
PyErr_SetString(PyExc_ValueError, err);
return NULL;
}
if (pwm_set_period_ns(key, period_ns) < 0) {
char err[2000];
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());