mbed I/F binding for mruby

Dependents:   mruby_mbed_web mirb_mbed

mbed-mruby

How to use

Class

Files at this revision

API Documentation at this revision

Comitter:
mzta
Date:
Mon Apr 13 05:20:15 2015 +0000
Parent:
0:158c61bb030f
Commit message:
- code refactoring.; - add SPI, SPISlave, I2C class to mruby-mbed (Incomplete).

Changed in this revision

mrbgems/mruby-mbed/mbed.c Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbDigitalOut.cpp Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbDigitalOut.h Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbI2C.cpp Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbI2C.h Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbMbedFunc.cpp Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbMbedFunc.h Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbPinName.cpp Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbPinName.h Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbSPI.cpp Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbSPI.h Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbSPISlave.cpp Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbSPISlave.h Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbTimer.cpp Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mbedif/mrbTimer.h Show annotated file Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mrbDigitalOut.cpp Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mrbDigitalOut.h Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mrbMbedFunc.cpp Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mrbMbedFunc.h Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mrbPinName.h Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mrbTimer.cpp Show diff for this revision Revisions of this file
mrbgems/mruby-mbed/mrbTimer.h Show diff for this revision Revisions of this file
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbed.c
--- a/mrbgems/mruby-mbed/mbed.c	Wed Mar 25 17:36:16 2015 +0000
+++ b/mrbgems/mruby-mbed/mbed.c	Mon Apr 13 05:20:15 2015 +0000
@@ -2,7 +2,6 @@
 ** mbed.c - mbed module
 **
 */
-
 #include "mruby.h"
 #include "mruby/data.h"
 
@@ -10,190 +9,37 @@
 #include "mrbDigitalOut.h"
 #include "mrbTimer.h"
 #include "mrbMbedFunc.h"
-
-/**
- * mbed
- */
-static mrb_value
-mrb_mbed_sleep(mrb_state *mrb, mrb_value obj)
-{
-    mrb_int t;
-    mrb_get_args(mrb, "i", &t);
-    mbedSleep(t);
-
-    return obj;
-}
-
-static mrb_value
-mrb_mbed_sleep_ms(mrb_state *mrb, mrb_value obj)
-{
-    mrb_int t;
-    mrb_get_args(mrb, "i", &t);
-    mbedSleepMS(t);
-
-    return obj;
-}
-
-static mrb_value
-mrb_mbed_sleep_us(mrb_state *mrb, mrb_value obj)
-{
-    mrb_int t;
-    mrb_get_args(mrb, "i", &t);
-    mbedSleepUS(t);
-
-    return obj;
-}
-
-/**
- * mbed::I2C
- */
-
-
-/**
- * mbed::DigitalOut
- */
-static void mbed_do_free(mrb_state *mrb, void *ptr) {
-    delDigitalOut(ptr);
-}
-
-static struct mrb_data_type mbed_do_type = {
-    "DigitalOut", mbed_do_free
-};
-
-static mrb_value
-mrb_mbed_do_init(mrb_state *mrb, mrb_value self)
-{
-    mrb_int pin;
-    mrb_get_args(mrb, "i", &pin);
-
-    DATA_TYPE(self) = &mbed_do_type;
-    DATA_PTR(self) = newDigitalOut(pin);
-
-    return self;
-}
-
-static mrb_value
-mrb_mbed_do_write(mrb_state *mrb, mrb_value self)
-{
-    void *do_obj = DATA_PTR(self);
-
-    mrb_int val;
-    mrb_get_args(mrb, "i", &val);
-
-    writeDigitalOut(do_obj, val);
-
-    return self;
-}
-
-static mrb_value
-mrb_mbed_do_read(mrb_state *mrb, mrb_value self)
-{
-    void *do_obj = DATA_PTR(self);
+#include "mrbI2C.h"
+#include "mrbSPI.h"
+#include "mrbSPISlave.h"
 
-    return mrb_fixnum_value(readDigitalOut(do_obj));
-}
-
-/**
- * mbed::Timer
- */
-static void mbed_timer_free(mrb_state *mrb, void *ptr) {
-    delTimer(ptr);
-}
-
-static struct mrb_data_type mbed_timer_type = {
-    "Timer", mbed_timer_free
-};
-
-static mrb_value
-mrb_mbed_timer_init(mrb_state *mrb, mrb_value self)
-{
-    void *timer_obj;
-
-    DATA_TYPE(self) = &mbed_timer_type;
-    DATA_PTR(self) = newTimer();
-
-    return self;
-}
-
-static mrb_value
-mrb_mbed_timer_start(mrb_state *mrb, mrb_value self)
-{
-    void *timer_obj = DATA_PTR(self);
-
-    startTimer(timer_obj);
-
-    return self;
-}
-
-static mrb_value
-mrb_mbed_timer_stop(mrb_state *mrb, mrb_value self)
-{
-    void *timer_obj = DATA_PTR(self);
-
-    stopTimer(timer_obj);
-
-    return self;
-}
-
-static mrb_value
-mrb_mbed_timer_reset(mrb_state *mrb, mrb_value self)
-{
-    void *timer_obj = DATA_PTR(self);
-
-    resetTimer(timer_obj);
-
-    return self;
-}
-
-static mrb_value
-mrb_mbed_timer_read(mrb_state *mrb, mrb_value self)
-{
-    void *timer_obj = DATA_PTR(self);
-
-    return mrb_float_value(mrb, readTimer(timer_obj));
-}
-
-static mrb_value
-mrb_mbed_timer_read_ms(mrb_state *mrb, mrb_value self)
-{
-    void *timer_obj = DATA_PTR(self);
-
-    return mrb_fixnum_value(readMSTimer(timer_obj));
-}
-
-static mrb_value
-mrb_mbed_timer_read_us(mrb_state *mrb, mrb_value self)
-{
-    void *timer_obj = DATA_PTR(self);
-
-    return mrb_fixnum_value(readUSTimer(timer_obj));
-}
-
-/* ------------------------------------------------------------------------*/
 void
 mrb_mruby_mbed_gem_init(mrb_state* mrb)
 {
     struct RClass *mrb_mbed;
     struct RClass *mrb_mbed_do;
     struct RClass *mrb_mbed_timer;
+    struct RClass *mrb_mbed_i2c;
+    struct RClass *mrb_mbed_spi;
+    struct RClass *mrb_mbed_spis;
 
     mrb_mbed = mrb_define_module(mrb, "Mbed");
-    mrb_define_const(mrb,   mrb_mbed,   "LED1",         mrb_fixnum_value(LED1));
-    mrb_define_const(mrb,   mrb_mbed,   "LED2",         mrb_fixnum_value(LED2));
-    mrb_define_const(mrb,   mrb_mbed,   "LED3",         mrb_fixnum_value(LED3));
-    mrb_define_const(mrb,   mrb_mbed,   "LED4",         mrb_fixnum_value(LED4));
-    mrb_define_const(mrb,   mrb_mbed,   "LED_RED",      mrb_fixnum_value(LED_RED));
-    mrb_define_const(mrb,   mrb_mbed,   "LED_GREEN",    mrb_fixnum_value(LED_GREEN));
-    mrb_define_const(mrb,   mrb_mbed,   "LED_BLUE",     mrb_fixnum_value(LED_BLUE));
-    mrb_define_const(mrb,   mrb_mbed,   "LED_USER",     mrb_fixnum_value(LED_USER));
-    mrb_define_module_function(mrb, mrb_mbed,   "sleep",    mrb_mbed_sleep, MRB_ARGS_REQ(1));
-    mrb_define_module_function(mrb, mrb_mbed,   "sleep_ms", mrb_mbed_sleep, MRB_ARGS_REQ(1));
-    mrb_define_module_function(mrb, mrb_mbed,   "sleep_us", mrb_mbed_sleep, MRB_ARGS_REQ(1));
+    
+    mrb_mbed_pinname_define(mrb, mrb_mbed);
+
+    mrb_define_module_function(mrb, mrb_mbed,   "sleep",    mrb_mbed_sleep,         MRB_ARGS_REQ(1));
+    mrb_define_module_function(mrb, mrb_mbed,   "wait",     mrb_mbed_sleep,         MRB_ARGS_REQ(1));
+    mrb_define_module_function(mrb, mrb_mbed,   "sleep_ms", mrb_mbed_sleep_ms,      MRB_ARGS_REQ(1));
+    mrb_define_module_function(mrb, mrb_mbed,   "wait_ms",  mrb_mbed_sleep_ms,      MRB_ARGS_REQ(1));
+    mrb_define_module_function(mrb, mrb_mbed,   "sleep_us", mrb_mbed_sleep_us,      MRB_ARGS_REQ(1));
+    mrb_define_module_function(mrb, mrb_mbed,   "wait_us",  mrb_mbed_sleep_us,      MRB_ARGS_REQ(1));
     
     mrb_mbed_do = mrb_define_class_under(mrb, mrb_mbed, "DigitalOut", mrb->object_class);
     mrb_define_method(mrb,  mrb_mbed_do,    "initialize",   mrb_mbed_do_init,       MRB_ARGS_REQ(1));
     mrb_define_method(mrb,  mrb_mbed_do,    "write",        mrb_mbed_do_write,      MRB_ARGS_REQ(1));
     mrb_define_method(mrb,  mrb_mbed_do,    "read",         mrb_mbed_do_read,       MRB_ARGS_NONE());
+    mrb_define_method(mrb,  mrb_mbed_do,    "high",         mrb_mbed_do_high,       MRB_ARGS_NONE());
+    mrb_define_method(mrb,  mrb_mbed_do,    "low",          mrb_mbed_do_low,        MRB_ARGS_NONE());
 
     mrb_mbed_timer = mrb_define_class_under(mrb, mrb_mbed, "Timer", mrb->object_class);
     mrb_define_method(mrb,  mrb_mbed_timer, "initialize",   mrb_mbed_timer_init,    MRB_ARGS_NONE());
@@ -203,6 +49,29 @@
     mrb_define_method(mrb,  mrb_mbed_timer, "read",         mrb_mbed_timer_read,    MRB_ARGS_NONE());
     mrb_define_method(mrb,  mrb_mbed_timer, "read_ms",      mrb_mbed_timer_read_ms, MRB_ARGS_NONE());
     mrb_define_method(mrb,  mrb_mbed_timer, "read_us",      mrb_mbed_timer_read_us, MRB_ARGS_NONE());
+    
+    mrb_mbed_i2c = mrb_define_class_under(mrb, mrb_mbed, "I2C", mrb->object_class);
+    mrb_define_method(mrb,  mrb_mbed_i2c,   "initialize",   mrb_mbed_i2c_init,      MRB_ARGS_REQ(2));
+    mrb_define_method(mrb,  mrb_mbed_i2c,   "frequency",    mrb_mbed_i2c_freq,      MRB_ARGS_REQ(1));
+    //mrb_define_method(mrb,  mrb_mbed_i2c,   "read",         mrb_mbed_i2c_read,      MRB_ARGS_REQ(1)|MRB_ARGS_OPT(3));
+    //mrb_define_method(mrb,  mrb_mbed_i2c,   "write",        mrb_mbed_i2c_write,     MRB_ARGS_REQ(1)|MRB_ARGS_OPT(3));    
+    mrb_define_method(mrb,  mrb_mbed_i2c,   "start",        mrb_mbed_i2c_start,     MRB_ARGS_NONE());    
+    mrb_define_method(mrb,  mrb_mbed_i2c,   "stop",         mrb_mbed_i2c_stop,      MRB_ARGS_NONE());    
+
+    mrb_mbed_spi = mrb_define_class_under(mrb, mrb_mbed, "SPI", mrb->object_class);
+    mrb_define_method(mrb,  mrb_mbed_spi,   "initialize",   mrb_mbed_spi_init,      MRB_ARGS_REQ(3));
+    mrb_define_method(mrb,  mrb_mbed_spi,   "frequency",    mrb_mbed_spi_freq,      MRB_ARGS_OPT(1));
+    mrb_define_method(mrb,  mrb_mbed_spi,   "format",       mrb_mbed_spi_format,    MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
+    mrb_define_method(mrb,  mrb_mbed_spi,   "write",        mrb_mbed_spi_write,     MRB_ARGS_REQ(1));
+
+    mrb_mbed_spis = mrb_define_class_under(mrb, mrb_mbed, "SPISlave", mrb->object_class);
+    mrb_define_method(mrb,  mrb_mbed_spis,  "initialize",   mrb_mbed_spis_init,     MRB_ARGS_REQ(4));
+    mrb_define_method(mrb,  mrb_mbed_spis,  "frequency",    mrb_mbed_spis_freq,     MRB_ARGS_OPT(1));
+    mrb_define_method(mrb,  mrb_mbed_spis,  "format",       mrb_mbed_spis_format,   MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
+    mrb_define_method(mrb,  mrb_mbed_spis,  "reply",        mrb_mbed_spis_reply,    MRB_ARGS_REQ(1));
+    mrb_define_method(mrb,  mrb_mbed_spis,  "read",         mrb_mbed_spis_read,     MRB_ARGS_NONE());
+    mrb_define_method(mrb,  mrb_mbed_spis,  "receive",      mrb_mbed_spis_receive,  MRB_ARGS_NONE());
+
 }
 
 void
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbDigitalOut.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbDigitalOut.cpp	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,63 @@
+#include "mbed.h"
+#include "mrbDigitalOut.h"
+
+static struct mrb_data_type mbed_do_type = {
+    "DigitalOut", mrb_mbed_do_free
+};
+
+mrb_value
+mrb_mbed_do_init(mrb_state *mrb, mrb_value self)
+{
+    mrb_int pin;
+    mrb_get_args(mrb, "i", &pin);
+    PinName pinname = static_cast<PinName>(pin);
+
+    DATA_TYPE(self) = &mbed_do_type;
+    DATA_PTR(self) = new DigitalOut(pinname);
+
+    return self;
+}
+
+void
+mrb_mbed_do_free(mrb_state *mrb, void *ptr)
+{
+    DigitalOut *obj = static_cast<DigitalOut*>(ptr);
+    delete obj;
+}
+
+mrb_value
+mrb_mbed_do_write(mrb_state *mrb, mrb_value self)
+{
+    DigitalOut *obj = static_cast<DigitalOut*>(DATA_PTR(self));
+
+    mrb_int val;
+    mrb_get_args(mrb, "i", &val);
+
+    obj->write(val);
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_do_read(mrb_state *mrb, mrb_value self)
+{
+    DigitalOut *obj = static_cast<DigitalOut*>(DATA_PTR(self));
+
+    return mrb_fixnum_value(obj->read());
+}
+
+mrb_value
+mrb_mbed_do_high(mrb_state *mrb, mrb_value self)
+{
+    DigitalOut *obj = static_cast<DigitalOut*>(DATA_PTR(self));
+
+    obj->write(1);
+}
+
+mrb_value
+mrb_mbed_do_low(mrb_state *mrb, mrb_value self)
+{
+    DigitalOut *obj = static_cast<DigitalOut*>(DATA_PTR(self));
+
+    obj->write(0);
+}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbDigitalOut.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbDigitalOut.h	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,22 @@
+#ifndef MRB_DIGITALOUT_H
+#define MRB_DIGITALOUT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mruby.h"
+#include "mruby/data.h"
+
+extern mrb_value mrb_mbed_do_init(mrb_state *mrb, mrb_value self);
+extern void mrb_mbed_do_free(mrb_state *mrb, void *ptr);
+extern mrb_value mrb_mbed_do_write(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_do_read(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_do_high(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_do_low(mrb_state *mrb, mrb_value self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbI2C.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbI2C.cpp	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,61 @@
+#include "mbed.h"
+#include "mrbI2C.h"
+
+static struct mrb_data_type mbed_i2c_type = {
+    "I2C", mrb_mbed_i2c_free
+};
+
+mrb_value
+mrb_mbed_i2c_init(mrb_state *mrb, mrb_value self)
+{
+    mrb_int sda, scl;
+
+    mrb_get_args(mrb, "ii", &sda, &scl);
+    PinName pin_sda = static_cast<PinName>(sda);
+    PinName pin_scl = static_cast<PinName>(scl);
+
+    DATA_TYPE(self) = &mbed_i2c_type;
+    DATA_PTR(self) = new I2C(pin_sda, pin_scl);
+
+    return self;
+}
+
+void
+mrb_mbed_i2c_free(mrb_state *mrb, void *ptr)
+{
+    I2C *obj = static_cast<I2C*>(ptr);
+    delete obj;
+}
+
+mrb_value
+mrb_mbed_i2c_freq(mrb_state *mrb, mrb_value self)
+{
+    I2C *obj = static_cast<I2C*>(DATA_PTR(self));
+
+    mrb_int freq;
+    mrb_get_args(mrb, "i", &freq);
+
+    obj->frequency(freq);
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_i2c_start(mrb_state *mrb, mrb_value self)
+{
+    I2C *obj = static_cast<I2C*>(DATA_PTR(self));
+
+    obj->start();
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_i2c_stop(mrb_state *mrb, mrb_value self)
+{
+    I2C *obj = static_cast<I2C*>(DATA_PTR(self));
+
+    obj->stop();
+
+    return self;
+}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbI2C.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbI2C.h	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,21 @@
+#ifndef MRB_I2C_H
+#define MRB_I2C_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mruby.h"
+#include "mruby/data.h"
+
+extern mrb_value mrb_mbed_i2c_init(mrb_state *mrb, mrb_value self);
+extern void mrb_mbed_i2c_free(mrb_state *mrb, void *ptr);
+extern mrb_value mrb_mbed_i2c_freq(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_i2c_start(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_i2c_stop(mrb_state *mrb, mrb_value self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbMbedFunc.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbMbedFunc.cpp	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+#include "mrbMbedFunc.h"
+
+mrb_value
+mrb_mbed_sleep(mrb_state *mrb, mrb_value obj)
+{
+    mrb_int t;
+    mrb_get_args(mrb, "i", &t);
+    wait((float)t);
+
+    return obj;
+}
+
+mrb_value
+mrb_mbed_sleep_ms(mrb_state *mrb, mrb_value obj)
+{
+    mrb_int t;
+    mrb_get_args(mrb, "i", &t);
+    wait_ms(t);
+
+    return obj;
+}
+
+mrb_value
+mrb_mbed_sleep_us(mrb_state *mrb, mrb_value obj)
+{
+    mrb_int t;
+    mrb_get_args(mrb, "i", &t);
+    wait_us(t);
+
+    return obj;
+}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbMbedFunc.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbMbedFunc.h	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,18 @@
+#ifndef MRB_MBEDFUNC_H
+#define MRB_MBEDFUNC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mruby.h"
+
+extern mrb_value mrb_mbed_sleep(mrb_state *mrb, mrb_value obj);
+extern mrb_value mrb_mbed_sleep_ms(mrb_state *mrb, mrb_value obj);
+extern mrb_value mrb_mbed_sleep_us(mrb_state *mrb, mrb_value obj);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbPinName.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbPinName.cpp	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,24 @@
+
+#include "mbed.h"
+#include "PinNames.h"
+
+#include "mrbPinName.h"
+
+void
+mrb_mbed_pinname_define(mrb_state *mrb, struct RClass *mrb_mbed)
+{
+    mrb_define_const(mrb,   mrb_mbed,   "LED1",         mrb_fixnum_value(LED1));
+    mrb_define_const(mrb,   mrb_mbed,   "LED2",         mrb_fixnum_value(LED2));
+    mrb_define_const(mrb,   mrb_mbed,   "LED_RED",      mrb_fixnum_value(LED_RED));
+    mrb_define_const(mrb,   mrb_mbed,   "LED_GREEN",    mrb_fixnum_value(LED_GREEN));
+    mrb_define_const(mrb,   mrb_mbed,   "LED_BLUE",     mrb_fixnum_value(LED_BLUE));
+    mrb_define_const(mrb,   mrb_mbed,   "I2C_SCL",      mrb_fixnum_value(I2C_SCL));
+    mrb_define_const(mrb,   mrb_mbed,   "I2C_SDA",      mrb_fixnum_value(I2C_SDA));
+
+#if defined(TARGET_MBED_MBRZA1H)
+    mrb_define_const(mrb,   mrb_mbed,   "LED3",         mrb_fixnum_value(LED3));
+    mrb_define_const(mrb,   mrb_mbed,   "LED4",         mrb_fixnum_value(LED4));
+    mrb_define_const(mrb,   mrb_mbed,   "LED_USER",     mrb_fixnum_value(LED_USER));
+#endif
+
+}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbPinName.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbPinName.h	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,16 @@
+#ifndef MRB_PINNAME_H
+#define MRB_PINNAME_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mruby.h"
+
+extern void mrb_mbed_pinname_define(mrb_state *mrb, struct RClass *mrb_mbed);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbSPI.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbSPI.cpp	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+#include "mrbSPI.h"
+
+static struct mrb_data_type mbed_spi_type = {
+    "SPI", mrb_mbed_spi_free
+};
+
+mrb_value
+mrb_mbed_spi_init(mrb_state *mrb, mrb_value self)
+{
+    mrb_int mosi, miso, sclk;
+
+    mrb_get_args(mrb, "iii", &mosi, &miso, &sclk);
+    PinName pin_mosi = static_cast<PinName>(mosi);
+    PinName pin_miso = static_cast<PinName>(miso);
+    PinName pin_sclk = static_cast<PinName>(sclk);
+
+    DATA_TYPE(self) = &mbed_spi_type;
+    DATA_PTR(self) = new SPI(pin_mosi, pin_miso, pin_sclk);
+
+    return self;
+}
+
+void
+mrb_mbed_spi_free(mrb_state *mrb, void *ptr)
+{
+    SPI *obj = static_cast<SPI*>(ptr);
+    delete obj;
+}
+
+mrb_value
+mrb_mbed_spi_freq(mrb_state *mrb, mrb_value self)
+{
+    SPI *obj = static_cast<SPI*>(DATA_PTR(self));
+
+    mrb_int freq;
+    if (mrb_get_args(mrb, "|i", &freq) == 0) {
+        obj->frequency();
+    } else {
+        obj->frequency(freq);
+    }
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_spi_format(mrb_state *mrb, mrb_value self)
+{
+    SPI *obj = static_cast<SPI*>(DATA_PTR(self));
+
+    mrb_int bits, mode;
+    if (mrb_get_args(mrb, "i|i", &bits, &mode) == 1) {
+        obj->format(bits);
+    } else {
+        obj->format(bits, mode);    
+    }
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_spi_write(mrb_state *mrb, mrb_value self)
+{
+    SPI *obj = static_cast<SPI*>(DATA_PTR(self));
+
+    mrb_int value;
+    mrb_get_args(mrb, "i", &value);
+    obj->write(value);
+
+    return self;
+}
\ No newline at end of file
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbSPI.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbSPI.h	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,21 @@
+#ifndef MRB_SPI_H
+#define MRB_SPI_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mruby.h"
+#include "mruby/data.h"
+
+extern mrb_value mrb_mbed_spi_init(mrb_state *mrb, mrb_value self);
+extern void mrb_mbed_spi_free(mrb_state *mrb, void *ptr);
+extern mrb_value mrb_mbed_spi_format(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_spi_freq(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_spi_write(mrb_state *mrb, mrb_value self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbSPISlave.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbSPISlave.cpp	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,100 @@
+#include "mbed.h"
+#include "mrbSPISlave.h"
+
+static struct mrb_data_type mbed_spis_type = {
+    "SPISlave", mrb_mbed_spis_free
+};
+
+mrb_value
+mrb_mbed_spis_init(mrb_state *mrb, mrb_value self)
+{
+    mrb_int mosi, miso, sclk, ssel;
+
+    mrb_get_args(mrb, "iiii", &mosi, &miso, &sclk, &ssel);
+    PinName pin_mosi = static_cast<PinName>(mosi);
+    PinName pin_miso = static_cast<PinName>(miso);
+    PinName pin_sclk = static_cast<PinName>(sclk);
+    PinName pin_ssel = static_cast<PinName>(ssel);
+
+    DATA_TYPE(self) = &mbed_spis_type;
+    DATA_PTR(self) = new SPISlave(pin_mosi, pin_miso, pin_sclk, pin_ssel);
+
+    return self;
+}
+
+void
+mrb_mbed_spis_free(mrb_state *mrb, void *ptr)
+{
+    SPISlave *obj = static_cast<SPISlave*>(ptr);
+    delete obj;
+}
+
+mrb_value
+mrb_mbed_spis_freq(mrb_state *mrb, mrb_value self)
+{
+    SPISlave *obj = static_cast<SPISlave*>(DATA_PTR(self));
+
+    mrb_int freq;
+    if (mrb_get_args(mrb, "|i", &freq) == 0) {
+        obj->frequency();
+    } else {
+        obj->frequency(freq);
+    }
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_spis_format(mrb_state *mrb, mrb_value self)
+{
+    SPISlave *obj = static_cast<SPISlave*>(DATA_PTR(self));
+
+    mrb_int bits, mode;
+    if (mrb_get_args(mrb, "i|i", &bits, &mode) == 1) {
+        obj->format(bits);
+    } else {
+        obj->format(bits, mode);    
+    }
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_spis_reply(mrb_state *mrb, mrb_value self)
+{
+    SPISlave *obj = static_cast<SPISlave*>(DATA_PTR(self));
+
+    mrb_int value;
+    mrb_get_args(mrb, "i", &value);
+    obj->reply(value);
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_spis_read(mrb_state *mrb, mrb_value self)
+{
+    SPISlave *obj = static_cast<SPISlave*>(DATA_PTR(self));
+
+    return mrb_fixnum_value(obj->read());
+}
+
+mrb_value
+mrb_mbed_spis_receive(mrb_state *mrb, mrb_value self)
+{
+    SPISlave *obj = static_cast<SPISlave*>(DATA_PTR(self));
+
+    mrb_value blk;
+    mrb_get_args(mrb, "&", &blk);
+    if (mrb_nil_p(blk)) {
+        mrb_raise(mrb, E_ARGUMENT_ERROR, "without a block");
+    } else {
+        while (obj->receive()) {
+            mrb_value ret = mrb_yield(mrb, blk, mrb_nil_value());
+            if (mrb_fixnum(ret)!= 0) {
+                break;
+            } 
+        }
+    }
+    return self;
+}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbSPISlave.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbSPISlave.h	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,24 @@
+#ifndef MRB_SPISLAVE_H
+#define MRB_SPISLAVE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mruby.h"
+#include "mruby/data.h"
+
+extern mrb_value mrb_mbed_spis_init(mrb_state *mrb, mrb_value self);
+extern void mrb_mbed_spis_free(mrb_state *mrb, void *ptr);
+extern mrb_value mrb_mbed_spis_format(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_spis_freq(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_spis_write(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_spis_reply(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_spis_read(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_spis_receive(mrb_state *mrb, mrb_value self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbTimer.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbTimer.cpp	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,73 @@
+#include "mbed.h"
+#include "mrbTimer.h"
+
+static struct mrb_data_type mbed_timer_type = {
+    "Timer", mrb_mbed_timer_free
+};
+
+mrb_value
+mrb_mbed_timer_init(mrb_state *mrb, mrb_value self)
+{
+    DATA_TYPE(self) = &mbed_timer_type;
+    DATA_PTR(self) = new Timer();
+
+    return self;
+}
+
+void
+mrb_mbed_timer_free(mrb_state *mrb, void *ptr)
+{
+    Timer *obj = static_cast<Timer*>(ptr);
+    delete obj;
+}
+
+mrb_value
+mrb_mbed_timer_start(mrb_state *mrb, mrb_value self)
+{
+    Timer *obj = static_cast<Timer*>(DATA_PTR(self));
+    obj->start();
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_timer_stop(mrb_state *mrb, mrb_value self)
+{
+    Timer *obj = static_cast<Timer*>(DATA_PTR(self));
+    obj->stop();
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_timer_reset(mrb_state *mrb, mrb_value self)
+{
+    Timer *obj = static_cast<Timer*>(DATA_PTR(self));
+    obj->reset();
+
+    return self;
+}
+
+mrb_value
+mrb_mbed_timer_read(mrb_state *mrb, mrb_value self)
+{
+    Timer *obj = static_cast<Timer*>(DATA_PTR(self));
+
+    return mrb_float_value(mrb, obj->read());
+}
+
+mrb_value
+mrb_mbed_timer_read_ms(mrb_state *mrb, mrb_value self)
+{
+    Timer *obj = static_cast<Timer*>(DATA_PTR(self));
+
+    return mrb_fixnum_value(obj->read_ms());
+}
+
+mrb_value
+mrb_mbed_timer_read_us(mrb_state *mrb, mrb_value self)
+{
+    Timer *obj = static_cast<Timer*>(DATA_PTR(self));
+
+    return mrb_fixnum_value(obj->read_us());
+}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mbedif/mrbTimer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrbgems/mruby-mbed/mbedif/mrbTimer.h	Mon Apr 13 05:20:15 2015 +0000
@@ -0,0 +1,24 @@
+#ifndef MRB_TIMER_H
+#define MRB_TIMER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "mruby.h"
+#include "mruby/data.h"
+
+extern mrb_value mrb_mbed_timer_init(mrb_state *mrb, mrb_value self);
+extern void mrb_mbed_timer_free(mrb_state *mrb, void *ptr);
+extern mrb_value mrb_mbed_timer_start(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_timer_stop(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_timer_reset(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_timer_read(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_timer_read_ms(mrb_state *mrb, mrb_value self);
+extern mrb_value mrb_mbed_timer_read_us(mrb_state *mrb, mrb_value self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mrbDigitalOut.cpp
--- a/mrbgems/mruby-mbed/mrbDigitalOut.cpp	Wed Mar 25 17:36:16 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-#include "mbed.h"
-#include "mrbDigitalOut.h"
-
-void *
-newDigitalOut(int pin)
-{
-    PinName pinname = static_cast<PinName>(pin);
-    return static_cast<void*>(new DigitalOut(pinname));
-}
-
-void
-delDigitalOut(void *ptr)
-{
-    DigitalOut *obj = static_cast<DigitalOut*>(ptr);
-    delete obj;
-}
-
-void
-writeDigitalOut(void *ptr, int val)
-{
-    DigitalOut *obj = static_cast<DigitalOut*>(ptr);
-    obj->write(val);
-}
-
-int
-readDigitalOut(void *ptr)
-{
-    DigitalOut *obj = static_cast<DigitalOut*>(ptr);
-    return obj->read();
-}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mrbDigitalOut.h
--- a/mrbgems/mruby-mbed/mrbDigitalOut.h	Wed Mar 25 17:36:16 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void *newDigitalOut(int pin);
-void delDigitalOut(void *object);
-void writeDigitalOut(void *object, int value);
-int readDigitalOut(void *object);
-
-#ifdef __cplusplus
-}
-#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mrbMbedFunc.cpp
--- a/mrbgems/mruby-mbed/mrbMbedFunc.cpp	Wed Mar 25 17:36:16 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-#include "mbed.h"
-#include "mrbMbedFunc.h"
-
-void
-mbedSleep(int t)
-{
-    wait((float)t);
-}
-
-void
-mbedSleepMS(int t)
-{
-    wait_ms(t);
-}
-
-void
-mbedSleepUS(int t)
-{
-    wait_us(t);
-}
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mrbMbedFunc.h
--- a/mrbgems/mruby-mbed/mrbMbedFunc.h	Wed Mar 25 17:36:16 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void mbedSleep(int t);
-void mbedSleepMS(int t);
-void mbedSleepUS(int t);
-
-#ifdef __cplusplus
-}
-#endif
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mrbPinName.h
--- a/mrbgems/mruby-mbed/mrbPinName.h	Wed Mar 25 17:36:16 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-
-#include "PinNames.h"
-
-
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mrbTimer.cpp
--- a/mrbgems/mruby-mbed/mrbTimer.cpp	Wed Mar 25 17:36:16 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-#include "mbed.h"
-#include "mrbTimer.h"
-
-void *
-newTimer()
-{
-    return static_cast<void*>(new Timer());
-}
-
-void
-delTimer(void *ptr)
-{
-    Timer *obj = static_cast<Timer*>(ptr);
-    delete obj;
-}
-
-void
-startTimer(void *ptr)
-{
-    Timer *obj = static_cast<Timer*>(ptr);
-    obj->start();
-}
-
-void
-stopTimer(void *ptr)
-{
-    Timer *obj = static_cast<Timer*>(ptr);
-    obj->stop();
-}
-
-void
-resetTimer(void *ptr)
-{
-    Timer *obj = static_cast<Timer*>(ptr);
-    obj->reset();
-}
-
-float
-readTimer(void *ptr)
-{
-    Timer *obj = static_cast<Timer*>(ptr);
-    return obj->read();
-}
-
-int
-readMSTimer(void *ptr)
-{
-    Timer *obj = static_cast<Timer*>(ptr);
-    return obj->read_ms();
-}
-
-int
-readUSTimer(void *ptr)
-{
-    Timer *obj = static_cast<Timer*>(ptr);
-    return obj->read_us();
-}
-
-
diff -r 158c61bb030f -r 8ccd1d494a4b mrbgems/mruby-mbed/mrbTimer.h
--- a/mrbgems/mruby-mbed/mrbTimer.h	Wed Mar 25 17:36:16 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void *newTimer();
-void delTimer(void *ptr);
-void startTimer(void *ptr);
-void stopTimer(void *ptr);
-void resetTimer(void *ptr);
-float readTimer(void *ptr);
-int readMSTimer(void *ptr);
-int readUSTimer(void *ptr);
-
-#ifdef __cplusplus
-}
-#endif
-
-