Skip to content

Commit 432eb5e

Browse files
committed
Initial commit of LEDC gem
1 parent 208917a commit 432eb5e

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
mruby-esp32-ledc
2+
============
3+
4+
LEDC (PWM) library for [mruby-esp32](https://github.com/mruby-esp32).
5+
6+
## Installation
7+
In `esp32_build_config.rb`, at the end of the `MRuby::CrossBuild` block, add the following:
8+
9+
```ruby
10+
conf.gem :github => 'dino-rb/mruby-esp32-ledc'
11+
```
12+
13+
In `CMakeLists.txt`, make sure the line `PRIV_REQUIRES` includes the `driver` component. Add it if needed:
14+
15+
```
16+
add_prebuilt_library(
17+
libmruby ${LIBMRUBY_FILE}
18+
PRIV_REQUIRES esp_wifi esp_hw_support esp_rom mqtt driver
19+
)
20+
````
21+
22+
## Example
23+
```ruby
24+
group = ESP32::LEDC_LOW_SPEED_MODE
25+
channel = ESP32::LEDC_CHANNEL_0
26+
timer = ESP32::LEDC_TIMER_0
27+
resolution = ESP32::LEDC_TIMER_10_BIT
28+
frequency = 1000
29+
pin = ESP32::GPIO::GPIO_NUM_2
30+
31+
ESP32::LEDC.timer_config(group, timer, resolution, frequency)
32+
ESP32::LEDC.channel_config(pin, group, timer, channel)
33+
34+
# Set the built-in LED to 1/8 brightness.
35+
ESP32::LEDC.write(group, channel, 128)
36+
```

mrbgem.rake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MRuby::Gem::Specification.new('mruby-esp32-ledc') do |spec|
2+
spec.license = 'MIT'
3+
spec.authors = 'vickash'
4+
5+
spec.cc.include_paths << "#{build.root}/src"
6+
end

mrblib/ledc.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module ESP32
2+
include Constants
3+
end

src/ledc.c

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)