前回できたlibmrubyc.aをlibディレクトリーに置いてみるるが全く無視されている様だ。
それではとダウンロードしたmrubycディレクトリーを丸ごとlibディレクトリーに置いてみる。
エラーは出ているがmrubycのMakefileを読み込んでうまい具合にやろうとしている様だ。
mruby/cを動かすためにmain.cppファイルを書き換える。
main()処理のサンプルが mrubyc/sample_c/sample_include.c
にある。
これをこのままmain.cppにコピーする。
このサンプルでは mrubyc のバイナリーを sample_include_bytecode.c
から読み込んで実行させている。
ファイルのパスが違うので、パスの変更と、Espressif IoT Development Framework では app_main() がメイン関数になるのでそこから呼び出す様に追加する。
#include <stdio.h>
#include <stdlib.h>
#include "mrubyc.h"
#include "../lib/mrubyc/sample_c/sample_include_bytecode.c"
#define MEMORY_SIZE (1024*30)
static uint8_t memory_pool[MEMORY_SIZE];
int main(void)
{
mrbc_init(memory_pool, MEMORY_SIZE);
if( mrbc_create_task(ary, 0) != NULL ){
mrbc_run();
}
return 0;
}
void app_main() {
main();
}
ヘッダーの読み込みができない様なので、インクルードディレクトリをplatformio.iniでビルドオプションとして追加する。
[env:m5stack-core-esp32]
platform = espressif32
board = m5stack-core-esp32
framework = espidf
build_flags=
-I lib/mrubyc/src
ビルドが進んでhalのコンパイルで躓いた。
どうやら使用するターゲットをフラグで指定している様だ。
これもビルドオプションで指定する。
ターゲットはM5StackなのでESP32であろうMRBC_USE_HAL_ESP32を定義する。
[env:m5stack-core-esp32]
platform = espressif32
board = m5stack-core-esp32
framework = espidf
build_flags=
-I lib/mrubyc/src
-DMRBC_USE_HAL_ESP32
コンパイルすると使用しないhalもlibディレクトリ内にあるのでコンパイルしようとしてエラーになる。
特定のディレクトリを除外する仕方がわからないので、exampleフォルダを作って使用しないhalのディレクトリをそこに移動させた。
これでコンパイルは通る様になった。
書き込んでみると、RTCWDT_RTC_RESETが繰り返し発生している。
rst:0x10 (RTCWDT_RTC_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:QIO, clock div:1
load:0x3fff0030,len:6656
load:0xffffffff,len:-1
ets Jun 8 2016 00:22:57
検索するとうまく書き込みできていないためらしい。
dioというmodeで書かないといけないらしい。
検索してもdioの指定についてうまくヒットしなかったが M5AtomS3 の指定が正にdioの指定だったのでそこからコピーしてきた。
[env:m5stack-core-esp32]
platform = espressif32
board = m5stack-core-esp32
framework = espidf
build_flags=
-I lib/mrubyc/src
-DMRBC_USE_HAL_ESP32
board_build.f_cpu = 240000000L
board_build.f_flash = 80000000L
board_build.flash_mode = dio
monitor_speed = 115200
これで書き込んでsampleと繰り返し表示される様になった。
* Executing task: platformio device monitor
--- Terminal on /dev/cu.usbserial-013F02F0 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
sample
sample
sample
sample
sample_include_bytecode.cの元になったsample_include_bytecode.rbを見てみる。
1秒毎にsampleを表示する様になっていてmrubycのコードが動いていることが確認できた。
while true
puts "sample"
sleep 1
end
platformioでmruby/cの開発環境が思っていたより簡単にできる事がわかった。
参考までにtreeを載せておく。
% tree
.
├── CMakeLists.txt
├── include
│ └── README
├── lib
│ ├── README
│ └── mrubyc
│ ├── Dockerfile
│ ├── Doxyfile
│ ├── Gemfile
│ ├── Gemfile.lock
│ ├── LICENSE
│ ├── Makefile
│ ├── README.md
│ ├── doc
│ │ ├── How_to_make_your_own_HAL.md
│ │ ├── compile.md
│ │ └── test.md
│ ├── mrblib
│ │ ├── Makefile
│ │ ├── array.rb
│ │ ├── global.rb
│ │ ├── hash.rb
│ │ ├── numeric.rb
│ │ ├── object.rb
│ │ ├── range.rb
│ │ └── string.rb
│ ├── sample_c
│ │ ├── Makefile
│ │ ├── myclass.rb
│ │ ├── sample_concurrent
│ │ ├── sample_concurrent.c
│ │ ├── sample_concurrent.dSYM
│ │ │ └── Contents
│ │ │ ├── Info.plist
│ │ │ └── Resources
│ │ │ └── DWARF
│ │ │ └── sample_concurrent
│ │ ├── sample_include
│ │ ├── sample_include.c
│ │ ├── sample_include.dSYM
│ │ │ └── Contents
│ │ │ ├── Info.plist
│ │ │ └── Resources
│ │ │ └── DWARF
│ │ │ └── sample_include
│ │ ├── sample_include_bytecode.c
│ │ ├── sample_include_bytecode.rb
│ │ ├── sample_myclass
│ │ ├── sample_myclass.c
│ │ ├── sample_myclass.dSYM
│ │ │ └── Contents
│ │ │ ├── Info.plist
│ │ │ └── Resources
│ │ │ └── DWARF
│ │ │ └── sample_myclass
│ │ ├── sample_no_scheduler
│ │ ├── sample_no_scheduler.c
│ │ ├── sample_no_scheduler.dSYM
│ │ │ └── Contents
│ │ │ ├── Info.plist
│ │ │ └── Resources
│ │ │ └── DWARF
│ │ │ └── sample_no_scheduler
│ │ ├── sample_scheduler
│ │ ├── sample_scheduler.c
│ │ └── sample_scheduler.dSYM
│ │ └── Contents
│ │ ├── Info.plist
│ │ └── Resources
│ │ └── DWARF
│ │ └── sample_scheduler
│ ├── src
│ │ ├── Makefile
│ │ ├── _autogen_builtin_symbol.h
│ │ ├── _autogen_class_array.h
│ │ ├── _autogen_class_exception.h
│ │ ├── _autogen_class_float.h
│ │ ├── _autogen_class_hash.h
│ │ ├── _autogen_class_integer.h
│ │ ├── _autogen_class_math.h
│ │ ├── _autogen_class_object.h
│ │ ├── _autogen_class_range.h
│ │ ├── _autogen_class_string.h
│ │ ├── _autogen_class_symbol.h
│ │ ├── alloc.c
│ │ ├── alloc.h
│ │ ├── alloc.o
│ │ ├── c_array.c
│ │ ├── c_array.h
│ │ ├── c_array.o
│ │ ├── c_hash.c
│ │ ├── c_hash.h
│ │ ├── c_hash.o
│ │ ├── c_math.c
│ │ ├── c_math.h
│ │ ├── c_math.o
│ │ ├── c_numeric.c
│ │ ├── c_numeric.h
│ │ ├── c_numeric.o
│ │ ├── c_object.c
│ │ ├── c_object.h
│ │ ├── c_object.o
│ │ ├── c_range.c
│ │ ├── c_range.h
│ │ ├── c_range.o
│ │ ├── c_string.c
│ │ ├── c_string.h
│ │ ├── c_string.o
│ │ ├── class.c
│ │ ├── class.h
│ │ ├── class.o
│ │ ├── console.c
│ │ ├── console.h
│ │ ├── console.o
│ │ ├── error.c
│ │ ├── error.h
│ │ ├── error.o
│ │ ├── example
│ │ │ ├── hal_pic24
│ │ │ │ └── hal.h
│ │ │ ├── hal_posix
│ │ │ │ ├── hal.c
│ │ │ │ ├── hal.h
│ │ │ │ └── hal.o
│ │ │ ├── hal_psoc5lp
│ │ │ │ ├── hal.c
│ │ │ │ └── hal.h
│ │ │ └── hal_rp2040
│ │ │ ├── hal.c
│ │ │ └── hal.h
│ │ ├── global.c
│ │ ├── global.h
│ │ ├── global.o
│ │ ├── hal_esp32
│ │ │ ├── hal.c
│ │ │ └── hal.h
│ │ ├── hal_selector.h
│ │ ├── hal_selector.mk
│ │ ├── keyvalue.c
│ │ ├── keyvalue.h
│ │ ├── keyvalue.o
│ │ ├── libmrubyc.a
│ │ ├── load.c
│ │ ├── load.h
│ │ ├── load.o
│ │ ├── mrblib.c
│ │ ├── mrblib.o
│ │ ├── mrubyc.h
│ │ ├── opcode.h
│ │ ├── rrt0.c
│ │ ├── rrt0.h
│ │ ├── rrt0.o
│ │ ├── symbol.c
│ │ ├── symbol.h
│ │ ├── symbol.o
│ │ ├── value.c
│ │ ├── value.h
│ │ ├── value.o
│ │ ├── vm.c
│ │ ├── vm.h
│ │ ├── vm.o
│ │ └── vm_config.h
│ ├── support
│ │ ├── common_sub.rb
│ │ ├── make_method_table.rb
│ │ └── make_symbol_table.rb
│ └── test
│ ├── array_test.rb
│ ├── bool_test.rb
│ ├── def_test.rb
│ ├── exception_test.rb
│ ├── global_test.rb
│ ├── hash_test.rb
│ ├── integer_test.rb
│ ├── math_test.rb
│ ├── models
│ │ ├── my_alias.rb
│ │ ├── my_block.rb
│ │ ├── my_equal3.rb
│ │ ├── my_instance_variable.rb
│ │ ├── my_super.rb
│ │ └── object.rb
│ ├── my_alias_test.rb
│ ├── my_block_test.rb
│ ├── my_equal3_test.rb
│ ├── my_instance_variable_test.rb
│ ├── my_nested_class_test.rb
│ ├── my_super_test.rb
│ ├── nil_class_test.rb
│ ├── object_test.rb
│ ├── op_basic_test.rb
│ ├── op_method_test.rb
│ ├── range_test.rb
│ ├── sprintf_test.rb
│ ├── string_ljust_rjust_test.rb
│ ├── string_split_test.rb
│ ├── string_strip_test.rb
│ ├── string_test.rb
│ ├── symbol_test.rb
│ └── tmp
│ └── hal -> ../../src/hal_posix
├── platformio.ini
├── sdkconfig.m5stack-core-esp32
├── src
│ ├── CMakeLists.txt
│ ├── main.c
│ └── scripts
└── test
└── README