TUsb Respiration Belt plugin 1.0

src/Plugin.cpp

Go to the documentation of this file.
00001 
00002 /*
00003 -----------------------------------------------------------------------------
00004 This source file is part of OpenSpace3D
00005 For the latest info, see http://www.openspace3d.com
00006 
00007 Copyright (c) 2010 I-maginer
00008 
00009 This program is free software; you can redistribute it and/or modify it under
00010 the terms of the GNU Lesser General Public License as published by the Free Software
00011 Foundation; either version 2 of the License, or (at your option) any later
00012 version.
00013 
00014 This program is distributed in the hope that it will be useful, but WITHOUT
00015 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00016 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00017 
00018 You should have received a copy of the GNU Lesser General Public License along with
00019 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00020 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00021 http://www.gnu.org/copyleft/lesser.txt
00022 
00023 You may alternatively use this source under the terms of a specific version of
00024 the OpenSpace3D Unrestricted License provided you have obtained such a license from
00025 I-maginer.
00026 -----------------------------------------------------------------------------
00027 */
00028 
00029 /*
00030  TUsb respiration belt library
00031  First version : august 2011
00032  Author : Aymeric Suteau
00033 */
00034 
00049 // Include Header File
00050 #include "Plugin.h"
00051 
00053 cbmachine ww;
00054 HWND HScol = NULL;      
00055 
00057 int OBJTUSBSCOL;
00058 
00060 //===== CB Connected ===
00061 int SCOL_TUSB_CONNECTED_CB      = 0;
00062 int TUSB_CONNECTED_CB;
00063 
00064 //===== CB Disconnected ===
00065 int SCOL_TUSB_DISCONNECTED_CB   = 1;
00066 int TUSB_DISCONNECTED_CB;
00067 
00068 //===== CB New data ===
00069 int SCOL_TUSB_NEW_DATA_CB       = 2;
00070 int TUSB_NEW_DATA_CB;
00071 
00072 //===== CB Battery level ===
00073 int SCOL_TUSB_BATTERY_LEVEL_CB  = 3;
00074 int TUSB_BATTERY_LEVEL_CB;
00075 
00076 
00088 
00089 int destroyTUsbObj(mmachine m, int handsys, int respirationBeltTab) 
00090 {
00091         // Read the first element of a TAB element (table of objects)
00092         RespirationBelt* RespirationBeltObj = (RespirationBelt*) MMfetch(m, MTOP(respirationBeltTab), 0);
00093   if (RespirationBeltObj == NULL)
00094   {
00095                 // Write the first element in the stack, without pulling it
00096                 MMset(m, 0, NIL); 
00097                 return 0; 
00098         }
00099 
00100         // Safely dispose of "RespirationBeltObj" pointer
00101         SAFE_DELETE(RespirationBeltObj);
00102   MMechostr(MSKDEBUG, "destroyTUsbObj ...SAFE_DELETE ok\n");
00103 
00104         // Write the first element of a TAB element
00105         MMstore(m, MTOP(respirationBeltTab), 0, 0);
00106 
00107         // Display debug message
00108         MMechostr(0, "RespirationBeltObj destroyed.\n");
00109         return 0;
00110 }
00111 
00112 
00121 int _CRTUsbDevice(mmachine m)
00122 {
00123         #ifdef  _SCOL_DEBUG_
00124                 MMechostr(0,"_CRTUsbDevice\n");
00125         #endif
00126 
00127         // Declare local variables
00128         int k = 0;
00129 
00130   // Get and test Channel
00131         int channel = MMget(m, 0);
00132         if (channel == NIL) 
00133   { 
00134                 MMechostr(MSKDEBUG, "Channel NIL\n");     // Display Warning in Scol Console
00135                 MMpull(m);
00136                 MMpush(m, NIL);
00137                 return 0;
00138         }
00139 
00140         // Create RespirationBelt instance
00141         RespirationBelt* respirationBelt = new RespirationBelt();
00142 
00143         // Allocate a block in the stack for a table of RespirationBelt objects
00144         int respirationBeltTab = MMmalloc(m, 1, TYPETAB);
00145         if (respirationBeltTab == NIL)
00146   {
00147                 MMechostr(0, "_CRTUsbDevice ...MMmalloc failed\n");
00148                 SAFE_DELETE(respirationBelt); 
00149                 MMpull(m);
00150                 return MMpush(m, NIL);
00151         }
00152         MMstore(m, respirationBeltTab, 0, (int)respirationBelt);
00153         MMpush(m, PTOM(respirationBeltTab));
00154 
00155         // Create a new TUsb respiration RespirationBelt object
00156         k = OBJcreate(m, OBJTUSBSCOL, (int)respirationBelt, -1, -1);
00157 
00158         #ifdef  _SCOL_DEBUG_
00159                 MMechostr(0,"ok\n");
00160         #endif
00161 
00162         // Return RespirationBelt object
00163         return k;
00164 }
00165 
00166 
00175 int _DSTUsbDevice(mmachine m)
00176 {
00177         #ifdef  _SCOL_DEBUG_
00178                 MMechostr(0,"_DSTUsbDevice\n");
00179   #endif
00180 
00181         // Get the table of RespirationBelt objects into the stack (without pulling it)
00182         int respirationBeltTab = MMget(m, 0);
00183         if (respirationBeltTab == NIL)
00184   {
00185                 MMechostr(0, "_DSTUsbDevice ...ObjTUsb NIL\n");
00186                 MMset(m, 0, NIL);
00187                 return 0;
00188         }
00189 
00190         // Destroy RespirationBelt object according to its type and magma object, and reinitialize the stack
00191   OBJdelTM(m, OBJTUSBSCOL, respirationBeltTab);
00192         MMset(m, 0, ITOM(0));
00193 
00194         #ifdef  _SCOL_DEBUG_
00195                 MMechostr(0,"ok\n");
00196         #endif
00197 
00198         return 0;
00199 }
00200 
00201 
00215 int _CBTUsbConnected(mmachine m)
00216 {
00217   return (OBJaddreflex(m, OBJTUSBSCOL, SCOL_TUSB_CONNECTED_CB));
00218 }
00219 
00220 int getTUsbConnectedCb(mmachine m, HWND h, unsigned msg, UINT id, LONG param, int *ret)
00221 {
00222         int k = 0;
00223         RespirationBelt* RespirationBeltObj = (RespirationBelt*) id;
00224 
00225         // OBJbeginreflex(mmachine, objecttype, objectptr, cbtype)
00226         if (OBJbeginreflex(m, OBJTUSBSCOL, (int)RespirationBeltObj, SCOL_TUSB_CONNECTED_CB))
00227   {
00228     MMechostr(0, "getTUsbConnectedCb -> OBJbeginreflex failed.\n");
00229                 return 0;
00230   }
00231         return (OBJcallreflex(m, 0));
00232 }
00233 
00234 
00248 int _CBTUsbDisconnected(mmachine m)
00249 {
00250   return (OBJaddreflex(m, OBJTUSBSCOL, SCOL_TUSB_DISCONNECTED_CB));
00251 }
00252 
00253 int getTUsbDisconnectedCb(mmachine m, HWND h, unsigned msg, UINT id, LONG param, int *ret)
00254 {
00255         int k = 0;
00256         RespirationBelt* RespirationBeltObj = (RespirationBelt*) id;
00257 
00258         // OBJbeginreflex(mmachine, objecttype, objectptr, cbtype)
00259         if (OBJbeginreflex(m, OBJTUSBSCOL, (int)RespirationBeltObj, SCOL_TUSB_DISCONNECTED_CB))
00260   {
00261     MMechostr(0, "getTUsbDisconnectedCb -> OBJbeginreflex failed.\n");
00262                 return 0;
00263   }
00264         return (OBJcallreflex(m, 0));
00265 }
00266 
00267 
00282 int _CBTUsbNewData(mmachine m)
00283 {
00284   return (OBJaddreflex(m, OBJTUSBSCOL, SCOL_TUSB_NEW_DATA_CB));
00285 }
00286 
00287 int getTUsbNewDataCb(mmachine m, HWND h, unsigned msg, UINT id, LONG param, int *ret)
00288 {
00289         int k = 0, nb = 0;
00290   BeltData* beltData = (BeltData*) param;
00291         RespirationBelt* RespirationBeltObj = (RespirationBelt*) id;
00292 
00293         // OBJbeginreflex(mmachine, objecttype, objectptr, cbtype)
00294         if (OBJbeginreflex(m, OBJTUSBSCOL, (int)RespirationBeltObj, SCOL_TUSB_NEW_DATA_CB))
00295   {
00296     SAFE_DELETE(beltData);
00297     MMechostr(0, "getTUsbNewDataCb -> OBJbeginreflex failed.\n");
00298                 return 0;
00299   }
00300 
00301   // Browse new data
00302   std::list<float> lData = beltData->GetBeltData();
00303   std::list<float>::iterator itData = lData.begin();
00304   while (itData != lData.end())
00305   {
00306     if (MMpush(m, FTOM(*itData)))
00307     {
00308       SAFE_DELETE(beltData);
00309       return MERRMEM;
00310     }
00311     itData++;
00312     nb++;
00313   }
00314 
00315   if (MMpush(m, NIL))
00316   {
00317     SAFE_DELETE(beltData);
00318     return MERRMEM;
00319   }
00320 
00321   for (int i = 0; i < nb; i++)
00322         {
00323     if (MMpush(m, 2*2))
00324     {
00325       SAFE_DELETE(beltData);
00326       return MERRMEM;
00327     }
00328                 if (k = MBdeftab(m))
00329     {
00330       SAFE_DELETE(beltData);
00331       return k;
00332     }
00333         }
00334 
00335   // Call reflex previously defined
00336         k = OBJcallreflex(m, 1);
00337   SAFE_DELETE(beltData);
00338   return k;
00339 }
00340 
00341 
00356 int _CBTUsbBatteryLevel(mmachine m)
00357 {
00358   return (OBJaddreflex(m, OBJTUSBSCOL, SCOL_TUSB_BATTERY_LEVEL_CB));
00359 }
00360 
00361 int getTUsbBatteryLevelCb(mmachine m, HWND h, unsigned msg, UINT id, LONG param, int *ret)
00362 {
00363         int k = 0;
00364         RespirationBelt* RespirationBeltObj = (RespirationBelt*) id;
00365 
00366         // OBJbeginreflex(mmachine, objecttype, objectptr, cbtype)
00367         if (OBJbeginreflex(m, OBJTUSBSCOL, (int)RespirationBeltObj, SCOL_TUSB_BATTERY_LEVEL_CB))
00368   {
00369     MMechostr(0, "getTUsbBatteryLevelCb -> OBJbeginreflex failed.\n");
00370                 return 0;
00371   }
00372 
00373   // Push battery level into the stack
00374   MMpush(m, FTOM(param));
00375         return (OBJcallreflex(m, 1));
00376 }
00377 
00378 
00386 
00387 
00389 #define NbTplPKG 7
00390 
00391 
00395 char *TplName[NbTplPKG] =
00396 {
00397         "ObjTUsb",
00398   "_CRTUsbDevice",
00399         "_DSTUsbDevice",
00400   "_CBTUsbConnected",
00401   "_CBTUsbDisconnected",
00402   "_CBTUsbNewData",
00403   "_CBTUsbBatteryLevel"
00404 };
00405 
00406 
00410 int (*TplFunc[NbTplPKG])(mmachine m)=
00411 {
00412         NULL,                                                                           // ObjTUsb
00413   _CRTUsbDevice,                                  // _CRTUsbDevice
00414         _DSTUsbDevice,                            // _DSTUsbDevice
00415   _CBTUsbConnected,       // _CBTUsbConnected
00416   _CBTUsbDisconnected,    // _CBTUsbDisconnected
00417   _CBTUsbNewData,         // _CBTUsbNewData
00418   _CBTUsbBatteryLevel     // _CBTUsbBatteryLevel
00419 };
00420 
00421 
00425 int TplNArg[NbTplPKG]=
00426 {
00427         TYPTYPE,    // ObjTUsb
00428   1,                                    // _CRTUsbDevice
00429         1,                                      // _DSTUsbDevice
00430   3,          // _CBTUsbConnected
00431   3,          // _CBTUsbDisconnected
00432   3,          // _CBTUsbNewData
00433   3           // _CBTUsbBatteryLevel
00434 };
00435 
00436 
00440 char* TplType[NbTplPKG]=
00441 {
00442         NULL,                                                                                                                                                                         // ObjTUsb
00443   "fun [Chn] ObjTUsb",                                                                                            // _CRTUsbDevice
00444         "fun [ObjTUsb] I",                                                                                                        // _DSTUsbDevice
00445   "fun [ObjTUsb fun [ObjTUsb u0] u1 u0] ObjTUsb",             // _CBTUsbConnected
00446   "fun [ObjTUsb fun [ObjTUsb u0] u1 u0] ObjTUsb",             // _CBTUsbDisconnected
00447   "fun [ObjTUsb fun [ObjTUsb u0 [F r1]] u1 u0] ObjTUsb",    // _CBTUsbNewData
00448   "fun [ObjTUsb fun [ObjTUsb u0 F] u1 u0] ObjTUsb"          // _CBTUsbBatteryLevel
00449 };
00450 
00451 
00459 // Everything inside _cond and _endcond is ignored by doxygen
00461 
00466 int LoadTUsb(mmachine m)
00467 {
00468   // Return variable for PKhardpak function
00469         int k;
00470 
00471         // Declare a new type of object ("OBJTUSBSCOL")
00472         OBJTUSBSCOL = OBJregister(4, 1, destroyTUsbObj, "OBJTUSBSCOL");
00473 
00474   // Get new user events and associate these events with a callback
00475   TUSB_CONNECTED_CB = OBJgetUserEvent();
00476   OBJdefEvent(TUSB_CONNECTED_CB, (int (__cdecl *)(struct Mmachine *, int, unsigned int, int, int, int *))getTUsbConnectedCb);
00477 
00478   TUSB_DISCONNECTED_CB = OBJgetUserEvent();
00479   OBJdefEvent(TUSB_DISCONNECTED_CB, (int (__cdecl *)(struct Mmachine *, int, unsigned int, int, int, int *))getTUsbDisconnectedCb);
00480 
00481   TUSB_NEW_DATA_CB = OBJgetUserEvent();
00482   OBJdefEvent(TUSB_NEW_DATA_CB, (int (__cdecl *)(struct Mmachine *, int, unsigned int, int, int, int *))getTUsbNewDataCb);
00483 
00484   TUSB_BATTERY_LEVEL_CB = OBJgetUserEvent();
00485   OBJdefEvent(TUSB_BATTERY_LEVEL_CB, (int (__cdecl *)(struct Mmachine *, int, unsigned int, int, int, int *))getTUsbBatteryLevelCb);
00486 
00487         // Load package
00488         k = PKhardpak(m, "FusionEngine", NbTplPKG, TplName, TplFunc, TplNArg, TplType);
00489 
00490   // Initialize COM library
00491   if (FAILED(::CoInitialize(NULL)))     
00492     return 0;
00493 
00494         return k;
00495 }
00497 
00498 
00502 extern "C" __declspec (dllexport) int ScolLoadPlugin(mmachine m, cbmachine w) 
00503 {
00504         SCOLinitplugin(w);
00505 
00506         // Get Scol window handle (for message callback)
00507         HScol = (HWND)SCgetExtra("hscol");      
00508         LoadTUsb(m);
00509         return 0;
00510 }
00511 
00512 
00516 extern "C" __declspec (dllexport) int ScolUnloadPlugin() 
00517 {
00518   // Uninitialize COM library
00519   ::CoUninitialize();
00520 
00521         return 0;
00522 }