% Data acquisition and labeler
clear all;
close all;


%% 1 - Acquisition of data
[FILENAME, PATHNAME, FILTERINDEX] = uigetfile;
pickMovie = strcat(PATHNAME,FILENAME); % Select a movie file
load(pickMovie);

prompt = 'Press INTRO when ready';
dummy = input(prompt,'s');
countMyos = 1;      % Number of Myo armbands used

%% Instantiate MyoMex
% After constructing a new |MyoMex| instance, we'll inspect its properties.

mm = MyoMex(countMyos)

m1 = mm.myoData(1);
if countMyos == 2, m2 = mm.myoData(2); end

pause(0.1); % wait briefly for the first data frame to come in
     
for mainO=1:3
    if (mainO==1)
         m1.isStreaming
    end
    
  for i=1:length(movie)

      imshow(movie{i});
            

      if(i>=length(movie)/2)
           sound(1)
      end
  end
  
  sound(1)
  for i=1:length(movie)
      axis equal
      imshow(movie{length(movie)+1-i});
      if(i>=length(movie)/2)
           sound(1)
      end
  end
end
    


pause(1);
m1.stopStreaming();
fprintf('Number of samples:               \t%d\n',length(m1.timeIMU_log));
m1.quat_log

%% 2 - Data storage and manual labeling
f={};
emg=m1.emg_log;
f.emgData=emg;


%% ---------------- Plottings
figure
plot(emg);   title('emg');
% figure
% subplot(8,1,1)
% plot(emg(:,1));   title('POD 1');
% subplot(8,1,2)
% plot(emg(:,2));   title('POD 2');
% subplot(8,1,3)
% plot(emg(:,3));  title('POD 3');
% subplot(8,1,4)
% plot(emg(:,4));  title('POD 4');
% subplot(8,1,5)
% plot(emg(:,5));   title('POD 5');
% subplot(8,1,6)
% plot(emg(:,6));   title('POD 6');
% subplot(8,1,7)
% plot(emg(:,7));  title('POD 7');
% subplot(8,1,8)
% plot(emg(:,8));  title('POD 8');

%% 3 - Manual labeling based on the plot
% Feature |   Pose   |  Color  |
%---------*----------*---------|
%    1    |   Rest   |  Cyan   |
%    2    |   Fist   |  Blue   |
%    3    |   WavL   | Magenta |
%    4    |   WavR   |   Red   |
%    5    |  Spread  |  Green  |
%    6    |  Pinch   |  Black  |

[clickLabel,y] = ginput;

labelChanges=length(clickLabel);
clickLabel=floor(clickLabel);

label=zeros(1,length(emg));
for lab = 1:labelChanges+1;
    if(lab==1)
        str2='Label the fragment of data [1-';
        showme = strcat(str2,num2str(clickLabel(1)),'] |1=Rest|2=Fist|3=WaveLeft|4=WaveRight|5=SpreadF|6=Pinch|7=RotateLeft|8=RotateRight|9=Thumb');
        disp(showme)
        prompt = 'Type the current label = ';
        manualLabel = input(prompt);
        label(1:clickLabel(1))=manualLabel;
    elseif(lab==labelChanges+1) 
        str2='Label the fragment of data [';
        showme = strcat(str2,num2str(clickLabel(lab-1)+1),'-',num2str(length(emg)),']');
        disp(showme)
        prompt = '|1=Rest, 2=Fist, 3=WaveLeft,4=WaveRight,5=SpreadF| = ';
        manualLabel = input(prompt);
        label(clickLabel(lab-1)+1:length(label))=manualLabel;
    else
        str2='Label the fragment of data [';
        showme = strcat(str2,num2str(clickLabel(lab-1)+1),'-',num2str(clickLabel(lab)),']');
        disp(showme)
        prompt = '|1=Rest, 2=Fist, 3=WaveLeft,4=WaveRight,5=SpreadF| = ';
        manualLabel = input(prompt);
        label(clickLabel(lab-1)+1:clickLabel(lab))=manualLabel;
    end
    
end

label=label';
f.label=label;

%% Storage in file

prompt = 'Write the file name: ';
str = input(prompt,'s');
s = strcat('data/train/',str,'.mat');         %Save in Train Folder
% s = strcat('data/test/',str,'.mat');            %Save in Test Folder
save(s,'f')



%% Finally, when you're done with |MyoMex|, don't forget to clean up!

mm.delete;
clear mm

