rpi_gpio 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 Nick Lowery
5
-
6
- Copyright (c) 2013-2014 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include "ruby.h"
28
- #include "c_gpio.h"
29
- #include "event_gpio.h"
30
- #include "cpuinfo.h"
31
- #include "common.h"
32
- #include "rb_pwm.h"
33
-
34
- void define_gpio_module_stuff(void);
35
- int mmap_gpio_mem(void);
36
- int is_gpio_input(unsigned int gpio);
37
- int is_gpio_output(unsigned int gpio);
38
- int is_rpi(void);
39
- VALUE GPIO_clean_up(int argc, VALUE *argv, VALUE self);
40
- VALUE GPIO_reset(VALUE self);
41
- VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash);
42
- VALUE GPIO_set_numbering(VALUE self, VALUE mode);
43
- VALUE GPIO_set_high(VALUE self, VALUE channel);
44
- VALUE GPIO_set_low(VALUE self, VALUE channel);
45
- VALUE GPIO_test_high(VALUE self, VALUE channel);
46
- VALUE GPIO_test_low(VALUE self, VALUE channel);
47
- VALUE GPIO_set_warnings(VALUE self, VALUE setting);
48
-
@@ -1,141 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 Nick Lowery
5
-
6
- Copyright (c) 2013-2014 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include "rb_pwm.h"
28
-
29
- extern VALUE m_GPIO;
30
- VALUE c_PWM = Qnil;
31
-
32
- void define_pwm_class_stuff(void)
33
- {
34
- c_PWM = rb_define_class_under(m_GPIO, "PWM", rb_cObject);
35
- rb_define_method(c_PWM, "initialize", PWM_initialize, 2);
36
- rb_define_method(c_PWM, "start", PWM_start, 1);
37
- rb_define_method(c_PWM, "gpio", PWM_get_gpio, 0);
38
- rb_define_method(c_PWM, "duty_cycle", PWM_get_duty_cycle, 0);
39
- rb_define_method(c_PWM, "duty_cycle=", PWM_set_duty_cycle, 1);
40
- rb_define_method(c_PWM, "frequency", PWM_get_frequency, 0);
41
- rb_define_method(c_PWM, "frequency=", PWM_set_frequency, 1);
42
- rb_define_method(c_PWM, "stop", PWM_stop, 0);
43
- rb_define_method(c_PWM, "running?", PWM_get_running, 0);
44
- }
45
-
46
- // RPi::GPIO::PWM#initialize
47
- VALUE PWM_initialize(VALUE self, VALUE channel, VALUE frequency)
48
- {
49
- int chan;
50
- unsigned int gpio;
51
-
52
- chan = NUM2INT(channel);
53
-
54
- // convert channel to gpio
55
- if (get_gpio_number(chan, &gpio))
56
- return Qnil;
57
-
58
- // ensure channel is set as output
59
- if (gpio_direction[gpio] != OUTPUT)
60
- {
61
- rb_raise(rb_eRuntimeError, "you must setup the GPIO channel as output "
62
- "first with RPi::GPIO.setup CHANNEL, :as => :output");
63
- return Qnil;
64
- }
65
-
66
- rb_iv_set(self, "@gpio", UINT2NUM(gpio));
67
- rb_iv_set(self, "@running", Qfalse);
68
- PWM_set_frequency(self, frequency);
69
- return self;
70
- }
71
-
72
- // RPi::GPIO::PWM#start
73
- VALUE PWM_start(VALUE self, VALUE duty_cycle)
74
- {
75
- pwm_start(NUM2UINT(rb_iv_get(self, "@gpio")));
76
- PWM_set_duty_cycle(self, duty_cycle);
77
- rb_iv_set(self, "@running", Qtrue);
78
- return self;
79
- }
80
-
81
- // RPi::GPIO::PWM#gpio
82
- VALUE PWM_get_gpio(VALUE self)
83
- {
84
- return rb_iv_get(self, "@gpio");
85
- }
86
-
87
- // RPi::GPIO::PWM#duty_cycle
88
- VALUE PWM_get_duty_cycle(VALUE self)
89
- {
90
- return rb_iv_get(self, "@duty_cycle");
91
- }
92
-
93
- // RPi::GPIO::PWM#duty_cycle=
94
- VALUE PWM_set_duty_cycle(VALUE self, VALUE duty_cycle)
95
- {
96
- float dc = (float) NUM2DBL(duty_cycle);
97
- if (dc < 0.0f || dc > 100.0f)
98
- {
99
- rb_raise(rb_eArgError, "duty cycle must be between 0.0 and 100.0");
100
- return Qnil;
101
- }
102
-
103
- rb_iv_set(self, "@duty_cycle", duty_cycle);
104
- pwm_set_duty_cycle(NUM2UINT(rb_iv_get(self, "@gpio")), dc);
105
- return self;
106
- }
107
-
108
- // RPi::GPIO::PWM#frequency
109
- VALUE PWM_get_frequency(VALUE self)
110
- {
111
- return rb_iv_get(self, "@frequency");
112
- }
113
-
114
- // RPi::GPIO::PWM#frequency=
115
- VALUE PWM_set_frequency(VALUE self, VALUE frequency)
116
- {
117
- float freq = (float) NUM2DBL(frequency);
118
- if (freq <= 0.0f)
119
- {
120
- rb_raise(rb_eArgError, "frequency must be greater than 0.0");
121
- return Qnil;
122
- }
123
-
124
- rb_iv_set(self, "@frequency", frequency);
125
- pwm_set_frequency(NUM2UINT(rb_iv_get(self, "@gpio")), freq);
126
- return self;
127
- }
128
-
129
- // RPi::GPIO::PWM#stop
130
- VALUE PWM_stop(VALUE self)
131
- {
132
- pwm_stop(NUM2UINT(rb_iv_get(self, "@gpio")));
133
- rb_iv_set(self, "@running", Qfalse);
134
- return self;
135
- }
136
-
137
- // RPi::GPIO::PWM#running?
138
- VALUE PWM_get_running(VALUE self)
139
- {
140
- return rb_iv_get(self, "@running");
141
- }
@@ -1,41 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 Nick Lowery
5
-
6
- Copyright (c) 2013-2014 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include "ruby.h"
28
- #include "soft_pwm.h"
29
- #include "common.h"
30
- #include "c_gpio.h"
31
-
32
- void define_pwm_class_stuff(void);
33
- VALUE PWM_initialize(VALUE self, VALUE channel, VALUE frequency);
34
- VALUE PWM_start(VALUE self, VALUE duty_cycle);
35
- VALUE PWM_get_gpio(VALUE self);
36
- VALUE PWM_get_duty_cycle(VALUE self);
37
- VALUE PWM_set_duty_cycle(VALUE self, VALUE duty_cycle);
38
- VALUE PWM_get_frequency(VALUE self);
39
- VALUE PWM_set_frequency(VALUE self, VALUE frequency);
40
- VALUE PWM_stop(VALUE self);
41
- VALUE PWM_get_running(VALUE self);
@@ -1,45 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 Nick Lowery
5
-
6
- Copyright (c) 2013-2014 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include "rpi_gpio.h"
28
- #include "rb_pwm.h"
29
- #include "rb_gpio.h"
30
-
31
- VALUE m_RPi = Qnil;
32
- VALUE m_GPIO = Qnil;
33
-
34
- void Init_rpi_gpio()
35
- {
36
- define_modules();
37
- define_gpio_module_stuff();
38
- define_pwm_class_stuff();
39
- }
40
-
41
- void define_modules(void)
42
- {
43
- m_RPi = rb_define_module("RPi");
44
- m_GPIO = rb_define_module_under(m_RPi, "GPIO");
45
- }
@@ -1,28 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 Nick Lowery
5
-
6
- Copyright (c) 2013-2014 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- void Init_rpi_gpio();
28
- void define_modules(void);
@@ -1,218 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 Nick Lowery
5
-
6
- Copyright (c) 2013-2014 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include <stdlib.h>
28
- #include <pthread.h>
29
- #include <time.h>
30
- #include "c_gpio.h"
31
- #include "soft_pwm.h"
32
- pthread_t threads;
33
-
34
- struct pwm
35
- {
36
- unsigned int gpio;
37
- float freq;
38
- float dutycycle;
39
- float basetime;
40
- float slicetime;
41
- struct timespec req_on, req_off;
42
- int running;
43
- struct pwm *next;
44
- };
45
- struct pwm *pwm_list = NULL;
46
-
47
- void remove_pwm(unsigned int gpio)
48
- {
49
- struct pwm *p = pwm_list;
50
- struct pwm *prev = NULL;
51
- struct pwm *temp;
52
-
53
- while (p != NULL)
54
- {
55
- if (p->gpio == gpio)
56
- {
57
- if (prev == NULL)
58
- pwm_list = p->next;
59
- else
60
- prev->next = p->next;
61
- temp = p;
62
- p = p->next;
63
- free(temp);
64
- } else {
65
- prev = p;
66
- p = p->next;
67
- }
68
- }
69
- }
70
-
71
- void calculate_times(struct pwm *p)
72
- {
73
- long long usec;
74
-
75
- usec = (long long)(p->dutycycle * p->slicetime * 1000.0);
76
- p->req_on.tv_sec = (int)(usec / 1000000LL);
77
- usec -= (long long)p->req_on.tv_sec * 1000000LL;
78
- p->req_on.tv_nsec = (long)usec * 1000L;
79
-
80
- usec = (long long)((100.0-p->dutycycle) * p->slicetime * 1000.0);
81
- p->req_off.tv_sec = (int)(usec / 1000000LL);
82
- usec -= (long long)p->req_off.tv_sec * 1000000LL;
83
- p->req_off.tv_nsec = (long)usec * 1000L;
84
- }
85
-
86
- void full_sleep(struct timespec *req)
87
- {
88
- struct timespec rem = {0};
89
-
90
- if (nanosleep(req,&rem) == -1)
91
- full_sleep(&rem);
92
- }
93
-
94
- void *pwm_thread(void *threadarg)
95
- {
96
- struct pwm *p = (struct pwm *)threadarg;
97
-
98
- while (p->running)
99
- {
100
-
101
- if (p->dutycycle > 0.0)
102
- {
103
- output_gpio(p->gpio, 1);
104
- full_sleep(&p->req_on);
105
- }
106
-
107
- if (p->dutycycle < 100.0)
108
- {
109
- output_gpio(p->gpio, 0);
110
- full_sleep(&p->req_off);
111
- }
112
- }
113
-
114
- // clean up
115
- output_gpio(p->gpio, 0);
116
- remove_pwm(p->gpio);
117
- pthread_exit(NULL);
118
- }
119
-
120
- struct pwm *add_new_pwm(unsigned int gpio)
121
- {
122
- struct pwm *new_pwm;
123
-
124
- new_pwm = malloc(sizeof(struct pwm));
125
- new_pwm->gpio = gpio;
126
- new_pwm->running = 0;
127
- new_pwm->next = NULL;
128
- // default to 1 kHz frequency, dutycycle 0.0
129
- new_pwm->freq = 1000.0;
130
- new_pwm->dutycycle = 0.0;
131
- new_pwm->basetime = 1.0; // 1 ms
132
- new_pwm->slicetime = 0.01; // 0.01 ms
133
- calculate_times(new_pwm);
134
- return new_pwm;
135
- }
136
-
137
- struct pwm *find_pwm(unsigned int gpio)
138
- {
139
- struct pwm *p = pwm_list;
140
-
141
- if (pwm_list == NULL)
142
- {
143
- pwm_list = add_new_pwm(gpio);
144
- return pwm_list;
145
- }
146
-
147
- while (p != NULL)
148
- {
149
- if (p->gpio == gpio)
150
- return p;
151
- if (p->next == NULL)
152
- {
153
- p->next = add_new_pwm(gpio);
154
- return p->next;
155
- }
156
- p = p->next;
157
- }
158
- return NULL;
159
- }
160
-
161
- void pwm_set_duty_cycle(unsigned int gpio, float dutycycle)
162
- {
163
- struct pwm *p;
164
-
165
- if (dutycycle < 0.0 || dutycycle > 100.0)
166
- {
167
- // btc fixme - error
168
- return;
169
- }
170
-
171
- if ((p = find_pwm(gpio)) != NULL)
172
- {
173
- p->dutycycle = dutycycle;
174
- calculate_times(p);
175
- }
176
- }
177
-
178
- void pwm_set_frequency(unsigned int gpio, float freq)
179
- {
180
- struct pwm *p;
181
-
182
- if (freq <= 0.0) // to avoid divide by zero
183
- {
184
- // btc fixme - error
185
- return;
186
- }
187
-
188
- if ((p = find_pwm(gpio)) != NULL)
189
- {
190
- p->basetime = 1000.0 / freq; // calculated in ms
191
- p->slicetime = p->basetime / 100.0;
192
- calculate_times(p);
193
- }
194
- }
195
-
196
- void pwm_start(unsigned int gpio)
197
- {
198
- struct pwm *p;
199
-
200
- if (((p = find_pwm(gpio)) == NULL) || p->running)
201
- return;
202
-
203
- p->running = 1;
204
- if (pthread_create(&threads, NULL, pwm_thread, (void *)p) != 0)
205
- {
206
- // btc fixme - error
207
- p->running = 0;
208
- return;
209
- }
210
- }
211
-
212
- void pwm_stop(unsigned int gpio)
213
- {
214
- struct pwm *p;
215
-
216
- if ((p = find_pwm(gpio)) != NULL)
217
- p->running = 0;
218
- }