mirror of
https://github.com/xtacocorex/CHIP_IO
synced 2025-07-20 04:43:21 +00:00
Reworked a lot of error handling.
This commit is contained in:
@ -32,13 +32,13 @@ SOFTWARE.
|
||||
#include "Python.h"
|
||||
#include "constants.h"
|
||||
#include "common.h"
|
||||
#include "c_softpwm.h"
|
||||
#include "c_softpwm.h"
|
||||
|
||||
// python function cleanup()
|
||||
static PyObject *py_cleanup(PyObject *self, PyObject *args)
|
||||
{
|
||||
// unexport the PWM
|
||||
softpwm_cleanup();
|
||||
softpwm_cleanup();
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
@ -47,7 +47,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args)
|
||||
static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
char *channel = NULL;
|
||||
float frequency = 2000.0;
|
||||
float duty_cycle = 0.0;
|
||||
int polarity = 0;
|
||||
@ -56,6 +56,7 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) {
|
||||
return NULL;
|
||||
}
|
||||
ASSRT(channel != NULL);
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid SOFTPWM key or name.");
|
||||
@ -79,7 +80,7 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!softpwm_start(key, duty_cycle, frequency, polarity))
|
||||
if (!softpwm_start(key, duty_cycle, frequency, polarity))
|
||||
return NULL;
|
||||
|
||||
Py_RETURN_NONE;
|
||||
@ -94,12 +95,12 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
|
||||
if (!PyArg_ParseTuple(args, "s", &channel))
|
||||
return NULL;
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
softpwm_disable(key);
|
||||
softpwm_disable(key);
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
@ -121,12 +122,12 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (softpwm_set_duty_cycle(key, duty_cycle) == -1) {
|
||||
if (softpwm_set_duty_cycle(key, duty_cycle) == -1) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
|
||||
return NULL;
|
||||
}
|
||||
@ -145,13 +146,13 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency))
|
||||
return NULL;
|
||||
|
||||
if ((frequency <= 0.0) || (frequency > 10000.0))
|
||||
if ((frequency <= 0.0) || (frequency > 10000.0))
|
||||
{
|
||||
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0 and less than 10000.0");
|
||||
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0 and less than 10000.0");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
Reference in New Issue
Block a user