USBHID test program

Dependencies:   USBDevice mbed

Java

Use UsbHid.java /media/uploads/victorix/usbhid.java for UsbHid communication

This is a UsbHidTestCase /media/uploads/victorix/usbhidtestcase.java program to verify the Java binding. Refer to the USBHID bindings page http://mbed.org/cookbook/USBHID-bindings-.

Use this HidTest.java file /media/uploads/victorix/hidtest.java to test with the HidTest program

Import programHidTest

USBHID test program

program

Committer:
victorix
Date:
Mon Feb 04 13:24:35 2013 +0000
Revision:
0:af855595c6fc
To test USBHID with a Java program.; See corresponding USBHID bindings;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
victorix 0:af855595c6fc 1 #include "mbed.h"
victorix 0:af855595c6fc 2 #include "USBHID.h"
victorix 0:af855595c6fc 3
victorix 0:af855595c6fc 4 /*
victorix 0:af855595c6fc 5 * HidTest.c
victorix 0:af855595c6fc 6 *
victorix 0:af855595c6fc 7 * Program to test the UsbHid with HidTest.java
victorix 0:af855595c6fc 8 * Java files in USBHID bindings page
victorix 0:af855595c6fc 9 *
victorix 0:af855595c6fc 10 * This test program is allways sending a non blocking message and expecting to read a non blocking message
victorix 0:af855595c6fc 11 * Messages are sent with the last byte cycling through '0'-'9' and LED4 toggles with each send
victorix 0:af855595c6fc 12 * When a messsage is received:
victorix 0:af855595c6fc 13 * LED2 toggles
victorix 0:af855595c6fc 14 * Received message is sent, blocking.
victorix 0:af855595c6fc 15 * A message is read blocking
victorix 0:af855595c6fc 16 * The recived message is now the new non blocking message to be sent
victorix 0:af855595c6fc 17 *
victorix 0:af855595c6fc 18 * victorix 04.02.2013
victorix 0:af855595c6fc 19 */
victorix 0:af855595c6fc 20 uint16_t vendor_id=0x1798;
victorix 0:af855595c6fc 21 uint16_t product_id=0x2013;
victorix 0:af855595c6fc 22 uint16_t product_release=0x0001;
victorix 0:af855595c6fc 23 bool connect=true;
victorix 0:af855595c6fc 24 #define HID_LEN 32
victorix 0:af855595c6fc 25
victorix 0:af855595c6fc 26 USBHID hid(HID_LEN, HID_LEN, vendor_id, product_id, product_release, connect);
victorix 0:af855595c6fc 27 HID_REPORT hidOut;
victorix 0:af855595c6fc 28 HID_REPORT hidIn;
victorix 0:af855595c6fc 29
victorix 0:af855595c6fc 30 DigitalOut l2(LED2);
victorix 0:af855595c6fc 31 DigitalOut l4(LED4);
victorix 0:af855595c6fc 32
victorix 0:af855595c6fc 33 int main() {
victorix 0:af855595c6fc 34
victorix 0:af855595c6fc 35 char s[HID_LEN]="Have a nice day :)";
victorix 0:af855595c6fc 36 memcpy(hidOut.data,s,HID_LEN);
victorix 0:af855595c6fc 37 hidOut.length=HID_LEN;
victorix 0:af855595c6fc 38
victorix 0:af855595c6fc 39 hidIn.length=HID_LEN;
victorix 0:af855595c6fc 40
victorix 0:af855595c6fc 41 while(1) {
victorix 0:af855595c6fc 42 l4=!l4; //toggle Led4
victorix 0:af855595c6fc 43 hid.sendNB(&hidOut); //send non blocking
victorix 0:af855595c6fc 44 //last byte used as a simple frame counter
victorix 0:af855595c6fc 45 if(hidOut.data[HID_LEN-1]>='0' && hidOut.data[HID_LEN-1]<'9')
victorix 0:af855595c6fc 46 hidOut.data[HID_LEN-1]++;
victorix 0:af855595c6fc 47 else hidOut.data[HID_LEN-1]='0';
victorix 0:af855595c6fc 48 wait(1); //wait 1s
victorix 0:af855595c6fc 49 if(hid.readNB(&hidIn)){ //read non blocking
victorix 0:af855595c6fc 50 l2=!l2; //toggle LED2
victorix 0:af855595c6fc 51 memcpy(hidOut.data, hidIn.data, hidIn.length); //copy input to output
victorix 0:af855595c6fc 52 hid.send(&hidOut); //send blocking
victorix 0:af855595c6fc 53 wait(2); //wait 2s
victorix 0:af855595c6fc 54 if(hid.read(&hidIn)){ //read blocking
victorix 0:af855595c6fc 55 memcpy(hidOut.data, hidIn.data, hidIn.length); //copy input to output
victorix 0:af855595c6fc 56 }
victorix 0:af855595c6fc 57 }
victorix 0:af855595c6fc 58 }
victorix 0:af855595c6fc 59 }