BattChecker.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2004 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 //
00012 //
00013 // [+]How to build / Usage
00014 // 
00015 // There are three ways you can use this sample program:  with AIBO only, 
00016 // as a simple Win32 program, or as a Win32 program with shared memory 
00017 // and MFC.
00018 // 
00019 // 1 : Not defining OPENR_WIN32API and OPENR_SHAREDMEM_FOR_MFCAPP
00020 //     This sample is for AIBO only (no PC required).
00021 //     The remaining battery charge level is printed to the Wireless LAN
00022 //     console.
00023 // 
00024 //     HOW TO USE:
00025 //       $ cd sample/BattCheker
00026 //       $ make install
00027 //       $ cd MS
00028 //       $ cp -rf OPEN-R <Memory Stick Drive letter>:
00029 // 
00030 // 2 : Define OPENR_WIN32API
00031 //     This sample uses Remote Processing OPEN-R for Windows only (without
00032 //     shared memory constructs or MFC).
00033 //     This is executed on the host PC.
00034 //     The remaining battery charge level is printed in a pop-up message
00035 //     window.
00036 // 
00037 //     HOW TO USE:
00038 //       $ cd sample/BattCheker/RP/robot
00039 //       $ make install
00040 //       $ cd MS
00041 //       $ cp -rf OPEN-R <Memory Stick Drive letter>:
00042 //       $ cd ../../host/win32
00043 //       $ make install
00044 //       $ /usr/local/OPEN_R_SDK/RP_OPENR_R/bin/start-rp-openr
00045 //      (Read the Installation guide to setup Remote Processing OPEN-R)
00046 //      (ipc-daemon must be executed before using start-rp-openr)
00047 // 
00048 // 3 : Define OPENR_WIN32API and OPENR_SHAREDMEM_FOR_MFCAPP
00049 //     This sample is for Remote Processing OPEN-R for Windows only.
00050 //     This is executed on the host PC, and uses shared memory to send data to
00051 //     BattViewer.
00052 //     BattViewer is an MFC application that is compiled by Visual C++.
00053 //     It retrieves the data from BattChecker by using inter-process
00054 //     communication.
00055 //     BattViewer shows you the remaining battery charge level using a progress
00056 //     bar.
00057 // 
00058 //     HOW TO USE:
00059 //       Build BattViewer.exe by using Visual C++ (It is made by VC++6.0)
00060 // 
00061 //       $ cd sample/BattCheker/RP/robot
00062 //       $ make install
00063 //       $ cd MS
00064 //       $ cp -rf OPEN-R <Memory Stick Drive letter>:
00065 //       (AIBO's program is same as the one above)
00066 //       $ cd ../../host/MFC
00067 //       $ make install
00068 //       $ /usr/local/OPEN_R_SDK/RP_OPENR_R/bin/start-rp-openr
00069 //      (Read Installation guide to setup Remote Processing OPEN-R)
00070 //      (ipc-daemon must be executed before using start-rp-openr)
00071 // 
00072 //       Execute BattViewer.exe by using VC++ or 
00073 //       by the normal procedure of starting a program (double click, etc).
00074 
00075 #include <math.h>
00076 #ifdef OPENR_WIN32API
00077 #include <windows.h>
00078 #include <stdio.h>
00079 #include <signal.h>
00080 #endif
00081 #include <OPENR/OPENRAPI.h>
00082 #include <OPENR/OUnits.h>
00083 #include <OPENR/OSyslog.h>
00084 #include <OPENR/core_macro.h>
00085 #include "BattChecker.h"
00086 
00087 #ifdef OPENR_WIN32API
00088 #ifdef OPENR_SHAREDMEM_FOR_MFCAPP
00089 static const char MapName[]         = "MemMap1";
00090 static const char MutexName[]       = "Mutex1";
00091 static HANDLE MapFileHandle = NULL;
00092 static HANDLE hMem1         = NULL;
00093 static HANDLE hMutex        = NULL;
00094 static LPVOID pShrMem1      = NULL;
00095 
00096 struct BCHK_DATA {
00097     int cap;
00098     HWND hWnd;
00099 };
00100 
00101 union SHAREDMEM {
00102         void *p_v;
00103         BCHK_DATA *p_d;
00104 };
00105 
00106 static SHAREDMEM pUnionShrMem;
00107 
00108 void sigint_handler(int);
00109 
00110 #endif
00111 #endif
00112 
00113 //
00114 // The initialization code to get sensor information is contained in this sample program
00115 // The sensor information which could get it with NotifySensor() isn't being used.
00116 // The sensor information can be gotten by easy remodeling.
00117 //
00118 
00119 BattChecker::BattChecker()
00120 {
00121     is_init = false;
00122 #ifdef OPENR_WIN32API
00123 #ifdef OPENR_SHAREDMEM_FOR_MFCAPP
00124     signal(SIGINT, &sigint_handler );
00125 #endif
00126 #endif
00127 }
00128 
00129 OStatus
00130 BattChecker::DoInit(const OSystemEvent& event)
00131 {
00132     OSYSDEBUG(("BattChecker::DoInit()\n"));
00133 
00134     NEW_ALL_SUBJECT_AND_OBSERVER;
00135     REGISTER_ALL_ENTRY;
00136     SET_ALL_READY_AND_NOTIFY_ENTRY;
00137 
00138     OPENR::SetMotorPower(opowerON);
00139     
00140     return oSUCCESS;
00141 }
00142 
00143 OStatus
00144 BattChecker::DoStart(const OSystemEvent& event)
00145 {
00146     OSYSDEBUG(("BattChecker::DoStart()\n"));
00147 
00148     ENABLE_ALL_SUBJECT;
00149     ASSERT_READY_TO_ALL_OBSERVER;
00150 
00151     return oSUCCESS;
00152 }
00153 
00154 OStatus
00155 BattChecker::DoStop(const OSystemEvent& event)
00156 {
00157     OSYSDEBUG(("BattChecker::DoStop()\n"));
00158 
00159     DISABLE_ALL_SUBJECT;
00160     DEASSERT_READY_TO_ALL_OBSERVER;
00161 
00162     return oSUCCESS;
00163 }
00164 
00165 OStatus
00166 BattChecker::DoDestroy(const OSystemEvent& event)
00167 {
00168     DELETE_ALL_SUBJECT_AND_OBSERVER;
00169 
00170 #ifdef OPENR_WIN32API
00171 #ifdef OPENR_SHAREDMEM_FOR_MFCAPP
00172     UnmapViewOfFile(pShrMem1);
00173     pUnionShrMem.p_v = NULL;
00174     CloseHandle(hMem1);
00175     CloseHandle(hMutex);
00176 #endif
00177 #endif
00178     return oSUCCESS;
00179 }
00180 
00181 
00182 void
00183 BattChecker::NotifySensor(const ONotifyEvent& event)
00184 {
00185     char buff[64];
00186     
00187     OSensorFrameVectorData* sensorVec = (OSensorFrameVectorData*)event.Data(0);
00188 
00189     if (is_init == false) {
00190         is_init = true;
00191 #ifdef OPENR_WIN32API
00192 #ifdef OPENR_SHAREDMEM_FOR_MFCAPP
00193         MapFileHandle = (HANDLE) 0xFFFFFFFF;
00194         hMutex = CreateMutex(NULL, FALSE, MutexName);
00195         if (hMutex == NULL)
00196             OSYSLOG1((osyslogERROR, "CreateMutex()\n"));
00197         WaitForSingleObject(hMutex, INFINITE);
00198         hMem1 = CreateFileMapping(MapFileHandle, NULL, PAGE_READWRITE, 0, 4096, MapName);
00199         DWORD dwExist = GetLastError();
00200         pUnionShrMem.p_v = pShrMem1 = MapViewOfFile(hMem1, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 4096);
00201         if (pShrMem1 && (dwExist != ERROR_ALREADY_EXISTS)) {
00202             // Initialize shared memory
00203             pUnionShrMem.p_d->hWnd = 0;
00204             pUnionShrMem.p_d->cap = 0;
00205         }
00206         ReleaseMutex(hMutex);
00207         if (hMem1 == NULL)
00208             OSYSLOG1((osyslogERROR, "CreateFileMapping()\n"));
00209         if (pShrMem1 == NULL)
00210             OSYSLOG1((osyslogERROR, "MapViewOfFile()\n"));
00211 #endif
00212 #endif
00213     }
00214     OPowerStatus result;
00215     OStatus err = OPENR::GetPowerStatus(&result);
00216     if (err == oSUCCESS){
00217         int capacity = (int)result.remainingCapacity;
00218         OSYSPRINT(("Batt[%d]\n",capacity));
00219 #ifdef OPENR_WIN32API
00220 #ifdef OPENR_SHAREDMEM_FOR_MFCAPP
00221         if (pShrMem1) {
00222             WaitForSingleObject(hMutex, INFINITE);
00223             // Get Window Handler of BattViewer
00224             HWND hWndBattViewer = pUnionShrMem.p_d->hWnd;
00225             // Set Battery capacity to the shared memory
00226             pUnionShrMem.p_d->cap = capacity;
00227             ReleaseMutex(hMutex);
00228             if (hWndBattViewer)
00229                 PostMessage(hWndBattViewer, WM_USER+1, 0, 0);
00230         }
00231 #else
00232         sprintf(buff, "Batt[%d]", capacity);
00233         MessageBox (NULL, buff, "Battery Remain", MB_OK);
00234 #endif // OPENR_SHAREDMEM_FOR_MFCAPP
00235 #endif // OPENR_WIN32API
00236     } else{
00237         OSYSLOG1((osyslogERROR, "Failed to GetPowerStatus()\n"));
00238     }
00239     observer[event.ObsIndex()]->AssertReady();
00240 }
00241 
00242 void
00243 BattChecker::ReadyEffector(const OReadyEvent& event)
00244 {
00245 // Dummy function to avoid a Shared Memory Error when the RP OPEN-R object is
00246 // terminated by pressing Ctrl-C
00247     return;
00248 }
00249 
00250 #ifdef OPENR_WIN32API
00251 #ifdef OPENR_SHAREDMEM_FOR_MFCAPP
00252 void sigint_handler(int a)
00253 {
00254     if (pShrMem1) {
00255         WaitForSingleObject(hMutex, INFINITE);
00256         // Get Window Handler of BattViewer
00257         HWND hWndBattViewer = pUnionShrMem.p_d->hWnd;
00258         // Set Battery capacity to the shared memory
00259         pUnionShrMem.p_d->cap = 0;
00260         ReleaseMutex(hMutex);
00261         if (hWndBattViewer)
00262             PostMessage(hWndBattViewer, WM_USER+1, 0, 0);
00263     }
00264     exit(1);
00265 }
00266 #endif
00267 #endif
00268 

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1