// python method PWM.set_frequency(channel, frequency) static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; float frequency = 1.0; static char *kwlist[] = {"channel", "frequency", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency)) return NULL; if (frequency <= 0.0) { PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); return NULL; } if (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } if (pwm_set_frequency(key, frequency) == -1) { PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); return NULL; } Py_RETURN_NONE; }
// python function start(channel, duty_cycle, freq) static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; float frequency = 2000.0; float duty_cycle = 0.0; static char *kwlist[] = {"channel", "duty_cycle", "frequency", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ff", kwlist, &channel, &duty_cycle, &frequency)) return NULL; if (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); 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; } pwm_start(key, duty_cycle, frequency); Py_RETURN_NONE; }
static PyObject *py_set_polarity(PyObject *self, PyObject *args, PyObject *kwargs){ char key[8]; char *channel; int polarity = 0; static char *kwlist[] = {"channel", "polarity", NULL}; if(!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, &channel, &polarity)) return NULL; if (polarity != 0 && polarity != 1) { PyErr_SetString(PyExc_ValueError, "polarity must be either 0 or 1"); return NULL; } if (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } if (pwm_set_polarity(key, polarity) == -1) { PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); return NULL; } Py_RETURN_NONE; }
// python method PWM.set_duty_cycle(channel, duty_cycle) static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwargs) { char key[8]; char *channel; float duty_cycle = 0.0; static char *kwlist[] = {"channel", "duty_cycle", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &duty_cycle)) 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 (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } if (pwm_set_duty_cycle(key, duty_cycle) == -1) { PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); return NULL; } 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; } }
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; } }
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; if (!PyArg_ParseTuple(args, "s", &channel)) return NULL; if (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } pwm_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; } }