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.
@@ -28,105 +28,135 @@
28
28
 
29
29
  #include "beaglebone_global_const.h"
30
30
 
31
- #include "ruby.h"
32
- #include "beaglebone.h"
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
- 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));
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
@@ -26,6 +26,7 @@
26
26
  */
27
27
 
28
28
  #include "gpio.h"
29
+ #include "beaglebone.h"
29
30
 
30
31
  #include <stdio.h>
31
32
  #include <stdlib.h>
data/ext/bonekit/gpio.h CHANGED
@@ -32,12 +32,6 @@
32
32
  extern "C" {
33
33
  #endif
34
34
 
35
- #define INPUT 0
36
- #define OUTPUT 1
37
-
38
- #define HIGH 1
39
- #define LOW 0
40
-
41
35
  #define GPIO_DIR "/sys/class/gpio"
42
36
  #define GPIO_LEN 64
43
37
 
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, unsigned int beaglebone_global_const)
44
+ int pin_init(pin_t * obj, beaglebone_t beaglebone_pin)
43
45
  {
44
46
  if(obj)
45
47
  {
46
- int gpio = beaglebone_gpio(beaglebone_global_const);
47
- int ain = beaglebone_ain(beaglebone_global_const);
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->_is_gpio)
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
- float pin_analog_value(pin_t * obj)
127
+ double pin_analog_value(pin_t * obj)
110
128
  {
111
- float analog_value = 0.0;
112
-
113
- if(obj->_is_gpio)
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 = (float)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 = (float)(((float)analog_value_raw)/((float)ADC_MAX_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->_is_gpio)
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 *, unsigned int);
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
- float pin_analog_value(pin_t *);
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
  }
@@ -29,7 +29,8 @@
29
29
  #include "pin_class.h"
30
30
 
31
31
  #include "ruby.h"
32
- #include "gpio.h"
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(pin_init(ptr, NUM2UINT(argv[0])) < 0) // pin must support gpio
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: If the pin doesn't support ADC, analog_value is the floating point equivalent of value.
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