rpi_gpio 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
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
+ }
@@ -0,0 +1,28 @@
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);
@@ -0,0 +1,218 @@
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
+ }
@@ -0,0 +1,32 @@
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
+ /* Software PWM using threads */
28
+
29
+ void pwm_set_duty_cycle(unsigned int gpio, float dutycycle);
30
+ void pwm_set_frequency(unsigned int gpio, float freq);
31
+ void pwm_start(unsigned int gpio);
32
+ void pwm_stop(unsigned int gpio);
data/lib/rpi_gpio.so ADDED
Binary file
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rpi_gpio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Nick Lowery
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby conversion of RPi.GPIO Python module
14
+ email: nick.a.lowery@gmail.com
15
+ executables: []
16
+ extensions:
17
+ - ext/rpi_gpio/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - ext/rpi_gpio/c_gpio.c
26
+ - ext/rpi_gpio/c_gpio.h
27
+ - ext/rpi_gpio/common.c
28
+ - ext/rpi_gpio/common.h
29
+ - ext/rpi_gpio/constants.c
30
+ - ext/rpi_gpio/constants.h
31
+ - ext/rpi_gpio/cpuinfo.c
32
+ - ext/rpi_gpio/cpuinfo.h
33
+ - ext/rpi_gpio/event_gpio.c
34
+ - ext/rpi_gpio/event_gpio.h
35
+ - ext/rpi_gpio/extconf.rb
36
+ - ext/rpi_gpio/rb_gpio.c
37
+ - ext/rpi_gpio/rb_gpio.h
38
+ - ext/rpi_gpio/rb_pwm.c
39
+ - ext/rpi_gpio/rb_pwm.h
40
+ - ext/rpi_gpio/rpi_gpio.c
41
+ - ext/rpi_gpio/rpi_gpio.h
42
+ - ext/rpi_gpio/soft_pwm.c
43
+ - ext/rpi_gpio/soft_pwm.h
44
+ - lib/rpi_gpio.so
45
+ homepage: https://github.com/ClockVapor/rpi_gpio
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.4.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Ruby conversion of RPi.GPIO Python module
69
+ test_files: []