bonekit 0.0.2-arm-linux

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ /*
2
+
3
+ hmc5883l_class.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_HMC5883L_CLASS_H__
29
+ #define BONEKIT_HMC5883L_CLASS_H__
30
+
31
+ void BoneKit_HMC5883L_class_init();
32
+
33
+ #endif
data/ext/bonekit/pin.c ADDED
@@ -0,0 +1,133 @@
1
+ /*
2
+
3
+ pin.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 "pin.h"
29
+
30
+ #include "gpio.h"
31
+ #include "adc.h"
32
+
33
+ #include <stdlib.h>
34
+
35
+ pin_t * pin_alloc()
36
+ {
37
+ pin_t * obj;
38
+ obj = malloc(sizeof(struct pin_s));
39
+ return obj;
40
+ }
41
+
42
+ int pin_init(pin_t * obj, unsigned int beaglebone_global_const)
43
+ {
44
+ if(obj)
45
+ {
46
+ int gpio = beaglebone_gpio(beaglebone_global_const);
47
+ int ain = beaglebone_ain(beaglebone_global_const);
48
+
49
+ int is_ain = (ain != -1);
50
+ int is_gpio = (gpio != -1);
51
+
52
+ if(!(is_ain || is_gpio)) // invalid pin
53
+ return -1;
54
+
55
+ obj->_ain = ain;
56
+ obj->_is_ain = is_ain;
57
+ obj->_gpio = gpio;
58
+ obj->_is_gpio = is_gpio;
59
+
60
+ if(is_gpio)
61
+ gpio_export(gpio);
62
+ else if(is_ain)
63
+ adc_enable();
64
+ }
65
+
66
+ return 0;
67
+ }
68
+
69
+ void pin_destroy(pin_t * obj)
70
+ {
71
+ if(obj->_is_gpio)
72
+ gpio_unexport(obj->_gpio);
73
+
74
+ free(obj);
75
+ }
76
+
77
+ int pin_mode(pin_t * obj)
78
+ {
79
+ int mode = -1;
80
+ if(obj->_is_gpio)
81
+ gpio_get_direction(obj->_gpio, &mode);
82
+ return mode;
83
+ }
84
+
85
+ void pin_set_mode(pin_t * obj, int mode)
86
+ {
87
+ if(obj->_is_gpio)
88
+ gpio_set_direction(obj->_gpio, mode);
89
+ }
90
+
91
+ int pin_value(pin_t * obj)
92
+ {
93
+ int value = 0;
94
+
95
+ if(obj->_is_gpio)
96
+ {
97
+ gpio_get_value(obj->_gpio, &value);
98
+ }
99
+ else if(obj->_is_ain)
100
+ {
101
+ int analog_value_raw; // [ADC_MIN_VALUE..ADC_MAX_VALUE]
102
+ adc_get_value(obj->_ain, &analog_value_raw);
103
+ value = analog_value_raw > ADC_THRESHOLD ? HIGH : LOW;
104
+ }
105
+
106
+ return value;
107
+ }
108
+
109
+ float pin_analog_value(pin_t * obj)
110
+ {
111
+ float analog_value = 0.0;
112
+
113
+ if(obj->_is_gpio)
114
+ {
115
+ int value;
116
+ gpio_get_value(obj->_gpio, &value);
117
+ analog_value = (float)value;
118
+ }
119
+ else if(obj->_is_ain)
120
+ {
121
+ int analog_value_raw; // [ADC_MIN_VALUE..ADC_MAX_VALUE]
122
+ adc_get_value(obj->_ain, &analog_value_raw);
123
+ analog_value = (float)(((float)analog_value_raw)/((float)ADC_MAX_VALUE));
124
+ }
125
+
126
+ return analog_value;
127
+ }
128
+
129
+ void pin_set_value(pin_t * obj, int value)
130
+ {
131
+ if(obj->_is_gpio)
132
+ gpio_set_value(obj->_gpio, value);
133
+ }
data/ext/bonekit/pin.h ADDED
@@ -0,0 +1,58 @@
1
+ /*
2
+
3
+ pin.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_PIN_H__
29
+ #define BONEKIT_PIN_H__
30
+
31
+ #ifdef __cplusplus
32
+ extern "C" {
33
+ #endif
34
+
35
+ struct pin_s
36
+ {
37
+ int _is_ain;
38
+ unsigned _ain;
39
+ int _is_gpio;
40
+ unsigned _gpio;
41
+ };
42
+
43
+ typedef struct pin_s pin_t;
44
+
45
+ pin_t * pin_alloc();
46
+ int pin_init(pin_t *, unsigned int);
47
+ void pin_destroy(pin_t *);
48
+ int pin_mode(pin_t *);
49
+ void pin_set_mode(pin_t *, int);
50
+ int pin_value(pin_t *);
51
+ float pin_analog_value(pin_t *);
52
+ void pin_set_value(pin_t *, int);
53
+
54
+ #ifdef __cplusplus
55
+ }
56
+ #endif
57
+
58
+ #endif
@@ -0,0 +1,175 @@
1
+ /*
2
+
3
+ pin_class.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
+
29
+ #include "pin_class.h"
30
+
31
+ #include "ruby.h"
32
+ #include "gpio.h"
33
+ #include "pin.h"
34
+
35
+ VALUE cBoneKit_Pin;
36
+
37
+ static void Pin_free(pin_t * ptr)
38
+ {
39
+ if(ptr == NULL)
40
+ return;
41
+
42
+ pin_destroy(ptr);
43
+ }
44
+
45
+ static VALUE Pin_alloc(VALUE class)
46
+ {
47
+ pin_t * ptr = pin_alloc();
48
+
49
+ VALUE self = Data_Wrap_Struct(class, 0, Pin_free, ptr);
50
+ return self;
51
+ }
52
+
53
+ /*
54
+ * call-seq:
55
+ * initialize(pin, mode=Input) -> Pin
56
+ *
57
+ * Returns a new Pin object with default value of 0
58
+ * @param [Integer] pin the beaglebone pin, identified by header (P8 or P9) and number (1-46) (ie. P9_31)
59
+ * @param [Integer] mode the pin mode to be used (Input or Output) (default is Input)
60
+ */
61
+ static VALUE Pin_initialize(int argc, VALUE* argv, VALUE self)
62
+ {
63
+ if (argc > 2 || argc == 0) // there should only be 1 or 2 arguments
64
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
65
+
66
+ pin_t * ptr;
67
+ Data_Get_Struct(self, pin_t, ptr);
68
+
69
+ if(pin_init(ptr, NUM2UINT(argv[0])) < 0) // pin must support gpio
70
+ rb_raise(rb_eArgError, "invalid pin (%d GPIO not supported)", NUM2UINT(argv[0]));
71
+
72
+ int mode = INPUT;
73
+
74
+ if(argc == 2)
75
+ mode = NUM2INT(argv[1]);
76
+
77
+ pin_set_mode(ptr, mode);
78
+
79
+ return self;
80
+ }
81
+
82
+ /*
83
+ * call-seq:
84
+ * value -> integer
85
+ *
86
+ * Reads the value from the pin. The values are [0,1].
87
+ */
88
+ static VALUE Pin_value(VALUE self)
89
+ {
90
+ pin_t * ptr;
91
+ Data_Get_Struct(self, pin_t, ptr);
92
+
93
+ return INT2NUM(pin_value(ptr));
94
+ }
95
+
96
+ /*
97
+ * call-seq:
98
+ * analog_value -> float
99
+ *
100
+ * Reads the analog value from the pin. The range of values is [0.0..1.0].
101
+ * NOTE: If the pin doesn't support ADC, analog_value is the floating point equivalent of value.
102
+ */
103
+ static VALUE Pin_analog_value(VALUE self)
104
+ {
105
+ pin_t * ptr;
106
+ Data_Get_Struct(self, pin_t, ptr);
107
+
108
+ return rb_float_new(pin_analog_value(ptr));
109
+ }
110
+
111
+
112
+ /*
113
+ * call-seq:
114
+ * value=(value) -> integer
115
+ *
116
+ * Write the value to the pin. The pin mode must be set to Output.
117
+ * Possible values: High, Low.
118
+ */
119
+ static VALUE Pin_set_value(VALUE self, VALUE value)
120
+ {
121
+ pin_t * ptr;
122
+ Data_Get_Struct(self, pin_t, ptr);
123
+ pin_set_value(ptr, NUM2INT(value));
124
+
125
+ return value;
126
+ }
127
+
128
+ /*
129
+ * call-seq:
130
+ * mode -> integer
131
+ *
132
+ * Returns the mode of the pin.
133
+ * Possible modes: Input, Output.
134
+ */
135
+ static VALUE Pin_mode(VALUE self)
136
+ {
137
+ pin_t * ptr;
138
+ Data_Get_Struct(self, pin_t, ptr);
139
+ return INT2NUM(pin_mode(ptr));
140
+ }
141
+
142
+ /*
143
+ * call-seq:
144
+ * mode=(mode) -> integer
145
+ *
146
+ * Sets the mode of the pin.
147
+ * Possible modes: Input, Output.
148
+ */
149
+ static VALUE Pin_set_mode(VALUE self, VALUE value)
150
+ {
151
+ pin_t * ptr;
152
+ Data_Get_Struct(self, pin_t, ptr);
153
+ pin_set_mode(ptr, NUM2INT(value));
154
+
155
+ return value;
156
+ }
157
+
158
+ void BoneKit_Pin_class_init()
159
+ {
160
+ cBoneKit_Pin = rb_define_class("Pin", rb_cObject);
161
+
162
+ rb_define_alloc_func(cBoneKit_Pin, Pin_alloc);
163
+
164
+ rb_define_method(cBoneKit_Pin, "initialize", Pin_initialize, -1);
165
+ rb_define_method(cBoneKit_Pin, "value", Pin_value, 0);
166
+ rb_define_method(cBoneKit_Pin, "analog_value", Pin_analog_value, 0);
167
+ rb_define_method(cBoneKit_Pin, "value=", Pin_set_value, 1);
168
+ rb_define_method(cBoneKit_Pin, "mode", Pin_mode, 0);
169
+ rb_define_method(cBoneKit_Pin, "mode=", Pin_set_mode, 1);
170
+
171
+ rb_define_global_const("High", INT2NUM(HIGH));
172
+ rb_define_global_const("Low", INT2NUM(LOW));
173
+ rb_define_global_const("Input", INT2NUM(INPUT));
174
+ rb_define_global_const("Output", INT2NUM(OUTPUT));
175
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+
3
+ pin_class.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_PIN_CLASS_H__
29
+ #define BONEKIT_PIN_CLASS_H__
30
+
31
+ void BoneKit_Pin_class_init();
32
+
33
+ #endif
@@ -0,0 +1,42 @@
1
+ /*
2
+
3
+ rbinit.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 "beaglebone_global_const.h"
29
+ #include "pin_class.h"
30
+ #include "hmc5883l_class.h"
31
+
32
+ void Init_bonekit(void)
33
+ {
34
+ // Constants
35
+ BoneKit_Beaglebone_global_const_init();
36
+
37
+ // IO
38
+ BoneKit_Pin_class_init();
39
+
40
+ // ICs
41
+ BoneKit_HMC5883L_class_init();
42
+ }
@@ -0,0 +1,3 @@
1
+ module BoneKit
2
+ VERSION = "0.0.2"
3
+ end
data/lib/bonekit.rb ADDED
@@ -0,0 +1,9 @@
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
data/lib/bonekit.so ADDED
Binary file
data/spec/pin_spec.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pin do
4
+
5
+ let :pin do
6
+ Pin.new 30
7
+ end
8
+
9
+ describe "mode" do
10
+ context "when default" do
11
+ it "returns Input" do
12
+ pin.mode.should eq(Input)
13
+ end
14
+ end
15
+
16
+ context "when set to Output" do
17
+ before(:each) { pin.mode = Output }
18
+ it "returns Output" do
19
+ pin.mode.should eq(Output)
20
+ end
21
+ end
22
+
23
+ context "when set to Input" do
24
+ before(:each) { pin.mode = Input }
25
+ it "returns Input" do
26
+ pin.mode.should eq(Input)
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "value" do
32
+ context "when default" do
33
+ it "returns 0" do
34
+ pin.value.should eq(0)
35
+ end
36
+ end
37
+
38
+ context "when default and mode is Output" do
39
+ before(:each) { pin.mode = Output }
40
+ it "returns 0" do
41
+ pin.value.should eq(0)
42
+ end
43
+ end
44
+
45
+ context "when set to 1 and mode is Output" do
46
+ before(:each) {
47
+ pin.mode = Output
48
+ pin.value = 1
49
+ }
50
+ it "returns 1" do
51
+ pin.value.should eq(1)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1 @@
1
+ require 'bonekit'
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'bonekit'
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class PinTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @pin = Pin.new 30
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def test_initialization
13
+ assert_equal(Input, @pin.mode)
14
+ assert_equal("in", File.open('/sys/class/gpio/gpio30/direction').read.chomp)
15
+ assert_equal(Low, @pin.value)
16
+ assert_equal("0", File.open('/sys/class/gpio/gpio30/value').read.chomp)
17
+ end
18
+
19
+ def test_mode
20
+ @pin.mode = Input
21
+ assert_equal(Input, @pin.mode)
22
+ assert_equal("in", File.open('/sys/class/gpio/gpio30/direction').read.chomp)
23
+
24
+ @pin.mode = Output
25
+ assert_equal(Output, @pin.mode)
26
+ assert_equal("out", File.open('/sys/class/gpio/gpio30/direction').read.chomp)
27
+ end
28
+
29
+ def test_value
30
+ @pin.mode = Output # pin must be configured as Output
31
+
32
+ @pin.value = Low
33
+ assert_equal(Low, @pin.value)
34
+ assert_equal("0", File.open('/sys/class/gpio/gpio30/value').read.chomp)
35
+
36
+ @pin.value = High
37
+ assert_equal(High, @pin.value)
38
+ assert_equal("1", File.open('/sys/class/gpio/gpio30/value').read.chomp)
39
+ end
40
+
41
+ end