%SETUP the Serial Port for Communication
%based off code found at Bruce's Webpage
% %=====clean up any leftover serial connections==============
clear all
clc
try
    fclose(instrfind) %close any bogus serial connections
end
% 
% %Set COMs and baudrate
COM = 'COM4'; %set when you check
baudrate = 9600; 
databits = 8;

s = serial(COM, 'DataBits', databits, 'BaudRate', baudrate, 'Parity', 'None');
set(s,'Timeout',10);
s.FlowControl = 'none';
fopen(s);

fprintf(s, 's');
packet = fscanf(s,'%d %d %d %d');
mode = packet(1);
x = packet(2);
y = packet(3);
action = packet(4);

import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;

robot = Robot;
screenSize = get(0, 'screensize');
system('mspaint &');
robot.mouseMove(x,y);
robot.mousePress(MouseEvent.BUTTON1_MASK);
for i = 1: 200

      robot.mouseMove(x+i, y+i);

      pause(0.00001);
end
fprintf(s, 's');
packet = fscanf(s,'%d %d %d');
mode = packet(1);
x = packet(2);
y = packet(3);

for i = 1: 200

      robot.mouseMove(x+i, y-i);

      pause(0.00001);
end
robot.mouseRelease(MouseEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_CONTROL);
pause(.5);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
pause(1);
robot.keyPress(KeyEvent.VK_S);

robot.keyRelease(KeyEvent.VK_S);
pause(2);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_ALT);
pause(.5);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_ALT);


 
for i = 1:10
robot.mouseWheel(1);
pause(0.5);
end

fclose(s);







