bonekit 0.0.2-arm-linux → 0.0.3-arm-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +16 -13
- data/Rakefile +4 -1
- data/bonekit.gemspec +2 -2
- data/examples/analog/fading.rb +28 -0
- data/ext/bonekit/beaglebone.c +13 -6
- data/ext/bonekit/beaglebone.h +115 -99
- data/ext/bonekit/beaglebone_global_const.c +128 -98
- data/ext/bonekit/beaglebone_global_const.h +6 -0
- data/ext/bonekit/gpio.c +1 -0
- data/ext/bonekit/gpio.h +0 -6
- data/ext/bonekit/pin.c +54 -12
- data/ext/bonekit/pin.h +9 -2
- data/ext/bonekit/pin_class.c +30 -4
- data/ext/bonekit/pwm.c +235 -0
- data/ext/bonekit/pwm.h +67 -0
- data/lib/bonekit/bonekit.so +0 -0
- data/lib/bonekit/version.rb +1 -1
- data/lib/bonekit.rb +2 -9
- data/spec/pin_spec.rb +44 -4
- data/test/bonekit-c/beaglebone_test.c +26 -0
- data/test/bonekit-c/pin_test.c +56 -0
- data/test/unit/pin_adc_test.rb +16 -0
- data/test/unit/{pin_test.rb → pin_gpio_test.rb} +2 -2
- data/test/unit/pin_pwm_test.rb +36 -0
- metadata +19 -8
- data/lib/bonekit.so +0 -0
@@ -28,105 +28,135 @@
|
|
28
28
|
|
29
29
|
#include "beaglebone_global_const.h"
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
VALUE cBoneKit_BeaglebonePin;
|
32
|
+
|
33
|
+
int Beaglebone_global_const_check_type(VALUE self)
|
34
|
+
{
|
35
|
+
return rb_obj_is_kind_of(self, cBoneKit_BeaglebonePin);
|
36
|
+
}
|
37
|
+
|
38
|
+
beaglebone_t * Beaglebone_global_const_to_beaglebone_t(VALUE self)
|
39
|
+
{
|
40
|
+
beaglebone_t * ptr;
|
41
|
+
Data_Get_Struct(self, beaglebone_t, ptr);
|
42
|
+
return ptr;
|
43
|
+
}
|
44
|
+
|
45
|
+
static void Beaglebone_global_const_free(void * ptr)
|
46
|
+
{
|
47
|
+
if(ptr == NULL)
|
48
|
+
return;
|
49
|
+
|
50
|
+
free(ptr);
|
51
|
+
}
|
52
|
+
|
53
|
+
static VALUE Beaglebone_global_const_new(VALUE class, beaglebone_t beaglebone_pin)
|
54
|
+
{
|
55
|
+
beaglebone_t * ptr = calloc(1, sizeof(beaglebone_t));
|
56
|
+
memcpy(ptr, &beaglebone_pin, sizeof(beaglebone_t));
|
57
|
+
|
58
|
+
VALUE self = Data_Wrap_Struct(class, 0, Beaglebone_global_const_free, ptr);
|
59
|
+
return self;
|
60
|
+
}
|
33
61
|
|
34
62
|
void BoneKit_Beaglebone_global_const_init()
|
35
63
|
{
|
36
|
-
|
37
|
-
|
38
|
-
rb_define_global_const("
|
39
|
-
rb_define_global_const("
|
40
|
-
rb_define_global_const("
|
41
|
-
rb_define_global_const("
|
42
|
-
rb_define_global_const("
|
43
|
-
rb_define_global_const("
|
44
|
-
rb_define_global_const("
|
45
|
-
rb_define_global_const("
|
46
|
-
rb_define_global_const("
|
47
|
-
rb_define_global_const("
|
48
|
-
rb_define_global_const("
|
49
|
-
rb_define_global_const("
|
50
|
-
rb_define_global_const("
|
51
|
-
rb_define_global_const("
|
52
|
-
rb_define_global_const("
|
53
|
-
rb_define_global_const("
|
54
|
-
rb_define_global_const("
|
55
|
-
rb_define_global_const("
|
56
|
-
rb_define_global_const("
|
57
|
-
rb_define_global_const("
|
58
|
-
rb_define_global_const("
|
59
|
-
rb_define_global_const("
|
60
|
-
rb_define_global_const("
|
61
|
-
rb_define_global_const("
|
62
|
-
rb_define_global_const("
|
63
|
-
rb_define_global_const("
|
64
|
-
rb_define_global_const("
|
65
|
-
rb_define_global_const("
|
66
|
-
rb_define_global_const("
|
67
|
-
rb_define_global_const("
|
68
|
-
rb_define_global_const("
|
69
|
-
rb_define_global_const("
|
70
|
-
rb_define_global_const("
|
71
|
-
rb_define_global_const("
|
72
|
-
rb_define_global_const("
|
73
|
-
rb_define_global_const("
|
74
|
-
rb_define_global_const("
|
75
|
-
rb_define_global_const("
|
76
|
-
rb_define_global_const("
|
77
|
-
rb_define_global_const("
|
78
|
-
rb_define_global_const("
|
79
|
-
rb_define_global_const("
|
80
|
-
rb_define_global_const("
|
81
|
-
rb_define_global_const("
|
82
|
-
rb_define_global_const("
|
83
|
-
rb_define_global_const("
|
84
|
-
rb_define_global_const("
|
85
|
-
rb_define_global_const("
|
86
|
-
rb_define_global_const("
|
87
|
-
rb_define_global_const("
|
88
|
-
rb_define_global_const("
|
89
|
-
rb_define_global_const("
|
90
|
-
rb_define_global_const("
|
91
|
-
rb_define_global_const("
|
92
|
-
rb_define_global_const("
|
93
|
-
rb_define_global_const("
|
94
|
-
rb_define_global_const("
|
95
|
-
rb_define_global_const("
|
96
|
-
rb_define_global_const("
|
97
|
-
rb_define_global_const("
|
98
|
-
rb_define_global_const("
|
99
|
-
rb_define_global_const("
|
100
|
-
rb_define_global_const("
|
101
|
-
rb_define_global_const("
|
102
|
-
rb_define_global_const("
|
103
|
-
rb_define_global_const("
|
104
|
-
rb_define_global_const("
|
105
|
-
rb_define_global_const("
|
106
|
-
rb_define_global_const("
|
107
|
-
rb_define_global_const("
|
108
|
-
rb_define_global_const("
|
109
|
-
rb_define_global_const("
|
110
|
-
rb_define_global_const("
|
111
|
-
rb_define_global_const("
|
112
|
-
rb_define_global_const("
|
113
|
-
rb_define_global_const("
|
114
|
-
rb_define_global_const("
|
115
|
-
rb_define_global_const("
|
116
|
-
rb_define_global_const("
|
117
|
-
rb_define_global_const("
|
118
|
-
rb_define_global_const("
|
119
|
-
rb_define_global_const("
|
120
|
-
rb_define_global_const("
|
121
|
-
rb_define_global_const("
|
122
|
-
rb_define_global_const("
|
123
|
-
rb_define_global_const("
|
124
|
-
rb_define_global_const("
|
125
|
-
rb_define_global_const("
|
126
|
-
rb_define_global_const("
|
127
|
-
rb_define_global_const("
|
128
|
-
rb_define_global_const("
|
129
|
-
rb_define_global_const("
|
130
|
-
rb_define_global_const("
|
131
|
-
rb_define_global_const("
|
64
|
+
cBoneKit_BeaglebonePin = rb_define_class("BeaglebonePin", rb_cObject);
|
65
|
+
|
66
|
+
rb_define_global_const("USR0", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, USR0));
|
67
|
+
rb_define_global_const("USR1", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, USR1));
|
68
|
+
rb_define_global_const("USR2", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, USR2));
|
69
|
+
rb_define_global_const("USR3", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, USR3));
|
70
|
+
rb_define_global_const("P8_1", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_1));
|
71
|
+
rb_define_global_const("P8_2", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_2));
|
72
|
+
rb_define_global_const("P8_3", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_3));
|
73
|
+
rb_define_global_const("P8_4", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_4));
|
74
|
+
rb_define_global_const("P8_5", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_5));
|
75
|
+
rb_define_global_const("P8_6", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_6));
|
76
|
+
rb_define_global_const("P8_7", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_7));
|
77
|
+
rb_define_global_const("P8_8", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_8));
|
78
|
+
rb_define_global_const("P8_9", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_9));
|
79
|
+
rb_define_global_const("P8_10", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_10));
|
80
|
+
rb_define_global_const("P8_11", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_11));
|
81
|
+
rb_define_global_const("P8_12", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_12));
|
82
|
+
rb_define_global_const("P8_13", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_13));
|
83
|
+
rb_define_global_const("P8_14", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_14));
|
84
|
+
rb_define_global_const("P8_15", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_15));
|
85
|
+
rb_define_global_const("P8_16", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_16));
|
86
|
+
rb_define_global_const("P8_17", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_17));
|
87
|
+
rb_define_global_const("P8_18", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_18));
|
88
|
+
rb_define_global_const("P8_19", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_19));
|
89
|
+
rb_define_global_const("P8_20", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_20));
|
90
|
+
rb_define_global_const("P8_21", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_21));
|
91
|
+
rb_define_global_const("P8_22", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_22));
|
92
|
+
rb_define_global_const("P8_23", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_23));
|
93
|
+
rb_define_global_const("P8_24", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_24));
|
94
|
+
rb_define_global_const("P8_25", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_25));
|
95
|
+
rb_define_global_const("P8_26", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_26));
|
96
|
+
rb_define_global_const("P8_27", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_27));
|
97
|
+
rb_define_global_const("P8_28", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_28));
|
98
|
+
rb_define_global_const("P8_29", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_29));
|
99
|
+
rb_define_global_const("P8_30", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_30));
|
100
|
+
rb_define_global_const("P8_31", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_31));
|
101
|
+
rb_define_global_const("P8_32", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_32));
|
102
|
+
rb_define_global_const("P8_33", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_33));
|
103
|
+
rb_define_global_const("P8_34", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_34));
|
104
|
+
rb_define_global_const("P8_35", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_35));
|
105
|
+
rb_define_global_const("P8_36", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_36));
|
106
|
+
rb_define_global_const("P8_37", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_37));
|
107
|
+
rb_define_global_const("P8_38", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_38));
|
108
|
+
rb_define_global_const("P8_39", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_39));
|
109
|
+
rb_define_global_const("P8_40", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_40));
|
110
|
+
rb_define_global_const("P8_41", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_41));
|
111
|
+
rb_define_global_const("P8_42", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_42));
|
112
|
+
rb_define_global_const("P8_43", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_43));
|
113
|
+
rb_define_global_const("P8_44", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_44));
|
114
|
+
rb_define_global_const("P8_45", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_45));
|
115
|
+
rb_define_global_const("P8_46", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P8_46));
|
116
|
+
rb_define_global_const("P9_1", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_1));
|
117
|
+
rb_define_global_const("P9_2", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_2));
|
118
|
+
rb_define_global_const("P9_3", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_3));
|
119
|
+
rb_define_global_const("P9_4", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_4));
|
120
|
+
rb_define_global_const("P9_5", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_5));
|
121
|
+
rb_define_global_const("P9_6", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_6));
|
122
|
+
rb_define_global_const("P9_7", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_7));
|
123
|
+
rb_define_global_const("P9_8", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_8));
|
124
|
+
rb_define_global_const("P9_9", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_9));
|
125
|
+
rb_define_global_const("P9_10", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_10));
|
126
|
+
rb_define_global_const("P9_11", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_11));
|
127
|
+
rb_define_global_const("P9_12", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_12));
|
128
|
+
rb_define_global_const("P9_13", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_13));
|
129
|
+
rb_define_global_const("P9_14", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_14));
|
130
|
+
rb_define_global_const("P9_15", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_15));
|
131
|
+
rb_define_global_const("P9_16", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_16));
|
132
|
+
rb_define_global_const("P9_17", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_17));
|
133
|
+
rb_define_global_const("P9_18", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_18));
|
134
|
+
rb_define_global_const("P9_19", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_19));
|
135
|
+
rb_define_global_const("P9_20", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_20));
|
136
|
+
rb_define_global_const("P9_21", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_21));
|
137
|
+
rb_define_global_const("P9_22", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_22));
|
138
|
+
rb_define_global_const("P9_23", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_23));
|
139
|
+
rb_define_global_const("P9_24", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_24));
|
140
|
+
rb_define_global_const("P9_25", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_25));
|
141
|
+
rb_define_global_const("P9_26", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_26));
|
142
|
+
rb_define_global_const("P9_27", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_27));
|
143
|
+
rb_define_global_const("P9_28", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_28));
|
144
|
+
rb_define_global_const("P9_29", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_29));
|
145
|
+
rb_define_global_const("P9_30", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_30));
|
146
|
+
rb_define_global_const("P9_31", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_31));
|
147
|
+
rb_define_global_const("P9_32", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_32));
|
148
|
+
rb_define_global_const("P9_33", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_33));
|
149
|
+
rb_define_global_const("P9_34", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_34));
|
150
|
+
rb_define_global_const("P9_35", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_35));
|
151
|
+
rb_define_global_const("P9_36", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_36));
|
152
|
+
rb_define_global_const("P9_37", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_37));
|
153
|
+
rb_define_global_const("P9_38", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_38));
|
154
|
+
rb_define_global_const("P9_39", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_39));
|
155
|
+
rb_define_global_const("P9_40", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_40));
|
156
|
+
rb_define_global_const("P9_41", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_41));
|
157
|
+
rb_define_global_const("P9_42", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_42));
|
158
|
+
rb_define_global_const("P9_43", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_43));
|
159
|
+
rb_define_global_const("P9_44", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_44));
|
160
|
+
rb_define_global_const("P9_45", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_45));
|
161
|
+
rb_define_global_const("P9_46", Beaglebone_global_const_new(cBoneKit_BeaglebonePin, P9_46));
|
132
162
|
}
|
@@ -28,6 +28,12 @@
|
|
28
28
|
#ifndef BONEKIT_BEAGLEBONE_GLOBAL_CONST_H__
|
29
29
|
#define BONEKIT_BEAGLEBONE_GLOBAL_CONST_H__
|
30
30
|
|
31
|
+
#include "ruby.h"
|
32
|
+
#include "beaglebone.h"
|
33
|
+
|
34
|
+
int Beaglebone_global_const_check_type(VALUE);
|
35
|
+
beaglebone_t * Beaglebone_global_const_to_beaglebone_t(VALUE);
|
36
|
+
|
31
37
|
void BoneKit_Beaglebone_global_const_init();
|
32
38
|
|
33
39
|
#endif
|
data/ext/bonekit/gpio.c
CHANGED
data/ext/bonekit/gpio.h
CHANGED
data/ext/bonekit/pin.c
CHANGED
@@ -29,8 +29,10 @@
|
|
29
29
|
|
30
30
|
#include "gpio.h"
|
31
31
|
#include "adc.h"
|
32
|
+
#include "pwm.h"
|
32
33
|
|
33
34
|
#include <stdlib.h>
|
35
|
+
#include <string.h>
|
34
36
|
|
35
37
|
pin_t * pin_alloc()
|
36
38
|
{
|
@@ -39,24 +41,31 @@ pin_t * pin_alloc()
|
|
39
41
|
return obj;
|
40
42
|
}
|
41
43
|
|
42
|
-
int pin_init(pin_t * obj,
|
44
|
+
int pin_init(pin_t * obj, beaglebone_t beaglebone_pin)
|
43
45
|
{
|
44
46
|
if(obj)
|
45
47
|
{
|
46
|
-
int gpio = beaglebone_gpio(
|
47
|
-
int ain = beaglebone_ain(
|
48
|
+
int gpio = beaglebone_gpio(beaglebone_pin);
|
49
|
+
int ain = beaglebone_ain(beaglebone_pin);
|
50
|
+
int pwm_mux_mode = beaglebone_pwm_mux_mode(beaglebone_pin);
|
48
51
|
|
49
52
|
int is_ain = (ain != -1);
|
50
53
|
int is_gpio = (gpio != -1);
|
54
|
+
int can_pwm = (pwm_mux_mode != -1);
|
51
55
|
|
52
56
|
if(!(is_ain || is_gpio)) // invalid pin
|
53
57
|
return -1;
|
54
58
|
|
59
|
+
memcpy(&(obj->_bp), &beaglebone_pin, sizeof(beaglebone_t));
|
60
|
+
|
55
61
|
obj->_ain = ain;
|
56
62
|
obj->_is_ain = is_ain;
|
57
63
|
obj->_gpio = gpio;
|
58
64
|
obj->_is_gpio = is_gpio;
|
59
65
|
|
66
|
+
obj->_can_pwm = can_pwm;
|
67
|
+
obj->_pwm_key = NULL; // lazy pwm export (first call to set_analog_value)
|
68
|
+
|
60
69
|
if(is_gpio)
|
61
70
|
gpio_export(gpio);
|
62
71
|
else if(is_ain)
|
@@ -71,6 +80,9 @@ void pin_destroy(pin_t * obj)
|
|
71
80
|
if(obj->_is_gpio)
|
72
81
|
gpio_unexport(obj->_gpio);
|
73
82
|
|
83
|
+
if(obj->_pwm_key != NULL)
|
84
|
+
pwm_unexport(obj->_pwm_key);
|
85
|
+
|
74
86
|
free(obj);
|
75
87
|
}
|
76
88
|
|
@@ -92,7 +104,13 @@ int pin_value(pin_t * obj)
|
|
92
104
|
{
|
93
105
|
int value = 0;
|
94
106
|
|
95
|
-
if(obj->
|
107
|
+
if(obj->_pwm_key != NULL)
|
108
|
+
{
|
109
|
+
double analog_value;
|
110
|
+
pwm_get_duty_cycle(obj->_pwm_key, &analog_value);
|
111
|
+
value = (int)analog_value;
|
112
|
+
}
|
113
|
+
else if(obj->_is_gpio)
|
96
114
|
{
|
97
115
|
gpio_get_value(obj->_gpio, &value);
|
98
116
|
}
|
@@ -106,28 +124,52 @@ int pin_value(pin_t * obj)
|
|
106
124
|
return value;
|
107
125
|
}
|
108
126
|
|
109
|
-
|
127
|
+
double pin_analog_value(pin_t * obj)
|
110
128
|
{
|
111
|
-
|
112
|
-
|
113
|
-
if(obj->
|
129
|
+
double analog_value = 0.0;
|
130
|
+
|
131
|
+
if(obj->_pwm_key != NULL)
|
132
|
+
{
|
133
|
+
pwm_get_duty_cycle(obj->_pwm_key, &analog_value);
|
134
|
+
}
|
135
|
+
else if(obj->_is_gpio)
|
114
136
|
{
|
115
137
|
int value;
|
116
138
|
gpio_get_value(obj->_gpio, &value);
|
117
|
-
analog_value = (
|
139
|
+
analog_value = (double)value;
|
118
140
|
}
|
119
141
|
else if(obj->_is_ain)
|
120
142
|
{
|
121
143
|
int analog_value_raw; // [ADC_MIN_VALUE..ADC_MAX_VALUE]
|
122
144
|
adc_get_value(obj->_ain, &analog_value_raw);
|
123
|
-
analog_value = (
|
145
|
+
analog_value = (double)(((double)analog_value_raw)/((double)ADC_MAX_VALUE));
|
124
146
|
}
|
125
|
-
|
147
|
+
|
126
148
|
return analog_value;
|
127
149
|
}
|
128
150
|
|
129
151
|
void pin_set_value(pin_t * obj, int value)
|
130
152
|
{
|
131
|
-
if(obj->
|
153
|
+
if(obj->_pwm_key != NULL)
|
154
|
+
pwm_set_duty_cycle(obj->_pwm_key, (double)value);
|
155
|
+
else if(obj->_is_gpio)
|
132
156
|
gpio_set_value(obj->_gpio, value);
|
133
157
|
}
|
158
|
+
|
159
|
+
void pin_set_analog_value(pin_t * obj, double value)
|
160
|
+
{
|
161
|
+
if(obj->_can_pwm)
|
162
|
+
{
|
163
|
+
if(obj->_pwm_key == NULL)
|
164
|
+
{
|
165
|
+
char pin_name[PIN_NAME_LEN];
|
166
|
+
beaglebone_pin_name(obj->_bp, pin_name); // retrieve pin name ie. P9_42
|
167
|
+
pwm_export(pin_name, &obj->_pwm_key); // retrieve pin pwm key
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
if(obj->_pwm_key != NULL)
|
172
|
+
pwm_set_duty_cycle(obj->_pwm_key, value);
|
173
|
+
else if(obj->_is_gpio)
|
174
|
+
gpio_set_value(obj->_gpio, (int)value);
|
175
|
+
}
|
data/ext/bonekit/pin.h
CHANGED
@@ -31,25 +31,32 @@
|
|
31
31
|
#ifdef __cplusplus
|
32
32
|
extern "C" {
|
33
33
|
#endif
|
34
|
+
|
35
|
+
#include "beaglebone.h"
|
34
36
|
|
35
37
|
struct pin_s
|
36
38
|
{
|
39
|
+
beaglebone_t _bp;
|
40
|
+
|
37
41
|
int _is_ain;
|
38
42
|
unsigned _ain;
|
39
43
|
int _is_gpio;
|
40
44
|
unsigned _gpio;
|
45
|
+
int _can_pwm;
|
46
|
+
char * _pwm_key;
|
41
47
|
};
|
42
48
|
|
43
49
|
typedef struct pin_s pin_t;
|
44
50
|
|
45
51
|
pin_t * pin_alloc();
|
46
|
-
int pin_init(pin_t *,
|
52
|
+
int pin_init(pin_t *, beaglebone_t);
|
47
53
|
void pin_destroy(pin_t *);
|
48
54
|
int pin_mode(pin_t *);
|
49
55
|
void pin_set_mode(pin_t *, int);
|
50
56
|
int pin_value(pin_t *);
|
51
|
-
|
57
|
+
double pin_analog_value(pin_t *);
|
52
58
|
void pin_set_value(pin_t *, int);
|
59
|
+
void pin_set_analog_value(pin_t *, double);
|
53
60
|
|
54
61
|
#ifdef __cplusplus
|
55
62
|
}
|
data/ext/bonekit/pin_class.c
CHANGED
@@ -29,7 +29,8 @@
|
|
29
29
|
#include "pin_class.h"
|
30
30
|
|
31
31
|
#include "ruby.h"
|
32
|
-
|
32
|
+
|
33
|
+
#include "beaglebone.h"
|
33
34
|
#include "pin.h"
|
34
35
|
|
35
36
|
VALUE cBoneKit_Pin;
|
@@ -66,7 +67,14 @@ static VALUE Pin_initialize(int argc, VALUE* argv, VALUE self)
|
|
66
67
|
pin_t * ptr;
|
67
68
|
Data_Get_Struct(self, pin_t, ptr);
|
68
69
|
|
69
|
-
if(
|
70
|
+
if(Beaglebone_global_const_check_type(argv[0]) == 0)
|
71
|
+
rb_raise(rb_eArgError, "wrong pin argument type Fixnum (expected BeaglebonePin)");
|
72
|
+
|
73
|
+
beaglebone_t * bp_ptr = (beaglebone_t *)Beaglebone_global_const_to_beaglebone_t(argv[0]);
|
74
|
+
beaglebone_t bp;
|
75
|
+
memcpy(&bp, bp_ptr, sizeof(beaglebone_t));
|
76
|
+
|
77
|
+
if(pin_init(ptr, bp) < 0) // pin must support gpio, ain, pwm
|
70
78
|
rb_raise(rb_eArgError, "invalid pin (%d GPIO not supported)", NUM2UINT(argv[0]));
|
71
79
|
|
72
80
|
int mode = INPUT;
|
@@ -98,7 +106,9 @@ static VALUE Pin_value(VALUE self)
|
|
98
106
|
* analog_value -> float
|
99
107
|
*
|
100
108
|
* Reads the analog value from the pin. The range of values is [0.0..1.0].
|
101
|
-
* NOTE:
|
109
|
+
* NOTE:
|
110
|
+
* If the pin doesn't support ADC, analog_value is the floating point equivalent of value.
|
111
|
+
* If the pin has been previously used to set analog values (PWM) its returns the last analog value set.
|
102
112
|
*/
|
103
113
|
static VALUE Pin_analog_value(VALUE self)
|
104
114
|
{
|
@@ -108,7 +118,6 @@ static VALUE Pin_analog_value(VALUE self)
|
|
108
118
|
return rb_float_new(pin_analog_value(ptr));
|
109
119
|
}
|
110
120
|
|
111
|
-
|
112
121
|
/*
|
113
122
|
* call-seq:
|
114
123
|
* value=(value) -> integer
|
@@ -125,6 +134,22 @@ static VALUE Pin_set_value(VALUE self, VALUE value)
|
|
125
134
|
return value;
|
126
135
|
}
|
127
136
|
|
137
|
+
/*
|
138
|
+
* call-seq:
|
139
|
+
* analog_value=(value) -> float
|
140
|
+
*
|
141
|
+
* Write the analog value to the pin. The pin must support Pulse-Width Modulation (PWM).
|
142
|
+
* Possible values: 0.0 to 1.0
|
143
|
+
*/
|
144
|
+
static VALUE Pin_set_analog_value(VALUE self, VALUE value)
|
145
|
+
{
|
146
|
+
pin_t * ptr;
|
147
|
+
Data_Get_Struct(self, pin_t, ptr);
|
148
|
+
pin_set_analog_value(ptr, NUM2DBL(value));
|
149
|
+
|
150
|
+
return value;
|
151
|
+
}
|
152
|
+
|
128
153
|
/*
|
129
154
|
* call-seq:
|
130
155
|
* mode -> integer
|
@@ -165,6 +190,7 @@ void BoneKit_Pin_class_init()
|
|
165
190
|
rb_define_method(cBoneKit_Pin, "value", Pin_value, 0);
|
166
191
|
rb_define_method(cBoneKit_Pin, "analog_value", Pin_analog_value, 0);
|
167
192
|
rb_define_method(cBoneKit_Pin, "value=", Pin_set_value, 1);
|
193
|
+
rb_define_method(cBoneKit_Pin, "analog_value=", Pin_set_analog_value, 1);
|
168
194
|
rb_define_method(cBoneKit_Pin, "mode", Pin_mode, 0);
|
169
195
|
rb_define_method(cBoneKit_Pin, "mode=", Pin_set_mode, 1);
|
170
196
|
|