USB HID sending a key code

Hi,

I want to send a single key code (‘F’) to the USB HID via an interrupt on port 0 pin 0. The interrupt code works.

When I add the send key code, something is sent to the PC. The PC becomes very slow, CPU usage jumps to 50% and the only way to get control of the PC is to uninstall the HID drivers the Wixel installs.

I must be doing something wrong. Any ideas?

#include <wixel.h>
#include <usb.h>
#include <usb_hid.h>
#include <gpio.h>
#include <board.h>
#include <stdio.h>
#include <cc2511_map.h>

unsigned char P0_0_interrupt;

ISR(P0INT,0)
{
 if (P0IFG & 0b00000001)
  {
   P0_0_interrupt = 1;
  }
 P0IFG &= 0b10000000;		//clear all but 7, used by USB 
 IRCON &= 0b10011111;		//clear port 0 flag and docs say bit six must always be 0
}

void initialize()
{
    systemInit();
	usbInit();
	setPort0PullType(0);		//all pins pulled low
								//setup a rising edge interrupt on P0_0.
	PICTL 		= 0b00011000;   //Port 0, inputs 0 to 7 interrupt enable 
	IEN1 		= 0b00100000;	//enable port 0 interrupts
	EA			= 1;			//enable all interrupts  
}

void sendKeyToUSB(uint8 keyCode)
{
  usbHidKeyboardInput.keyCodes[0] = keyCode;
  usbHidKeyboardInputUpdated = 1;	
}

void main()
{
	initialize();

    while(1)
    {
	
	usbHidService();
	
    if (P0_0_interrupt == 1)
	 { 
	  LED_RED_TOGGLE();		
	  sendKeyToUSB(0x46);		//F key
	  P0_0_interrupt = 0;
	 }
	    
   }
}

Thanks,

Mark

Hello, Mark.

HID key code 0x46 is “Print Screen”, not F. You can see the list of valid HID keycodes and the constants we defined for them here:

pololu.github.io/wixel-sdk/usb__ … ts_8h.html

The sendKeyToUSB function tells the computer that the key is being pressed, but your app does not have any code to tell the computer that the key has been released. Your app would need to wait until usbHidKeyboardInputUpdated has changed back to 0, then set keyCodes[0] back to 0 and set the flag to 1 again.

Your current program is actually just holding down the Print Screen button and your computer is probably busy making screenshots.

–David

Hi,

Thanks David.

I got the key code from usb_hid.c in error.

The program is now sending an “f”.

If I wanted to send an “F” would I need to send a shift key, an f key and then a 0 key code for up key? or ???

Thanks again,

Mark

I think it is sufficient to send one frame of input where both shift and “F” are pressed, and then later send a frame where they are both released. There are key codes for shift, but you could also just set the approriate bit in the “modifiers” field, which is documented here:

pololu.github.io/wixel-sdk/struc … o_r_t.html

–David

Hello,

Thanks for the response.

When I started messing with the modifier all kinds of interesting things began to happen and they were not things I wanted to happen. :slight_smile: I was able to work out how to send an uppercase letter. The code is below if anyone is interested.

Now on to using the radio.

Thanks for your help.

Mark

#include <wixel.h>
#include <usb.h>
#include <usb_hid.h>
#include <gpio.h>
#include <board.h>
#include <stdio.h>
#include <cc2511_map.h>

unsigned char P0_0_interrupt, keySent;

ISR(P0INT,0)
{
 if (P0IFG & 0b00000001)
  {
   P0_0_interrupt = 1;
  }
 P0IFG &= 0b10000000;		//clear all but 7, used by USB 
 IRCON &= 0b10011111;		//clear port 0 flag and docs say bit six must always be 0
}

void initialize()
{
    systemInit();
	usbInit();
	setPort0PullType(0);		//all pins pulled low
								//setup a rising edge interrupt on P0_0.
	PICTL 		= 0b00011000;   //Port 0, inputs 0 to 7 interrupt enable 
	IEN1 		= 0b00100000;	//enable port 0 interrupts
	EA			= 1;			//enable all interrupts  
}

void sendKeyToUSB(uint8 keyCode)
{
  if (keyCode == KEY_F)
  {
   usbHidKeyboardInput.keyCodes[0] = KEY_SHIFT_LEFT;
   usbHidKeyboardInput.keyCodes[1] = keyCode;
  }
  else
  {
   usbHidKeyboardInput.keyCodes[0] = keyCode;	//KEY_NONE
   usbHidKeyboardInput.keyCodes[1] = keyCode;
  }
  
  usbHidKeyboardInputUpdated = 1;	
}

void main()
{
	initialize();

    while(1)
    {
	
	usbHidService();
	
	if (P0_0_interrupt == 1)
	 { 
	  LED_RED_TOGGLE();		
	  sendKeyToUSB(KEY_F);
	  keySent			= 1;	//flag that key was sent	
	  P0_0_interrupt 	= 0;
	 }
	else if ((keySent == 1) & (usbHidKeyboardInputUpdated == 0)) 
    {							//waiting for key to be sent
	 sendKeyToUSB(KEY_NONE);	//up key command
	 keySent = 0;				//clear flag 
	}
	    
   }
}