示例#1
0
//*****************************************************************************
//
// 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 provides simulated movements of the .
//
//*****************************************************************************
void
CustomHidChangeHandler(void)
{
    unsigned long ulRetcode;
    unsigned long ulButton, ulButton2;


    //
    // Initialize report data
    //
    signed char VolumeReport[1]; //The array isn't needed but keeps consistent with reports with >1 byte
    VolumeReport[0]=0; //Don't normally send any data



    //
    //Read Left button and move mouse if pressed
    //
    ulButton = ROM_GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4);
    if(ulButton == 0)
    {
    	VolumeReport[0]=2; //Volume Up (00000010)
    }

    //
    //Read Right button and sent ABCDEF if pressed
    //
    ulButton2 = ROM_GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0);
    if(ulButton2 == 0)
    {
    	VolumeReport[0]=1; //Volume Down (00000001)
    }

    //
    // Send Device Report
    //
	g_eCustomHidState = CUSTOMHID_STATE_SENDING;
	ulRetcode = USBDHIDCustomHidStateChange((void *)&g_sCustomHidDevice, 0, VolumeReport); //0 is passed for reportID since this custom device doesn't have multiple reports

	// Did we schedule the report for transmission?
	//

	if(ulRetcode == CUSTOMHID_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 provides simulated movements of the .
//
//*****************************************************************************
void
CustomHidChangeHandler(void)
{
    unsigned long ulRetcode;
    unsigned long ulButton, ulButton2;


    //
    // Initialize all keys to 0 - this unpresses a key if you press one
    //
    signed char DeviceReport[8];
    DeviceReport[0]=0; //First 8 buttons
    DeviceReport[1]=0; //Second 8 buttons
    DeviceReport[2]=0; //X
    DeviceReport[3]=0; //Y
    DeviceReport[4]=0; //Z
    DeviceReport[5]=0; //Rx



    //
    //Read Left button and move mouse if pressed
    //
    ulButton = ROM_GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4);
    if(ulButton == 0)
    {
    	DeviceReport[0]=0x0F;
    	DeviceReport[1]=0;
    	DeviceReport[2]=-60;
    	DeviceReport[3]=-60;
    	DeviceReport[4]=-125;
    	DeviceReport[5]=-125;
    }

    //
    //Read Right button and sent ABCDEF if pressed
    //
    ulButton2 = ROM_GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0);
    if(ulButton2 == 0)
    {
    	DeviceReport[0]=0;
    	DeviceReport[1]=0xF0;
    	DeviceReport[2]=60;
    	DeviceReport[3]=60;
    	DeviceReport[4]=125;
    	DeviceReport[5]=125;
    }

    //
    // Send Device Report
    //
	g_eCustomHidState = CUSTOMHID_STATE_SENDING;
	ulRetcode = USBDHIDCustomHidStateChange((void *)&g_sCustomHidDevice, 0, DeviceReport); //0 is passed for reportID since this custom device doesn't have multiple reports

	// Did we schedule the report for transmission?
	//

	if(ulRetcode == CUSTOMHID_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;
		}
	}
}
//*****************************************************************************
//
// Sends a string of characters via the USB HID keyboard interface.
//
//*****************************************************************************
void
SendString(char *pcStr)
{
    unsigned long ulChar;

    //
    // Loop while there are more characters in the string.
    //
    while(*pcStr)
    {
        //
        // Get the next character from the string.
        //
        ulChar = *pcStr++;

        //
        // Skip this character if it is a non-printable character.
        //
        if((ulChar < ' ') || (ulChar > '~'))
        {
            continue;
        }

        //
        // Convert the character into an index into the keyboard usage code
        // table.
        //
        ulChar -= ' ';

        //
        // Send the key press message.
        //
        g_eKeyboardState = STATE_SENDING;
        if(USBDHIDKeyboardKeyStateChange((void *)&g_sKeyboardDevice,
                                         g_ppcKeyUsageCodes[ulChar][0],
                                         g_ppcKeyUsageCodes[ulChar][1],
                                         true) != KEYB_SUCCESS)
        {
            return;
        }

        //
        // Wait until the key press message has been sent.
        //
        if(!WaitForSendIdle(MAX_SEND_DELAY))
        {
            g_bConnected = 0;
            return;
        }

        //
        // Send the key release message.
        //
        g_eKeyboardState = STATE_SENDING;
        if(USBDHIDKeyboardKeyStateChange((void *)&g_sKeyboardDevice,
                                         0, g_ppcKeyUsageCodes[ulChar][1],
                                         false) != KEYB_SUCCESS)
        {
            return;
        }

        //
        // Wait until the key release message has been sent.
        //
        if(!WaitForSendIdle(MAX_SEND_DELAY))
        {
            g_bConnected = 0;
            return;
        }
    }
}
//*****************************************************************************
//
// 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);
}