Go Down

Topic: Help with mouse input and output? (Read 7614 times) previous topic - next topic

weird_dave

A hub isn't going to help, but a host shield might. I took a quick look at one of the host shields and it appears it would need modifying to work, it uses 5v I/O, it's actually level shifted from 3v3, so it's just a case of removing it.
Another option would be a foot operated switch connected to the Due (possibly more convenient than trying to fit one somewhere easy to reach by hand). This has the advantage that you don't need some other button to disable autofire.
Note that with the Due-inline method, any special features of your mice will likely be unavailable. There may also be side-effects such as finding a bug in the code in the middle of a shootout :D


David1937

I am not an expert in any means like this like you are but I did some research and I am wondering if this solution would work.

https://www.amazon.com/IOGEAR-Cable-Switch-Audio-GCS72U/dp/B002K0TU2C

My thought process with this is I found a slue of these things that have the ability to use one mouse/keyboard or even monitor for two PCs. I was wondering if I could use this instead for the inline method because as I understood what you said is the reason the inline method would be hard to do is because you would have to transfer the movements, mouse clicks etc through the board and you would have to do it like code (kinda like coding like a all new mouse or making a air mouse out of these things). So I was wondering if the mouse was split where one could go into my computer like normal and other other could go into the arduino board reading "getBUttonStatus" and stuff would that work?
Or is there another reason why this won't work?

David1937

A hub isn't going to help, but a host shield might. I took a quick look at one of the host shields and it appears it would need modifying to work, it uses 5v I/O, it's actually level shifted from 3v3, so it's just a case of removing it.
Another option would be a foot operated switch connected to the Due (possibly more convenient than trying to fit one somewhere easy to reach by hand). This has the advantage that you don't need some other button to disable autofire.
Note that with the Due-inline method, any special features of your mice will likely be unavailable. There may also be side-effects such as finding a bug in the code in the middle of a shootout :D


Sorry, meant to quote that ^^^.

weird_dave

KVMs switch control between the 2 computers so the mouse control only appears on one of the USB cables at a time. There isn't really an off the shelf solution for that as nobody wants to supply exactly the same mouse controls to 2 pcs (and if you did, you'd use a software solution to do it via the network so that you can account for different screen resolutions and mouse acceleration settings in the OS).
You'd also have the same USB conundrum to solve, you'd need to be a host to the mouse and a client (device) to the PC.
I don't think forwarding the mouse controls using the inline method will be too hard, there's just the issue of what method you use to achieve it. For simplicity with no extra hardware, I'd go for serial mouse emulation via the programming port and attach the real mouse to the native port.

David1937

KVMs switch control between the 2 computers so the mouse control only appears on one of the USB cables at a time. There isn't really an off the shelf solution for that as nobody wants to supply exactly the same mouse controls to 2 pcs (and if you did, you'd use a software solution to do it via the network so that you can account for different screen resolutions and mouse acceleration settings in the OS).
You'd also have the same USB conundrum to solve, you'd need to be a host to the mouse and a client (device) to the PC.
I don't think forwarding the mouse controls using the inline method will be too hard, there's just the issue of what method you use to achieve it. For simplicity with no extra hardware, I'd go for serial mouse emulation via the programming port and attach the real mouse to the native port.
Okay even though I probably wouldn't do the inline method how do I do that?

weird_dave

Okay even though I probably wouldn't do the inline method how do I do that?
1) Connect the USB mouse to the native USB port on the Due using an on-the-go adaptor (I admit, I've never used one or done this so I am making the assumption that this is the correct method)
2) Write Arduino code to read the mouse buttons and movement
3) connect the programming port to the PC and install serial port mouse drivers
4) Write Arduino code to send serial port mouse buttons and movement obtained from the code in (2)
5) write yet more code which sits between (2) and (4) which alters the state of the mouse button as appropriate
You may have to google around for code you like the look of for the serial mouse emulation.
I would do (1) and (2) then output the mouse controls to the serial monitor in the Arduino IDE, then continue with (3) and (4) when I was happy that the mouse was being read correctly. I would then only do (5) when the  Arduino was successfully passing all the mouse control to the PC.


David1937

#21
Jan 11, 2017, 10:21 pm Last Edit: Jan 11, 2017, 10:27 pm by David1937
1) Connect the USB mouse to the native USB port on the Due using an on-the-go adaptor (I admit, I've never used one or done this so I am making the assumption that this is the correct method)
2) Write Arduino code to read the mouse buttons and movement
3) connect the programming port to the PC and install serial port mouse drivers
4) Write Arduino code to send serial port mouse buttons and movement obtained from the code in (2)
5) write yet more code which sits between (2) and (4) which alters the state of the mouse button as appropriate
You may have to google around for code you like the look of for the serial mouse emulation.
I would do (1) and (2) then output the mouse controls to the serial monitor in the Arduino IDE, then continue with (3) and (4) when I was happy that the mouse was being read correctly. I would then only do (5) when the  Arduino was successfully passing all the mouse control to the PC.


I think this method would be good to but I really don't understand how I would code it. I am in college right now and I don't have the time to make something like this so the only way I could use this solution is to pay someone to do that. I understand the serial communication method on the side of the Arduino (as in reading serial from the computer) but I really don't understand how to write it with something like AutoHotkey. Like I said my best option would be to pay for someone to write this code for me because I don't have much time available to research let alone code because of my exams that are coming up and they are more of a priority.

You are way smarter than me on this so when I go to research I usually don't end up with many search results because I don't think I am looking up the right thing.

For the KVM switch thing I found that solution out of luck and didn't really understand why it would work or why it wouldn't work. I thought basically what it does it is it switches back and forth the connection to two PC's which allows two mice and two keyboards to be connected simultaneously. Which is why I thought that it could be a possible solution to my problem because with my theory if I did that then I wouldn't have to code any crazy mice movement translations. 

Also what is westftw's ideas? I don't understand them completely but they sound good and I ignored them just because of the fact I don't understand what he is saying.

weird_dave

There is no autoit/autohotkey code to write on the PC for that solution, it uses serial mouse drivers. For debugging you just use the Arduino serial monitor to see what's going on so again, no PC code to write there either.

The other reason a KVM might not work is the same problem of mapping the left mouse button to autofire, how does windows know which left mouse button is correct? It may just hold the mouse button down all the time and not autofire/repeat. But that's irrelevent, KVMs don't split the control between 2 PCs, could you imagine trying to look at the output from 2 PCs on 1 monitor that's switching between the 2 at a fast rate? Not that this would even work because the video output from the 2 PCs isn't synced, so the monitor will take some time to lock on to the signal when it switches.
From the product description:
"Users can switch between computers using a manual pushbutton switch located on a wired remote that can be placed on your desk for convenience."

westftw's ideas both solve the usbhost/client problem by making either the input or the output a ps2 mouse connection. this only works if you have a ps2 mouse or a ps2 connection on the PC. You also need an appropriate ps2 connector, either to plug the mouse into or to plug into the PC.

David1937

I think that I finally understand your previous post, but just correct me if I am wrong.

So basically what I was thinking of coding it is basically using many if statements and put them in the void loop ()?

I am not going to use the exact code for lack of time.

If (get.Button(left)) //will have to make this one more complex if I was thinking of making the clicker run while I had this pressed, but for now I am just wondering the code for the mouse emulation.
{
Mouse.press(left))
}

while (get.Button(right)) //emulate the right click and other mouse buttons to the computer with this code???
{
Mouse.press(right)
}
if (!get.Button(right))
{
Mouse.release(right)
}

This is where I get confused, couldn't I just used getXchange() and getYChange() and just make those values be set as variables where a few lines later just does mouse.move()?

I think that I am missing a big portion of this but with your good explaining I am starting to understand it more.
If I were to pay someone to make this code for me what is a reliable place to go and what would something like this cost to make?



weird_dave

There are some issues. Firstly, I went for a google and came back without finding any serial mouse emulation. I did find PS2 emulation:
http://playground.arduino.cc/ComponentLib/Ps2mouse
but this does require wiring up a PS2 connection with the Arduino, I'd find an old PS2 mouse and "borrow" the cable. The emulation code is at the bottom of that page.

The while loop you have there will block mouse movement while the right button is pressed.

The mouse.press() commands are for emulating mouse movements via the native USB port, but this is where your USB mouse will be connected, so there will be a different command depending on the emulation solution (serial or PS2). Same for mouse.move().

David1937

There are some issues. Firstly, I went for a google and came back without finding any serial mouse emulation. I did find PS2 emulation:
http://playground.arduino.cc/ComponentLib/Ps2mouse
but this does require wiring up a PS2 connection with the Arduino, I'd find an old PS2 mouse and "borrow" the cable. The emulation code is at the bottom of that page.

The while loop you have there will block mouse movement while the right button is pressed.

The mouse.press() commands are for emulating mouse movements via the native USB port, but this is where your USB mouse will be connected, so there will be a different command depending on the emulation solution (serial or PS2). Same for mouse.move().
Okay gotcha and what do you mean by "borrowing" the cable, if I wanted to use a gaming mouse which is USB how would I do that? Is it the same?

I found something online that takes a USB connection and turns it into ps/2 like a converter, is that somewhat what you are talking about?

weird_dave

Okay gotcha and what do you mean by "borrowing" the cable, if I wanted to use a gaming mouse which is USB how would I do that? Is it the same?
You plug the USB mouse into the Native port on the Arduino Due, then find an old PS2 mouse or keyboard and cut the cable off it so you can connect the cut end to the Arduino and the connector end into the PC, so that the Arduino can pretend to be a PS2 mouse, passing the the USB mouse controls to the PC.

I found something online that takes a USB connection and turns it into ps/2 like a converter, is that somewhat what you are talking about?
I wasn't thinking of using those, but you might need one if your PC doesn't have a PS2 port
If it looks like this then yes, it should work:


But not if it looks like this:



David1937

Alright I think I am close here for coming up with a solution. I have a few questions:

So now that I have a script that will basically emulate a USB mouse, where and how do I add the code where it autoclicks? Will the two codes interfere at all? Will this downgrade the performace of my mouse to make it harder to play? Like my mouse is set to 1000hz by defualt but I am fine playing on 500hz if I have too but I don't want to play on anything that is delayed noticably.

How do I take attach the PS/2 cable into the Arduino?

Thanks!

weird_dave

Take a look at the PS2 code, it should indicate which pins to connect to.

Where and how the autoclick will fit depends on the code you have.
You will need to record the time the button is pressed, something like:
Code: [Select]
//define these somewhere sensible
bool LeftState = false;// state of the real mouse button
bool LeftOutput = false;// to store the state of the fake mouse left button
unsigned long ClickTime;// to store the time of a click/release
#define Rate 50 // press and release time, in milliseconds [rep rate is 1000/(2*Rate) ]

// this goes in the loop
  bool newLeftState = mouse.getButton(LEFT_BUTTON);
  if (newLeftState != LeftState) {
    LeftState = newLeftState;
    if (LeftState)  {
      ClickTime = millis();
      Mouse.press(MOUSE_LEFT);
      LeftOutput = true;
    }
  }

Then when the button is pressed, alternate the button state at a fixed rate:
Code: [Select]
// this goes in the loop
if (LeftState)  {
    if (millis() - ClickTime >= Rate) {
      ClickTime = millis(); // new start time for timing the next change in left mouse state
      LeftOutput != LeftOutput; //toggle the state
      if (LeftOutput) {
        Mouse.press(MOUSE_LEFT);
      }
      else  {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }

obviously Mouse.press() and Mouse.release() will change depending on the PS2 code, which I've not had time to look at.

David1937

Take a look at the PS2 code, it should indicate which pins to connect to.

Where and how the autoclick will fit depends on the code you have.
You will need to record the time the button is pressed, something like:
Code: [Select]
//define these somewhere sensible
bool LeftState = false;// state of the real mouse button
bool LeftOutput = false;// to store the state of the fake mouse left button
unsigned long ClickTime;// to store the time of a click/release
#define Rate 50 // press and release time, in milliseconds [rep rate is 1000/(2*Rate) ]

// this goes in the loop
  bool newLeftState = mouse.getButton(LEFT_BUTTON);
  if (newLeftState != LeftState) {
    LeftState = newLeftState;
    if (LeftState)  {
      ClickTime = millis();
      Mouse.press(MOUSE_LEFT);
      LeftOutput = true;
    }
  }

Then when the button is pressed, alternate the button state at a fixed rate:
Code: [Select]
// this goes in the loop
if (LeftState)  {
    if (millis() - ClickTime >= Rate) {
      ClickTime = millis(); // new start time for timing the next change in left mouse state
      LeftOutput != LeftOutput; //toggle the state
      if (LeftOutput) {
        Mouse.press(MOUSE_LEFT);
      }
      else  {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }

obviously Mouse.press() and Mouse.release() will change depending on the PS2 code, which I've not had time to look at.

Okay thanks I think I gotcha.

I guess all that is left to know is how to make that autoclicker and my usb work with this code an I am all good. Instead of a fixed rate could I do it at a random rate btw? Or would the code have to change a lot because of this?

Go Up