|
1 | | -#include <stdbool.h> |
2 | | -#include <mrubyc.h> |
3 | 1 | #include "../include/gpio.h" |
4 | 2 |
|
5 | | -#define GETIV(str) mrbc_instance_getiv(&v[0], mrbc_str_to_symid(#str)) |
6 | | -#define READ(pin) GPIO_read(pin_num(vm, pin)) |
7 | | -#define WRITE(pin, val) GPIO_write(pin_num(vm, pin), val) |
| 3 | +#if defined(PICORB_VM_MRUBY) |
8 | 4 |
|
9 | | -static uint8_t |
10 | | -pin_num(mrbc_vm *vm, mrbc_value pin) |
11 | | -{ |
12 | | - switch (mrbc_type(pin)) { |
13 | | - case MRBC_TT_INTEGER: { |
14 | | - return (uint8_t)pin.i; |
15 | | - } |
16 | | - case MRBC_TT_STRING: { |
17 | | - return GPIO_pin_num_from_char(pin.string->data); |
18 | | - } |
19 | | - case MRBC_TT_SYMBOL: { |
20 | | - return GPIO_pin_num_from_char((const uint8_t *)mrbc_symid_to_str(pin.i)); |
21 | | - } |
22 | | - default: |
23 | | - mrbc_raise(vm, MRBC_CLASS(ArgumentError), "Wrong GPIO pin value"); |
24 | | - return -1; |
25 | | - } |
26 | | -} |
| 5 | +#include "mruby/gpio.c" |
27 | 6 |
|
28 | | -/* |
29 | | - * GPIO._init(pin) |
30 | | - */ |
31 | | -static void |
32 | | -c__init(mrbc_vm *vm, mrbc_value *v, int argc) |
33 | | -{ |
34 | | - uint8_t pin_number = pin_num(vm, v[1]); |
35 | | - if (-1 < pin_number) GPIO_init(pin_number); |
36 | | - SET_INT_RETURN(0); |
37 | | -} |
| 7 | +#elif defined(PICORB_VM_MRUBYC) |
38 | 8 |
|
39 | | -/* |
40 | | - * GPIO.set_function_at(pin, function) |
41 | | - */ |
42 | | -static void |
43 | | -c_set_function_at(mrbc_vm *vm, mrbc_value *v, int argc) |
44 | | -{ |
45 | | - GPIO_set_function(pin_num(vm, v[1]), GET_INT_ARG(2)); |
46 | | - SET_INT_RETURN(0); |
47 | | -} |
48 | | - |
49 | | - |
50 | | -/* |
51 | | - * GPIO.set_dir_at(pin, dir) |
52 | | - */ |
53 | | -static void |
54 | | -c_set_dir_at(mrbc_vm *vm, mrbc_value *v, int argc) |
55 | | -{ |
56 | | - GPIO_set_dir(pin_num(vm, v[1]), GET_INT_ARG(2)); |
57 | | - SET_INT_RETURN(0); |
58 | | -} |
59 | | - |
60 | | -/* |
61 | | - * GPIO.open_drain_at(pin) |
62 | | - */ |
63 | | -static void |
64 | | -c_open_drain_at(mrbc_vm *vm, mrbc_value *v, int argc) |
65 | | -{ |
66 | | - GPIO_open_drain(pin_num(vm, v[1])); |
67 | | - SET_INT_RETURN(0); |
68 | | -} |
69 | | - |
70 | | -/* |
71 | | - * GPIO.pull_up_at(pin) |
72 | | - */ |
73 | | -static void |
74 | | -c_pull_up_at(mrbc_vm *vm, mrbc_value *v, int argc) |
75 | | -{ |
76 | | - GPIO_pull_up(pin_num(vm, v[1])); |
77 | | - SET_INT_RETURN(0); |
78 | | -} |
79 | | - |
80 | | -/* |
81 | | - * GPIO.pull_down_at(pin) |
82 | | - */ |
83 | | -static void |
84 | | -c_pull_down_at(mrbc_vm *vm, mrbc_value *v, int argc) |
85 | | -{ |
86 | | - GPIO_pull_down(pin_num(vm, v[1])); |
87 | | - SET_INT_RETURN(0); |
88 | | -} |
89 | | - |
90 | | -/* |
91 | | - * GPIO.high_at?(pin) |
92 | | - */ |
93 | | -static void |
94 | | -c_high_at_q(mrbc_vm *vm, mrbc_value *v, int argc) |
95 | | -{ |
96 | | - //if (READ(v[1]) == 0) { |
97 | | - if (GPIO_read(GET_INT_ARG(1)) == 0) { |
98 | | - SET_FALSE_RETURN(); |
99 | | - } else { |
100 | | - SET_TRUE_RETURN(); |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -/* |
105 | | - * GPIO.high?(pin) |
106 | | - */ |
107 | | -static void |
108 | | -c_high_q(mrbc_vm *vm, mrbc_value *v, int argc) |
109 | | -{ |
110 | | - if (READ(GETIV(pin)) == 0) { |
111 | | - SET_FALSE_RETURN(); |
112 | | - } else { |
113 | | - SET_TRUE_RETURN(); |
114 | | - } |
115 | | -} |
116 | | - |
117 | | -/* |
118 | | - * GPIO.low_at?(pin) |
119 | | - */ |
120 | | -static void |
121 | | -c_low_at_q(mrbc_vm *vm, mrbc_value *v, int argc) |
122 | | -{ |
123 | | - if (READ(v[1]) == 0) { |
124 | | - SET_TRUE_RETURN(); |
125 | | - } else { |
126 | | - SET_FALSE_RETURN(); |
127 | | - } |
128 | | -} |
129 | | - |
130 | | -/* |
131 | | - * GPIO.low?(pin) |
132 | | - */ |
133 | | -static void |
134 | | -c_low_q(mrbc_vm *vm, mrbc_value *v, int argc) |
135 | | -{ |
136 | | - if (READ(GETIV(pin)) == 0) { |
137 | | - SET_TRUE_RETURN(); |
138 | | - } else { |
139 | | - SET_FALSE_RETURN(); |
140 | | - } |
141 | | -} |
142 | | - |
143 | | -/* |
144 | | - * GPIO.read_at(pin) |
145 | | - */ |
146 | | -static void |
147 | | -c_read_at(mrbc_vm *vm, mrbc_value *v, int argc) |
148 | | -{ |
149 | | - SET_INT_RETURN(READ(v[1])); |
150 | | -} |
151 | | - |
152 | | -/* |
153 | | - * GPIO.read(pin) |
154 | | - */ |
155 | | -static void |
156 | | -c_read(mrbc_vm *vm, mrbc_value *v, int argc) |
157 | | -{ |
158 | | - SET_INT_RETURN(READ(GETIV(pin))); |
159 | | -} |
160 | | - |
161 | | -/* |
162 | | - * GPIO.write_at(pin, val) |
163 | | - */ |
164 | | -static void |
165 | | -c_write_at(mrbc_vm *vm, mrbc_value *v, int argc) |
166 | | -{ |
167 | | - if (v[2].tt != MRBC_TT_INTEGER) { |
168 | | - mrbc_raise(vm, MRBC_CLASS(ArgumentError), "Wrong value. 0 and 1 are only valid"); |
169 | | - return; |
170 | | - } |
171 | | - WRITE(v[1], GET_INT_ARG(2)); |
172 | | - SET_INT_RETURN(0); |
173 | | -} |
174 | | - |
175 | | -/* |
176 | | - * GPIO#write(val) |
177 | | - */ |
178 | | -static void |
179 | | -c_write(mrbc_vm *vm, mrbc_value *v, int argc) |
180 | | -{ |
181 | | - if (v[1].tt != MRBC_TT_INTEGER) { |
182 | | - mrbc_raise(vm, MRBC_CLASS(ArgumentError), "Wrong value. 0 and 1 are only valid"); |
183 | | - return; |
184 | | - } |
185 | | - WRITE(GETIV(pin), GET_INT_ARG(1)); |
186 | | - SET_INT_RETURN(0); |
187 | | -} |
188 | | - |
189 | | -#define SET_CLASS_CONST(cls, cst) \ |
190 | | - mrbc_set_class_const(mrbc_class_##cls, mrbc_str_to_symid(#cst), &mrbc_integer_value(cst)) |
191 | | - |
192 | | -void |
193 | | -mrbc_gpio_init(mrbc_vm *vm) |
194 | | -{ |
195 | | - mrbc_class *mrbc_class_GPIO = mrbc_define_class(vm, "GPIO", mrbc_class_object); |
196 | | - |
197 | | - SET_CLASS_CONST(GPIO, IN); |
198 | | - SET_CLASS_CONST(GPIO, OUT); |
199 | | - SET_CLASS_CONST(GPIO, HIGH_Z); |
200 | | - SET_CLASS_CONST(GPIO, PULL_UP); |
201 | | - SET_CLASS_CONST(GPIO, PULL_DOWN); |
202 | | - SET_CLASS_CONST(GPIO, OPEN_DRAIN); |
203 | | - SET_CLASS_CONST(GPIO, ALT); |
204 | | - |
205 | | - mrbc_define_method(vm, mrbc_class_GPIO, "_init", c__init); |
206 | | - mrbc_define_method(vm, mrbc_class_GPIO, "set_function_at", c_set_function_at); |
207 | | - mrbc_define_method(vm, mrbc_class_GPIO, "set_dir_at", c_set_dir_at); |
208 | | - mrbc_define_method(vm, mrbc_class_GPIO, "pull_up_at", c_pull_up_at); |
209 | | - mrbc_define_method(vm, mrbc_class_GPIO, "pull_down_at", c_pull_down_at); |
210 | | - mrbc_define_method(vm, mrbc_class_GPIO, "open_drain_at", c_open_drain_at); |
211 | | - mrbc_define_method(vm, mrbc_class_GPIO, "high_at?", c_high_at_q); |
212 | | - mrbc_define_method(vm, mrbc_class_GPIO, "high?", c_high_q); |
213 | | - mrbc_define_method(vm, mrbc_class_GPIO, "low_at?", c_low_at_q); |
214 | | - mrbc_define_method(vm, mrbc_class_GPIO, "low?", c_low_q); |
215 | | - mrbc_define_method(vm, mrbc_class_GPIO, "read_at", c_read_at); |
216 | | - mrbc_define_method(vm, mrbc_class_GPIO, "read", c_read); |
217 | | - mrbc_define_method(vm, mrbc_class_GPIO, "write_at", c_write_at); |
218 | | - mrbc_define_method(vm, mrbc_class_GPIO, "write", c_write); |
219 | | -} |
| 9 | +#include "mrubyc/gpio.c" |
220 | 10 |
|
| 11 | +#endif |
0 commit comments