/
sf1player.cpp
366 lines (347 loc) · 15.3 KB
/
sf1player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (C) 2013-2014 Thalmic Labs Inc.
// Distributed under the Myo SDK license agreement. See LICENSE.txt for details.
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <string>
#include <sstream>
#include <vector>
#include <ApplicationServices/ApplicationServices.h>
#include <unistd.h>
// The only file that needs to be included to use the Myo C++ SDK is myo.hpp.
#include <myo/myo.hpp>
using std::cout;
using std::endl;
const int runFreq =5;
// Classes that inherit from myo::DeviceListener can be used to receive events from Myo devices. DeviceListener
// provides several virtual functions for handling different kinds of events. If you do not override an event, the
// default behavior is to do nothing.
class DataCollector : public myo::DeviceListener {
public:
CGEventSourceRef source;
std::string acceldata;
const double straightPunchStart = -2.5;
const double straightPunchEnd = -0.5;
const int hadoukenStart = 4; // forward = 8 (+/-5; - is RIGHT)
const int hadoukenEnd = 6;
bool hadouken = false;
bool punching = false;
bool kicking = false;
bool lastdirleft = false; //(P1 default faces right first)
bool movleft, movright, movdown, movup = false;
std::vector<myo::Myo*> knownMyos;
DataCollector()
: onArm(false), roll_w(0), pitch_w(0), yaw_w(0), currentPose()
{
acceldata="";
}
//std::string acceldata = "";
void onPair(myo::Myo* myo, uint64_t timestamp, myo::FirmwareVersion firmwareVersion)
{
knownMyos.push_back(myo);
std::cout << "Paired with " << identifyMyo(myo) << "." << 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;
}
// onUnpair() is called whenever the Myo is disconnected from Myo Connect by the user.
void onUnpair(myo::Myo* myo, uint64_t timestamp)
{
// We've lost a Myo.
// Let's clean up some leftover state.
roll_w = 0;
pitch_w = 0;
yaw_w = 0;
onArm = false;
}
void onAccelerometerData(myo::Myo* myo, uint64_t timestamp, const myo::Vector3<float>& accel)
{
/*if(yaw_w>=hadoukenEnd&&hadouken&&identifyMyo(myo)==0){
hadouken = false;
}
if(currentPose.toString() == "waveOut"&&yaw_w<hadoukenStart&&!hadouken&&identifyMyo(myo)==0){
hadouken = true;
}*/
if(accel.x()>=straightPunchEnd&&punching&&identifyMyo(myo)==0) {
punching = false;
}
if(accel.x()<straightPunchStart&&!punching&&identifyMyo(myo)==0) {
punching = true;
std::cout<<"PAWWNNNNCHHHH PLAYER ONE"<<std::endl;
myo->vibrate(myo::Myo::vibrationShort);
}
if((accel.x()>=-1.0/*||pitch_w>=14*/)&&kicking&&identifyMyo(myo)==1) {
kicking = false;
}
if((accel.x()<straightPunchStart/*||pitch_w<=13*/)&&!kicking&&identifyMyo(myo)==1) {
kicking = true;
std::cout<<"KIIIIIICK PLAYER ONE"<<std::endl;
}
if(roll_w >= 6 && roll_w <= 10&&identifyMyo(myo)==0) { //[4,6], [7,9] [10,12]
movleft = false;
movright = false;
}
if(roll_w <= 6&&identifyMyo(myo)==0) {
movleft = true;
movright = false;
lastdirleft = true;
}
if(roll_w >= 10&&identifyMyo(myo)==0) {
movright = true;
movleft = false;
lastdirleft = false;
}
if(pitch_w >= 8 && pitch_w <= 12&&identifyMyo(myo)==0) { //[~7], [8~11] [12~]
movdown = false;
movup = false;
}
if(pitch_w <= 7&&identifyMyo(myo)==0) {
movup = true;
movdown = false;
}
if(pitch_w >= 13&&identifyMyo(myo)==0) {
movdown = true;
movup = false;
}
std::ostringstream strs;
strs << accel.x();
std::string str = strs.str();
acceldata = str;
acceldata += ", ";
strs << accel.y();
str = strs.str();
acceldata += str;
acceldata += ", ";
strs << accel.z();
str = strs.str();
acceldata += str;
//if(identifyMyo(myo)==0) std::cout<<"0: " << acceldata<<std::endl;
//if(identifyMyo(myo)==1) std::cout<<"1: " << acceldata<<std::endl;
/*if(hadouken) {
CGEventRef LRArrowPress;
CGEventRef LRArrowRelease;
CGEventRef LRArrowPress2;
CGEventRef LRArrowRelease2;
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef downArrowPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)125, true);
CGEventPost(kCGAnnotatedSessionEventTap, downArrowPress);
CGEventRef downArrowRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)125, false);
CGEventPost(kCGAnnotatedSessionEventTap, downArrowRelease);
if(lastdirleft){
LRArrowPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)123, true);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowPress);
LRArrowRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)123, false);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowRelease);
LRArrowPress2 = CGEventCreateKeyboardEvent(source, (CGKeyCode)123, true);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowPress2);
LRArrowRelease2 = CGEventCreateKeyboardEvent(source, (CGKeyCode)123, false);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowRelease2);
}else{
LRArrowPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)124, true);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowPress);
LRArrowRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)124, false);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowRelease);
LRArrowPress2 = CGEventCreateKeyboardEvent(source, (CGKeyCode)124, true);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowPress2);
LRArrowRelease2 = CGEventCreateKeyboardEvent(source, (CGKeyCode)124, false);
CGEventPost(kCGAnnotatedSessionEventTap, LRArrowRelease2);
}
CGEventRef aPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)13, true);
CGEventPost(kCGAnnotatedSessionEventTap, aPress);
CGEventRef aRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)13, false);
CGEventPost(kCGAnnotatedSessionEventTap, aRelease);
CFRelease(downArrowPress);
CFRelease(LRArrowPress);
CFRelease(downArrowRelease);
CFRelease(LRArrowRelease);
CFRelease(LRArrowPress2);
CFRelease(LRArrowRelease2);
CFRelease(aPress);
CFRelease(aRelease);
cout << "Hadoukened" << endl;
hadouken = false;
}
if(!hadouken){
//zzzzzz neeeds moar hadouken
}*/
if(punching){
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)13, true);
CGEventPost(kCGAnnotatedSessionEventTap, punchPress);
CFRelease(punchPress);
CFRelease(source);
}
if(!punching){
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)13, false);
CGEventPost(kCGAnnotatedSessionEventTap, punchRelease);
CFRelease(punchRelease);
CFRelease(source);
}
if(kicking){
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef kickPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)2, true);
CGEventPost(kCGAnnotatedSessionEventTap, kickPress);
CFRelease(kickPress);
CFRelease(source);
}
if(!kicking){
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef kickRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)2, false);
CGEventPost(kCGAnnotatedSessionEventTap, kickRelease);
CFRelease(kickRelease);
CFRelease(source);
}
if(!punching && !kicking && !hadouken){
if(movleft) {
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)123, true);
CGEventPost(kCGAnnotatedSessionEventTap, punchPress);
CFRelease(punchPress);
CFRelease(source);
}
if(movright) {
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)124, true);
CGEventPost(kCGAnnotatedSessionEventTap, punchPress);
CFRelease(punchPress);
CFRelease(source);
}
if(movdown) {
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)125, true);
CGEventPost(kCGAnnotatedSessionEventTap, punchPress);
CFRelease(punchPress);
CFRelease(source);
}
if(movup) {
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)126, true);
CGEventRef punchRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)126, false);
CGEventPost(kCGAnnotatedSessionEventTap, punchPress);
CGEventPost(kCGAnnotatedSessionEventTap, punchRelease);
CFRelease(punchRelease);
CFRelease(punchPress);
CFRelease(source);
}
if(!movright){
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)124, false);
CGEventPost(kCGAnnotatedSessionEventTap, punchRelease);
CFRelease(punchRelease);
CFRelease(source);
}
if(!movleft){
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)123, false);
CGEventPost(kCGAnnotatedSessionEventTap, punchRelease);
CFRelease(punchRelease);
CFRelease(source);
}
if(!movdown){
source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef punchRelease = CGEventCreateKeyboardEvent(source, (CGKeyCode)125, false);
CGEventPost(kCGAnnotatedSessionEventTap, punchRelease);
CFRelease(punchRelease);
CFRelease(source);
}
}
}
// onOrientationData() is called whenever the Myo device provides its current orientation, which is represented as a unit quaternion.
void onOrientationData(myo::Myo* myo, uint64_t timestamp, const myo::Quaternion<float>& quat)
{
using std::atan2;
using std::asin;
using std::sqrt;
// Calculate Euler angles (roll, pitch, and yaw) from the unit quaternion.
float roll = atan2(2.0f * (quat.w() * quat.x() + quat.y() * quat.z()),
1.0f - 2.0f * (quat.x() * quat.x() + quat.y() * quat.y()));
float pitch = asin(2.0f * (quat.w() * quat.y() - quat.z() * quat.x()));
float yaw = atan2(2.0f * (quat.w() * quat.z() + quat.x() * quat.y()),
1.0f - 2.0f * (quat.y() * quat.y() + quat.z() * quat.z()));
// Convert the floating point angles in radians to a scale from 0 to 20.
roll_w = static_cast<int>((roll + (float)M_PI)/(M_PI * 2.0f) * 18);
pitch_w = static_cast<int>((pitch + (float)M_PI/2.0f)/M_PI * 18);
yaw_w = static_cast<int>((yaw + (float)M_PI)/(M_PI * 2.0f) * 18);
}
void onPose(myo::Myo* myo, uint64_t timestamp, myo::Pose pose)
{
currentPose = pose;
//std::cout << "Myo " << identifyMyo(myo) << " switched to pose " << pose.toString() << "." << std::endl;
}
size_t identifyMyo(myo::Myo* myo) {
for (size_t i = 0; i < knownMyos.size(); ++i) {
if (knownMyos[i] == myo) {
return i + 1;
}
}
return 0;
}
void onArmRecognized(myo::Myo* myo, uint64_t timestamp, myo::Arm arm, myo::XDirection xDirection)
{
onArm = true;
whichArm = arm;
}
void onArmLost(myo::Myo* myo, uint64_t timestamp)
{
onArm = false;
}
void print()
{
// Clear the current line
//std::cout << '\r';
if (onArm) {
std::string poseString = currentPose.toString();
//std:: cout << "Roll: " << roll_w << " Pitch: " << pitch_w << " Yaw: " << yaw_w <<std::endl;
}
if(punching || kicking) {
//std::cout << acceldata << std::endl;
}
std::cout << std::flush;
}
// These values are set by onArmRecognized() and onArmLost() above.
bool onArm;
myo::Arm whichArm;
// These values are set bys sonOrientationData() ansdss onPose() above.
int roll_w, pitch_w, yaw_w;
myo::Pose currentPose;
};
int main(int argc, char** argv)
{
try {
// First, we create a Hub with our application identifier. Be sure not to use the com.example namespace when
// publishing your application. The Hub provides access to one or more Myos.
myo::Hub hub("com.example.hello-myo");
std::cout << "Attempting to find a Myo..." << std::endl;
// Next, we attempt to find a Myo to use. If a Myo is already paired in Myo Connect, this will return that Myo
// immediately.
myo::Myo* myo = hub.waitForMyo(10000);
if (!myo) {
throw std::runtime_error("Unable to find a Myo!");
}
std::cout << "Connected to a Myo armband!" << std::endl << std::endl;
DataCollector collector;
// Hub::addListener() takes the address of any object whose class inherits from DeviceListener, and will cause
// Hub::run() to send events to all registered device listeners.
hub.addListener(&collector);
while (1) { // main loop
// In each iteration of our main loop, we run the Myo event loop for a set number of milliseconds.
// Display update frequency
hub.run(runFreq);
// After processing events, call print() to print.
collector.print();
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Press enter to continue.";
std::cin.ignore();
return 1;
}
}