rpi_gpio 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,199 +0,0 @@
1
- /*
2
- Copyright (c) 2013 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
- // ensure channel set as output
51
- if (gpio_direction[self->gpio] != OUTPUT)
52
- {
53
- PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an output first");
54
- return -1;
55
- }
56
-
57
- if (frequency <= 0.0)
58
- {
59
- PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
60
- return -1;
61
- }
62
-
63
- self->freq = frequency;
64
-
65
- pwm_set_frequency(self->gpio, self->freq);
66
- return 0;
67
- }
68
-
69
- // python method PWM.start(self, dutycycle)
70
- static PyObject *PWM_start(PWMObject *self, PyObject *args)
71
- {
72
- float dutycycle;
73
-
74
- if (!PyArg_ParseTuple(args, "f", &dutycycle))
75
- return NULL;
76
-
77
- if (dutycycle < 0.0 || dutycycle > 100.0)
78
- {
79
- PyErr_SetString(PyExc_ValueError, "dutycycle must have a value from 0.0 to 100.0");
80
- return NULL;
81
- }
82
-
83
- self->dutycycle = dutycycle;
84
- pwm_set_duty_cycle(self->gpio, self->dutycycle);
85
- pwm_start(self->gpio);
86
- Py_RETURN_NONE;
87
- }
88
-
89
- // python method PWM.ChangeDutyCycle(self, dutycycle)
90
- static PyObject *PWM_ChangeDutyCycle(PWMObject *self, PyObject *args)
91
- {
92
- float dutycycle = 0.0;
93
- if (!PyArg_ParseTuple(args, "f", &dutycycle))
94
- return NULL;
95
-
96
- if (dutycycle < 0.0 || dutycycle > 100.0)
97
- {
98
- PyErr_SetString(PyExc_ValueError, "dutycycle must have a value from 0.0 to 100.0");
99
- return NULL;
100
- }
101
-
102
- self->dutycycle = dutycycle;
103
- pwm_set_duty_cycle(self->gpio, self->dutycycle);
104
- Py_RETURN_NONE;
105
- }
106
-
107
- // python method PWM. ChangeFrequency(self, frequency)
108
- static PyObject *PWM_ChangeFrequency(PWMObject *self, PyObject *args)
109
- {
110
- float frequency = 1.0;
111
-
112
- if (!PyArg_ParseTuple(args, "f", &frequency))
113
- return NULL;
114
-
115
- if (frequency <= 0.0)
116
- {
117
- PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
118
- return NULL;
119
- }
120
-
121
- self->freq = frequency;
122
-
123
- pwm_set_frequency(self->gpio, self->freq);
124
- Py_RETURN_NONE;
125
- }
126
-
127
- // python function PWM.stop(self)
128
- static PyObject *PWM_stop(PWMObject *self, PyObject *args)
129
- {
130
- pwm_stop(self->gpio);
131
- Py_RETURN_NONE;
132
- }
133
-
134
- // deallocation method
135
- static void PWM_dealloc(PWMObject *self)
136
- {
137
- pwm_stop(self->gpio);
138
- Py_TYPE(self)->tp_free((PyObject*)self);
139
- }
140
-
141
- static PyMethodDef
142
- PWM_methods[] = {
143
- { "start", (PyCFunction)PWM_start, METH_VARARGS, "Start software PWM\ndutycycle - the duty cycle (0.0 to 100.0)" },
144
- { "ChangeDutyCycle", (PyCFunction)PWM_ChangeDutyCycle, METH_VARARGS, "Change the duty cycle\ndutycycle - between 0.0 and 100.0" },
145
- { "ChangeFrequency", (PyCFunction)PWM_ChangeFrequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 1.0)" },
146
- { "stop", (PyCFunction)PWM_stop, METH_VARARGS, "Stop software PWM" },
147
- { NULL }
148
- };
149
-
150
- PyTypeObject PWMType = {
151
- PyVarObject_HEAD_INIT(NULL,0)
152
- "RPi.GPIO.PWM", // tp_name
153
- sizeof(PWMObject), // tp_basicsize
154
- 0, // tp_itemsize
155
- (destructor)PWM_dealloc, // tp_dealloc
156
- 0, // tp_print
157
- 0, // tp_getattr
158
- 0, // tp_setattr
159
- 0, // tp_compare
160
- 0, // tp_repr
161
- 0, // tp_as_number
162
- 0, // tp_as_sequence
163
- 0, // tp_as_mapping
164
- 0, // tp_hash
165
- 0, // tp_call
166
- 0, // tp_str
167
- 0, // tp_getattro
168
- 0, // tp_setattro
169
- 0, // tp_as_buffer
170
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flag
171
- "Pulse Width Modulation class", // tp_doc
172
- 0, // tp_traverse
173
- 0, // tp_clear
174
- 0, // tp_richcompare
175
- 0, // tp_weaklistoffset
176
- 0, // tp_iter
177
- 0, // tp_iternext
178
- PWM_methods, // tp_methods
179
- 0, // tp_members
180
- 0, // tp_getset
181
- 0, // tp_base
182
- 0, // tp_dict
183
- 0, // tp_descr_get
184
- 0, // tp_descr_set
185
- 0, // tp_dictoffset
186
- (initproc)PWM_init, // tp_init
187
- 0, // tp_alloc
188
- 0, // tp_new
189
- };
190
-
191
- PyTypeObject *PWM_init_PWMType(void)
192
- {
193
- // Fill in some slots in the type, and make it ready
194
- PWMType.tp_new = PyType_GenericNew;
195
- if (PyType_Ready(&PWMType) < 0)
196
- return NULL;
197
-
198
- return &PWMType;
199
- }
@@ -1,24 +0,0 @@
1
- /*
2
- Copyright (c) 2013 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
- PyTypeObject PWMType;
24
- PyTypeObject *PWM_init_PWMType(void);
@@ -1,214 +0,0 @@
1
- /*
2
- Copyright (c) 2013 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 <stdlib.h>
24
- #include <pthread.h>
25
- #include <time.h>
26
- #include "c_gpio.h"
27
- #include "soft_pwm.h"
28
- pthread_t threads;
29
-
30
- struct pwm
31
- {
32
- unsigned int gpio;
33
- float freq;
34
- float dutycycle;
35
- float basetime;
36
- float slicetime;
37
- struct timespec req_on, req_off;
38
- int running;
39
- struct pwm *next;
40
- };
41
- struct pwm *pwm_list = NULL;
42
-
43
- void remove_pwm(unsigned int gpio)
44
- {
45
- struct pwm *p = pwm_list;
46
- struct pwm *prev = NULL;
47
- struct pwm *temp;
48
-
49
- while (p != NULL)
50
- {
51
- if (p->gpio == gpio)
52
- {
53
- if (prev == NULL)
54
- pwm_list = p->next;
55
- else
56
- prev->next = p->next;
57
- temp = p;
58
- p = p->next;
59
- free(temp);
60
- } else {
61
- prev = p;
62
- p = p->next;
63
- }
64
- }
65
- }
66
-
67
- void calculate_times(struct pwm *p)
68
- {
69
- long long usec;
70
-
71
- usec = (long long)(p->dutycycle * p->slicetime * 1000.0);
72
- p->req_on.tv_sec = (int)(usec / 1000000LL);
73
- usec -= (long long)p->req_on.tv_sec * 1000000LL;
74
- p->req_on.tv_nsec = (long)usec * 1000L;
75
-
76
- usec = (long long)((100.0-p->dutycycle) * p->slicetime * 1000.0);
77
- p->req_off.tv_sec = (int)(usec / 1000000LL);
78
- usec -= (long long)p->req_off.tv_sec * 1000000LL;
79
- p->req_off.tv_nsec = (long)usec * 1000L;
80
- }
81
-
82
- void full_sleep(struct timespec *req)
83
- {
84
- struct timespec rem = {0};
85
-
86
- if (nanosleep(req,&rem) == -1)
87
- full_sleep(&rem);
88
- }
89
-
90
- void *pwm_thread(void *threadarg)
91
- {
92
- struct pwm *p = (struct pwm *)threadarg;
93
-
94
- while (p->running)
95
- {
96
-
97
- if (p->dutycycle > 0.0)
98
- {
99
- output_gpio(p->gpio, 1);
100
- full_sleep(&p->req_on);
101
- }
102
-
103
- if (p->dutycycle < 100.0)
104
- {
105
- output_gpio(p->gpio, 0);
106
- full_sleep(&p->req_off);
107
- }
108
- }
109
-
110
- // clean up
111
- output_gpio(p->gpio, 0);
112
- remove_pwm(p->gpio);
113
- pthread_exit(NULL);
114
- }
115
-
116
- struct pwm *add_new_pwm(unsigned int gpio)
117
- {
118
- struct pwm *new_pwm;
119
-
120
- new_pwm = malloc(sizeof(struct pwm));
121
- new_pwm->gpio = gpio;
122
- new_pwm->running = 0;
123
- new_pwm->next = NULL;
124
- // default to 1 kHz frequency, dutycycle 0.0
125
- new_pwm->freq = 1000.0;
126
- new_pwm->dutycycle = 0.0;
127
- new_pwm->basetime = 1.0; // 1 ms
128
- new_pwm->slicetime = 0.01; // 0.01 ms
129
- calculate_times(new_pwm);
130
- return new_pwm;
131
- }
132
-
133
- struct pwm *find_pwm(unsigned int gpio)
134
- {
135
- struct pwm *p = pwm_list;
136
-
137
- if (pwm_list == NULL)
138
- {
139
- pwm_list = add_new_pwm(gpio);
140
- return pwm_list;
141
- }
142
-
143
- while (p != NULL)
144
- {
145
- if (p->gpio == gpio)
146
- return p;
147
- if (p->next == NULL)
148
- {
149
- p->next = add_new_pwm(gpio);
150
- return p->next;
151
- }
152
- p = p->next;
153
- }
154
- return NULL;
155
- }
156
-
157
- void pwm_set_duty_cycle(unsigned int gpio, float dutycycle)
158
- {
159
- struct pwm *p;
160
-
161
- if (dutycycle < 0.0 || dutycycle > 100.0)
162
- {
163
- // btc fixme - error
164
- return;
165
- }
166
-
167
- if ((p = find_pwm(gpio)) != NULL)
168
- {
169
- p->dutycycle = dutycycle;
170
- calculate_times(p);
171
- }
172
- }
173
-
174
- void pwm_set_frequency(unsigned int gpio, float freq)
175
- {
176
- struct pwm *p;
177
-
178
- if (freq <= 0.0) // to avoid divide by zero
179
- {
180
- // btc fixme - error
181
- return;
182
- }
183
-
184
- if ((p = find_pwm(gpio)) != NULL)
185
- {
186
- p->basetime = 1000.0 / freq; // calculated in ms
187
- p->slicetime = p->basetime / 100.0;
188
- calculate_times(p);
189
- }
190
- }
191
-
192
- void pwm_start(unsigned int gpio)
193
- {
194
- struct pwm *p;
195
-
196
- if (((p = find_pwm(gpio)) == NULL) || p->running)
197
- return;
198
-
199
- p->running = 1;
200
- if (pthread_create(&threads, NULL, pwm_thread, (void *)p) != 0)
201
- {
202
- // btc fixme - error
203
- p->running = 0;
204
- return;
205
- }
206
- }
207
-
208
- void pwm_stop(unsigned int gpio)
209
- {
210
- struct pwm *p;
211
-
212
- if ((p = find_pwm(gpio)) != NULL)
213
- p->running = 0;
214
- }
@@ -1,28 +0,0 @@
1
- /*
2
- Copyright (c) 2013 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
- /* Software PWM using threads */
24
-
25
- void pwm_set_duty_cycle(unsigned int gpio, float dutycycle);
26
- void pwm_set_frequency(unsigned int gpio, float freq);
27
- void pwm_start(unsigned int gpio);
28
- void pwm_stop(unsigned int gpio);