OSDN Git Service

2.0.2
[mimic/MiMicSDK.git] / misc / mbedJS / mbddJS.API / mbedJS / mbed.PwmOut.js
index 10d9aec..6fe758d 100644 (file)
@@ -14,8 +14,8 @@ var MI=MiMicJS;
  * インスタンスをバインドするMCUオブジェクトです。\r
  * @param {PinName} i_params\r
  * ピンIDを指定します。\r
- * @param {HashMap|Generator} i_event\r
- * 非同期イベントハンドラの連想配列、又はGeneratorです。\r
+ * @param {HashMap|Generator|function} i_handler\r
+ * 非同期イベントハンドラの連想配列、Generator、コールバック関数の何れかを指定します。\r
  * <p>\r
  * 非同期イベントハンドラの場合、関数はイベントハンドラで結果を通知します。\r
  * <ul>\r
@@ -52,7 +52,10 @@ var MI=MiMicJS;
  * </ul>\r
  * <p>\r
  * Generatorを指定した場合、コールバック関数の引数はyiledの戻り値として取得できます。\r
+ * </p>\r
  * <p>\r
+ * コールバック関数を指定した場合、RPCが完了したときに呼び出されます。メンバ関数のイベントハンドラは個別に設定する必要があります。\r
+ * </p>\r
  * @return {mbedJS.PwmOut}\r
  * @example //Callback\r
  * var mcu=new mbedJS.Mcu("192.168.128.39",\r
@@ -122,17 +125,17 @@ var CLASS=function PwmOut(i_mcu,i_params,i_handler)
                var _t=this;\r
                _t._mcu=i_mcu;\r
                _t._lc=CLASS;\r
-               if(MI.isGenerator(i_handler)){_t._gen=i_handler;}\r
-               else if(i_handler){_t._event=i_handler}\r
-               function cb(j)\r
-               {\r
-                       _t._oid=j.result[0];\r
-                       if(_t._event.onNew){_t._event.onNew();}\r
-                       if(_t._gen){_t._gen.next(_t);}\r
-                       _t._lc=null;\r
-               }\r
+               var cb=MI._initHandler.call(_t,i_handler);\r
                MI.assertInt(i_params);\r
-               return _t._mcu.rpc(_t.RPC_NS+":_new1",i_params,cb);\r
+               _t._mcu.rpc(_t.RPC_NS+":_new1",i_params,\r
+                       function(j)\r
+                       {\r
+                               _t._oid=j.result[0];\r
+                               if(cb){cb();}\r
+                               if(_t._gen){_t._gen.next(_t);}\r
+                               _t._lc=null;\r
+                       }\r
+               );\r
        }catch(e){\r
                throw new MI.MiMicException(e);\r
        }\r
@@ -180,12 +183,13 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onWrite);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.write;\r
                        MI.assertNumber(i_value);\r
                        return _t._mcu.rpc(_t.RPC_NS+":write_fx",_t._oid+","+Math.round(i_value*10000),\r
                        function(j){\r
-                               if(_t._event.onWrite){_t._event.onWrite();}\r
+                               if(cb){cb();}\r
                                if(_t._gen){_t._gen.next();}\r
                                 _t._lc=null;\r
                        });\r
@@ -207,13 +211,14 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onRead);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.read;\r
                        return _t._mcu.rpc(_t.RPC_NS+":read_fx",_t._oid,\r
                        function (j)\r
                        {\r
                                var v=j.result[0]/10000;\r
-                               if(_t._event.onRead){_t._event.onRead(v);}\r
+                               if(cb){cb(v);}\r
                                if(_t._gen){_t._gen.next(v);}\r
                                 _t._lc=null;\r
                        });\r
@@ -237,12 +242,13 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onPeriod);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.period;\r
                        MI.assertInt(i_value);\r
                        return _t._mcu.rpc(_t.RPC_NS+":period_fx",_t._oid+","+Math.round(i_value*10000),\r
                        function(j){\r
-                               if(_t._event.onPeriod){_t._event.onPeriod();}\r
+                               if(cb){cb();}\r
                                if(_t._gen){_t._gen.next();}\r
                                 _t._lc=null;\r
                        });\r
@@ -266,12 +272,13 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onPeriod_ms);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.period_ms;\r
                        MI.assertInt(i_value);\r
                        return _t._mcu.rpc(_t.RPC_NS+":period_ms",_t._oid+","+Math.round(i_value),\r
                        function(j){\r
-                               if(_t._event.onPeriod_ms){_t._event.onPeriod_ms();}\r
+                               if(cb){cb();}\r
                                if(_t._gen){_t._gen.next();}\r
                                 _t._lc=null;\r
                        });\r
@@ -295,12 +302,13 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onPeriod_us);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.period_us;\r
                        MI.assertInt(i_value);\r
                        return _t._mcu.rpc(_t.RPC_NS+":period_us",_t._oid+","+Math.round(i_value),\r
                        function(j){\r
-                               if(_t._event.onPeriod_us){_t._event.onPeriod_us();}\r
+                               if(cb){cb();}\r
                                if(_t._gen){_t._gen.next();}\r
                                 _t._lc=null;\r
                        });\r
@@ -324,12 +332,13 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onPulsewidth);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.pulsewidth;\r
                        MI.assertInt(i_value);\r
                        return _t._mcu.rpc(_t.RPC_NS+":pulsewidth_fx",_t._oid+","+Math.round(i_value*10000),\r
                        function(j){\r
-                               if(_t._event.onPulsewidth){_t._event.onPulsewidth();}\r
+                               if(cb){cb();}\r
                                if(_t._gen){_t._gen.next();}\r
                                 _t._lc=null;\r
                        });\r
@@ -352,15 +361,17 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onPulsewidth_ms);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.pulsewidth_ms;\r
                        MI.assertInt(i_value);\r
                        return _t._mcu.rpc(_t.RPC_NS+":pulsewidth_ms",_t._oid+","+Math.round(i_value),\r
-                       function(j){\r
-                               if(_t._event.onPulsewidth_ms){_t._event.onPulsewidth_ms();}\r
-                               if(_t._gen){_t._gen.next();}\r
-                                _t._lc=null;\r
-                       });\r
+                               function(j){\r
+                                       if(cb){cb();}\r
+                                       if(_t._gen){_t._gen.next();}\r
+                                        _t._lc=null;\r
+                               }\r
+                       );\r
                }catch(e){\r
                        throw new MI.MiMicException(e);\r
                }\r
@@ -380,15 +391,17 @@ CLASS.prototype=
        {\r
                try{\r
                        var _t=this;\r
-                       if(this._gen && this._lc){throw new MI.MiMicException(MI.Error.NG_YIELD_NOT_COMPLETED);}\r
+                       var cb=MI._getCb(arguments,_t._event.onPulsewidth_us);\r
+                       MI._assertYield.call(_t);\r
                        _t._lc=CLASS.pulsewidth_us;\r
                        MI.assertInt(i_value);\r
                        return _t._mcu.rpc(_t.RPC_NS+":pulsewidth_us",_t._oid+","+Math.round(i_value),\r
-                       function(j){\r
-                               if(_t._event.onPulsewidth_us){_t._event.onPulsewidth_us();}\r
-                               if(_t._gen){_t._gen.next();}\r
-                                _t._lc=null;\r
-                       });\r
+                               function(j){\r
+                                       if(cb){cb();}\r
+                                       if(_t._gen){_t._gen.next();}\r
+                                        _t._lc=null;\r
+                               }\r
+                       );\r
                }catch(e){\r
                        throw new MI.MiMicException(e);\r
                }\r