GR-SAKURAでmrubyをインタラクティブに使う

http://d.hatena.ne.jp/kyab/20130621を参考にmirb-hostbasedを使って、インタラクティブに接続する。

ターゲット側には、https://github.com/kyab/mruby-arduinoを組み込んでおけば、LED等の操作も可能になる。

GR-SAKURA用に幾つか修正したmruby-arduinoを、https://github.com/masahino/mruby-arduino/tree/gr-sakuraに置いた。 build_config.rbのターゲット側の設定で、-DBOARD_GRSAKURAをコンパイルオプションに追加する必要がある。

ビルド環境の用意は、http://qiita.com/jjzak/items/8e9d9d98370181aa97faを参考にすれば良い。

mirb-hostbasedからの入力を受けて、結果を返すスケッチは、mirb-hostbasedのsamples/target配下のコードを参考に、GR-SAKURA用に修正する。 修正点は、rxduino.hをインクルードするのと、itoaが使えないので代りにsnprintfなどを使うのみ。

@@ -1,3 +1,4 @@
+#include <rxduino.h>

 #include <Servo.h>

@@ -72,7 +73,8 @@

 void reportMem(){
        char str[15];
-       itoa(total_allocated_mem, str, 10);
+//     itoa(total_allocated_mem, str, 10);
+       snprintf(str, 10, "%d", total_allocated_mem);
        Serial.print("(taget):TOTAL_ALLOCATED : ");
        Serial.println(str);
 }
@@ -88,7 +90,8 @@

     if(!ret){
       char str[15];
-      itoa(size, str, 10);
+//      itoa(size, str, 10);
+      snprintf(str, 10, "%d", size);
       Serial.print("(target):allocation error. requested size = ");
       Serial.println(str);
       reportMem();

予想以上にあっさり出来た。