| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "Python.h" |
| 24 |
#include "soft_pwm.h" |
| 25 |
#include "py_pwm.h" |
| 26 |
#include "common.h" |
| 27 |
#include "c_gpio.h" |
| 28 |
|
| 29 |
typedef struct |
| 30 |
{ |
| 31 |
PyObject_HEAD |
| 32 |
unsigned int gpio; |
| 33 |
float freq; |
| 34 |
float dutycycle; |
| 35 |
} PWMObject; |
| 36 |
|
| 37 |
|
| 38 |
static int PWM_init(PWMObject *self, PyObject *args, PyObject *kwds) |
| 39 |
{ |
| 40 |
int channel; |
| 41 |
float frequency; |
| 42 |
|
| 43 |
if (!PyArg_ParseTuple(args, "if", &channel, &frequency)) |
| 44 |
return -1; |
| 45 |
|
| 46 |
|
| 47 |
if (get_gpio_number(channel, &(self->gpio))) |
| 48 |
return -1; |
| 49 |
|
| 50 |
|
| 51 |
if (pwm_exists(self->gpio)) |
| 52 |
{ |
| 53 |
PyErr_SetString(PyExc_RuntimeError, "A PWM object already exists for this GPIO channel"); |
| 54 |
return -1; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
if (gpio_direction[self->gpio] != OUTPUT) |
| 59 |
{ |
| 60 |
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an output first"); |
| 61 |
return -1; |
| 62 |
} |
| 63 |
|
| 64 |
if (frequency <= 0.0) |
| 65 |
{ |
| 66 |
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
self->freq = frequency; |
| 71 |
|
| 72 |
pwm_set_frequency(self->gpio, self->freq); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
static PyObject *PWM_start(PWMObject *self, PyObject *args) |
| 78 |
{ |
| 79 |
float dutycycle; |
| 80 |
|
| 81 |
if (!PyArg_ParseTuple(args, "f", &dutycycle)) |
| 82 |
return NULL; |
| 83 |
|
| 84 |
if (dutycycle < 0.0 || dutycycle > 100.0) |
| 85 |
{ |
| 86 |
PyErr_SetString(PyExc_ValueError, "dutycycle must have a value from 0.0 to 100.0"); |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
|
| 90 |
self->dutycycle = dutycycle; |
| 91 |
pwm_set_duty_cycle(self->gpio, self->dutycycle); |
| 92 |
pwm_start(self->gpio); |
| 93 |
Py_RETURN_NONE; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
static PyObject *PWM_ChangeDutyCycle(PWMObject *self, PyObject *args) |
| 98 |
{ |
| 99 |
float dutycycle = 0.0; |
| 100 |
if (!PyArg_ParseTuple(args, "f", &dutycycle)) |
| 101 |
return NULL; |
| 102 |
|
| 103 |
if (dutycycle < 0.0 || dutycycle > 100.0) |
| 104 |
{ |
| 105 |
PyErr_SetString(PyExc_ValueError, "dutycycle must have a value from 0.0 to 100.0"); |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
self->dutycycle = dutycycle; |
| 110 |
pwm_set_duty_cycle(self->gpio, self->dutycycle); |
| 111 |
Py_RETURN_NONE; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
static PyObject *PWM_ChangeFrequency(PWMObject *self, PyObject *args) |
| 116 |
{ |
| 117 |
float frequency = 1.0; |
| 118 |
|
| 119 |
if (!PyArg_ParseTuple(args, "f", &frequency)) |
| 120 |
return NULL; |
| 121 |
|
| 122 |
if (frequency <= 0.0) |
| 123 |
{ |
| 124 |
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
self->freq = frequency; |
| 129 |
|
| 130 |
pwm_set_frequency(self->gpio, self->freq); |
| 131 |
Py_RETURN_NONE; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
static PyObject *PWM_stop(PWMObject *self, PyObject *args) |
| 136 |
{ |
| 137 |
pwm_stop(self->gpio); |
| 138 |
Py_RETURN_NONE; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
static void PWM_dealloc(PWMObject *self) |
| 143 |
{ |
| 144 |
pwm_stop(self->gpio); |
| 145 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 146 |
} |
| 147 |
|
| 148 |
static PyMethodDef |
| 149 |
PWM_methods[] = { |
| 150 |
{ "start", (PyCFunction)PWM_start, METH_VARARGS, "Start software PWM\ndutycycle - the duty cycle (0.0 to 100.0)" }, |
| 151 |
{ "ChangeDutyCycle", (PyCFunction)PWM_ChangeDutyCycle, METH_VARARGS, "Change the duty cycle\ndutycycle - between 0.0 and 100.0" }, |
| 152 |
{ "ChangeFrequency", (PyCFunction)PWM_ChangeFrequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 1.0)" }, |
| 153 |
{ "stop", (PyCFunction)PWM_stop, METH_VARARGS, "Stop software PWM" }, |
| 154 |
{ NULL } |
| 155 |
}; |
| 156 |
|
| 157 |
PyTypeObject PWMType = { |
| 158 |
PyVarObject_HEAD_INIT(NULL,0) |
| 159 |
"RPi.GPIO.PWM", |
| 160 |
sizeof(PWMObject), |
| 161 |
0, |
| 162 |
(destructor)PWM_dealloc, |
| 163 |
0, |
| 164 |
0, |
| 165 |
0, |
| 166 |
0, |
| 167 |
0, |
| 168 |
0, |
| 169 |
0, |
| 170 |
0, |
| 171 |
0, |
| 172 |
0, |
| 173 |
0, |
| 174 |
0, |
| 175 |
0, |
| 176 |
0, |
| 177 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 178 |
"Pulse Width Modulation class", |
| 179 |
0, |
| 180 |
0, |
| 181 |
0, |
| 182 |
0, |
| 183 |
0, |
| 184 |
0, |
| 185 |
PWM_methods, |
| 186 |
0, |
| 187 |
0, |
| 188 |
0, |
| 189 |
0, |
| 190 |
0, |
| 191 |
0, |
| 192 |
0, |
| 193 |
(initproc)PWM_init, |
| 194 |
0, |
| 195 |
0, |
| 196 |
}; |
| 197 |
|
| 198 |
PyTypeObject *PWM_init_PWMType(void) |
| 199 |
{ |
| 200 |
|
| 201 |
PWMType.tp_new = PyType_GenericNew; |
| 202 |
if (PyType_Ready(&PWMType) < 0) |
| 203 |
return NULL; |
| 204 |
|
| 205 |
return &PWMType; |
| 206 |
} |