USBHost Usb.task() get mouse data after given time

Hello,
I want to build an electric ruler with a mouse. So I connected a USB Mouse to a USB Shield, which is attatched to an arduino uno.
For getting startet I just wanted to print out the delta x and delta y movement of the mouse.
There is an example code out there:

// USB
#include <usbhub.h>
USB     Usb;
USBHub     Hub(&Usb);

// Human Interface Device
#include <hidboot.h>
HIDBoot<USB_HID_PROTOCOL_MOUSE>    HidMouse(&Usb);

// SPI
#include <SPI.h>

// Mouse variables
int dx;
int dy;
int X = 0;
int Y = 0;

// ----- Mouse Report Parser
class MouseRptParser : public MouseReportParser
{
  protected:
    void OnMouseMove  (MOUSEINFO *mi);
    void OnLeftButtonUp (MOUSEINFO *mi);
    void OnLeftButtonDown (MOUSEINFO *mi);
    void OnRightButtonUp  (MOUSEINFO *mi);
    void OnRightButtonDown  (MOUSEINFO *mi);
    void OnMiddleButtonUp (MOUSEINFO *mi);
    void OnMiddleButtonDown (MOUSEINFO *mi);
};
void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{
  // ----- Code by LINGIB
  int dx = mi->dX;
  int dy = mi->dY;
  X += dx;
  Y += dy;

  Serial.print("dx=");
  Serial.print(dx);
  Serial.print(" dy=");
  Serial.print(dy);
  Serial.print(" X=");
  Serial.print(X);
  Serial.print(" Y=");
  Serial.println(Y);

  //    // ----- Original code
  //    Serial.print("dx=");
  //    Serial.print(mi->dX, DEC);
  //    Serial.print(" dy=");
  //    Serial.println(mi->dY, DEC);
};
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
{
  Serial.println("L Butt Up");
};
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
{
  Serial.println("L Butt Dn");
};
void MouseRptParser::OnRightButtonUp  (MOUSEINFO *mi)
{
  Serial.println("R Butt Up");
};
void MouseRptParser::OnRightButtonDown  (MOUSEINFO *mi)
{
  Serial.println("R Butt Dn");
};
void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
{
  Serial.println("M Butt Up");
};
void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
{
  Serial.println("M Butt Dn");
};
MouseRptParser                               Prs;

// -----------------
//  setup()
// -----------------
void setup()
{
  // Serial port
  Serial.begin( 115200 );

  // Inititialise USB card
  Usb.Init();
  HidMouse.SetReportParser(0, &Prs);

  // Clear mouse variables
  Serial.println("Mouse Ready");
  X = 0;
  Y = 0;
}

// -----------------
//  loop()
// -----------------
void loop()
{
  Usb.Task();
}

is there a possibility to ask for mouse-movement after a given time, e.g. every 200 ms?
The problem is, I want to move the mouse very slow. The above code is giving me at a wide range of very slow movements every time delta x = 1 or delta y = 1. I think, when I ask for the mousedata every 200 ms, then the mouse can sum the delta x and y values on chip. I thinks then I get bigger values of delta x and y, especially for very slow movement. I tried:

unsigned long last_run = millis();
//  loop()
// -----------------
void loop()
{
   if (millis() - last_run > 200) {
last_run = millis();
Usb.Task();
}
}

But this gave me wrong results.

I don't think so. I would even be surprised if that really works. The mouse will send the USB messages, If you don't get them in time you may simply loose some of them but the mouse chip won't even know about that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.