%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 = 'COM6'; %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);

%offsetx= 7;
%offsety= 145;

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

robot = Robot;
screenSize = get(0, 'screensize');
screenx = screenSize(3)/896;
screeny = screenSize(4)/672;

imagecount = -1;	%names for paint from 00-99
paintopen = 0; % makes sure we don't have more than 1 paint open at a time
firstopen = 0;
mousehold = 0;

while (1)
    
    fprintf(s, 's');
    packet = fscanf(s,'%d %d %d %d');
    mode = packet(1);
    x = packet(2);
    y = packet(3);
    action = packet(4);
    
    if((x>63)&&(y>47)&&(x<959)&&(y<719))
        robot.mouseMove((x-63)*screenx,(y-47)*screeny);
        pause(0.000025);
    end
    
    if(0== mode)	%whiteboard mode
        %functions can be scroll, move mouse, click
        if (0==action)
            if(y>511)
                robot.mouseWheel(6);
            elseif(y<255)
                robot.mouseWheel(-6);
            end
            pause(.250);
            
        elseif (1==action)
            robot.mousePress(MouseEvent.BUTTON1_MASK);
            pause(.005);
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
            pause(1);
        elseif (2==action)
            robot.mousePress(MouseEvent.BUTTON3_MASK);
            pause(.005);
            robot.mouseRelease(MouseEvent.BUTTON3_MASK);
            pause(1);
        end
        
        
    elseif (1== mode)	%blackboard mode
        
        %functions can be open paint, write, save & close
        %if((1~=action)&&(1==mousehold))
        %    mousehold=0;
        %end
        
        if ((0==action) && (0==paintopen))
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
            system('mspaint &');
            pause(0.3);
            if (0==firstopen)
                robot.keyPress(KeyEvent.VK_ALT);
                pause(.1);
                robot.keyPress(KeyEvent.VK_SPACE);
                robot.keyRelease(KeyEvent.VK_SPACE);
                robot.keyRelease(KeyEvent.VK_ALT);
                
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
                pause(.05);
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
                pause(.05);
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
                pause(.05);
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
                pause(.5);
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);
                pause(.1);
                firstopen = 1;
            end
            paintopen = 1;
            
        elseif (1==action)
            %if (0==mousehold)
            robot.mousePress(MouseEvent.BUTTON1_MASK);
            %    mousehold =1;
            %end
            
        elseif ((2==action) && (1==paintopen))
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
            paintopen = 0;
            imagecount = imagecount + 1;
            robot.keyPress(KeyEvent.VK_F12);
            pause(.5);
            robot.keyPress(KeyEvent.VK_F12);
            pause(1);
            
            switch floor(imagecount/10);
                case 0
                    robot.keyPress(KeyEvent.VK_0);
                    robot.keyRelease(KeyEvent.VK_0);
                case 1
                    robot.keyPress(KeyEvent.VK_1);
                    robot.keyRelease(KeyEvent.VK_1);
                case 2
                    robot.keyPress(KeyEvent.VK_2);
                    robot.keyRelease(KeyEvent.VK_2);
                case 3
                    robot.keyPress(KeyEvent.VK_3);
                    robot.keyRelease(KeyEvent.VK_3);
                case 4
                    robot.keyPress(KeyEvent.VK_4);
                    robot.keyRelease(KeyEvent.VK_4);
                case 5
                    robot.keyPress(KeyEvent.VK_5);
                    robot.keyRelease(KeyEvent.VK_5);
                case 6
                    robot.keyPress(KeyEvent.VK_6);
                    robot.keyRelease(KeyEvent.VK_6);
                case 7
                    robot.keyPress(KeyEvent.VK_7);
                    robot.keyRelease(KeyEvent.VK_7);
                case 8
                    robot.keyPress(KeyEvent.VK_8);
                    robot.keyRelease(KeyEvent.VK_8);
                otherwise
                    robot.keyPress(KeyEvent.VK_9);
                    robot.keyRelease(KeyEvent.VK_9);
            end
            pause(.5);
            switch mod(imagecount,10);
                case 0
                    robot.keyPress(KeyEvent.VK_0);
                    robot.keyRelease(KeyEvent.VK_0);
                case 1
                    robot.keyPress(KeyEvent.VK_1);
                    robot.keyRelease(KeyEvent.VK_1);
                case 2
                    robot.keyPress(KeyEvent.VK_2);
                    robot.keyRelease(KeyEvent.VK_2);
                case 3
                    robot.keyPress(KeyEvent.VK_3);
                    robot.keyRelease(KeyEvent.VK_3);
                case 4
                    robot.keyPress(KeyEvent.VK_4);
                    robot.keyRelease(KeyEvent.VK_4);
                case 5
                    robot.keyPress(KeyEvent.VK_5);
                    robot.keyRelease(KeyEvent.VK_5);
                case 6
                    robot.keyPress(KeyEvent.VK_6);
                    robot.keyRelease(KeyEvent.VK_6);
                case 7
                    robot.keyPress(KeyEvent.VK_7);
                    robot.keyRelease(KeyEvent.VK_7);
                case 8
                    robot.keyPress(KeyEvent.VK_8);
                    robot.keyRelease(KeyEvent.VK_8);
                otherwise
                    robot.keyPress(KeyEvent.VK_9);
                    robot.keyRelease(KeyEvent.VK_9);
            end
            
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            
            robot.keyPress(KeyEvent.VK_ALT);
            pause(.5);
            robot.keyPress(KeyEvent.VK_F4);
            robot.keyRelease(KeyEvent.VK_F4);
            robot.keyRelease(KeyEvent.VK_ALT);
        else
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
        end
        
    end
    
end
fclose(s);

