You are viewing an older revision! See the latest version

USBHID bindings

Purpose of this webpage

The purpose of this webpage is to develop programs:

  • which are able to send and read raw data from an mbed HID device.
  • in different programming languages
  • running on different platforms

USBHID bindings status

LanguageWindowsLinuxMac OS
PythonYes
C
C++
Java
Others

Mbed test case code

On the mbed, we want to test that we are able to:

  • send raw data
  • receive raw data

For this a simple program has been developed:

#include "mbed.h"
#include "USBHID.h"

//We declare a USBHID device. Input out output reports have a length of 8 bytes
USBHID hid(8, 8);

//This report will contain data to be sent
HID_REPORT send_report;
HID_REPORT recv_report;

Serial pc(USBTX, USBRX);

int main(void) {
    send_report.length = 8;

    while (1) {
        //Fill the report
        for (int i = 0; i < send_report.length; i++) {
            send_report.data[i] = rand() & 0xff;
        }
            
        //Send the report
        hid.send(&send_report);
        
        //try to read a msg
        if(hid.readNB(&recv_report)) {
            pc.printf("recv: ");
            for(int i = 0; i < recv_report.length; i++) {
                pc.printf("%d ", recv_report.data[i]);
            }
            pc.printf("\r\n");
        }
        
        wait(0.1);
    }
}

Import programUSBHID_TestCase

USBHID test case

Computer side program

To validate the test case and write a Yes in the USBHID bindings status ;), a program has to prove that it is able to:

  • Read data from the mbed
  • Send data to the mbed

Example in Python on Windows

On Windows I use pywinusb to communicate with the mbed

Code

#
#Simple example on how to send and receive data to the Mbed over USB (on windows) using pywinusb
#
import pywinusb.hid as hid
from time import sleep
import random
	
# handler called when a report is received
def rx_handler(data):
    print 'recv: ', data

def findHIDDevice(mbed_usage, mbed_vendor_id):
    # Find all devices connected
    all_devices = hid.HidDeviceFilter(vendor_id = mbed_vendor_id).get_devices()
    
    if not all_devices:
        print "No device connected"
    else:
        # search for the Mbed
        for HIDdevice in all_devices:
            try:
                HIDdevice.open()
                # browse output reports
                for report in HIDdevice.find_output_reports():
                    if mbed_usage in report:

                        #MBED found
                        print 'Mbed detected'

                        #Attach a custom handler when a data is received
                        HIDdevice.set_raw_data_handler(rx_handler)
                        
                        #send a report each 0.2 second. The report is a random array of 8 bytes
                        while True:
                            for i in range(8):
                                report[mbed_usage][i] = random.randint(0, 255)
                            report.send()
                            sleep(0.2)
            except:
                print 'close'
                HIDdevice.close()
		

if __name__ == '__main__':
    # The vendor ID used in the Mbed program
    mbed_vendor_id = 0x1234 

    # Vendor page and usage_id = 2
    mbed_usage = hid.get_full_usage_id(0xffab, 0x02)

    # Search the Mbed, attach rx handler and send data
    findHIDDevice(mbed_usage, mbed_vendor_id)

Demo

/media/uploads/samux/hid_mbed1.png /media/uploads/samux/py_hid1.png

  • On the left screenshot, we can see data received by the mbed: a random array containing 8 values as expected.
  • On the right screenshot, we can see values sent by the mbed to the python script. The last 8 values printed are random values sent by the mbed. The first byte represents the report_id. As there is not report_id for this USB device, by default, there is 0.

Conclusion

An example using python has been developed to communicate with the mbed but it would be great to develop a lot of programs in C, C++, Java, Haskell, Scala, Factor, ... having the same capabilities.

Feel free to contribute to this webpage by developing programs able to communicate with your mbed. You can use your own protocol on top of the USBHID layer to design your own USB device!


All wikipages