// python function cleanup(channel) static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; static char *kwlist[] = {"channel", NULL}; clear_error_msg(); // Channel is optional if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &channel)) { return NULL; } // The !channel fixes issues #50 if (channel == NULL || strcmp(channel, "\0") == 0) { softpwm_cleanup(); } else { if (!get_key(channel, key)) { softpwm_cleanup(); } softpwm_disable(key); } Py_RETURN_NONE; }
int stopPwm(char *channel) { char key[8]; int allowed = -1; clear_error_msg(); if (!get_pwm_key(channel, key)) { printf("Invalid PWM key or name."); return 0; } // 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()); return 0; } else if (allowed == 0) { char err[2000]; snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key); return 0; } if (pwm_disable(key) < 0) { char err[2000]; snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg()); return 0; } }
static int init_module(void) { clear_error_msg(); // If we make it here, we're good to go if (DEBUG) printf(" ** init_module: setup complete **\n"); module_setup = 1; return 0; }
int startPwm(char *channel, float duty_cycle, int polarity) { char key[8]; float frequency = 2000.0; int allowed = -1; clear_error_msg(); if (!module_setup) { init_module(); } if (!get_pwm_key(channel, key)) { printf("Invalid PWM key or name."); return 0; } // 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()); return 0; } else if (allowed == 0) { char err[2000]; snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key); return 0; } if (duty_cycle < 0.0 || duty_cycle > 100.0) { printf("duty_cycle must have a value from 0.0 to 100.0"); return 0; } if (frequency <= 0.0) { printf("frequency must be greater than 0.0"); return 0; } if (polarity < 0 || polarity > 1) { printf("polarity must be either 0 or 1"); return 0; } if (pwm_start(key, duty_cycle, frequency, polarity) < 0) { char err[2000]; snprintf(err, sizeof(err), "Unable to start PWM: %s (%s)", channel, get_error_msg()); return 0; } }
// python method PWM.set_frequency(channel, frequency) static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; int gpio; int allowed = -1; float frequency = 1.0; static char *kwlist[] = {"channel", "frequency", NULL}; clear_error_msg(); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency)) return NULL; if ((frequency <= 0.0) || (frequency > 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)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } // check to ensure gpio is one of the allowed pins // Not protecting the call as if the get_key() fails, we won't make it here get_gpio_number(channel, &gpio); // 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 (softpwm_set_frequency(key, frequency) == -1) { PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); return NULL; } Py_RETURN_NONE; }
int cleanupPwm(char *channel) { char key[8]; clear_error_msg(); // The !channel fixes issues #50 if (channel == NULL || strcmp(channel, "\0") == 0) { pwm_cleanup(); } else { if (!get_pwm_key(channel, key)) { pwm_cleanup(); } pwm_disable(key); } }
// python function stop(channel) static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; int gpio; int allowed = -1; clear_error_msg(); if (!PyArg_ParseTuple(args, "s", &channel)) return NULL; if (!get_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } // check to ensure gpio is one of the allowed pins // Not protecting the call as if the get_key() fails, we won't make it here get_gpio_number(channel, &gpio); // 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; } softpwm_disable(key); Py_RETURN_NONE; }
int setPwmFrequency(char *channel, float frequency) { char key[8]; int allowed = -1; clear_error_msg(); if (frequency <= 0.0) { printf("frequency must be greater than 0.0"); return 0; } if (!get_pwm_key(channel, key)) { printf("Invalid PWM key or name."); return 0; } // 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()); return 0; } else if (allowed == 0) { char err[2000]; snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key); return 0; } if (pwm_set_frequency(key, frequency) < 0) { char err[2000]; snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg()); return 0; } }
int setPwmDutyCycle(char *channel, float duty_cycle) { char key[8]; int allowed = -1; clear_error_msg(); if (duty_cycle < 0.0 || duty_cycle > 100.0) { printf("duty_cycle must have a value from 0.0 to 100.0"); return 0; } if (!get_pwm_key(channel, key)) { printf("Invalid PWM key or name."); return 0; } // 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()); return 0; } else if (allowed == 0) { char err[2000]; snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key); return 0; } if (pwm_set_duty_cycle(key, duty_cycle) == -1) { char err[2000]; snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg()); return 0; } }
// python function start(channel, duty_cycle, freq, polarity) static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel = NULL; float frequency = 2000.0; float duty_cycle = 0.0; int polarity = 0; int gpio; int allowed = -1; static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL}; clear_error_msg(); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) { return NULL; } ASSRT(channel != NULL); if (!module_setup) { init_module(); } if (!get_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid SOFTPWM key or name."); return NULL; } // check to ensure gpio is one of the allowed pins // Not protecting the call as if the get_key() fails, we won't make it here get_gpio_number(channel, &gpio); // 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 (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; } if (frequency <= 0.0) { PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); return NULL; } if (polarity < 0 || polarity > 1) { PyErr_SetString(PyExc_ValueError, "polarity must be either 0 or 1"); return NULL; } if (softpwm_start(key, duty_cycle, frequency, polarity) < 0) { printf("softpwm_start failed"); char err[2000]; snprintf(err, sizeof(err), "Error starting softpwm on pin %s (%s)", key, get_error_msg()); PyErr_SetString(PyExc_RuntimeError, err); return NULL; } Py_RETURN_NONE; }