//***************************************************************************** // // Main routine for the mouse. // //***************************************************************************** void USBMouseMain(void) { // // Disable interrupts while changing the variables below. // IntMasterDisable(); switch(sMouseState.eState) { case MOUSE_PENDING: { // // Send the report since there is one pending. // USBDHIDMouseStateChange((void *)&g_sMouseDevice, (uint8_t)sMouseState.i32X, (uint8_t)sMouseState.i32Y, sMouseState.ui8Buttons); // // Clear out the report data so that pending data does not // continually accumulate. // sMouseState.i32X = 0; sMouseState.i32Y = 0; if(sMouseState.ui8Buttons) { // // Need to always send out that all buttons are released. // sMouseState.ui8Buttons = 0; sMouseState.eState = MOUSE_SENDING_PEND; } else { // // Switch to sending state and wait for the transmit to be // complete. // sMouseState.eState = MOUSE_SENDING; } break; } case MOUSE_DISCONNECT: case MOUSE_SENDING: case MOUSE_SENDING_PEND: case MOUSE_IDLE: default: { break; } } // // Enable interrupts now that the main loop is complete. // IntMasterEnable(); }
//***************************************************************************** // // This function provides simulated movements of the mouse. // //***************************************************************************** void MoveHandler(void) { unsigned long ulRetcode; char cDeltaX, cDeltaY; // // Determine the direction to move the mouse. // ulRetcode = g_ulSysTickCount % (4 * SYSTICKS_PER_SECOND); if(ulRetcode < SYSTICKS_PER_SECOND) { cDeltaX = MOUSE_MOVE_INC; cDeltaY = 0; } else if(ulRetcode < (2 * SYSTICKS_PER_SECOND)) { cDeltaX = 0; cDeltaY = MOUSE_MOVE_INC; } else if(ulRetcode < (3 * SYSTICKS_PER_SECOND)) { cDeltaX = (char)MOUSE_MOVE_DEC; cDeltaY = 0; } else { cDeltaX = 0; cDeltaY = (char)MOUSE_MOVE_DEC; } // // Tell the HID driver to send this new report. // g_eMouseState = MOUSE_STATE_SENDING; ulRetcode = USBDHIDMouseStateChange((void *)&g_sMouseDevice, cDeltaX, cDeltaY, 0); // // Did we schedule the report for transmission? // if(ulRetcode == MOUSE_SUCCESS) { // // Wait for the host to acknowledge the transmission if all went well. // if(!WaitForSendIdle(MAX_SEND_DELAY)) { // // The transmission failed, so assume the host disconnected and go // back to waiting for a new connection. // g_bConnected = false; } } }
//***************************************************************************** // // This function handles updates due to touchscreen input. // // This function is called periodically from the main loop to check the // touchscreen state and, if necessary, send a HID report back to the host // system. // // Returns Returns \b true on success or \b false if an error is detected. // //***************************************************************************** static void TouchHandler(void) { long lDeltaX, lDeltaY; static unsigned char ucButtons = 0; // // Is someone pressing the screen or has the button changed state? If so, // we determine how far they have dragged their finger/stylus and use this // to calculate mouse position changes to send to the host. // if(g_bScreenPressed || (ucButtons != g_ucButtons)) { // // Calculate how far we moved since the last time we checked. This // rather odd layout prevents a compiler warning about undefined order // of volatile accesses. // lDeltaX = g_lScreenX; lDeltaX -= g_lScreenStartX; lDeltaY = g_lScreenY; lDeltaY -= g_lScreenStartY; // // Reset our start position. // g_lScreenStartX = g_lScreenX; g_lScreenStartY = g_lScreenY; // // Was there any movement or change in button state? // if(lDeltaX || lDeltaY || (ucButtons != g_ucButtons)) { // // Yes - send a report back to the host after clipping the deltas // to the maximum we can support. // lDeltaX = (lDeltaX > 127) ? 127 : lDeltaX; lDeltaX = (lDeltaX < -128) ? -128 : lDeltaX; lDeltaY = (lDeltaY > 127) ? 127 : lDeltaY; lDeltaY = (lDeltaY < -128) ? -128 : lDeltaY; // // Remember the current button state. // ucButtons = g_ucButtons; // // Send the report back to the host. // USBDHIDMouseStateChange((void *)&g_sMouseDevice, (char)lDeltaX, (char)lDeltaY, ucButtons); } // // Update the button portion of the display. // UpdateDisplay(ucButtons, false); } }
//***************************************************************************** // // This function handles updates due to the buttons. // // This function is called from the main loop each time the buttons need to // be checked. If it detects an update it will schedule an transfer to the // host. // // Returns Returns \b true on success or \b false if an error is detected. // //***************************************************************************** tBoolean ButtonHandler(void) { unsigned char ucButtons, ucChanged, ucRepeat; unsigned long ulRetcode; char cDeltaX, cDeltaY; tBoolean bSuccess; // // Determine the state of the pushbuttons. // ucButtons = ButtonsPoll(&ucChanged, &ucRepeat); // // Update the display to show which buttons are currently pressed. // UpdateDisplay(ucButtons); // // If we are connected to the host and the select button changed state or // we see a repeat message from any of the direction buttons, go ahead and // send a mouse state change to the host. // if((ucChanged & SELECT_BUTTON) || (ucRepeat & ~SELECT_BUTTON)) { // // Assume no change in the position until we determine otherwise. // cDeltaX = 0; cDeltaY = 0; // // Up button is held down // if(BUTTON_REPEAT(UP_BUTTON, ucRepeat)) { cDeltaY = MOUSE_MOVE_DEC; } // // Down button is held down. // if(BUTTON_REPEAT(DOWN_BUTTON, ucRepeat)) { cDeltaY = MOUSE_MOVE_INC; } // // Left button is held down. // if(BUTTON_REPEAT(LEFT_BUTTON, ucRepeat)) { cDeltaX = MOUSE_MOVE_DEC; } // // Right button is held down // if(BUTTON_REPEAT(RIGHT_BUTTON, ucRepeat)) { cDeltaX = MOUSE_MOVE_INC; } // // Tell the HID driver to send this new report for us. Note that a 0 // in ucButtons indicates that the relevant button is pressed so we // set the MOUSE_REPORT_BUTTON_1 bit if SELECT_BUTTON is clear in // ucButtons. // DEBUG_PRINT("Sending (0x%02x, 0x%02x), button %s.\n", cDeltaX, cDeltaY, ((ucButtons & SELECT_BUTTON) ? "released" : "pressed")); g_eMouseState = MOUSE_STATE_SENDING; ulRetcode = USBDHIDMouseStateChange((void *)&g_sMouseDevice, cDeltaX, cDeltaY, ((ucButtons & SELECT_BUTTON) ? 0 : MOUSE_REPORT_BUTTON_1)); // // Did we schedule the report for transmission? // if(ulRetcode == MOUSE_SUCCESS) { // // Wait for the host to acknowledge the transmission if all went // well. // bSuccess = WaitForSendIdle(MAX_SEND_DELAY); // // Did we time out waiting for the packet to be sent? // if (!bSuccess) { // // Yes - assume the host disconnected and go back to // waiting for a new connection. // DEBUG_PRINT("Send timed out!\n"); g_bConnected = 0; } } else { // // An error was reported when trying to send the report. This may // be due to host disconnection but could also be due to a clash // between our attempt to send a report and the driver sending the // last report in response to an idle timer timeout so we don't // jump to the conclusion that we were disconnected in this case. // DEBUG_PRINT("Can't send report.\n"); bSuccess = false; } } else { // // There was no change in the state of the buttons so we have nothing // to do. // bSuccess = true; } return(bSuccess); }
//***************************************************************************** // // This function handles updates due to touchscreen input. // // This function is called periodically from the main loop to check the // touchscreen state and, if necessary, send a HID report back to the host // system. // // Returns Returns \b true on success or \b false if an error is detected. // //***************************************************************************** static void TouchHandler(void) { short lDeltaX = 0; short lDeltaY = 0; unsigned int ulLoop; static unsigned char ucButtons = 0; static unsigned int eventCount =0; // //If there is a touch event, consider this as a first touch // if(g_iTouch) { // //Reset the delta // lDeltaX =0; lDeltaY =0; eventCount++; // //Wait for some more event to confirm a movement // if(eventCount >= 20) { // //There is a movement, so reset the touch flag // g_iTouch= 0; // //Set the movement flag // g_move = 1; // //reset the event couter // eventCount = 0; } // //Wait for 10ms and see if still there is a movement // delay(10); } else { // // If there is movement // if(g_move) { // //Wait for some more time to confirm // delay(50); // //Reset the deltas // lDeltaX =0; lDeltaY =0; // //Reset the movement flag // g_move = 0; } // //If still not released the touch // else if(!TouchReleaseDetect()) { // //get the current x and y // lDeltaY = g_lScreenY; lDeltaX = g_lScreenX; // //Calculate the delta from the previous value // lDeltaX -= g_lScreenStartX; lDeltaY -= g_lScreenStartY; delay(20); // //check is delta is more than 15, then reset to a lesser value // if(lDeltaX > 15) lDeltaX = 10; if(lDeltaX < -15) lDeltaX = -10; if(lDeltaY > 15) lDeltaY = 5; if(lDeltaY < -15) lDeltaY = -5; // //if there is continious touch at the same point dont allow the cursor to move // if(lDeltaX < 3 && lDeltaX > -3) lDeltaX =0; if(lDeltaY < 3 && lDeltaY > -3) lDeltaY =0; // //Reset the touch flag // g_iTouch = 0; } } // // Is the press within the button area? If so, determine which // button has been pressed. // if(g_lScreenY >= (GrContextDpyHeightGet(&g_sContext) - BUTTON_HEIGHT)) { lDeltaX =0; lDeltaY =0; // // Run through the list of buttons to determine which one was // pressed. // if(!g_released) { for(ulLoop = 0; ulLoop < NUM_MOUSE_BUTTONS; ulLoop++) { if((g_lScreenX >= g_sMouseButtons[ulLoop].usX) && (g_lScreenX < (g_sMouseButtons[ulLoop].usX + g_sMouseButtons[ulLoop].usWidth))) { g_ucButtons |= g_sMouseButtons[ulLoop].ucReportFlag; break; } } } else { g_released = 0; } } // //Store the current x and y, we need it to calculate the delta // g_lScreenStartX = g_lScreenX; g_lScreenStartY = g_lScreenY; // // Was there any movement or change in button state? // if(lDeltaX || lDeltaY || (ucButtons != g_ucButtons)) { // // Yes - send a report back to the host after clipping the deltas // to the maximum we can support. // lDeltaX = (lDeltaX > 127) ? 127 : lDeltaX; lDeltaX = (lDeltaX < -128) ? -128 : lDeltaX; lDeltaY = (lDeltaY > 127) ? 127 : lDeltaY; lDeltaY = (lDeltaY < -128) ? -128 : lDeltaY; // // Remember the current button state. // ucButtons = g_ucButtons; // // Send the report back to the host. // USBDHIDMouseStateChange((void *)&g_sMouseDevice, (char)lDeltaX, (char)lDeltaY, ucButtons); } // // Update the button portion of the display. // UpdateDisplay(ucButtons, false); }