mrubyブリッジ
mrubyブリッジは,TECS CDL の signature 記述に基づき,mruby と TECS コンポーネントをブリッジする,「mruby の C 言語実装クラスであり TECS のセルタイプとなるコンポーネント」です.
実装
コンポーネント記述 (.cdl)
tButton.cdl
tButton.cdl
import_C("gr_peach.h");
signature sButton {
bool_t isPressed(void);
};
celltype tButton {
entry sButton eButton;
};
bridge.cdl
bridge.cdl
...
import(<tButton.cdl>);
/*
* シグニチャプラグイン MrubyBridgePlugin の呼び出し。
*/
...
generate( MrubyBridgePlugin, sButton, "" );
/*
* サンプルプログラムの定義
*/
...
cell nMruby::tsButton BridgeButton {
cTECS = Button.eButton;
};
...
cell tButton Button {};
セルタイプコード (.c)
tecsmerge を使用して作ったテンプレートに実装する
tButton.c
bool_t
eButton_isPressed(CELLIDX idx)
{
CELLCB *p_cellcb;
if (VALID_IDX(idx)) {
p_cellcb = GET_CELLCB(idx);
}
else {
} /* end if VALID_IDX(idx) */
return gr_peach_get_button();
}
mruby (.rb)
クラスの継承。Rubyっぽくする。
Button.rb
class Button < TECS::TsButton
private
def initialize()
super "BridgeButton"
end
@@instance = self.new
public
def self.pressed?
@@instance.isPressed
end
end
VMの設定
作ったクラスをVMに追加する
VM1.cdl
cell nMruby::tMruby Mruby {
mrubyFile =
"$(MRUBY_LIB_DIR)/RTOS.rb "
"$(MRUBY_LIB_DIR)/LED.rb "
"$(MRUBY_LIB_DIR)/Button.rb "
"$(MRUBY_APP_DIR)/button_sample.rb";
cInit = VM_TECSInitializer.eInitialize;
cSerialPort = SerialPort1.eSerialPort;
};
サンプルアプリケーション
button_sample.rb
begin
loop do
LED.color = :red
LED.color = :blue if Button.pressed?
end
rescue => e
puts e
end