Optimize mouse code, add notes.

This commit is contained in:
Andrew J. Kroll 2013-12-25 04:49:01 -05:00
parent d17e1f94cb
commit b8fb19fb90
2 changed files with 130 additions and 42 deletions

View file

@ -18,7 +18,10 @@ e-mail : support@circuitsathome.com
void MouseReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
MOUSEINFO *pmi = (MOUSEINFO*)buf;
// Future:
// bool event;
#if 0
if (prevState.mouseInfo.bmLeftButton == 0 && pmi->bmLeftButton == 1)
OnLeftButtonDown(pmi);
@ -43,6 +46,82 @@ void MouseReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *bu
if (len > sizeof (MOUSEINFO))
for (uint8_t i = 0; i<sizeof (MOUSEINFO); i++)
prevState.bInfo[i] = buf[i];
#else
//
// Optimization idea:
//
// 1: Don't pass the structure on every event. Buttons would not need it.
// 2: Only pass x/y values in the movement routine.
//
// These two changes (with the ones I have made) will save extra flash.
// The only "bad" thing is that it could break old code.
//
// Future thoughts:
//
// The extra space gained can be used for a generic mouse event that can be called
// when there are _ANY_ changes. This one you _MAY_ want to pass everything, however the
// sketch could already have noted these facts to support drag/drop scroll wheel stuff, etc.
//
// Why do we need to pass the structure for buttons?
// The function call not enough of a hint for what is happening?
if(prevState.mouseInfo.bmLeftButton != pmi->bmLeftButton ) {
if(pmi->bmLeftButton) {
OnLeftButtonDown(pmi);
} else {
OnLeftButtonUp(pmi);
}
// Future:
// event = true;
}
if(prevState.mouseInfo.bmRightButton != pmi->bmRightButton) {
if(pmi->bmRightButton) {
OnRightButtonDown(pmi);
} else {
OnRightButtonUp(pmi);
}
// Future:
// event = true;
}
if(prevState.mouseInfo.bmMiddleButton != prevState.mouseInfo.bmMiddleButton) {
if(pmi->bmMiddleButton) {
OnMiddleButtonDown(pmi);
} else {
OnMiddleButtonUp(pmi);
}
// Future:
// event = true;
}
//
// Scroll wheel(s), are not part of the spec, but we could support it.
// Logitech wireless keyboard and mouse combo reports scroll wheel in byte 4
// We wouldn't even need to save this information.
//if(len > 3) {
//}
//
// Mice only report motion when they actually move!
// Why not just pass the x/y values to simplify things??
if(pmi->dX || pmi->dY) {
OnMouseMove(pmi);
// Future:
// event = true;
}
//
// Future:
// Provide a callback that operates on the gathered events from above.
//
// if(event) OnMouse();
//
// Only the first byte matters (buttons). We do NOT need to save position info.
prevState.bInfo[0] = buf[0];
#endif
};
void KeyboardReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {

View file

@ -425,7 +425,7 @@ uint8_t HIDBoot<BOOT_PROTOCOL>::Init(uint8_t parent, uint8_t port, bool lowspeed
USBTRACE2("PROTOCOL SET HID_BOOT rcode:", rcode);
rcode = SetIdle(i, 0, 0);
USBTRACE2("SET_IDLE rcode:", rcode);
if(rcode) goto FailSetIdle;
// if(rcode) goto FailSetIdle; This can fail.
// Get the RPIPE and just throw it away.
SinkParser<USBReadParser, uint16_t, uint16_t> sink;
rcode = GetReportDescr(i, &sink);
@ -480,10 +480,10 @@ FailSetProtocol:
goto Fail;
#endif
FailSetIdle:
#ifdef DEBUG_USB_HOST
USBTRACE("SetIdle:");
#endif
//FailSetIdle:
//#ifdef DEBUG_USB_HOST
// USBTRACE("SetIdle:");
//#endif
Fail:
#ifdef DEBUG_USB_HOST
@ -538,9 +538,9 @@ uint8_t HIDBoot<BOOT_PROTOCOL>::Poll() {
if(bPollEnable && qNextPollTime <= millis()) {
// To-do: optimize manually, getting rid of the loop
// To-do: optimize manually, using the for loop only if needed.
for(int i = 0; i < epMUL(BOOT_PROTOCOL); i++) {
const uint8_t const_buff_len = 16;
const uint16_t const_buff_len = 16;
uint8_t buf[const_buff_len];
USBTRACE3("(hidboot.h) i=", i, 0x81);
@ -548,25 +548,34 @@ uint8_t HIDBoot<BOOT_PROTOCOL>::Poll() {
USBTRACE3("(hidboot.h) epInfo[epInterruptInIndex + i].maxPktSize=", epInfo[epInterruptInIndex + i].maxPktSize, 0x81);
uint16_t read = (uint16_t)epInfo[epInterruptInIndex + i].maxPktSize;
uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex + i].epAddr, &read, buf);
if(!rcode) {
rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex + i].epAddr, &read, buf);
// SOME buggy dongles report extra keys (like sleep) using a 2 byte packet on the wrong endpoint.
// Since keyboard and mice must report at least 3 bytes, we ignore the extra data.
if(!rcode && read > 2) {
if(pRptParser[i])
pRptParser[i]->Parse((HID*)this, 0, (uint8_t)read, buf);
#if 0 // Set this to 1 to print the incoming data
for(uint8_t i = 0; i < read; i++) {
PrintHex<uint8_t > (buf[i], 0x80);
USB_HOST_SERIAL.write(' ');
}
if(read)
USB_HOST_SERIAL.println();
#endif
#ifdef DEBUG_USB_HOST
// We really don't care about errors and anomalies unless we are debugging.
} else {
if(rcode != hrNAK) {
USBTRACE3("(hidboot.h) Poll:", rcode, 0x81);
//break;
}
if(!rcode && read) {
USBTRACE3("(hidboot.h) Strange read count: ", read, 0x80);
USBTRACE3("(hidboot.h) Interface:", i, 0x80);
}
}
if(!rcode && read && (UsbDEBUGlvl > 0x7f)) {
for(uint8_t i = 0; i < read; i++) {
PrintHex<uint8_t > (buf[i], 0x80);
USBTRACE1(" ", 0x80);
}
if(read)
USBTRACE1("\r\n", 0x80);
#endif
}
}
qNextPollTime = millis() + bInterval;
}