22SWSensor::SWSensor(SSensorType type) :
SSensor(type)
29 SAFE_RELEASE(mISensor);
35 memset(&sensorData, 0,
sizeof(sensorData));
37 if (mEnabled && mISensor)
39 SENSOR_TYPE_ID iSensorType;
40 mISensor->GetType(&iSensorType);
41 if (IsEqualGUID(iSensorType, SENSOR_TYPE_ACCELEROMETER_3D))
44 ISensorDataReport* sensorReport(NULL);
46 hr = mISensor->GetData(&sensorReport);
51 PROPVARIANT x, y, z, tm;
52 hr = sensorReport->GetSensorValue(SENSOR_DATA_TYPE_TIMESTAMP, &tm);
53 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, &x);
54 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Y_G, &y);
55 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Z_G, &z);
61 sensorData.delta = GetElapsedTime(std::chrono::duration<int64_t, std::nano>(std::chrono::system_clock::now().time_since_epoch()).count());
62 sensorData.vec3 =
Vector3d(x.dblVal, y.dblVal, z.dblVal);
63 CallSensorCb(sensorData);
65 else if (IsEqualGUID(iSensorType, SENSOR_TYPE_COMPASS_3D))
68 ISensorDataReport* sensorReport(NULL);
70 hr = mISensor->GetData(&sensorReport);
75 PROPVARIANT x, y, z, tm;
76 hr = sensorReport->GetSensorValue(SENSOR_DATA_TYPE_TIMESTAMP, &tm);
77 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_MAGNETIC_HEADING_X_DEGREES, &x);
78 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_MAGNETIC_HEADING_Y_DEGREES, &y);
79 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_MAGNETIC_HEADING_Z_DEGREES, &z);
85 sensorData.delta = GetElapsedTime(std::chrono::duration<int64_t, std::nano>(std::chrono::system_clock::now().time_since_epoch()).count());
86 sensorData.vec3 =
Vector3d(x.dblVal, y.dblVal, z.dblVal);
87 CallSensorCb(sensorData);
89 else if (IsEqualGUID(iSensorType, SENSOR_TYPE_GYROMETER_3D))
92 ISensorDataReport* sensorReport(NULL);
94 hr = mISensor->GetData(&sensorReport);
99 PROPVARIANT x, y, z, tm;
100 hr = sensorReport->GetSensorValue(SENSOR_DATA_TYPE_TIMESTAMP, &tm);
101 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_X_DEGREES_PER_SECOND_SQUARED, &x);
102 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_Y_DEGREES_PER_SECOND_SQUARED, &y);
103 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_Z_DEGREES_PER_SECOND_SQUARED, &z);
109 sensorData.delta = GetElapsedTime(std::chrono::duration<int64_t, std::nano>(std::chrono::system_clock::now().time_since_epoch()).count());
110 sensorData.vec3 =
Vector3d(x.dblVal, y.dblVal, z.dblVal);
111 CallSensorCb(sensorData);
113 else if (IsEqualGUID(iSensorType, SENSOR_TYPE_AMBIENT_LIGHT))
116 ISensorDataReport* sensorReport(NULL);
118 hr = mISensor->GetData(&sensorReport);
124 hr = sensorReport->GetSensorValue(SENSOR_DATA_TYPE_TIMESTAMP, &tm);
125 hr = hr && sensorReport->GetSensorValue(SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX, &l);
131 sensorData.delta = GetElapsedTime(std::chrono::duration<int64_t, std::nano>(std::chrono::system_clock::now().time_since_epoch()).count());
132 sensorData.fval = (float) l.dblVal;
133 CallSensorCb(sensorData);
147void SWSensor::SetISensor(ISensor* isensor)
152ISensor* SWSensor::GetISensor()
164 HRESULT hr = CoCreateInstance(CLSID_SensorManager,
165 NULL, CLSCTX_INPROC_SERVER,
166 IID_ISensorManager, (
void**)&mISensorManager);
170 ISensorCollection * sensorList;
171 hr = mISensorManager->GetSensorsByCategory(SENSOR_CATEGORY_ALL, &sensorList);
174 ULONG sensorCount(0);
175 sensorList->GetCount(&sensorCount);
176 MMechostr(MSKDEBUG,
"SWSensorManager() : %d sensors has been found", sensorCount);
179 for (
unsigned int i = 0; i < sensorCount; i++)
182 hr = sensorList->GetAt(i, &wsensor);
185 SENSOR_TYPE_ID iSensorType;
186 wsensor->GetType(&iSensorType);
188 if (IsEqualGUID(iSensorType, SENSOR_TYPE_ACCELEROMETER_3D))
190 MMechostr(MSKDEBUG,
"SWSensorManager() : an accelerometer has been found (%d)", i);
191 SWSensor* sensor =
new SWSensor(SSENSOR_TYPE_ACCELEROMETER);
192 sensor->SetISensor(wsensor);
193 mSensorList.insert(sensor);
194 SetSensorDelay(sensor, 10);
196 else if (IsEqualGUID(iSensorType, SENSOR_TYPE_COMPASS_3D))
198 MMechostr(MSKDEBUG,
"SWSensorManager() : an magnetometer has been found (%d)", i);
199 SWSensor* sensor =
new SWSensor(SSENSOR_TYPE_MAGNETIC_FIELD);
200 sensor->SetISensor(wsensor);
201 mSensorList.insert(sensor);
202 SetSensorDelay(sensor, 10);
204 else if (IsEqualGUID(iSensorType, SENSOR_TYPE_GYROMETER_3D))
206 MMechostr(MSKDEBUG,
"SWSensorManager() : an gyrometer has been found (%d)", i);
207 SWSensor* sensor =
new SWSensor(SSENSOR_TYPE_GYROSCOPE);
208 sensor->SetISensor(wsensor);
209 mSensorList.insert(sensor);
210 SetSensorDelay(sensor, 10);
212 else if (IsEqualGUID(iSensorType, SENSOR_TYPE_AMBIENT_LIGHT))
214 MMechostr(MSKDEBUG,
"SWSensorManager() : a light sensor has been found (%d)", i);
215 SWSensor* sensor =
new SWSensor(SSENSOR_TYPE_LIGHT);
216 sensor->SetISensor(wsensor);
217 mSensorList.insert(sensor);
218 SetSensorDelay(sensor, 10);
226 MMechostr(MSKDEBUG,
"SSensorManager() : an unknown sensor has been found (%d)", i);
234 SAFE_RELEASE(sensorList);
247SWSensorManager::~SWSensorManager()
251 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
253 SWSensor* sensor = (SWSensor*) (*it);
257 mSingletonInstance = NULL;
260int SWSensorManager::SetSensorDelay(
SSensor* sensor,
unsigned long millisecondsDelay)
264 ISensor* wsensor = ((SWSensor*)sensor)->GetISensor();
266 IPortableDeviceValues* propsToSet = NULL;
267 IPortableDeviceValues* propsReturn = NULL;
269 HRESULT hr = CoCreateInstance(__uuidof(PortableDeviceValues), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&propsToSet));
273 hr = propsToSet->SetUnsignedIntegerValue(SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, millisecondsDelay);
277 hr = wsensor->SetProperties(propsToSet, &propsReturn);
280 HRESULT hrError = S_OK;
281 hr = propsReturn->GetErrorValue(SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, &hrError);
284 wprintf_s(L
"\nSetting current report interval failed with error 0x%X\n", hrError);
288 else if (hr == E_ACCESSDENIED)
295 SAFE_RELEASE(propsToSet);
296 SAFE_RELEASE(propsReturn);
307int SWSensorManager::SetSensorEnable(SSensorType sensorType,
bool state)
309 if (IsAvailable(sensorType))
316void SWSensorManager::Vibrate(
long millis)
321void SWSensorManager::StartVibration()
326void SWSensorManager::StopVibration()
331void SWSensorManager::VibratePattern(std::vector<int> pattern,
bool loop)
336void SWSensorManager::StartLocationService()
341void SWSensorManager::StopLocationService()
346bool SWSensorManager::GetLocation(
float &longitude,
float &latitude,
float &altitude)