|
| 1 | +#include <mruby.h> |
| 2 | +#include <mruby/value.h> |
| 3 | + |
| 4 | +#include "soc/io_mux_reg.h" |
| 5 | +#include "driver/gpio.h" |
| 6 | +#include "driver/ledc.h" |
| 7 | + |
| 8 | +static mrb_value |
| 9 | +mrb_esp32_ledc_timer_config(mrb_state *mrb, mrb_value self) { |
| 10 | + mrb_value group, timer, res, freq; |
| 11 | + |
| 12 | + mrb_get_args(mrb, "oooo", &group, &timer, &res, &freq); |
| 13 | + |
| 14 | + if (!mrb_fixnum_p(group) || !mrb_fixnum_p(timer) || !mrb_fixnum_p(res) || !mrb_fixnum_p(freq)) { |
| 15 | + return mrb_nil_value(); |
| 16 | + } |
| 17 | + |
| 18 | + // Timer config |
| 19 | + ledc_timer_config_t ledc_timer = { |
| 20 | + .speed_mode = mrb_fixnum(group), |
| 21 | + .timer_num = mrb_fixnum(timer), |
| 22 | + .duty_resolution = mrb_fixnum(res), |
| 23 | + .freq_hz = mrb_fixnum(freq), |
| 24 | + .clk_cfg = LEDC_AUTO_CLK |
| 25 | + }; |
| 26 | + ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer)); |
| 27 | + |
| 28 | + return self; |
| 29 | +} |
| 30 | + |
| 31 | +static mrb_value |
| 32 | +mrb_esp32_ledc_channel_config(mrb_state *mrb, mrb_value self) { |
| 33 | + // Only set pin and channel for now. Can add more config later. |
| 34 | + mrb_value pin, group, timer, ch; |
| 35 | + |
| 36 | + mrb_get_args(mrb, "oooo", &pin, &group, &timer, &ch); |
| 37 | + |
| 38 | + if (!mrb_fixnum_p(pin) || !mrb_fixnum_p(group) || !mrb_fixnum_p(timer) || !mrb_fixnum_p(ch)) { |
| 39 | + return mrb_nil_value(); |
| 40 | + } |
| 41 | + |
| 42 | + // Channel config |
| 43 | + ledc_channel_config_t ledc_channel = { |
| 44 | + .gpio_num = mrb_fixnum(pin), |
| 45 | + .speed_mode = mrb_fixnum(group), |
| 46 | + .timer_sel = mrb_fixnum(timer), |
| 47 | + .channel = mrb_fixnum(ch), |
| 48 | + .intr_type = LEDC_INTR_DISABLE, |
| 49 | + .duty = 0, // Set duty to 0% |
| 50 | + .hpoint = 0 |
| 51 | + }; |
| 52 | + ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel)); |
| 53 | + |
| 54 | + return self; |
| 55 | +} |
| 56 | + |
| 57 | +static mrb_value |
| 58 | +mrb_esp32_ledc_write(mrb_state *mrb, mrb_value self) { |
| 59 | + mrb_value group, ch, duty; |
| 60 | + |
| 61 | + mrb_get_args(mrb, "ooo", &group, &ch, &duty); |
| 62 | + |
| 63 | + if (!mrb_fixnum_p(group) || !mrb_fixnum_p(ch) || !mrb_fixnum_p(duty)) { |
| 64 | + return mrb_nil_value(); |
| 65 | + } |
| 66 | + |
| 67 | + // Write the duty cycle. |
| 68 | + ESP_ERROR_CHECK(ledc_set_duty(mrb_fixnum(group), mrb_fixnum(ch), mrb_fixnum(duty))); |
| 69 | + ESP_ERROR_CHECK(ledc_update_duty(mrb_fixnum(group), mrb_fixnum(ch))); |
| 70 | + |
| 71 | + return self; |
| 72 | +} |
| 73 | + |
| 74 | +static mrb_value |
| 75 | +mrb_esp32_ledc_detach(mrb_state *mrb, mrb_value self) { |
| 76 | + mrb_value pin; |
| 77 | + |
| 78 | + mrb_get_args(mrb, "o", &pin); |
| 79 | + |
| 80 | + if (!mrb_fixnum_p(pin)) { |
| 81 | + return mrb_nil_value(); |
| 82 | + } |
| 83 | + |
| 84 | + // PIN_FUNC_GPIO = 2. Normal GPIO. |
| 85 | + // Last param is output inversion. |
| 86 | + gpio_iomux_out(mrb_fixnum(pin), PIN_FUNC_GPIO, false); |
| 87 | + |
| 88 | + return self; |
| 89 | +} |
| 90 | + |
| 91 | +void |
| 92 | +mrb_mruby_esp32_ledc_gem_init(mrb_state* mrb) |
| 93 | +{ |
| 94 | + struct RClass *esp32, *ledc, *constants; |
| 95 | + |
| 96 | + esp32 = mrb_define_module(mrb, "ESP32"); |
| 97 | + |
| 98 | + ledc = mrb_define_module_under(mrb, esp32, "LEDC"); |
| 99 | + mrb_define_module_function(mrb, ledc, "timer_config", mrb_esp32_ledc_timer_config, MRB_ARGS_REQ(4)); |
| 100 | + mrb_define_module_function(mrb, ledc, "channel_config", mrb_esp32_ledc_channel_config, MRB_ARGS_REQ(4)); |
| 101 | + mrb_define_module_function(mrb, ledc, "write", mrb_esp32_ledc_write, MRB_ARGS_REQ(3)); |
| 102 | + mrb_define_module_function(mrb, ledc, "detach", mrb_esp32_ledc_write, MRB_ARGS_REQ(1)); |
| 103 | + |
| 104 | + constants = mrb_define_module_under(mrb, esp32, "Constants"); |
| 105 | + |
| 106 | +#define define_const(SYM) \ |
| 107 | + do { \ |
| 108 | + mrb_define_const(mrb, constants, #SYM, mrb_fixnum_value(SYM)); \ |
| 109 | + } while (0) |
| 110 | + |
| 111 | + // LEDC channel groups. |
| 112 | + // High speed not available on some devices. |
| 113 | + define_const(LEDC_HIGH_SPEED_MODE); |
| 114 | + define_const(LEDC_LOW_SPEED_MODE); |
| 115 | + |
| 116 | + // LEDC channel numbers. 8 channels per group. |
| 117 | + define_const(LEDC_CHANNEL_0); |
| 118 | + define_const(LEDC_CHANNEL_1); |
| 119 | + define_const(LEDC_CHANNEL_2); |
| 120 | + define_const(LEDC_CHANNEL_3); |
| 121 | + define_const(LEDC_CHANNEL_4); |
| 122 | + define_const(LEDC_CHANNEL_5); |
| 123 | + define_const(LEDC_CHANNEL_6); |
| 124 | + define_const(LEDC_CHANNEL_7); |
| 125 | + define_const(LEDC_CHANNEL_MAX); |
| 126 | + |
| 127 | + // LEDC timer numbers. 4 timers per group of 8 channels. |
| 128 | + define_const(LEDC_TIMER_0); |
| 129 | + define_const(LEDC_TIMER_1); |
| 130 | + define_const(LEDC_TIMER_2); |
| 131 | + define_const(LEDC_TIMER_3); |
| 132 | + define_const(LEDC_TIMER_MAX); |
| 133 | + |
| 134 | + // LEDC timer resolutions. |
| 135 | + define_const(LEDC_TIMER_1_BIT); |
| 136 | + define_const(LEDC_TIMER_2_BIT); |
| 137 | + define_const(LEDC_TIMER_3_BIT); |
| 138 | + define_const(LEDC_TIMER_4_BIT); |
| 139 | + define_const(LEDC_TIMER_5_BIT); |
| 140 | + define_const(LEDC_TIMER_6_BIT); |
| 141 | + define_const(LEDC_TIMER_7_BIT); |
| 142 | + define_const(LEDC_TIMER_8_BIT); |
| 143 | + define_const(LEDC_TIMER_9_BIT); |
| 144 | + define_const(LEDC_TIMER_10_BIT); |
| 145 | + define_const(LEDC_TIMER_11_BIT); |
| 146 | + define_const(LEDC_TIMER_12_BIT); |
| 147 | + define_const(LEDC_TIMER_13_BIT); |
| 148 | + define_const(LEDC_TIMER_14_BIT); |
| 149 | + define_const(LEDC_TIMER_15_BIT); |
| 150 | + define_const(LEDC_TIMER_16_BIT); |
| 151 | + define_const(LEDC_TIMER_17_BIT); |
| 152 | + define_const(LEDC_TIMER_18_BIT); |
| 153 | + define_const(LEDC_TIMER_19_BIT); |
| 154 | + define_const(LEDC_TIMER_20_BIT); |
| 155 | + define_const(LEDC_TIMER_BIT_MAX); |
| 156 | +} |
| 157 | + |
| 158 | +void |
| 159 | +mrb_mruby_esp32_ledc_gem_final(mrb_state* mrb) |
| 160 | +{ |
| 161 | +} |
0 commit comments