bonekit 0.0.2-arm-linux → 0.0.3-arm-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/bonekit/pwm.c ADDED
@@ -0,0 +1,235 @@
1
+ /*
2
+
3
+ pwm.c
4
+ BoneKit
5
+
6
+ Copyright (cc) 2012 Luis Laugga.
7
+ Some rights reserved, all wrongs deserved.
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
10
+ this software and associated documentation files (the "Software"), to deal in
11
+ the Software without restriction, including without limitation the rights to
12
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13
+ the Software, and to permit persons to whom the Software is furnished to do so,
14
+ subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ */
27
+
28
+ #include "pwm.h"
29
+
30
+ #include <stdio.h>
31
+ #include <stdlib.h>
32
+ #include <fcntl.h>
33
+ #include <unistd.h>
34
+ #include <string.h>
35
+ #include <sys/types.h>
36
+ #include <dirent.h>
37
+
38
+ int pwm_enable()
39
+ {
40
+ int fd;
41
+ if((fd = open(SLOTS, O_WRONLY)) < 0)
42
+ return -1;
43
+
44
+ const char * am33xx_pwm = "am33xx_pwm";
45
+ write(fd, am33xx_pwm, strlen(am33xx_pwm));
46
+ close(fd);
47
+
48
+ return 0;
49
+ }
50
+
51
+ int pwm_enable_pin(const char * pin)
52
+ {
53
+ if(pwm_enable() < 0) // enable pwm
54
+ return -1;
55
+
56
+ int fd;
57
+ if((fd = open(SLOTS, O_WRONLY)) < 0)
58
+ return -1;
59
+
60
+ char bone_pwm_pin[15];
61
+ snprintf(bone_pwm_pin, 15, "bone_pwm_%s", pin);
62
+ write(fd, bone_pwm_pin, strlen(bone_pwm_pin)); // enable pwm pin
63
+ close(fd);
64
+
65
+ return 0;
66
+ }
67
+
68
+ int pwm_get_key(const char * pin, char ** pwm_key)
69
+ {
70
+ DIR * dd;
71
+ struct dirent * df;
72
+
73
+ dd = opendir(OCP_DIR);
74
+ if(dd != NULL)
75
+ {
76
+ while((df = readdir(dd)))
77
+ {
78
+ if(strstr(df->d_name, pin))
79
+ {
80
+ int pwm_key_len = strlen(df->d_name)+1;
81
+ *pwm_key = (char *)calloc(pwm_key_len, sizeof(char));
82
+ snprintf(*pwm_key, pwm_key_len, "%s", df->d_name);
83
+ closedir(dd);
84
+ return 0;
85
+ }
86
+ }
87
+ closedir (dd);
88
+ }
89
+
90
+ return -1;
91
+ }
92
+
93
+ int pwm_export(const char * pin, char ** pwm_key)
94
+ {
95
+ if(pwm_enable_pin(pin) < 0) // enable pin
96
+ return -1;
97
+
98
+ if(pwm_get_key(pin, pwm_key) < 0) // get key for pin
99
+ return -1;
100
+
101
+ // Set default values
102
+ pwm_set_polarity(*pwm_key, PWM_POLARITY);
103
+ pwm_set_period(*pwm_key, PWM_PERIOD_NS);
104
+
105
+ return 0;
106
+ }
107
+
108
+ int pwm_unexport(char * pwm_key)
109
+ {
110
+ if(pwm_key == NULL)
111
+ return -1;
112
+
113
+ // TODO disable pwm for key
114
+ free(pwm_key);
115
+
116
+ return 0;
117
+ }
118
+
119
+ int pwm_read(char * pwm_key, const char * filename, char * value, unsigned int length)
120
+ {
121
+ if(pwm_key == NULL)
122
+ return -1;
123
+
124
+ int fd, len;
125
+ char filepath[PWM_LEN];
126
+ snprintf(filepath, sizeof(filepath), "%s/%s/%s", OCP_DIR, pwm_key, filename);
127
+
128
+ if((fd = open(filepath, O_RDONLY | O_NONBLOCK)) < 0)
129
+ return -1;
130
+
131
+ lseek(fd, 0, SEEK_SET);
132
+
133
+ read(fd, value, length);
134
+ sscanf(value, "%s\n", value);
135
+ close(fd);
136
+
137
+ return 0;
138
+ }
139
+
140
+ int pwm_write(char * pwm_key, const char * filename, char * value)
141
+ {
142
+ if(pwm_key == NULL)
143
+ return -1;
144
+
145
+ int fd;
146
+ char filepath[PWM_LEN];
147
+ snprintf(filepath, sizeof(filepath), "%s/%s/%s", OCP_DIR, pwm_key, filename);
148
+
149
+ if((fd = open(filepath, O_WRONLY)) < 0)
150
+ return -1;
151
+
152
+ write(fd, value, strlen(value));
153
+ close(fd);
154
+
155
+ return 0;
156
+ }
157
+
158
+ int pwm_set_polarity(char * pwm_key, int polarity)
159
+ {
160
+ if(pwm_key == NULL)
161
+ return -1;
162
+
163
+ char value[PWM_LEN];
164
+ snprintf(value, PWM_LEN, "%d", polarity);
165
+ pwm_write(pwm_key, "polarity", value);
166
+
167
+ return 0;
168
+ }
169
+
170
+ int pwm_get_polarity(char * pwm_key, int * polarity)
171
+ {
172
+ if(pwm_key == NULL)
173
+ return -1;
174
+
175
+ char value[PWM_LEN];
176
+ pwm_read(pwm_key, "polarity", value, PWM_LEN);
177
+ sscanf(value, "%d", polarity);
178
+
179
+ return 0;
180
+ }
181
+
182
+ int pwm_set_period(char * pwm_key, unsigned long period)
183
+ {
184
+ if(pwm_key == NULL)
185
+ return -1;
186
+
187
+ char value[PWM_LEN];
188
+ snprintf(value, PWM_LEN, "%lu", period);
189
+ pwm_write(pwm_key, "period", value);
190
+ }
191
+
192
+ int pwm_get_period(char * pwm_key, unsigned long * period)
193
+ {
194
+ if(pwm_key == NULL)
195
+ return -1;
196
+
197
+ char value[PWM_LEN];
198
+ pwm_read(pwm_key, "period", value, PWM_LEN);
199
+ sscanf(value, "%lu", period);
200
+
201
+ return 0;
202
+ }
203
+
204
+ int pwm_set_duty_cycle(char * pwm_key, double duty_cycle) // duty_cycle interval [0.0..1.0]
205
+ {
206
+ if(pwm_key == NULL)
207
+ return -1;
208
+
209
+ if(duty_cycle < 0.0) // Check duty cycle upper bound
210
+ duty_cycle = 0.0;
211
+
212
+ if(duty_cycle > 1.0) // Check duty cycle lower bound
213
+ duty_cycle = 1.0;
214
+
215
+ char value[PWM_LEN];
216
+ snprintf(value, PWM_LEN, "%lu", (unsigned long)(duty_cycle * PWM_PERIOD_NS));
217
+ pwm_write(pwm_key, "duty", value);
218
+
219
+ return 0;
220
+ }
221
+
222
+ int pwm_get_duty_cycle(char * pwm_key, double * duty_cycle)
223
+ {
224
+ if(pwm_key == NULL)
225
+ return -1;
226
+
227
+ char value[PWM_LEN];
228
+ pwm_read(pwm_key, "duty", value, PWM_LEN);
229
+ unsigned long duty_cycle_ns = 0.0;
230
+ sscanf(value, "%lu", &duty_cycle_ns);
231
+
232
+ *duty_cycle = (double)(duty_cycle_ns/((double)PWM_PERIOD_NS));
233
+
234
+ return 0;
235
+ }
data/ext/bonekit/pwm.h ADDED
@@ -0,0 +1,67 @@
1
+ /*
2
+
3
+ pwm.h
4
+ BoneKit
5
+
6
+ Copyright (cc) 2012 Luis Laugga.
7
+ Some rights reserved, all wrongs deserved.
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
10
+ this software and associated documentation files (the "Software"), to deal in
11
+ the Software without restriction, including without limitation the rights to
12
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13
+ the Software, and to permit persons to whom the Software is furnished to do so,
14
+ subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ */
27
+
28
+ #ifndef BONEKIT_PWM_H__
29
+ #define BONEKIT_PWM_H__
30
+
31
+ #ifdef __cplusplus
32
+ extern "C" {
33
+ #endif
34
+
35
+ #define OCP_DIR "/sys/devices/ocp.2"
36
+ #define SLOTS "/sys/devices/bone_capemgr.8/slots"
37
+
38
+ #define PWM_LEN 64
39
+
40
+ #define PWM_PERIOD_NS 500000 // nanoseconds
41
+ #define PWM_POLARITY 0 // ...
42
+
43
+ int pwm_enable();
44
+ int pwm_enable_pin(const char *);
45
+
46
+ int pwm_get_key(const char *, char **);
47
+
48
+ int pwm_export(const char *, char **);
49
+ int pwm_unexport(char *);
50
+
51
+ int pwm_read(char *, const char * , char *, unsigned int);
52
+ int pwm_write(char *, const char *, char *);
53
+
54
+ int pwm_set_polarity(char *, int);
55
+ int pwm_get_polarity(char *, int *);
56
+
57
+ int pwm_set_period(char *, unsigned long);
58
+ int pwm_get_period(char *, unsigned long *);
59
+
60
+ int pwm_set_duty_cycle(char *, double);
61
+ int pwm_get_duty_cycle(char *, double *);
62
+
63
+ #ifdef __cplusplus
64
+ }
65
+ #endif
66
+
67
+ #endif
Binary file
@@ -1,3 +1,3 @@
1
1
  module BoneKit
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/bonekit.rb CHANGED
@@ -1,9 +1,2 @@
1
- base_path = File.expand_path(File.dirname(__FILE__))
2
- require File.join(base_path, 'bonekit', 'version')
3
- begin
4
- m = /(\d+.\d+)/.match(RUBY_VERSION)
5
- ver = m[1]
6
- require File.join(base_path, 'bonekit', ver, 'bonekit')
7
- rescue LoadError
8
- require File.join(base_path, 'bonekit', 'bonekit')
9
- end
1
+ require 'bonekit/version'
2
+ require 'bonekit/bonekit'
data/spec/pin_spec.rb CHANGED
@@ -2,11 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe Pin do
4
4
 
5
- let :pin do
6
- Pin.new 30
7
- end
8
-
9
5
  describe "mode" do
6
+
7
+ let :pin do
8
+ Pin.new P9_13
9
+ end
10
+
10
11
  context "when default" do
11
12
  it "returns Input" do
12
13
  pin.mode.should eq(Input)
@@ -29,6 +30,11 @@ describe Pin do
29
30
  end
30
31
 
31
32
  describe "value" do
33
+
34
+ let :pin do
35
+ Pin.new P9_11
36
+ end
37
+
32
38
  context "when default" do
33
39
  it "returns 0" do
34
40
  pin.value.should eq(0)
@@ -52,4 +58,38 @@ describe Pin do
52
58
  end
53
59
  end
54
60
  end
61
+
62
+ describe "analog_value" do
63
+
64
+ let :pin do
65
+ Pin.new P9_39 # AIN
66
+ end
67
+
68
+ it "returns value between 0.0 and 1.0" do
69
+ pin.analog_value.should be >= 0.0
70
+ pin.analog_value.should be <= 1.0
71
+ end
72
+ end
73
+
74
+ describe "analog_value=" do
75
+
76
+ let :pin do
77
+ Pin.new P9_42 # PWM
78
+ end
79
+
80
+ context "when default" do
81
+ it "returns 0.0" do
82
+ pin.analog_value.should eq(0.0)
83
+ end
84
+ end
85
+
86
+ context "when set to 1.0" do
87
+ before(:each) {
88
+ pin.analog_value = 1.0
89
+ }
90
+ it "returns 1.0" do
91
+ pin.analog_value.should eq(1.0)
92
+ end
93
+ end
94
+ end
55
95
  end
@@ -0,0 +1,26 @@
1
+ #include "../../ext/bonekit/beaglebone.h"
2
+
3
+ #include <stdio.h>
4
+
5
+ int main()
6
+ {
7
+ beaglebone_t p = P9_42;
8
+
9
+ printf("p %s %x\n", p.pin_name, p.pin_mask);
10
+
11
+ char pin_name[PIN_NAME_LEN];
12
+ int gpio, ain, pwm_mux_mode;
13
+
14
+ beaglebone_pin_name(P9_42, pin_name);
15
+ gpio = beaglebone_gpio(P9_42);
16
+ ain = beaglebone_ain(P9_42);
17
+ pwm_mux_mode = beaglebone_pwm_mux_mode(P9_42);
18
+
19
+ printf("%s %d %d %d\n", pin_name, gpio, ain, pwm_mux_mode);
20
+
21
+ beaglebone_t u = USR1;
22
+
23
+ printf("u %s %x\n", u.pin_name, u.pin_mask);
24
+
25
+ return 0;
26
+ }
@@ -0,0 +1,56 @@
1
+ #include "../../ext/bonekit/pin.h"
2
+
3
+ #include <stdio.h>
4
+
5
+ int main()
6
+ {
7
+ beaglebone_t p = P9_42;
8
+
9
+ pin_t * pin = pin_alloc();
10
+ pin_init(pin, P9_42);
11
+
12
+ pin_set_analog_value(pin, 1.0);
13
+
14
+ sleep(1);
15
+
16
+ pin_set_analog_value(pin, 0.5);
17
+
18
+ sleep(1);
19
+
20
+ pin_set_analog_value(pin, 0.0);
21
+
22
+ pin_destroy(pin);
23
+
24
+ pin = pin_alloc();
25
+ pin_init(pin, P9_13);
26
+
27
+ pin_set_mode(pin, OUTPUT);
28
+ pin_set_value(pin, HIGH);
29
+
30
+ sleep(1);
31
+
32
+ pin_set_value(pin, LOW);
33
+
34
+ sleep(1);
35
+
36
+ pin_set_value(pin, HIGH);
37
+
38
+ sleep(1);
39
+
40
+ pin_destroy(pin);
41
+
42
+ pin = pin_alloc();
43
+ pin_init(pin, P9_39);
44
+
45
+ printf("analog value %f\n", pin_analog_value(pin));
46
+
47
+ sleep(1);
48
+
49
+ printf("analog value %f\n", pin_analog_value(pin));
50
+
51
+ sleep(1);
52
+
53
+ pin_destroy(pin);
54
+
55
+ return 0;
56
+ }
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class PinAdcTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @pin = Pin.new P9_39 # AIN 0
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def test_analog_value
13
+ assert((@pin.analog_value - (File.open('/sys/bus/iio/devices/iio:device0/in_voltage0_raw').read.chomp.to_f/4095.0)).abs < 0.001)
14
+ end
15
+
16
+ end
@@ -1,9 +1,9 @@
1
1
  require 'test_helper'
2
2
 
3
- class PinTest < Test::Unit::TestCase
3
+ class PinGpioTest < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
- @pin = Pin.new 30
6
+ @pin = Pin.new P9_11
7
7
  end
8
8
 
9
9
  def teardown
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class PinPwmTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @pin = Pin.new P9_42 # PWM
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def ocp_pwm_value
13
+ File.open('/sys/devices/ocp.2/pwm_test_P9_42.14/duty').read.chomp.to_f/File.open('/sys/devices/ocp.2/pwm_test_P9_42.14/period').read.chomp.to_f
14
+ end
15
+
16
+ def test_set_analog_value
17
+ @pin.analog_value = 0.0
18
+ assert_equal(@pin.analog_value, ocp_pwm_value)
19
+
20
+ @pin.analog_value = 0.5
21
+ assert_equal(@pin.analog_value, ocp_pwm_value)
22
+
23
+ @pin.analog_value = 1.0
24
+ assert_equal(@pin.analog_value, ocp_pwm_value)
25
+
26
+ @pin.analog_value = -0.1
27
+ assert_equal(0.0, @pin.analog_value)
28
+ assert_equal(0.0, ocp_pwm_value)
29
+
30
+
31
+ @pin.analog_value = 1.1
32
+ assert_equal(1.0, @pin.analog_value)
33
+ assert_equal(1.0, ocp_pwm_value)
34
+ end
35
+
36
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bonekit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: arm-linux
6
6
  authors:
7
7
  - Luis Laugga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-23 00:00:00.000000000 Z
11
+ date: 2013-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,16 +44,16 @@ dependencies:
44
44
  requirements:
45
45
  - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 0.9.0
47
+ version: 0.9.1
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
- version: 0.9.0
54
+ version: 0.9.1
55
55
  - !ruby/object:Gem::Dependency
56
- name: rpec
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ! '>='
@@ -95,6 +95,7 @@ files:
95
95
  - Rakefile
96
96
  - bonekit.gemspec
97
97
  - examples/analog/analog_read.rb
98
+ - examples/analog/fading.rb
98
99
  - examples/basics/blink.rb
99
100
  - examples/basics/digital_read.rb
100
101
  - examples/basics/switch.rb
@@ -115,14 +116,20 @@ files:
115
116
  - ext/bonekit/pin.h
116
117
  - ext/bonekit/pin_class.c
117
118
  - ext/bonekit/pin_class.h
119
+ - ext/bonekit/pwm.c
120
+ - ext/bonekit/pwm.h
118
121
  - ext/bonekit/rbinit.c
119
122
  - lib/bonekit.rb
120
123
  - lib/bonekit/version.rb
121
124
  - spec/pin_spec.rb
122
125
  - spec/spec_helper.rb
126
+ - test/bonekit-c/beaglebone_test.c
127
+ - test/bonekit-c/pin_test.c
123
128
  - test/test_helper.rb
124
- - test/unit/pin_test.rb
125
- - lib/bonekit.so
129
+ - test/unit/pin_adc_test.rb
130
+ - test/unit/pin_gpio_test.rb
131
+ - test/unit/pin_pwm_test.rb
132
+ - lib/bonekit/bonekit.so
126
133
  homepage: http://laugga.com/bonekit
127
134
  licenses:
128
135
  - MIT
@@ -150,5 +157,9 @@ summary: Physical interaction toolkit for the beaglebone.
150
157
  test_files:
151
158
  - spec/pin_spec.rb
152
159
  - spec/spec_helper.rb
160
+ - test/bonekit-c/beaglebone_test.c
161
+ - test/bonekit-c/pin_test.c
153
162
  - test/test_helper.rb
154
- - test/unit/pin_test.rb
163
+ - test/unit/pin_adc_test.rb
164
+ - test/unit/pin_gpio_test.rb
165
+ - test/unit/pin_pwm_test.rb
data/lib/bonekit.so DELETED
Binary file