// This sample illustrates how to interface with multiple Myo armbands and distinguish between them.
#include <iostream>
#include <stdexcept>
#include <vector>
#include <myo/myo.hpp>
class PrintMyoEvents : public myo::DeviceListener {
public:
// Every time Myo Connect successfully pairs with a Myo armband, this function will be called.
//
// You can rely on the following rules:
// - onPair() will only be called once for each Myo device
// - no other events will occur involving a given Myo device before onPair() is called with it
//
// If you need to do some kind of per-Myo preparation before handling events, you can safely do it in onPair().
void onPair(myo::Myo* myo, uint64_t timestamp, myo::FirmwareVersion firmwareVersion)
{
// Print out the MAC address of the armband we paired with.
// The pointer address we get for a Myo is unique - in other words, it's safe to compare two Myo pointers to
// see if they're referring to the same Myo.
// Add the Myo pointer to our list of known Myo devices. This list is used to implement identifyMyo() below so
// that we can give each Myo a nice short identifier.
knownMyos.push_back(myo);
// Now that we've added it to our list, get our short ID for it and print it out.
std::cout << "Paired with " << identifyMyo(myo) << "." << std::endl;
}
void onPose(myo::Myo* myo, uint64_t timestamp, myo::Pose pose)
{
std::cout << "Myo " << identifyMyo(myo) << " switched to pose " << pose.toString() << "." << std::endl;
}
void onConnect(myo::Myo* myo, uint64_t timestamp, myo::FirmwareVersion firmwareVersion)
{
std::cout << "Myo " << identifyMyo(myo) << " has connected." << std::endl;
}
void onDisconnect(myo::Myo* myo, uint64_t timestamp)
{
std::cout << "Myo " << identifyMyo(myo) << " has disconnected." << std::endl;
}
// This is a utility function implemented for this sample that maps a myo::Myo* to a unique ID starting at 1.
// It does so by looking for the Myo pointer in knownMyos, which onPair() adds each Myo into as it is paired.
size_t identifyMyo(myo::Myo* myo) {
// Walk through the list of Myo devices that we've seen pairing events for.
for (size_t i = 0; i < knownMyos.size(); ++i) {
// If two Myo pointers compare equal, they refer to the same Myo device.
if (knownMyos[i] == myo) {
return i + 1;
}
}
return 0;
}
// We store each Myo pointer that we pair with in this list, so that we can keep track of the order we've seen
// each Myo and give it a unique short identifier (see onPair() and identifyMyo() above).
std::vector<myo::Myo*> knownMyos;
};
int main(int argc, char** argv)
{
try {
myo::Hub hub("com.example.multiple-myos");
// Instantiate the PrintMyoEvents class we defined above, and attach it as a listener to our Hub.
PrintMyoEvents printer;
hub.addListener(&printer);
while (1) {
// Process events for 10 milliseconds at a time.
hub.run(10);
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Press enter to continue.";
std::cin.ignore();
return 1;
}
}