27 extern int MYO_CONNECTED_CB;
28 extern int MYO_DISCONNECTED_CB;
29 extern int MYO_ORIENTATION_CB;
30 extern int MYO_ACCEL_CB;
31 extern int MYO_POSE_CB;
32 extern int MYO_ARMRECOGNIZED_CB;
33 extern int MYO_ARMLOST_CB;
45 mDirection = xDirectionUnknown;
47 mPrevQuat = myo::Quaternion<float>(0.0f, 0.0f, 0.0f, 1.0f);
56 myo::Myo* SMyo::GetMyoInstance()
61 void SMyo::SetMyoInstance(myo::Myo* myoInstance)
63 mMyoInstance = myoInstance;
66 void SMyo::SetDirection(XDirection direction)
68 mDirection = direction;
71 XDirection SMyo::GetDirection()
79 OBJpostEvent(MYO_CONNECTED_CB, SCOL_PTR
this, 0);
82 void SMyo::Disconnect()
85 OBJpostEvent(MYO_DISCONNECTED_CB, SCOL_PTR
this, 0);
88 void SMyo::SendPose(myo::Pose pose)
92 OBJpostEvent(MYO_POSE_CB, SCOL_PTR
this, (
int)data);
95 void SMyo::SendOrientation(myo::Quaternion<float> quat)
98 quat = quat.normalized();
99 myo::Quaternion<float> dir = Quaternion<float>(0.0f, 1.0f, 0.0f, 0.0f);
100 quat = (mDirection != xDirectionTowardWrist) ? Quaternion<float>(quat.y(), quat.z(), quat.x(), quat.w()) : Quaternion<float>(quat.y(), quat.z(), quat.x(), quat.w());
101 data->quat = quat * dir;
103 OBJpostEvent(MYO_ORIENTATION_CB, SCOL_PTR
this, (
int)data);
106 void SMyo::SendAccel(myo::Vector3<float> vec)
111 OBJpostEvent(MYO_ACCEL_CB, SCOL_PTR
this, (
int)data);
114 void SMyo::Vibrate(myo::Myo::VibrationType type)
117 mMyoInstance->vibrate(type);
124 SMyoManager::SMyoManager()
130 SMyoManager::~SMyoManager()
135 MyoList::iterator ismyo;
136 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
138 SMyo* myoInstance = (*ismyo);
139 SAFE_DELETE(myoInstance);
140 ismyo = mMyoList.erase(ismyo);
143 SAFE_DELETE(mMyoHub);
156 void SMyoManager::Kill()
165 SMyo* SMyoManager::AddMyoInstance()
167 boost::upgrade_lock< boost::shared_mutex > lock(mMutex);
172 mMyoHub =
new myo::Hub(
"com.scol.myo");
175 mMyoHub->setLockingPolicy(Hub::lockingPolicyNone);
176 mMyoHub->addListener(
this);
179 mThread = boost::thread(boost::bind(&SMyoManager::UpdateThread,
this));
182 catch(std::exception &e)
185 MMechostr(MSKRUNTIME,
"Myo error : %s", e.what());
189 SMyo* myoInstance = 0;
194 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
196 myoInstance =
new SMyo();
197 mMyoList.insert(myoInstance);
203 void SMyoManager::RemoveMyoInstance(
SMyo* myoInstance)
206 boost::upgrade_lock< boost::shared_mutex > lock(mMutex);
208 if (myoInstance->GetMyoInstance())
211 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
212 mMyoAvailable.insert(myoInstance->GetMyoInstance());
215 MyoList::iterator ismyo = mMyoList.find(myoInstance);
216 if (ismyo != mMyoList.end())
219 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
221 mMyoList.erase(ismyo);
222 SAFE_DELETE(myoInstance);
226 if (mMyoList.empty() && mMyoHub != 0)
232 mMyoHub->removeListener(
this);
234 SAFE_DELETE(mMyoHub);
235 mMyoAvailable.clear();
239 void SMyoManager::UpdateThread()
244 boost::shared_lock< boost::shared_mutex > lock(mMutex);
248 MyoList::iterator ismyo;
250 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
252 SMyo* myoInstance = (*ismyo);
253 if (myoInstance->GetMyoInstance() == 0)
255 if (mMyoAvailable.size() > 0)
257 myo::Myo*
myo = (*mMyoAvailable.begin());
258 myoInstance->SetMyoInstance(myo);
259 myoInstance->Connect();
260 mMyoAvailable.erase(mMyoAvailable.begin());
271 if (nbPaired < mMyoList.size())
276 myo = mMyoHub->waitForMyo(1);
278 catch (std::exception &e)
280 std::string mess = e.what();
281 MMechostr(MSKDEBUG,
"Myo Exception : %s", e.what());
290 void SMyoManager::onPair(Myo *
myo, uint64_t timestamp, FirmwareVersion firmwareVersion)
296 void SMyoManager::onConnect(Myo *
myo, uint64_t timestamp, FirmwareVersion firmwareVersion)
300 mMyoAvailable.insert(
myo);
302 MyoList::iterator ismyo;
303 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
305 SMyo* myoInstance = (*ismyo);
306 if (myoInstance->GetMyoInstance() == 0)
308 myoInstance->SetMyoInstance(
myo);
309 myoInstance->Connect();
311 MyoAvailables::iterator availableIt = mMyoAvailable.find(
myo);
312 if (availableIt != mMyoAvailable.end())
313 mMyoAvailable.erase(availableIt);
319 void SMyoManager::onDisconnect(Myo *
myo, uint64_t timestamp)
323 MyoAvailables::iterator availableIt = mMyoAvailable.find(
myo);
324 if (availableIt != mMyoAvailable.end())
325 mMyoAvailable.erase(availableIt);
327 MyoList::iterator ismyo;
328 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
330 SMyo* myoInstance = (*ismyo);
331 if (myoInstance->GetMyoInstance() ==
myo)
333 myoInstance->SetMyoInstance(0);
334 myoInstance->Disconnect();
340 void SMyoManager::onPose(Myo *
myo, uint64_t timestamp, Pose pose)
345 MyoList::iterator ismyo;
346 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
348 SMyo* myoInstance = (*ismyo);
349 if (myoInstance->GetMyoInstance() ==
myo)
351 myoInstance->SendPose(pose);
357 void SMyoManager::onOrientationData(Myo *
myo, uint64_t timestamp,
const Quaternion< float > &rotation)
362 MyoList::iterator ismyo;
363 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
365 SMyo* myoInstance = (*ismyo);
366 if (myoInstance->GetMyoInstance() ==
myo)
368 myoInstance->SendOrientation(rotation);
374 void SMyoManager::onAccelerometerData(Myo *
myo, uint64_t timestamp,
const Vector3< float > &accel)
379 MyoList::iterator ismyo;
380 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
382 SMyo* myoInstance = (*ismyo);
383 if (myoInstance->GetMyoInstance() ==
myo)
385 myoInstance->SendAccel(accel);
391 void SMyoManager::onGyroscopeData(Myo *
myo, uint64_t timestamp,
const Vector3< float > &gyro)
397 void SMyoManager::onRssi(Myo *
myo, uint64_t timestamp, int8_t rssi)
403 void SMyoManager::onArmSync(Myo*
myo, uint64_t timestamp, Arm arm, XDirection xDirection)
408 MyoList::iterator ismyo;
409 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
411 SMyo* myoInstance = (*ismyo);
412 myoInstance->SetDirection(xDirection);
413 if (myoInstance->GetMyoInstance() ==
myo)
415 OBJpostEvent(MYO_ARMRECOGNIZED_CB, (
int)myoInstance, (
int)arm);
421 void SMyoManager::onArmUnsync(Myo*
myo, uint64_t timestamp)
426 MyoList::iterator ismyo;
427 for (ismyo = mMyoList.begin(); ismyo != mMyoList.end(); ismyo++)
429 SMyo* myoInstance = (*ismyo);
430 if (myoInstance->GetMyoInstance() ==
myo)
432 OBJpostEvent(MYO_ARMLOST_CB, (
int)myoInstance, 0);
438 void SMyoManager::onUnlock(Myo*
myo, uint64_t timestamp)
442 void SMyoManager::onLock(Myo*
myo, uint64_t timestamp)