|
| 1 | +#include <mruby.h> |
| 2 | +#include <mruby/value.h> |
| 3 | + |
| 4 | +#include "driver/gpio.h" |
| 5 | + |
| 6 | +static mrb_value |
| 7 | +mrb_esp32_gpio_pin_mode(mrb_state *mrb, mrb_value self) { |
| 8 | + mrb_value pin, dir; |
| 9 | + |
| 10 | + mrb_get_args(mrb, "oo", &pin, &dir); |
| 11 | + |
| 12 | + if (!mrb_fixnum_p(pin) || !mrb_fixnum_p(dir)) { |
| 13 | + return mrb_nil_value(); |
| 14 | + } |
| 15 | + |
| 16 | + gpio_set_direction(mrb_fixnum(pin), mrb_fixnum(dir)); |
| 17 | + |
| 18 | + return self; |
| 19 | +} |
| 20 | + |
| 21 | +static mrb_value |
| 22 | +mrb_esp32_gpio_digital_write(mrb_state *mrb, mrb_value self) { |
| 23 | + mrb_value pin, level; |
| 24 | + |
| 25 | + mrb_get_args(mrb, "oo", &pin, &level); |
| 26 | + |
| 27 | + if (!mrb_fixnum_p(pin) || !mrb_fixnum_p(level)) { |
| 28 | + return mrb_nil_value(); |
| 29 | + } |
| 30 | + |
| 31 | + gpio_set_level(mrb_fixnum(pin), mrb_fixnum(level)); |
| 32 | + |
| 33 | + return self; |
| 34 | +} |
| 35 | + |
| 36 | +static mrb_value |
| 37 | +mrb_esp32_gpio_digital_read(mrb_state *mrb, mrb_value self) { |
| 38 | + mrb_value pin; |
| 39 | + |
| 40 | + mrb_get_args(mrb, "o", &pin); |
| 41 | + |
| 42 | + if (!mrb_fixnum_p(pin)) { |
| 43 | + return mrb_nil_value(); |
| 44 | + } |
| 45 | + |
| 46 | + return mrb_fixnum_value(gpio_get_level(mrb_fixnum(pin))); |
| 47 | +} |
| 48 | + |
| 49 | +void |
| 50 | +mrb_mruby_esp32_gpio_gem_init(mrb_state* mrb) |
| 51 | +{ |
| 52 | + struct RClass *esp32, *gpio, *constants; |
| 53 | + |
| 54 | + esp32 = mrb_define_module(mrb, "ESP32"); |
| 55 | + |
| 56 | + gpio = mrb_define_module_under(mrb, esp32, "GPIO"); |
| 57 | + mrb_define_module_function(mrb, gpio, "pinMode", mrb_esp32_gpio_pin_mode, MRB_ARGS_REQ(2)); |
| 58 | + mrb_define_module_function(mrb, gpio, "digitalWrite", mrb_esp32_gpio_digital_write, MRB_ARGS_REQ(2)); |
| 59 | + mrb_define_module_function(mrb, gpio, "digitalRead", mrb_esp32_gpio_digital_read, MRB_ARGS_REQ(1)); |
| 60 | + |
| 61 | + constants = mrb_define_module_under(mrb, gpio, "Constants"); |
| 62 | + |
| 63 | +#define define_const(SYM) \ |
| 64 | + do { \ |
| 65 | + mrb_define_const(mrb, constants, #SYM, mrb_fixnum_value(SYM)); \ |
| 66 | + } while (0) |
| 67 | + |
| 68 | + define_const(GPIO_NUM_0); |
| 69 | + define_const(GPIO_NUM_1); |
| 70 | + define_const(GPIO_NUM_2); |
| 71 | + define_const(GPIO_NUM_3); |
| 72 | + define_const(GPIO_NUM_4); |
| 73 | + define_const(GPIO_NUM_5); |
| 74 | + define_const(GPIO_NUM_6); |
| 75 | + define_const(GPIO_NUM_7); |
| 76 | + define_const(GPIO_NUM_8); |
| 77 | + define_const(GPIO_NUM_9); |
| 78 | + define_const(GPIO_NUM_10); |
| 79 | + define_const(GPIO_NUM_11); |
| 80 | + define_const(GPIO_NUM_12); |
| 81 | + define_const(GPIO_NUM_13); |
| 82 | + define_const(GPIO_NUM_14); |
| 83 | + define_const(GPIO_NUM_15); |
| 84 | + define_const(GPIO_NUM_16); |
| 85 | + define_const(GPIO_NUM_17); |
| 86 | + define_const(GPIO_NUM_18); |
| 87 | + define_const(GPIO_NUM_19); |
| 88 | + |
| 89 | + define_const(GPIO_NUM_21); |
| 90 | + define_const(GPIO_NUM_22); |
| 91 | + define_const(GPIO_NUM_23); |
| 92 | + |
| 93 | + define_const(GPIO_NUM_25); |
| 94 | + define_const(GPIO_NUM_26); |
| 95 | + define_const(GPIO_NUM_27); |
| 96 | + |
| 97 | + define_const(GPIO_NUM_32); |
| 98 | + define_const(GPIO_NUM_33); |
| 99 | + define_const(GPIO_NUM_34); |
| 100 | + define_const(GPIO_NUM_35); |
| 101 | + define_const(GPIO_NUM_36); |
| 102 | + define_const(GPIO_NUM_37); |
| 103 | + define_const(GPIO_NUM_38); |
| 104 | + define_const(GPIO_NUM_39); |
| 105 | + define_const(GPIO_NUM_MAX); |
| 106 | + |
| 107 | + mrb_define_const(mrb, constants, "LOW", mrb_fixnum_value(0)); |
| 108 | + mrb_define_const(mrb, constants, "HIGH", mrb_fixnum_value(1)); |
| 109 | + |
| 110 | + mrb_define_const(mrb, constants, "INPUT", mrb_fixnum_value(GPIO_MODE_INPUT)); |
| 111 | + mrb_define_const(mrb, constants, "OUTPUT", mrb_fixnum_value(GPIO_MODE_OUTPUT)); |
| 112 | +} |
| 113 | + |
| 114 | +void |
| 115 | +mrb_mruby_esp32_gpio_gem_final(mrb_state* mrb) |
| 116 | +{ |
| 117 | +} |
0 commit comments