% http://mlconnect.chschmid.com/index.php/MLC_Matlab_API

%% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% initialize the MLC environment
mlcInitMLC;

disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

% start the default server
dev_android = mlcStartDefaultServer;

disp('A kapcsolat létrejött!');








%{
%% Full control view %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

% start full control view
mlcLaunchFullControlView(dev_android);

% send a command
mlcFCVCommand(dev_android,'a=1');

% send a result
mlcFCVResult(dev_android,'a = 1');

% send an error message
mlcFCVError(dev_android,'Error in blabla...');

% wait for a user input
userInput = mlcFCVWaitForInput(dev_android,1);

% unlock the user input field
mlcFCVUnlockUserInput(dev_android);
%}




%% Full control Command Window %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

% start full control view
mlcLaunchFullControlView(dev_android);

mlcFCVResult(dev_android, 'Kilepes majd az exit paranccsal');

% wait for a user input
userInput = mlcFCVWaitForInput(dev_android,1);

% unlock the user input field
mlcFCVUnlockUserInput(dev_android);

% process the user input
%while ~strcmpi(userInput,'exit_mlc')
while ~strcmpi(userInput,'exit')
    try
        % evaluate the user string as a MATLAB command
        eredmeny = evalc(userInput);
        % send back the result to the device
        %mlcFCVResult(dev_android,userInput);
        mlcFCVResult(dev_android, eredmeny);
    catch e
        % if the command has not valid MATLAB syntax, send the error message back to the device
        mlcFCVError(dev_android,e.message);
    end
    % unlock the input field again
    mlcFCVUnlockUserInput(dev_android);
    % wait for an other input
    userInput = mlcFCVWaitForInput(dev_android,1);
end

%% Image view %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

disp('Abra atkuldes');

% start image view
mlcLaunchImageView(dev_android);

% send a matlab figure to the device
% define a figure number
figNum=1;
% create the figure
figure(figNum);
% plot some nice stuff
surfc(peaks(40));
% send it to the device
mlcIVWriteImage(dev_android,figNum);

pause(10);





%% Sensor view %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

disp('Szenzor-nezet');

% start the sensor view
mlcLaunchSensorView(dev_android);

% store the total list of sensors
sensors={'accelerometer','orientation','gyroscope','proximity', ...
         'light','magneticfield','pressure','temperature'};
     
sensorType=cell(1);
validInd=1;     

% read out all sensor information
for si=1:length(sensors)
    % read the actual sensor 
    %sensorInfo = mlcGetSensorInfo(dev_android,sensors{si});
    sensorInfo = mlcSensorGetInfo(dev_android,sensors{si});
    % if it exists
    if ~strcmpi(sensorInfo,'No sensor')
        sensorType{validInd}=[sensors{si} ' - ' sensorInfo];
        validInd=validInd+1;
    end
end

%% Sensor values %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

% read out the acceleration sensor data
%accSensorData=mlcReadSensor(dev_android,'accelerometer');
accSensorData=mlcSensorRead(dev_android,'accelerometer');

% read out the orientation sensor data
%oriSensorData = mlcReadSensor(dev_android,'orientation');
oriSensorData = mlcSensorRead(dev_android,'orientation');

% read out the proximity sensor data
%proSensorData = mlcReadSensor(dev_android,'proximity');
proSensorData = mlcSensorRead(dev_android,'proximity');

% read out the magnetic sensor data
%magSensorData = mlcReadSensor(dev_android,'magneticfield');
magSensorData = mlcSensorRead(dev_android,'magneticfield');



%{
%% Screen settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

% set the orientation to portrait mode
mlcSetOrientation(dev_android,'portrait');

disp('''Landscape'' módhoz nyomj meg egy billentyűt...');
pause

% set the orientation to landscape mode
mlcSetOrientation(dev_android,'landscape');

disp('''Auto'' módhoz nyomj meg egy billentyűt...');

pause

% set the orientation to auto mode
mlcSetOrientation(dev_android,'auto');

disp('Üzenet küldéséhez nyomj meg egy billentyűt...');
pause

% send a message to the screen
mlcLaunchToast(dev_android,'Üzenet.',2000);

%}



%% Acceleration sensor - data direction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

disp('Gyorsulasmero adatfolyama 3D vektorkent');

% start the sensor view
mlcLaunchSensorView(dev_android);

% indicator of max number of trial commands
wasError=0;

% plot the direction vector
figure;

accDir=line([0 0],[0 0],[0 0],'Color','k','LineWidth',2);

xlim([-15 15]);
ylim([-15 15]);
zlim([-15 15]);

xlabel('X');
ylabel('Y');
zlabel('Z');

view(70,10);

grid on;

%while ~wasError
for index=1:100
    try
        % read out the accelerometer sensor data
        %devACC = mlcReadSensor(dev_android,'accelerometer');
        devACC = mlcSensorRead(dev_android,'accelerometer');
        % set the new direction to the vector
        set(accDir,'XData',[0 devACC(1)],'YData',[0 devACC(2)],'ZData',[0 devACC(3)]);
    catch e
        wasError=1;
        disp('Max number of commands reached.');
    end
    pause(0.1); % 0.01 volt
end


%% Position from acceleration %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

disp('Gyorsulasmero adatfolyamabol segesseg es megtett ut szamolasa integralassal');

% start the sensor view
mlcLaunchSensorView(dev_android);

% allocate the position variable
devACC=[0 0 0];
acci=1;

% error indicator
wasError=0;

figure;
hold on;

%while ~wasError
for index=1:100
    try
        % read out the accelerometer sensor data
        %devACC(acci,:) = mlcReadSensor(dev_android,'accelerometer');
        devACC(acci,:) = mlcSensorRead(dev_android,'accelerometer');
        % calculate the velocity by integrating the acceleration
        devVEL = cumtrapz(devACC).*0.1;
        % calculate the position by integrating the velocity
        devPOS = cumtrapz(devVEL).*0.1;
        
        % plot the values as a running line display
        subplot(311);
        plot(devACC,'LineWidth',2);
        title('Acceleration');
        subplot(312);
        plot(devVEL,'LineWidth',2);
        title('Velocity');
        subplot(313);
        plot(devPOS,'LineWidth',2);
        title('Position');
        
        % increment the index variable
        acci=acci+1;
    catch e
        wasError=1;
        disp('Max number of commands reached.');
    end
    pause(0.1); % 0.01 volt
end






%% Figure rotation using acceleration sensor data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

disp('Abraforgatas a gyorsulasmero adatfolyama alapjan');

% start the sensor view
mlcLaunchSensorView(dev_android);

accGain=10;
accShift=-70;

figure;

surfc(peaks(30));
view([0 70]);


% start image view
mlcLaunchImageView(dev_android);

wasError=0;

%while ~wasError
for index=1:100
%     try
        %accSensorData = mlcReadSensor(dev_android,'accelerometer');
        accSensorData = mlcSensorRead(dev_android,'accelerometer');
        set(gca,'View',[accGain*accSensorData(1) accGain*accSensorData(2)+accShift]);
        % send the current figure to the device
        mlcIVWriteImage(dev_android,gcf);
%     catch e
%         wasError=1;
%         disp('Max number of commands reached.');
%     end
    pause(0.1); % 0.01 volt
end




%% Control of the mouse pointer location %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================


disp('Egermutato mozgatasa a gyorsulasmero adatfolyama alapjan');

% start the senso view
mlcLaunchSensorView(dev_android);

% read out the accelerometer sensor data
%accSensorData = mlcReadSensor(dev_android,'accelerometer');
accSensorData = mlcSensorRead(dev_android,'accelerometer');

accGain=100;
accShift=500;


ScreenSize=get(0,'ScreenSize');

wasError=0;

%while ~wasError
for index=1:100
    try
        %accSensorData = mlcReadSensor(dev_android,'accelerometer');
        accSensorData = mlcSensorRead(dev_android,'accelerometer');
        % set the new pointer location
        set(0,'PointerLocation',[-accGain*accSensorData(1)+accShift accGain*accSensorData(2)+accShift]);

    catch e
        wasError=1;
        disp('Max number of commands reached.');
    end

    pause(0.1); % 0.01
end

%% Magnetic sensor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%===================================================================
if ~mlcIsConnected(dev_android)
    % initialize the MLC environment
    mlcInitMLC;

    disp('Nyomd meg a ''Connect to server'' gombot a telefonon...');

    % start the default server
    dev_android = mlcStartDefaultServer;

    disp('A kapcsolat létrejött!');
end
%===================================================================

disp('A magnetometer adatfolyama');

% start the sensor view
mlcLaunchSensorView(dev_android);

% indicator of max number of trial commands
wasError=0;

% plot the direction vector
figure;

% space allocation for the variable
devMAG=[0 0 0];
magind=1;

%while ~wasError
for index=1:100
    try
        % read out the accelerometer sensor data
        %devMAG(magind,:) = mlcReadSensor(dev_android,'magneticfield');
        devMAG(magind,:) = mlcSensorRead(dev_android,'magneticfield');
        magind=magind+1;
        plot(devMAG,'LineWidth',2);
    catch e
        wasError=1;
        disp('Max number of commands reached.');
    end
    pause(0.1); % 0.01
end

disp('Viszlat');


