-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP3SpellerTask.cpp
More file actions
1513 lines (1337 loc) · 46.8 KB
/
Copy pathP3SpellerTask.cpp
File metadata and controls
1513 lines (1337 loc) · 46.8 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
// $Id: P3SpellerTask.cpp 4008 2012-05-15 12:42:40Z mellinger $
// Authors: schalk@wadsworth.org, vkamat@cambridgeconsultants.com,
// pbrunner@wadsworth.org, shzeng, juergen.mellinger@uni-tuebingen.de
// Description: The task filter for a P300 based speller providing multiple
// menus.
//
// $BEGIN_BCI2000_LICENSE$
//
// This file is part of BCI2000, a platform for real-time bio-signal research.
// [ Copyright (C) 2000-2012: BCI2000 team and many external contributors ]
//
// BCI2000 is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// BCI2000 is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY
// - without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
//
// $END_BCI2000_LICENSE$
////////////////////////////////////////////////////////////////////////////////
#include "PCHIncludes.h"
#pragma hdrstop
#include "P3SpellerTask.h"
#include "SpellerCommand.h"
#include "TextStimulus.h"
#include "ImageStimulus.h"
#include "SoundStimulus.h"
#include "SpeechStimulus.h"
#include "AudioSpellerTarget.h"
#include "lsl_cpp.h"
#include "Localization.h"
#include "FileUtils.h"
#include "OSThread.h"
#include "core_expt.h"
#include <algorithm>
#include <iomanip>
using namespace std;
using namespace GUI;
RegisterFilter( P3SpellerTask, 3 );
P3SpellerTask::P3SpellerTask()
: mNumberOfSequences( 0 ),
mInterpretMode_( InterpretModes::None ),
mDisplayResults( false ),
mTestMode( false ),
mCurMenu( 0 ),
mNumMatrixRows( 0 ),
mNumMatrixCols( 0 ),
mSequenceCount( 0 ),
mSequencePos( mSequence.begin() ),
mAvoidStimulusRepetition( false ),
mSleepMode( 0 ),
mPaused( false ),
mpStatusBar( NULL ),
mpTextWindow( NULL ),
mSummaryFile( SummaryFileExtension().c_str() ),
mRunCount( 0 ),
mNumSelections( 0 ),
mSleepDuration( 0 ),
mTargetRow( 0 ),
mTargetCol( 0 ),
mFirstSequence( true ),
mInfo("MyEventStream_bci2k","Markers",1,lsl::IRREGULAR_RATE,lsl::cf_string,"myuniquesourceid23445"),
mStreamOutlet(mInfo, 0, 360)
{
BEGIN_PARAMETER_DEFINITIONS
"Application:Sequencing int NumberOfSequences= 15 15 1 % // "
"number of sequences in a set of intensifications",
"Application:Speller%20Targets matrix TargetDefinitions= "
"36 "
"{Display Enter Display%20Size Icon%20File Sound} "
"A A 1 % % " "B B 1 % % " "C C 1 % % " "D D 1 % % " "E E 1 % % " "F F 1 % % "
"G G 1 % % " "H H 1 % % " "I I 1 % % " "J J 1 % % " "K K 1 % % " "L L 1 % % "
"M M 1 % % " "N N 1 % % " "O O 1 % % " "P P 1 % % " "Q Q 1 % % " "R R 1 % % "
"S S 1 % % " "T T 1 % % " "U U 1 % % " "V V 1 % % " "W W 1 % % " "X X 1 % % "
"Y Y 1 % % " "Z Z 1 % % " "1 1 1 % % " "2 2 1 % % " "3 3 1 % % " "4 4 1 % % "
"5 5 1 % % " "6 6 1 % % " "7 7 1 % % " "8 8 1 % % " "9 9 1 % % " "_ %20 1 % % "
"% % % // speller target properties",
"Application:Speller%20Targets intlist NumMatrixColumns= 1 "
"6 6 1 % // display matrices' column number(s)",
"Application:Speller%20Targets intlist NumMatrixRows= 1 "
"6 6 0 % // display matrices' row number(s)",
"Application:Audio%20Stimuli int AudioStimuliOn= 0 "
"0 0 1 // Audio Stimuli Mode (0=no, 1=yes) (boolean)",
"Application:Audio%20Stimuli matrix AudioStimuliRowsFiles= "
"{ 1 2 3 4 5 6 } " // row labels
"{ filename } " // filename
"./voice/1.wav "
"./voice/2.wav "
"./voice/3.wav "
"./voice/4.wav "
"./voice/5.wav "
"./voice/6.wav "
" // audio stimuli rows files ",
"Application:Audio%20Stimuli matrix AudioStimuliColsFiles= "
"{ 1 2 3 4 5 6 } " // column labels
"{ filename } " // filename
"./voice/a.wav "
"./voice/b.wav "
"./voice/c.wav "
"./voice/d.wav "
"./voice/e.wav "
"./voice/f.wav "
" // audio stimuli column files ",
"Application:Speller%20Targets floatlist TargetWidth= 1 5 0 0 100 // "
"target width in percent of screen width",
"Application:Speller%20Targets floatlist TargetHeight= 1 5 0 0 100 // "
"target height in percent of screen height",
"Application:Speller%20Targets floatlist TargetTextHeight= 1 10 0 0 100 // "
"height of target labels in percent of screen height",
"Application:Speller%20Targets stringlist BackgroundColor= 1 0x000000 "
"0x505050 % % // target background color (color)",
"Application:Speller%20Targets stringlist TextColor= 1 0x000000 "
"0x505050 % % // text color (color)",
"Application:Speller%20Targets stringlist TextColorIntensified= 1 0x0000FF "
"0x505050 % % // intensified text color (color)",
"Application:Speller%20Targets intlist IconHighlightMode= 1 1 "
"1 0 4 // icon highlight method "
"0: Show/Hide, "
"1: Intensify, "
"2: Grayscale, "
"3: Invert, "
"4: Dim "
" (enumeration)",
"Application:Speller%20Targets floatlist IconHighlightFactor= 1 0.5 "
"0.5 0 % // scale factor for highlighted icon pixel values",
"Application:Speller int FirstActiveMenu= 1 "
"1 1 % // Index of first active menu",
"Application:Speller float StatusBarSize= 10 0 0 100 // "
"size of status bar in percent of screen height",
"Application:Speller float StatusBarTextHeight= 8 0 0 100 // "
"size of status bar text in percent of screen height",
"Application:Speller string TextToSpell= % % % % // "
" character or string to spell in offline copy mode",
"Application:Speller string TextResult= % % % % // "
"user spelling result",
"Application:Speller int TestMode= 0 0 0 1 // "
"select targets by clicking on their associated stimuli (0=no, 1=yes) (boolean)",
"Application:Speller string DestinationAddress= % % % % // "
"network address for speller output in IP:port format",
"Application:Text%20Window int TextWindowEnabled= 0 "
"0 0 1 // Show Text Window (0=no, 1=yes) (boolean)",
"Application:Text%20Window int TextWindowLeft= 640 0 0 % // "
"Text Window X location",
"Application:Text%20Window int TextWindowTop= 0 0 0 % // "
"Text Window Y location",
"Application:Text%20Window int TextWindowWidth= 512 512 0 % // "
"Text Window Width",
"Application:Text%20Window int TextWindowHeight= 512 512 0 % // "
"Text Window Height",
"Application:Text%20Window string TextWindowFontName= Courier % % % // "
"Text Window Font Name",
"Application:Text%20Window int TextWindowFontSize= 10 4 1 % // "
"Text Window Font Size",
"Application:Text%20Window string TextWindowFilePath= % % % % // "
"Path for Saved Text File (directory)",
END_PARAMETER_DEFINITIONS
BEGIN_STATE_DEFINITIONS
"SelectedTarget 16 0 0 0",
"SelectedRow 8 0 0 0",
"SelectedColumn 8 0 0 0",
"SpellerMenu 8 0 0 0",
END_STATE_DEFINITIONS
LANGUAGES "German",
BEGIN_LOCALIZED_STRINGS
"TIME OUT !!!",
"Zeit abgelaufen!",
"Waiting to start ...",
"Warte ...",
"Sleeping--Select SLEEP twice to resume",
"Angehalten: Zweimal SLEEP fur Weiter",
"Select SLEEP once more to resume",
"Angehalten: Noch einmal SLEEP fur Weiter",
"Paused--Select PAUSE again to resume",
"Angehalten: Noch einmal PAUSE fur Weiter",
END_LOCALIZED_STRINGS
}
P3SpellerTask::~P3SpellerTask()
{
mStimuli.DeleteObjects();
Speller::DeleteObjects();
delete mpTextWindow;
}
void
P3SpellerTask::OnPreflight( const SignalProperties& /*Input*/ ) const
{
Parameter( "TextResult" );
Parameter( "DisplayResults" );
Parameter( "DestinationAddress" );
PreflightCondition( Parameter( "FirstActiveMenu" ) <= Parameter( "TargetDefinitions" )->NumRows() );
// Try loading all the menus to detect configuration errors.
GUI::Rect rect;
GraphDisplay preflightDisplay;
SetOfStimuli preflightStimuli;
AssociationMap preflightAssociations;
struct : public Speller
{ void OnEnter( const std::string& ) {} } preflightSpeller;
int numMenus = NumMenus(),
interpretMode = Parameter( "InterpretMode" );
for( int i = 0; i < numMenus; ++i )
{
LoadMenu(
i,
rect,
preflightDisplay,
preflightStimuli,
preflightAssociations,
preflightSpeller
);
if( interpretMode == InterpretModes::Copy && i + 1 == Parameter( "FirstActiveMenu" ) )
{
string commands;
for( SetOfSpellerTargets::const_iterator j = preflightSpeller.Targets().begin(); j != preflightSpeller.Targets().end(); ++j )
{
istringstream iss( ( *j )->EntryText() );
SpellerCommand command;
while( iss >> command )
if( !command.Code().empty() )
commands += string( " " ) + command.Code();
}
if( !commands.empty() )
bciout << "Speller commands are not supported in copy spelling "
<< "mode, and may result in inconsistent target suggestions. "
<< "Menu " << i + 1
<< " contains the following speller commands: "
<< commands
<< "."
<< endl;
if( !preflightSpeller.TrySpelling( Parameter( "TextToSpell" ) ) )
bcierr << "TextToSpell cannot be spelled using TargetDefinitions "
<< "in menu " << i + 1
<< endl;
if( !preflightSpeller.TrySpelling( Parameter( "TextResult" ) ) )
bcierr << "TextResult cannot be spelled using TargetDefinitions "
<< "in menu " << i + 1
<< endl;
}
}
preflightStimuli.DeleteObjects();
preflightSpeller.DeleteObjects();
if( interpretMode == InterpretModes::Copy )
{
if( Parameter( "TextToSpell" ) == "" )
bciout << "Empty TextToSpell parameter in copy spelling mode" << endl;
}
if( interpretMode != InterpretModes::None )
{
int numberOfSequences = Parameter( "NumberOfSequences" );
if( OptionalParameter( "EpochsToAverage", numberOfSequences ) > numberOfSequences )
bciout << "EpochsToAverage is larger than NumberOfSequences."
<< " This implies that multiple sequences enter into"
<< " a single classification."
<< endl;
}
State( "Running" );
State( "Recording" );
// Parameters of text window
Parameter( "TextWindowFontName" );
Parameter( "TextWindowFontSize" );
Parameter( "TextWindowFilePath" );
// Parameters accessed for the summary file only
OptionalParameter( "ID_Montage" );
OptionalParameter( "ID_Amp" );
OptionalParameter( "ID_System" );
OptionalParameter( "OperatorVersion" );
OptionalParameter( "EEGSourceVersion" );
OptionalParameter( "SignalProcessingVersion" );
OptionalParameter( "ApplicationVersion" );
OptionalParameter( "Classifier" );
//user parameters
Parameter( "DataDirectory" );
Parameter( "SubjectName" );
Parameter( "SubjectSession" );
Parameter( "SubjectRun" );
}
void
P3SpellerTask::OnInitialize( const SignalProperties& /*Input*/ )
{
mStimuli.DeleteObjects();
Speller::DeleteObjects();
Associations().clear();
if( mpStatusBar == NULL )
mpStatusBar = new StatusBar( Display() );
GUI::Rect statusBarRect =
{ 0, 0, 1.0, Parameter( "StatusBarSize" ) / 100 };
mpStatusBar->SetTextHeight( Parameter( "StatusBarTextHeight" ) / Parameter( "StatusBarSize" ) )
.SetColor( RGBColor::Gray )
.SetTextColor( RGBColor::Yellow )
.SetObjectRect( statusBarRect );
ClearTextHistory();
mCurMenu = static_cast<int>( Parameter( "FirstActiveMenu" ) - 1 );
while( !mMenuHistory.empty() )
mMenuHistory.pop();
mMenuHistory.push( mCurMenu );
mMatrixRect.left = 0;
mMatrixRect.top = statusBarRect.bottom;
mMatrixRect.right = 1.0;
mMatrixRect.bottom = 1.0;
LoadMenu(
mCurMenu,
mMatrixRect,
Display(),
mStimuli,
Associations(),
*this
);
mNumMatrixRows = MenuRows( mCurMenu );
mNumMatrixCols = MenuCols( mCurMenu );
InitSequence();
mNumberOfSequences = Parameter( "NumberOfSequences" );
mDisplayResults = ( Parameter( "DisplayResults" ) != 0 );
mTestMode = ( Parameter( "TestMode" ) != 0 );
mInterpretMode_ = Parameter( "InterpretMode" );
switch( mInterpretMode_ )
{
case InterpretModes::None:
case InterpretModes::Free:
mTextToSpell = "";
break;
case InterpretModes::Copy:
mTextToSpell = ( string )Parameter( "TextToSpell" );
break;
}
mGoalText = mTextToSpell;
mpStatusBar->SetGoalText( mGoalText );
delete mpTextWindow;
mpTextWindow = NULL;
if( Parameter( "TextWindowEnabled" ) == 1 )
{
mpTextWindow = new TextWindow;
mpTextWindow->SetLeft( Parameter( "TextWindowLeft" ) )
.SetTop( Parameter( "TextWindowTop" ) )
.SetWidth( Parameter( "TextWindowWidth" ) )
.SetHeight( Parameter( "TextWindowHeight" ) )
.SetFontName( Parameter( "TextWindowFontName" ) )
.SetFontSize( Parameter( "TextWindowFontSize" ) )
.Show();
}
const char *Markertypes[] = {"start", "target", "non-target"};
string mrk = Markertypes[0];
mStreamOutlet.push_sample(&mrk);
// UDP connection
mConnection.close();
mConnection.clear();
mSocket.close();
string destinationAddress = Parameter( "DestinationAddress" );
if( destinationAddress != "" )
{
mSocket.open( destinationAddress.c_str() );
mConnection.open( mSocket );
if( !mConnection.is_open() )
bciout << "Could not connect to " << destinationAddress << endl;
}
}
void
P3SpellerTask::OnStartRun()
{
// Non-summary file
Display().ClearClicks();
if( mInterpretMode_ == InterpretModes::Copy )
ClearTextHistory();
InitSequence();
DetermineAttendedTarget();
DisplayMessage( LocalizableString( "Waiting to start ..." ) );
mNumSelections = 0;
mSleepDuration = 0;
mSleepMode = dontSleep;
mPaused = false;
State( "SpellerMenu" ) = mCurMenu + 1;
// Summary file
mSummaryFile << "System ID = " << OptionalParameter( "ID_System", "N/A" ) << '\t'
<< "Amp ID = " << OptionalParameter( "ID_Amp", "N/A" ) << '\t'
<< "Montage ID = " << OptionalParameter( "ID_Montage", "N/A" ) << '\n'
<< "\nSW Versions:\n";
if( Parameters->Exists( "OperatorVersion" ) )
mSummaryFile << "Operator:\n\t"
<< Parameter( "OperatorVersion" )( "Revision" )
<< '\n';
if( Parameters->Exists( "EEGSourceVersion" ) )
mSummaryFile << "EEGSource:\n\t"
<< Parameter( "EEGSourceVersion" )( "Revision" )
<< '\n';
if( Parameters->Exists( "SignalProcessingVersion" ) )
mSummaryFile << "Signal Processing:\n\t"
<< Parameter( "SignalProcessingVersion" )( "Revision" )
<< '\n';
if( Parameters->Exists( "ApplicationVersion" ) )
mSummaryFile << "Application:\n\t"
<< Parameter( "ApplicationVersion" )( "Revision" )
<< '\n';
mSummaryFile << "\n---------------------------------------------------"
<< endl;
if( Parameters->Exists( "Classifier" ) )
{
mSummaryFile << "Classifier Matrix:\n";
ParamRef Classifier = Parameter( "Classifier" );
for( int row = 0; row < Classifier->NumRows(); ++row )
{
for( int col = 0; col < Classifier->NumColumns(); ++col )
mSummaryFile << Classifier( row, col ) << ' ';
mSummaryFile << '\n';
}
mSummaryFile << flush;
}
++mRunCount;
if( mInterpretMode_ == InterpretModes::Free )
{
AppLog << "Start of run " << mRunCount << " in online (free) mode\n";
mSummaryFile << "*** START OF RUN " << mRunCount << " IN ONLINE MODE ***\n";
}
else
{
AppLog << "Start of run " << mRunCount << " in offline (copy) mode\n";
mSummaryFile << "*** START OF RUN " << mRunCount << " IN OFFLINE MODE ***\n";
}
AppLog << flush;
mSummaryFile << "Date = " << StringDate() << "\t\t"
<< "Time = " << StringTime() << "\n"
<< "Num of Sequences = " << mNumberOfSequences
<< "\nMATRIX SIZE(s)\n";
int numMenus = NumMenus();
for( int i = 0; i < numMenus; ++i )
mSummaryFile << MenuRows( i ) << " x "
<< MenuCols( i ) << '\n';
mSelectionSummary.str() = "Selections in this run:\n";
/**/
// save a bitmap per run to use for the eye tracker background
//DataDir = "c:\\elcl\\data\\";
DataDir = Parameter("DataDirectory");
SubjName = Parameter("SubjectName");
SubjSess = Parameter("SubjectSession");
SubjRun = Parameter("SubjectRun");
DataDir = DataDir + "\\" + SubjName + SubjSess + "\\";
edf_file = SubjSess + SubjRun;
edf_dir = "c:\\elcl\\data\\";
// save screenshot to overlay eyetracker images onto
OPENFILENAME ofn;
char szFileName[512];
static HBITMAP hDesktopCompatibleBitmap=NULL;
static HDC hDesktopCompatibleDC=NULL;
static HDC hDesktopDC=NULL;
static HWND hDesktopWnd=NULL;
LPVOID pBits=NULL;
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biBitCount=32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biWidth=GetSystemMetrics(SM_CXSCREEN);
bmpInfo.bmiHeader.biHeight=GetSystemMetrics(SM_CYSCREEN);
bmpInfo.bmiHeader.biPlanes=1;
bmpInfo.bmiHeader.biSizeImage=abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount/8;
hDesktopWnd=GetDesktopWindow();
hDesktopDC=GetDC(hDesktopWnd);
hDesktopCompatibleDC=CreateCompatibleDC(hDesktopDC);
hDesktopCompatibleBitmap=CreateDIBSection(hDesktopDC,&bmpInfo,DIB_RGB_COLORS,&pBits,NULL,0);
if(hDesktopCompatibleDC==NULL || hDesktopCompatibleBitmap == NULL)
{
//ErrorMessage("Unable to Create Desktop Compatible DC/Bitmap");
}
SelectObject(hDesktopCompatibleDC,hDesktopCompatibleBitmap);
std::string temp = DataDir + edf_file + ".bmp";
strcpy(szFileName,&temp[0]);
ZeroMemory(&ofn,sizeof(ofn));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
ofn.lpstrFilter="Bitmap Files (*.bmp)\0*.bmp\0";
ofn.lpstrDefExt="bmp";
ofn.lpstrFile=szFileName;
ofn.nMaxFile=512;
ofn.hwndOwner = NULL;
//if(!GetSaveFileName(&ofn)) break;
SetCursor(LoadCursor(NULL,IDC_WAIT));
int nWidth=GetSystemMetrics(SM_CXSCREEN);
int nHeight=GetSystemMetrics(SM_CYSCREEN);
HDC hBmpFileDC=CreateCompatibleDC(hDesktopDC);
HBITMAP hBmpFileBitmap=CreateCompatibleBitmap(hDesktopDC,nWidth,nHeight);
HBITMAP hOldBitmap = (HBITMAP) SelectObject(hBmpFileDC,hBmpFileBitmap);
BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);
SelectObject(hBmpFileDC,hOldBitmap);
//SaveBitmap(ofn.lpstrFile,hBmpFileBitmap);
HDC hdc=NULL;
FILE* fp=NULL;
LPVOID pBuf=NULL;
BITMAPFILEHEADER bmpFileHeader;
do{
hdc=GetDC(NULL);
ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
GetDIBits(hdc,hBmpFileBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
if(bmpInfo.bmiHeader.biSizeImage<=0)
bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
if((pBuf=malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)
{
MessageBox(NULL,"Unable to Allocate Bitmap Memory","Error",MB_OK|MB_ICONERROR);
break;
}
bmpInfo.bmiHeader.biCompression=BI_RGB;
GetDIBits(hdc,hBmpFileBitmap,0,bmpInfo.bmiHeader.biHeight,pBuf,&bmpInfo,DIB_RGB_COLORS);
if((fp=fopen(ofn.lpstrFile,"wb"))==NULL)
{
MessageBox(NULL,"Unable to Create Bitmap File","Error",MB_OK|MB_ICONERROR);
break;
}
bmpFileHeader.bfReserved1=0;
bmpFileHeader.bfReserved2=0;
bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
bmpFileHeader.bfType='MB';
bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
}while(false);
{
if(hdc)
ReleaseDC(NULL,hdc);
if(pBuf)
free(pBuf);
if(fp)
fclose(fp);
}
DeleteDC(hBmpFileDC);
DeleteObject(hBmpFileBitmap);
SetCursor(LoadCursor(NULL,IDC_ARROW));
/**/
// open eye tracker
if(open_eyelink_connection(0) !=0) // connect to the tracker
{
printf("Failed to connect to tracker \n");
return;
}
// eyecmd_printf("link_sample_data = RIGHT,GAZE"); // tell tracker to send data over the link
// start recording send samples and events to the edf file, the last two zeros are to not send them back to pc
// open a new file for this trial, soon name to bitmap hooks...
std::string tmp = edf_dir + edf_file + ".edf";
//MessageBox(NULL, DataDir.c_str(),"",MB_OK|MB_ICONERROR);
create_path(&tmp[0],1,0);
open_data_file(&edf_file[0]);
eyemsg_printf("session start"); // start msg?
if(start_recording(1,1,0,0) != 0)
{
printf("failed to start recording \n");
//return -1;
}
eyemsg_printf("P3StartRun"); // start msg?
}
void
P3SpellerTask::OnStopRun()
{
if( mInterpretMode_ == InterpretModes::Free )
Parameter( "TextResult" ) = mTextHistory.top();
DisplayMessage( LocalizableString( "TIME OUT !!!" ) );
// App log
AppLog << "******************************" << endl;
// Summary file
mSummaryFile << "*** RUN SUMMARY ***\n"
<< "System Pause Duration (in seconds): " << mSleepDuration << '\n'
<< "Number of Selections = " << mNumSelections << '\n';
if( mInterpretMode_ == InterpretModes::Copy ) // in copy spelling
mSummaryFile << "Expected Copy Spelling Characters: "
<< mTextToSpell
<< '\n';
mSummaryFile << mSelectionSummary.str() << endl;
mSelectionSummary.clear();
mSelectionSummary.str( "" );
//close eye tracker
eyemsg_printf("P3StartRun"); // bracketing stop msg
stop_recording(); // stop recording
eyecmd_printf("close_data_file"); // close data file
std::string tmp = DataDir + edf_file + ".edf";
receive_data_file(&edf_file[0], &tmp[0], 0);
close_eyelink_system(); // disconnect from tracker
}
void
P3SpellerTask::OnPreSequence()
{
DisplayMessage( "" );
}
void
P3SpellerTask::DoPreSequence( const GenericSignal&, bool& /*doProgress*/ )
{
CheckSwitchMenu();
}
void
P3SpellerTask::OnSequenceBegin()
{
State( "SelectedRow" ) = 0;
State( "SelectedColumn" ) = 0;
State( "SelectedTarget" ) = 0;
// before each sequence, find the next letter to be spelled (mEntryText
if( mInterpretMode_ == InterpretModes::Copy ) {
SpellerTarget* pSuggestedTarget = NULL;
SequenceOfSpellerTargets currentlySpelled,
toBeSpelled;
Speller::TrySpelling( mTextHistory.top(), ¤tlySpelled );
Speller::TrySpelling( mTextToSpell, &toBeSpelled );
if( toBeSpelled.size() > currentlySpelled.size() )
pSuggestedTarget = toBeSpelled[ currentlySpelled.size() ];
mEntryText = pSuggestedTarget->EntryText();
int targetID = pSuggestedTarget ? pSuggestedTarget->Tag() : 0;
mTargetRow = targetID ? ( targetID - 1 ) / mNumMatrixCols + 1 : 0;
mTargetCol = targetID ? ( targetID - 1 ) % mNumMatrixCols + 1 : 0;
}
}
void
P3SpellerTask::OnPostRun()
{
State( "SelectedRow" ) = 0;
State( "SelectedColumn" ) = 0;
State( "SelectedTarget" ) = 0;
}
void
P3SpellerTask::OnStimulusBegin( int inStimulusCode )
{
Associations()[ inStimulusCode ].Present();
mStreamOutlet.push_sample(&mMarker);
//msg to eye tracker
eyemsg_printf(&mMarker[0]); // bracketing stop msg
}
int
P3SpellerTask::OnNextStimulusCode()
{
// Return values of this function determine sequencing in the following way:
// A zero stimulus code ends the current sequence of stimuli.
// A null sequence (no nonzero stimulus codes between two zero codes) ends
// the run.
int result = 0;
bool outputMarker = false;
outputMarker = !mFirstSequence;
if( !mSequence.empty() )
{
if( mSequencePos == mSequence.end() )
{ // During a run, we always use the same sequence object and re-shuffle it.
//
// Sequences should fulfil the constraint that no stimulus
// presented on the previous sequence's last stimulus presentation
// may be presented on the next sequence's first stimulus presentation
// (unless this is impossible by the way stimuli are grouped).
int prevStimulusCode = *mSequence.rbegin();
do
{
random_shuffle( mSequence.begin(), mSequence.end(), RandomNumberGenerator );
} while( mAvoidStimulusRepetition
&& Associations().StimuliIntersect( *mSequence.begin(), prevStimulusCode ) );
mSequencePos = mSequence.begin();
if( ++mSequenceCount == mNumberOfSequences )
{
result = 0;
mSequenceCount = 0;
mFirstSequence = true;
outputMarker = false;
}
else
{
result = *mSequencePos++;
mFirstSequence = false;
}
}
else
{
result = *mSequencePos++;
mFirstSequence = false;
}
}
// here want access to copy text to be spelled (in terms of which two sequence positions it occupies, send target/non-target on that basis
if (outputMarker) {
if( mInterpretMode_ == InterpretModes::Copy ) {
const char *Markertypes[] = {"start", "target", "non-target"};
mMarker = "xx";
if ((result==(mTargetRow)) || (result==(mTargetCol+mNumMatrixCols))) {
mMarker = Markertypes[1];
//eyetracker timestamp
eyemsg_printf("target");
}
else {
mMarker = Markertypes[2];
//eyetracker timestamp
eyemsg_printf("non-target");
}
//mStreamOutlet.push_sample(&mMarker);
AppLog << "mSequencePos: "
<< result
<< " mrk: "
<< mMarker
<< endl;
}
}
return result;
}
void
P3SpellerTask::DoPostSequence( const GenericSignal&, bool& /*doProgress*/ )
{
CheckSwitchMenu();
}
Target*
P3SpellerTask::OnClassResult( const ClassResult& inResult )
{
// We override the standard ClassResult handler
// - to additionally provide the SelectedTarget, SelectedRow, SelectedColumn
// states,
// - to handle user clicks on visual stimuli,
// - to log the classification result.
// Clear the display's queue of clicked objects, and store a pointer to the
// last clicked stimulus.
Stimulus* pClickedStimulus = NULL;
while( !Display().ObjectsClicked().empty() )
{
Stimulus* pStimulus = dynamic_cast<Stimulus*>( Display().ObjectsClicked().front() );
if( pStimulus != NULL )
pClickedStimulus = pStimulus;
Display().ObjectsClicked().pop();
}
Target* pTarget = NULL;
if( mTestMode && pClickedStimulus != NULL )
{ // Fake an ideal ERP result, i.e. a binary response to the clicked stimulus.
// This allows for testing result processing as well.
ClassResult fakeResult;
GenericSignal fakeSignal( 1, 1 );
for( AssociationMap::const_iterator i = Associations().begin();
i != Associations().end(); ++i )
{
fakeSignal( 0, 0 ) = i->second.Contains( pClickedStimulus );
fakeResult[ i->first ].push_back( fakeSignal );
}
pTarget = Associations().ClassifyTargets( fakeResult ).MostLikelyTarget();
}
else
pTarget = Associations().ClassifyTargets( inResult ).MostLikelyTarget();
// Compute the "Selected*" states from the result.
// These are for documentation purposes only, and may lose their meaning
// when targets are not grouped into rows and columns.
int targetID = pTarget ? pTarget->Tag() : 0;
State( "SelectedTarget" ) = targetID;
State( "SelectedRow" ) = targetID ? ( targetID - 1 ) / mNumMatrixCols + 1 : 0;
State( "SelectedColumn" ) = targetID ? ( targetID - 1 ) % mNumMatrixCols + 1 : 0;
// Write classification signal details into the application log.
size_t numAverages = 0;
for( ClassResult::const_iterator i = inResult.begin(); i != inResult.end(); ++i )
numAverages += i->second.size();
AppLog << "This is the end of this sequence: "
<< numAverages * mNumberOfSequences << " total intensifications"
<< endl;
// Report mean responses to log file but not to screen log.
AppLog.File << "Mean responses for each stimulus:\n";
for( ClassResult::const_iterator i = inResult.begin(); i != inResult.end(); ++i )
{
float mean = 0.0;
for( size_t j = 0; j < i->second.size(); ++j )
mean += i->second[ j ]( 0, 0 );
mean /= i->second.size();
AppLog.File << "Response for Stimulus Code " << i->first << ": "
<< setprecision( 2 ) << fixed << mean
<< "\n";
}
return pTarget;
}
// Speller events.
void
P3SpellerTask::OnEnter( const std::string& inText )
{
AppLog << "Selected command: " << inText << endl;
if( mConnection.is_open() )
mConnection << "P3Speller_Output " << inText << endl;
istringstream iss( inText );
SpellerCommand command;
while( iss >> command )
{ // Interpret input as a sequence of commands interspersed with plain text.
if( mDisplayResults )
{
if( command.Code() == "SLEEP" )
{
if( !mPaused )
OnSleep();
}
else if( command.Code() == "PAUSE" )
{
if( mSleepMode == dontSleep )
OnPause();
}
else if( !mPaused && mSleepMode == dontSleep )
{
if( command.Code() == "" )
OnText( command.Value() );
else if( command.Code() == "BS" )
OnBackspace();
else if( command.Code() == "DW" )
OnDeleteWord();
else if( command.Code() == "UNDO" )
OnUndo();
else if( command.Code() == "END" )
OnEnd();
else if( command.Code() == "GTO" || command.Code() == "GOTO" )
OnGoto( ::atoi( command.Value().c_str() ) - 1 );
else if( command.Code() == "BK" || command.Code() == "BACK" )
OnBack();
else if( command.Code() == "SAVE" )
OnSave();
else if( command.Code() == "RETR" )
OnRetrieve();
else
bcierr << "Unknown command: " << command << endl;
}
if( mSleepMode == sleep2 && command.Code() != "SLEEP" )
{ // Reset to sleeping.
mSleepMode = dontSleep;
OnSleep();
}
}
else if( command.Code() == "" )
{
OnText( command.Value() );
}
}
if( mDisplayResults )
{
mpStatusBar->SetResultText( mTextHistory.top() );
if( mpTextWindow != NULL )
mpTextWindow->SetText( mTextHistory.top() );
}
DetermineAttendedTarget();
mSelectionSummary << inText << ' ';
if( ++mNumSelections % 10 == 0 )
mSelectionSummary << '\n';
}
// Event handlers associated with individual speller commands.
void
P3SpellerTask::OnText( const std::string& inText )
{
// add plain string input to the spelled text
mTextHistory.push( mTextHistory.top() + inText );
}
void
P3SpellerTask::OnBackspace()
{
if( !mTextHistory.top().empty() )
mTextHistory.push( mTextHistory.top().substr( 0, mTextHistory.top().length() - 1 ) );
}
void
P3SpellerTask::OnDeleteWord()
{
// delete last word and space characters following it
if( !mTextHistory.top().empty() )
{
string curText = mTextHistory.top();
size_t spacePos = curText.length();
while( spacePos != 0 && ::isspace( curText[ --spacePos ] ) )
;
while( spacePos != 0 && !::isspace( curText[ --spacePos ] ) )
;
mTextHistory.push( curText.substr( 0, spacePos ) );
}
}
void
P3SpellerTask::OnUndo()
{
// undo the last change
if( mTextHistory.size() > 1 )
mTextHistory.pop();
}