bonekit 0.0.2-arm-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +20 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +91 -0
- data/Rakefile +43 -0
- data/bonekit.gemspec +29 -0
- data/examples/analog/analog_read.rb +9 -0
- data/examples/basics/blink.rb +10 -0
- data/examples/basics/digital_read.rb +9 -0
- data/examples/basics/switch.rb +10 -0
- data/ext/bonekit/adc.c +92 -0
- data/ext/bonekit/adc.h +53 -0
- data/ext/bonekit/beaglebone.c +46 -0
- data/ext/bonekit/beaglebone.h +141 -0
- data/ext/bonekit/beaglebone_global_const.c +132 -0
- data/ext/bonekit/beaglebone_global_const.h +33 -0
- data/ext/bonekit/extconf.rb +32 -0
- data/ext/bonekit/gpio.c +147 -0
- data/ext/bonekit/gpio.h +60 -0
- data/ext/bonekit/hmc5883l.c +107 -0
- data/ext/bonekit/hmc5883l.h +92 -0
- data/ext/bonekit/hmc5883l_class.c +66 -0
- data/ext/bonekit/hmc5883l_class.h +33 -0
- data/ext/bonekit/pin.c +133 -0
- data/ext/bonekit/pin.h +58 -0
- data/ext/bonekit/pin_class.c +175 -0
- data/ext/bonekit/pin_class.h +33 -0
- data/ext/bonekit/rbinit.c +42 -0
- data/lib/bonekit/version.rb +3 -0
- data/lib/bonekit.rb +9 -0
- data/lib/bonekit.so +0 -0
- data/spec/pin_spec.rb +55 -0
- data/spec/spec_helper.rb +1 -0
- data/test/test_helper.rb +2 -0
- data/test/unit/pin_test.rb +41 -0
- metadata +154 -0
@@ -0,0 +1,132 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
beaglebone_global_const.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 "beaglebone_global_const.h"
|
30
|
+
|
31
|
+
#include "ruby.h"
|
32
|
+
#include "beaglebone.h"
|
33
|
+
|
34
|
+
void BoneKit_Beaglebone_global_const_init()
|
35
|
+
{
|
36
|
+
rb_define_global_const("USR0", INT2NUM(USR0));
|
37
|
+
rb_define_global_const("USR1", INT2NUM(USR1));
|
38
|
+
rb_define_global_const("USR2", INT2NUM(USR2));
|
39
|
+
rb_define_global_const("USR3", INT2NUM(USR3));
|
40
|
+
rb_define_global_const("P8_1", INT2NUM(P8_1));
|
41
|
+
rb_define_global_const("P8_2", INT2NUM(P8_2));
|
42
|
+
rb_define_global_const("P8_3", INT2NUM(P8_3));
|
43
|
+
rb_define_global_const("P8_4", INT2NUM(P8_4));
|
44
|
+
rb_define_global_const("P8_5", INT2NUM(P8_5));
|
45
|
+
rb_define_global_const("P8_6", INT2NUM(P8_6));
|
46
|
+
rb_define_global_const("P8_7", INT2NUM(P8_7));
|
47
|
+
rb_define_global_const("P8_8", INT2NUM(P8_8));
|
48
|
+
rb_define_global_const("P8_9", INT2NUM(P8_9));
|
49
|
+
rb_define_global_const("P8_10", INT2NUM(P8_10));
|
50
|
+
rb_define_global_const("P8_11", INT2NUM(P8_11));
|
51
|
+
rb_define_global_const("P8_12", INT2NUM(P8_12));
|
52
|
+
rb_define_global_const("P8_13", INT2NUM(P8_13));
|
53
|
+
rb_define_global_const("P8_14", INT2NUM(P8_14));
|
54
|
+
rb_define_global_const("P8_15", INT2NUM(P8_15));
|
55
|
+
rb_define_global_const("P8_16", INT2NUM(P8_16));
|
56
|
+
rb_define_global_const("P8_17", INT2NUM(P8_17));
|
57
|
+
rb_define_global_const("P8_18", INT2NUM(P8_18));
|
58
|
+
rb_define_global_const("P8_19", INT2NUM(P8_19));
|
59
|
+
rb_define_global_const("P8_20", INT2NUM(P8_20));
|
60
|
+
rb_define_global_const("P8_21", INT2NUM(P8_21));
|
61
|
+
rb_define_global_const("P8_22", INT2NUM(P8_22));
|
62
|
+
rb_define_global_const("P8_23", INT2NUM(P8_23));
|
63
|
+
rb_define_global_const("P8_24", INT2NUM(P8_24));
|
64
|
+
rb_define_global_const("P8_25", INT2NUM(P8_25));
|
65
|
+
rb_define_global_const("P8_26", INT2NUM(P8_26));
|
66
|
+
rb_define_global_const("P8_27", INT2NUM(P8_27));
|
67
|
+
rb_define_global_const("P8_28", INT2NUM(P8_28));
|
68
|
+
rb_define_global_const("P8_29", INT2NUM(P8_29));
|
69
|
+
rb_define_global_const("P8_30", INT2NUM(P8_30));
|
70
|
+
rb_define_global_const("P8_31", INT2NUM(P8_31));
|
71
|
+
rb_define_global_const("P8_32", INT2NUM(P8_32));
|
72
|
+
rb_define_global_const("P8_33", INT2NUM(P8_33));
|
73
|
+
rb_define_global_const("P8_34", INT2NUM(P8_34));
|
74
|
+
rb_define_global_const("P8_35", INT2NUM(P8_35));
|
75
|
+
rb_define_global_const("P8_36", INT2NUM(P8_36));
|
76
|
+
rb_define_global_const("P8_37", INT2NUM(P8_37));
|
77
|
+
rb_define_global_const("P8_38", INT2NUM(P8_38));
|
78
|
+
rb_define_global_const("P8_39", INT2NUM(P8_39));
|
79
|
+
rb_define_global_const("P8_40", INT2NUM(P8_40));
|
80
|
+
rb_define_global_const("P8_41", INT2NUM(P8_41));
|
81
|
+
rb_define_global_const("P8_42", INT2NUM(P8_42));
|
82
|
+
rb_define_global_const("P8_43", INT2NUM(P8_43));
|
83
|
+
rb_define_global_const("P8_44", INT2NUM(P8_44));
|
84
|
+
rb_define_global_const("P8_45", INT2NUM(P8_45));
|
85
|
+
rb_define_global_const("P8_46", INT2NUM(P8_46));
|
86
|
+
rb_define_global_const("P9_1", INT2NUM(P9_1));
|
87
|
+
rb_define_global_const("P9_2", INT2NUM(P9_2));
|
88
|
+
rb_define_global_const("P9_3", INT2NUM(P9_3));
|
89
|
+
rb_define_global_const("P9_4", INT2NUM(P9_4));
|
90
|
+
rb_define_global_const("P9_5", INT2NUM(P9_5));
|
91
|
+
rb_define_global_const("P9_6", INT2NUM(P9_6));
|
92
|
+
rb_define_global_const("P9_7", INT2NUM(P9_7));
|
93
|
+
rb_define_global_const("P9_8", INT2NUM(P9_8));
|
94
|
+
rb_define_global_const("P9_9", INT2NUM(P9_9));
|
95
|
+
rb_define_global_const("P9_10", INT2NUM(P9_10));
|
96
|
+
rb_define_global_const("P9_11", INT2NUM(P9_11));
|
97
|
+
rb_define_global_const("P9_12", INT2NUM(P9_12));
|
98
|
+
rb_define_global_const("P9_13", INT2NUM(P9_13));
|
99
|
+
rb_define_global_const("P9_14", INT2NUM(P9_14));
|
100
|
+
rb_define_global_const("P9_15", INT2NUM(P9_15));
|
101
|
+
rb_define_global_const("P9_16", INT2NUM(P9_16));
|
102
|
+
rb_define_global_const("P9_17", INT2NUM(P9_17));
|
103
|
+
rb_define_global_const("P9_18", INT2NUM(P9_18));
|
104
|
+
rb_define_global_const("P9_19", INT2NUM(P9_19));
|
105
|
+
rb_define_global_const("P9_20", INT2NUM(P9_20));
|
106
|
+
rb_define_global_const("P9_21", INT2NUM(P9_21));
|
107
|
+
rb_define_global_const("P9_22", INT2NUM(P9_22));
|
108
|
+
rb_define_global_const("P9_23", INT2NUM(P9_23));
|
109
|
+
rb_define_global_const("P9_24", INT2NUM(P9_24));
|
110
|
+
rb_define_global_const("P9_25", INT2NUM(P9_25));
|
111
|
+
rb_define_global_const("P9_26", INT2NUM(P9_26));
|
112
|
+
rb_define_global_const("P9_27", INT2NUM(P9_27));
|
113
|
+
rb_define_global_const("P9_28", INT2NUM(P9_28));
|
114
|
+
rb_define_global_const("P9_29", INT2NUM(P9_29));
|
115
|
+
rb_define_global_const("P9_30", INT2NUM(P9_30));
|
116
|
+
rb_define_global_const("P9_31", INT2NUM(P9_31));
|
117
|
+
rb_define_global_const("P9_32", INT2NUM(P9_32));
|
118
|
+
rb_define_global_const("P9_33", INT2NUM(P9_33));
|
119
|
+
rb_define_global_const("P9_34", INT2NUM(P9_34));
|
120
|
+
rb_define_global_const("P9_35", INT2NUM(P9_35));
|
121
|
+
rb_define_global_const("P9_36", INT2NUM(P9_36));
|
122
|
+
rb_define_global_const("P9_37", INT2NUM(P9_37));
|
123
|
+
rb_define_global_const("P9_38", INT2NUM(P9_38));
|
124
|
+
rb_define_global_const("P9_39", INT2NUM(P9_39));
|
125
|
+
rb_define_global_const("P9_40", INT2NUM(P9_40));
|
126
|
+
rb_define_global_const("P9_41", INT2NUM(P9_41));
|
127
|
+
rb_define_global_const("P9_42", INT2NUM(P9_42));
|
128
|
+
rb_define_global_const("P9_43", INT2NUM(P9_43));
|
129
|
+
rb_define_global_const("P9_44", INT2NUM(P9_44));
|
130
|
+
rb_define_global_const("P9_45", INT2NUM(P9_45));
|
131
|
+
rb_define_global_const("P9_46", INT2NUM(P9_46));
|
132
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
beaglebone_global_const.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_BEAGLEBONE_GLOBAL_CONST_H__
|
29
|
+
#define BONEKIT_BEAGLEBONE_GLOBAL_CONST_H__
|
30
|
+
|
31
|
+
void BoneKit_Beaglebone_global_const_init();
|
32
|
+
|
33
|
+
#endif
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# extconf.rb
|
3
|
+
# BoneKit
|
4
|
+
#
|
5
|
+
# Copyright (c) 2012 Luis Laugga.
|
6
|
+
# Some rights reserved, all wrongs deserved.
|
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 of
|
12
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
13
|
+
# 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, FITNESS
|
20
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
21
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
22
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
23
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
#
|
25
|
+
|
26
|
+
require 'mkmf'
|
27
|
+
|
28
|
+
create_makefile('bonekit/bonekit')
|
29
|
+
|
30
|
+
require 'rake/extensiontask'
|
31
|
+
|
32
|
+
Rake::ExtensionTask.new('bonekit')
|
data/ext/bonekit/gpio.c
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
gpio.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 "gpio.h"
|
29
|
+
|
30
|
+
#include <stdio.h>
|
31
|
+
#include <stdlib.h>
|
32
|
+
#include <fcntl.h>
|
33
|
+
#include <unistd.h>
|
34
|
+
#include <string.h>
|
35
|
+
|
36
|
+
int gpio_read(unsigned int gpio, const char * filename, char * value, unsigned int length)
|
37
|
+
{
|
38
|
+
int fd, len;
|
39
|
+
char filepath[GPIO_LEN];
|
40
|
+
snprintf(filepath, sizeof(filepath), "%s/gpio%d/%s", GPIO_DIR, gpio, filename);
|
41
|
+
|
42
|
+
if((fd = open(filepath, O_RDONLY | O_NONBLOCK)) < 0)
|
43
|
+
return -1;
|
44
|
+
|
45
|
+
lseek(fd, 0, SEEK_SET);
|
46
|
+
read(fd, value, length);
|
47
|
+
close(fd);
|
48
|
+
|
49
|
+
return 0;
|
50
|
+
}
|
51
|
+
|
52
|
+
int gpio_write(unsigned int gpio, const char * filename, char * value)
|
53
|
+
{
|
54
|
+
int fd;
|
55
|
+
char filepath[GPIO_LEN];
|
56
|
+
snprintf(filepath, sizeof(filepath), "%s/gpio%d/%s", GPIO_DIR, gpio, filename);
|
57
|
+
|
58
|
+
if((fd = open(filepath, O_WRONLY)) < 0)
|
59
|
+
return -1;
|
60
|
+
|
61
|
+
write(fd, value, strlen(value));
|
62
|
+
close(fd);
|
63
|
+
|
64
|
+
return 0;
|
65
|
+
}
|
66
|
+
|
67
|
+
int gpio_export(unsigned int gpio)
|
68
|
+
{
|
69
|
+
int fd, len;
|
70
|
+
char str_gpio[10];
|
71
|
+
|
72
|
+
if((fd = open("/sys/class/gpio/export", O_WRONLY)) < 0)
|
73
|
+
return -1;
|
74
|
+
|
75
|
+
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
|
76
|
+
write(fd, str_gpio, len);
|
77
|
+
close(fd);
|
78
|
+
}
|
79
|
+
|
80
|
+
int gpio_unexport(unsigned int gpio)
|
81
|
+
{
|
82
|
+
int fd, len;
|
83
|
+
char str_gpio[10];
|
84
|
+
|
85
|
+
if((fd = open("/sys/class/gpio/unexport", O_WRONLY)) < 0)
|
86
|
+
return -1;
|
87
|
+
|
88
|
+
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
|
89
|
+
write(fd, str_gpio, len);
|
90
|
+
close(fd);
|
91
|
+
}
|
92
|
+
|
93
|
+
int gpio_set_direction(unsigned int gpio, unsigned int direction)
|
94
|
+
{
|
95
|
+
if(direction == OUTPUT)
|
96
|
+
{
|
97
|
+
if(gpio_write(gpio, "direction", "out") < 0)
|
98
|
+
return -1;
|
99
|
+
}
|
100
|
+
else
|
101
|
+
{
|
102
|
+
if(gpio_write(gpio, "direction", "in") < 0)
|
103
|
+
return -1;
|
104
|
+
}
|
105
|
+
|
106
|
+
return 0;
|
107
|
+
}
|
108
|
+
|
109
|
+
int gpio_get_direction(unsigned int gpio, unsigned int * direction)
|
110
|
+
{
|
111
|
+
const unsigned int length = 3;
|
112
|
+
char buffer[length];
|
113
|
+
|
114
|
+
if(gpio_read(gpio, "direction", buffer, length) < 0)
|
115
|
+
return -1;
|
116
|
+
|
117
|
+
if(strcmp(buffer, "out") == 0)
|
118
|
+
*direction = OUTPUT;
|
119
|
+
else
|
120
|
+
*direction = INPUT;
|
121
|
+
|
122
|
+
return 0;
|
123
|
+
}
|
124
|
+
|
125
|
+
int gpio_set_value(unsigned int gpio, unsigned int value)
|
126
|
+
{
|
127
|
+
char buffer[GPIO_LEN];
|
128
|
+
sprintf(buffer, "%d", value); // convert value to string, decimal base
|
129
|
+
|
130
|
+
if(gpio_write(gpio, "value", buffer) < 0)
|
131
|
+
return -1;
|
132
|
+
|
133
|
+
return 0;
|
134
|
+
}
|
135
|
+
|
136
|
+
int gpio_get_value(unsigned int gpio, unsigned int * value)
|
137
|
+
{
|
138
|
+
const unsigned int length = 2;
|
139
|
+
char buffer[length];
|
140
|
+
|
141
|
+
if(gpio_read(gpio, "value", buffer, length) < 0)
|
142
|
+
return -1;
|
143
|
+
|
144
|
+
*value = atoi(buffer);
|
145
|
+
|
146
|
+
return 0;
|
147
|
+
}
|
data/ext/bonekit/gpio.h
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
gpio.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_GPIO_H__
|
29
|
+
#define BONEKIT_GPIO_H__
|
30
|
+
|
31
|
+
#ifdef __cplusplus
|
32
|
+
extern "C" {
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#define INPUT 0
|
36
|
+
#define OUTPUT 1
|
37
|
+
|
38
|
+
#define HIGH 1
|
39
|
+
#define LOW 0
|
40
|
+
|
41
|
+
#define GPIO_DIR "/sys/class/gpio"
|
42
|
+
#define GPIO_LEN 64
|
43
|
+
|
44
|
+
int gpio_read(unsigned int, const char * , char *, unsigned int);
|
45
|
+
int gpio_write(unsigned int, const char *, char *);
|
46
|
+
|
47
|
+
int gpio_export(unsigned int);
|
48
|
+
int gpio_unexport(unsigned int);
|
49
|
+
|
50
|
+
int gpio_set_direction(unsigned int, unsigned int);
|
51
|
+
int gpio_get_direction(unsigned int, unsigned int*);
|
52
|
+
|
53
|
+
int gpio_set_value(unsigned int, unsigned int);
|
54
|
+
int gpio_get_value(unsigned int, unsigned int*);
|
55
|
+
|
56
|
+
#ifdef __cplusplus
|
57
|
+
}
|
58
|
+
#endif
|
59
|
+
|
60
|
+
#endif
|
@@ -0,0 +1,107 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
hmc5883l.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 "hmc5883l.h"
|
29
|
+
|
30
|
+
#define M_PI 3.14159265358979323846
|
31
|
+
|
32
|
+
void selectDevice(int fd, int addr, char * name)
|
33
|
+
{
|
34
|
+
if (ioctl(fd, I2C_SLAVE, addr) < 0)
|
35
|
+
{
|
36
|
+
fprintf(stderr, "%s not present\n", name);
|
37
|
+
exit(1);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
void writeToDevice(int fd, int reg, int val)
|
42
|
+
{
|
43
|
+
char buf[2];
|
44
|
+
buf[0]=reg;
|
45
|
+
buf[1]=val;
|
46
|
+
|
47
|
+
if (write(fd, buf, 2) != 2)
|
48
|
+
{
|
49
|
+
fprintf(stderr, "Can't write to device\n");
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
void readFromDevice(int fd, uint8_t * buf, int len)
|
54
|
+
{
|
55
|
+
int readRegister = 0x03;
|
56
|
+
if((write(fd, &readRegister, 1)) != 1)
|
57
|
+
{
|
58
|
+
//error
|
59
|
+
}
|
60
|
+
else
|
61
|
+
{
|
62
|
+
if(read(fd, buf, len) != len)
|
63
|
+
{
|
64
|
+
//error
|
65
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
hmc5883l_t * hmc5883l_create()
|
70
|
+
{
|
71
|
+
hmc5883l_t * obj;
|
72
|
+
obj = malloc(sizeof(struct hmc5883l_s));
|
73
|
+
if(obj)
|
74
|
+
{
|
75
|
+
obj->_scale = 1;
|
76
|
+
|
77
|
+
if ((obj->_fd = open("/dev/i2c-1", O_RDWR)) < 0)
|
78
|
+
{
|
79
|
+
// Open port for reading and writing
|
80
|
+
//printf("HMC5883L::Failed to open i2c bus\n");
|
81
|
+
}
|
82
|
+
|
83
|
+
selectDevice(obj->_fd, HMC5883L_Address, HMC5883L_Name);
|
84
|
+
writeToDevice(obj->_fd, ModeRegister, Measurement_Continuous);
|
85
|
+
}
|
86
|
+
|
87
|
+
return obj;
|
88
|
+
}
|
89
|
+
|
90
|
+
void hmc5883l_destroy(hmc5883l_t * obj)
|
91
|
+
{
|
92
|
+
free(obj);
|
93
|
+
}
|
94
|
+
|
95
|
+
float hmc5883l_heading(hmc5883l_t * obj)
|
96
|
+
{
|
97
|
+
unsigned char buf[16];
|
98
|
+
readFromDevice(obj->_fd, buf, 6);
|
99
|
+
|
100
|
+
short x = (buf[0] << 8) | buf[1];
|
101
|
+
short y = (buf[4] << 8) | buf[5];
|
102
|
+
short z = (buf[2] << 8) | buf[3];
|
103
|
+
|
104
|
+
float angle = atan2(y, x) * 180 / M_PI;
|
105
|
+
|
106
|
+
return angle;
|
107
|
+
}
|
@@ -0,0 +1,92 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
hmc5883l.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_H__
|
29
|
+
#define BONEKIT_HMC5883L_H__
|
30
|
+
|
31
|
+
#ifdef __cplusplus
|
32
|
+
extern "C" {
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#include <stdio.h>
|
36
|
+
#include <stdlib.h>
|
37
|
+
#include <stdint.h>
|
38
|
+
#include <fcntl.h>
|
39
|
+
#include <unistd.h>
|
40
|
+
#include <string.h>
|
41
|
+
#include <sys/ioctl.h>
|
42
|
+
#include <sys/types.h>
|
43
|
+
#include <sys/stat.h>
|
44
|
+
#include <linux/i2c-dev.h>
|
45
|
+
#include <math.h>
|
46
|
+
|
47
|
+
#define HMC5883L_Address 0x1E
|
48
|
+
#define HMC5883L_Name "HMC5883L"
|
49
|
+
#define ConfigurationRegisterA 0x00
|
50
|
+
#define ConfigurationRegisterB 0x01
|
51
|
+
#define ModeRegister 0x02
|
52
|
+
#define DataRegisterBegin 0x03
|
53
|
+
|
54
|
+
#define Measurement_Continuous 0x00
|
55
|
+
#define Measurement_SingleShot 0x01
|
56
|
+
#define Measurement_Idle 0x03
|
57
|
+
|
58
|
+
#define ErrorCode_1 "Scalenotvalid"//Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1"
|
59
|
+
#define ErrorCode_1_Num 1
|
60
|
+
|
61
|
+
typedef struct
|
62
|
+
{
|
63
|
+
float XAxis;
|
64
|
+
float YAxis;
|
65
|
+
float ZAxis;
|
66
|
+
} hmc5883l_scaled_t;
|
67
|
+
|
68
|
+
typedef struct
|
69
|
+
{
|
70
|
+
int XAxis;
|
71
|
+
int YAxis;
|
72
|
+
int ZAxis;
|
73
|
+
} hmc5883l_raw_t;
|
74
|
+
|
75
|
+
struct hmc5883l_s
|
76
|
+
{
|
77
|
+
float _scale;
|
78
|
+
float _fd;
|
79
|
+
float _buffer[32];
|
80
|
+
};
|
81
|
+
|
82
|
+
typedef struct hmc5883l_s hmc5883l_t;
|
83
|
+
|
84
|
+
hmc5883l_t * hmc5883l_create();
|
85
|
+
void hmc5883l_destroy(hmc5883l_t *);
|
86
|
+
float hmc5883l_heading(hmc5883l_t *);
|
87
|
+
|
88
|
+
#ifdef __cplusplus
|
89
|
+
}
|
90
|
+
#endif
|
91
|
+
|
92
|
+
#endif
|
@@ -0,0 +1,66 @@
|
|
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
|
+
#include "hmc5883l_class.h"
|
29
|
+
|
30
|
+
#include "ruby.h"
|
31
|
+
#include "hmc5883l.h"
|
32
|
+
|
33
|
+
VALUE cBoneKit_HMC5883L;
|
34
|
+
|
35
|
+
static void HMC5883L_free(void * ptr)
|
36
|
+
{
|
37
|
+
if(ptr == NULL)
|
38
|
+
return;
|
39
|
+
|
40
|
+
hmc5883l_destroy(ptr);
|
41
|
+
}
|
42
|
+
|
43
|
+
static VALUE HMC5883L_new(VALUE class)
|
44
|
+
{
|
45
|
+
hmc5883l_t * ptr = hmc5883l_create();
|
46
|
+
VALUE wrap_struct = Data_Wrap_Struct(class, 0, HMC5883L_free, ptr);
|
47
|
+
return wrap_struct;
|
48
|
+
}
|
49
|
+
|
50
|
+
static VALUE HMC5883L_heading(VALUE self)
|
51
|
+
{
|
52
|
+
double value = 0.0f;
|
53
|
+
hmc5883l_t * ptr;
|
54
|
+
Data_Get_Struct(self, hmc5883l_t, ptr);
|
55
|
+
value = hmc5883l_heading(ptr);
|
56
|
+
return rb_float_new(value);
|
57
|
+
}
|
58
|
+
|
59
|
+
void BoneKit_HMC5883L_class_init()
|
60
|
+
{
|
61
|
+
cBoneKit_HMC5883L = rb_define_class("HMC5883L", rb_cObject);
|
62
|
+
|
63
|
+
rb_define_singleton_method(cBoneKit_HMC5883L, "new", HMC5883L_new, 0);
|
64
|
+
|
65
|
+
rb_define_method(cBoneKit_HMC5883L, "heading", HMC5883L_heading, 0);
|
66
|
+
}
|