Debian Codegraph @

source/py_pwm.c

raw symbols SCIP
1 /*
2 Copyright (c) 2013-2018 Ben Croston
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
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 // python method PWM.__init__(self, channel, frequency)
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 // convert channel to gpio
47 if (get_gpio_number(channel, &(self->gpio)))
48 return -1;
49
50 // does soft pwm already exist on this channel?
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 // ensure channel set as output
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 // python method PWM.start(self, dutycycle)
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 // python method PWM.ChangeDutyCycle(self, dutycycle)
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 // python method PWM. ChangeFrequency(self, frequency)
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 // python function PWM.stop(self)
135 static PyObject *PWM_stop(PWMObject *self, PyObject *args)
136 {
137 pwm_stop(self->gpio);
138 Py_RETURN_NONE;
139 }
140
141 // deallocation method
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", // tp_name
160 sizeof(PWMObject), // tp_basicsize
161 0, // tp_itemsize
162 (destructor)PWM_dealloc, // tp_dealloc
163 0, // tp_print
164 0, // tp_getattr
165 0, // tp_setattr
166 0, // tp_compare
167 0, // tp_repr
168 0, // tp_as_number
169 0, // tp_as_sequence
170 0, // tp_as_mapping
171 0, // tp_hash
172 0, // tp_call
173 0, // tp_str
174 0, // tp_getattro
175 0, // tp_setattro
176 0, // tp_as_buffer
177 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flag
178 "Pulse Width Modulation class", // tp_doc
179 0, // tp_traverse
180 0, // tp_clear
181 0, // tp_richcompare
182 0, // tp_weaklistoffset
183 0, // tp_iter
184 0, // tp_iternext
185 PWM_methods, // tp_methods
186 0, // tp_members
187 0, // tp_getset
188 0, // tp_base
189 0, // tp_dict
190 0, // tp_descr_get
191 0, // tp_descr_set
192 0, // tp_dictoffset
193 (initproc)PWM_init, // tp_init
194 0, // tp_alloc
195 0, // tp_new
196 };
197
198 PyTypeObject *PWM_init_PWMType(void)
199 {
200 // Fill in some slots in the type, and make it ready
201 PWMType.tp_new = PyType_GenericNew;
202 if (PyType_Ready(&PWMType) < 0)
203 return NULL;
204
205 return &PWMType;
206 }