git clone https://github.com/hasumikin/mrubyc-template-esp32.git 04
cd 04
- create
mrblib/loops/master.rb
- create
mrblib/models/led.rb
- edit
main/main.c
(you can replace whole of the file with code below)
make
make flash
make monitor
# mrblib/models/led.rb | |
class Led | |
def initialize(pin) | |
@pin = pin | |
gpio_init_output(@pin) | |
turn_off | |
end | |
def turn_on | |
gpio_set_level(@pin, 1) | |
puts "turned on" | |
end | |
def turn_off | |
gpio_set_level(@pin, 0) | |
puts "turned off" | |
end | |
end |
/* main/main.c */ | |
#include "driver/gpio.h" | |
#include "mrubyc.h" | |
#include "models/led.h" | |
#include "loops/master.h" | |
#define MEMORY_SIZE (1024*40) | |
static uint8_t memory_pool[MEMORY_SIZE]; | |
static void c_gpio_init_output(mrb_vm *vm, mrb_value *v, int argc) { | |
int pin = GET_INT_ARG(1); | |
console_printf("init pin %d\n", pin); | |
gpio_set_direction(pin, GPIO_MODE_OUTPUT); | |
} | |
static void c_gpio_set_level(mrb_vm *vm, mrb_value *v, int argc){ | |
int pin = GET_INT_ARG(1); | |
int level = GET_INT_ARG(2); | |
gpio_set_level(pin, level); | |
} | |
void app_main(void) { | |
mrbc_init(memory_pool, MEMORY_SIZE); | |
mrbc_define_method(0, mrbc_class_object, "gpio_init_output", c_gpio_init_output); | |
mrbc_define_method(0, mrbc_class_object, "gpio_set_level", c_gpio_set_level); | |
mrbc_create_task( led, 0 ); | |
mrbc_create_task( master, 0 ); | |
mrbc_run(); | |
} |
# mrblib/loops/master.rb | |
led = Led.new(19) | |
while true | |
led.turn_on | |
sleep 1 | |
led.turn_off | |
sleep 1 | |
end |