#include <Mouse.h>
#include <hiduniversal.h>
#include <SPI.h>
#include "hidmouserptparser.h"

USB Usb;
HIDUniversal Hid(&Usb);
HIDMouseEvents MouEvents;
HIDMouseReportParser Mou(&MouEvents);

int dx;
int dy;
int dxn;
int dyn;
int index;
int num_size;
int jump = 127;


void setup() {
    Mouse.begin();
    Serial.begin(115200);
    Serial.println("Start");
    Serial.setTimeout(1);
    if (Usb.Init() == -1)
        Serial.println("OSC did not start.");
        delay(200);

    if (!Hid.SetReportParser(0, &Mou))
       ErrorMessage<uint8_t > (PSTR("SetReportParser"), 1);
}

void loop() {

    if (Serial.available())
    {

        

        String data = Serial.readString();
        if (data =="shoot"){
            Mouse.click();
        }
        else if (data.substring(0,6) == "silent")
        {
            data.remove(0,6);
            index = 0;
            num_size = data.indexOf(":", index);
            dx = data.substring(index,num_size).toInt();
            data.remove(0,num_size+1);
            dy = data.toInt();
            dxn = dx *-1;
            dyn = dy *-1;

            if (dx > 0){
                while (dx > 127)
                {
                    dx -= 127;
                    Mouse.move(127,0);
                }
                Mouse.move(dx,0);
            }
            else if (dx < 0){
                while (dx < -127)
                {
                    dx += 127;
                    Mouse.move(-127,0);
                }
                Mouse.move(dx,0);
            }
            if (dy > 0){
                while (dy > 127)
                {
                    dy -= 127;
                    Mouse.move(0,127);
                }
                Mouse.move(0,dy);
            }
            else if (dy < 0){
                while (dy < -127)
                {
                    dy += 127;
                    Mouse.move(0,-127);
                }
                Mouse.move(0,dy);
            }
            Mouse.click();
            if (dxn > 0){
                while (dxn > 127)
                {
                    dxn -= 127;
                    Mouse.move(127,0);
                }
                Mouse.move(dxn,0);
            }
            else if (dxn < 0){
                while (dxn < -127)
                {
                    dxn += 127;
                    Mouse.move(-127,0);
                }
                Mouse.move(dxn,0);
            }
            if (dyn > 0){
                while (dyn > 127)
                {
                    dyn -= 127;
                    Mouse.move(0,127);
                }
                Mouse.move(0,dyn);
            }
            else if (dyn < 0){
                while (dyn < -127)
                {
                    dyn += 127;
                    Mouse.move(0,-127);
                }
                Mouse.move(0,dyn);
            }
        }

        else{
            index = 0;
            num_size = data.indexOf(":", index);
            dx = data.substring(index,num_size).toInt();
            data.remove(0,num_size+1);
            dy = data.toInt();

            if (dx > 0){
                while (dx > jump)
                {
                    dx -= jump;
                    Mouse.move(jump,0);
                }
                Mouse.move(dx,0);
            }
            else if (dx < 0){
                while (dx < -jump)
                {
                    dx += jump;
                    Mouse.move(-jump,0);
                }
                Mouse.move(dx,0);
            }
            if (dy > 0){
                while (dy > jump)
                {
                    dy -= jump;
                    Mouse.move(0,jump);
                }
                Mouse.move(0,dy);
            }
            else if (dy < 0){
                while (dy < -jump)
                {
                    dy += jump;
                    Mouse.move(0,-jump);
                }
                Mouse.move(0,dy);
            }
        }



    }
    Usb.Task();
}
 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}