コンピュータを楽しもう!!

今、自分が面白くていろいろやってみたことを書き綴りたいと思います。連絡先はtarosa.yでgmail.comです。

GR-SAKURAでmrubyを動かす (3)

GR-SAKURA用の libmruby.a作成することができたので、 jjzakさんの「GR-SAKURAでmrubyを使う」にある例を参考に、GR-SAKURAのプログラムを作成してみたいと思います。

mrubyフォルダを作成する

GR-SAKURA用のプロジェクトにmruby用のフォルダを作成します。今回はgr_mrubyというフォルダを作成しました。そこに build/grsakura/lib/にあるlibmruby.aをコピーしました。また、include/以下をそのままコピーしました。

minao@tarosa8 ~/gr-sakura/localcompile/gr_mruby
$ ls -l
合計 3528
drwxr-xr-x+ 1 minao None       0 106 16:54 include
-rwxr-xr-x  1 minao None 3609744 106 16:43 libmruby.a

rubyプログラムの作成

GR-SAKURAで動かすrubyのサンプルプログラムを作成します。sample.rbという名前で下記のようなrubyプログラムを作成しました。

HIGH = 1
LOW = 0

cdigitalWrite(100, LOW);
cdigitalWrite(101, LOW);
cdigitalWrite(102, LOW);
cdigitalWrite(103, LOW);

k = Math.sqrt(4)
if k==2 then
    cdigitalWrite(100, HIGH);
else
    cdigitalWrite(101, HIGH);
end 

loop do
end

cdigitalWrite()は、mrubyに追加するGR-SAKURA用の関数です。mrbgemのMathを組み込んだので、Math.sqrt(4)が正しく処理されて 2であれば、右端のLEDが点灯します。正しく処理されなければ、たぶん、ハングアップしてLEDは点きません。

sample.rbをコンパイルする

sample.rbをコンパイルして中間言語にし、それをGR-SAKURAのビルドモジュールに組み込めるように、Cのソースに変換します。
変換には、mrubyをmakeしたときに生成されたmrbcを使います。mrbcはmrubyをmakeしたフォルダのbin/以下にあります。

$ ~/gr-sakura/mruby/mruby-master/bin/mrbc -Bcode sample.rb

Bcodeオプションは、コンパイル結果をcソースとして出力するオプションです。これでsample.cが作成されました。
以下がsample.cです。

#include <stdint.h>
const uint8_t code[] = {
0x52,0x49,0x54,0x45,0x30,0x30,0x30,0x31,0xcb,0xcf,0x00,0x00,0x01,0x39,0x4d,0x41,
0x54,0x5a,0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x01,0x1b,0x30,0x30,
0x30,0x30,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0xef,0x00,0x02,0x00,0x05,0x00,0x00,
0x00,0x29,0x01,0x40,0x00,0x03,0x01,0x00,0x00,0x12,0x01,0x3f,0xff,0x83,0x01,0x00,
0x00,0x92,0x01,0x00,0x00,0x06,0x01,0xc0,0x31,0x83,0x02,0x00,0x00,0x91,0x01,0x00,
0x81,0x20,0x01,0x00,0x00,0x06,0x01,0xc0,0x32,0x03,0x02,0x00,0x00,0x91,0x01,0x00,
0x81,0x20,0x01,0x00,0x00,0x06,0x01,0xc0,0x32,0x83,0x02,0x00,0x00,0x91,0x01,0x00,
0x81,0x20,0x01,0x00,0x00,0x06,0x01,0xc0,0x33,0x03,0x02,0x00,0x00,0x91,0x01,0x00,
0x81,0x20,0x01,0x00,0x01,0x91,0x01,0xc0,0x01,0x83,0x01,0x01,0x00,0xa0,0x00,0x80,
0x80,0x01,0x01,0x00,0x40,0x01,0x01,0xc0,0x00,0x83,0x01,0x01,0x40,0xb2,0x01,0x40,
0x02,0x99,0x01,0x00,0x00,0x06,0x01,0xc0,0x31,0x83,0x02,0x00,0x00,0x11,0x01,0x00,
0x81,0x20,0x00,0x40,0x02,0x17,0x01,0x00,0x00,0x06,0x01,0xc0,0x32,0x03,0x02,0x00,
0x00,0x11,0x01,0x00,0x81,0x20,0x01,0x00,0x00,0x06,0x01,0x80,0x03,0x40,0x01,0x01,
0x80,0x21,0x00,0x00,0x00,0x4a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x04,
0x48,0x49,0x47,0x48,0x00,0x00,0x03,0x4c,0x4f,0x57,0x00,0x00,0x0d,0x63,0x64,0x69,
0x67,0x69,0x74,0x61,0x6c,0x57,0x72,0x69,0x74,0x65,0x00,0x00,0x04,0x4d,0x61,0x74,
0x68,0x00,0x00,0x04,0x73,0x71,0x72,0x74,0x00,0x00,0x02,0x3d,0x3d,0x00,0x00,0x04,
0x6c,0x6f,0x6f,0x70,0x00,0x00,0x00,0x00,0x1c,0x00,0x01,0x00,0x02,0x00,0x00,0x00,
0x02,0x00,0x80,0x00,0x05,0x00,0x80,0x00,0x29,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x45,0x4e,0x44,0x00,0x00,0x00,0x00,0x08,
};

makefileの修正

makefileにmrubyのライブラリとヘッダファイルの場所を追記します。
LIBFILESの最後に ./gr_mruby/libmruby.a を追記します。

LIBFILES = ./gr_common/lib/libc.a ./gr_common/lib/libg.a ./gr_common/lib/libgcc.a ./gr_common/lib/libm.a ./gr_common/lib/librxduino.a ./gr_common/lib/libsim.a ./gr_common/lib/libstdc++.a ./gr_common/lib/libsupc++.a ./gr_common/lib/libtkdnhal.a ./gr_mruby/libmruby.a 


CCINCの最後に -I./gr_mruby/include を追記します。

CCINC = -I./gr_build -I./gr_common -I./gr_common/include -I./gr_common/include/rxduino -I./gr_common/include/tkdnhal -I./gr_common/lib -I./gr_mruby/include 


sample.cをコンパイルする必要があるので、OBJFILES に ./sample.oを追記します。

OBJFILES = ./sample.o ./gr_sketch.o ./gr_common/intvect.o ./gr_common/lowlevel.o 

続きます。