00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
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
00115
00116
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
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
00224 HWND hWndBattViewer = pUnionShrMem.p_d->hWnd;
00225
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
00246
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
00257 HWND hWndBattViewer = pUnionShrMem.p_d->hWnd;
00258
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