Using USBKeyboard with InterruptIn

22 May 2013

Hello guys! I'm new in C++ programming, picked it up for school project purposes. So I am to use mbed to emulate USBKeyboard for gaming purposes (yes, I am trying to create something like a joystick/controller for pc gaming). So I wrote the following program with the limited programming knowledge I have.

Currently testing it with push buttons mounted on breadboard. My problem is, once you've pressed a single button, the text would print non-stop, no matter what program you're on (note pad, browser, word document, mbed compiler etc). It works on games like I intended, however the game character would just keep running in one single direction. Pressing other buttons wouldn't change its state, and it only stops when I press the reset button on mbed.

I've connected the pins like the guys did in this video

I'm guessing the problem lies with the interrupt or that the USBKeyboard library isn't compatible with interrupt. Are there any ways to break the interrupt? Please advise, thanks you!

#include "mbed.h"
#include "USBKeyboard.h"
 
USBKeyboard keyboard;
 
InterruptIn a(p23);
InterruptIn d(p24);
 
DigitalOut myled(LED2);
 
void ai ()
{
    keyboard.keyCode('a');
}
 
void di ()
{
    keyboard.keyCode('d');
}
 
int main(void)
{
    a.rise(&ai);
    d.rise(&di);
    
    while (1) {
    myled = !myled;
    }
}
28 Aug 2013

Hey,

I did a quick search on USB + Interrupt but couldn't find anything specific on that subject. However, with interrupt's you always need to keep in mind that you don't want to do lengthy operations, no waiting etc. ISR's needs to be served as fast as possible. So you don't want to send serial data, call other ISR stuff, waiting etc.. Keep them short, read something and bail out.

That Said, what you properly want to look into is using MBED's RTOS system : http://mbed.org/handbook/RTOS With the RTOS you can much easer signal a other system (thread) to send some data at correct intervals and do your work.

I have used the RTOS with USBHID and that worked out quite fine.