USBHID Hello World

Dependencies:   mbed USBDevice

Committer:
samux
Date:
Fri Mar 01 13:22:56 2013 +0000
Revision:
7:53a2d7e81b22
Parent:
6:3f614fb580cd
use latest USBDevice lib (FRDM-KL25Z support)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 3:8f52e961548d 1 #include "mbed.h"
samux 3:8f52e961548d 2 #include "USBHID.h"
samux 6:3f614fb580cd 3
samux 5:b1d34990a376 4 //We declare a USBHID device. By default input and output reports are 64 bytes long.
samux 5:b1d34990a376 5 USBHID hid(8, 8);
samux 6:3f614fb580cd 6
samux 5:b1d34990a376 7 Serial pc(USBTX, USBRX);
samux 6:3f614fb580cd 8
samux 3:8f52e961548d 9 //This report will contain data to be sent
samux 3:8f52e961548d 10 HID_REPORT send_report;
samux 5:b1d34990a376 11 HID_REPORT recv_report;
samux 6:3f614fb580cd 12
samux 5:b1d34990a376 13 DigitalOut l1(LED1);
samux 6:3f614fb580cd 14
samux 3:8f52e961548d 15 int main(void) {
samux 5:b1d34990a376 16 send_report.length = 8;
samux 6:3f614fb580cd 17
samux 3:8f52e961548d 18 while (1) {
samux 5:b1d34990a376 19
samux 5:b1d34990a376 20 //Fill the report
samux 5:b1d34990a376 21 for (int i = 0; i < send_report.length; i++)
samux 5:b1d34990a376 22 send_report.data[i] = rand() & 0xff;
samux 6:3f614fb580cd 23
samux 3:8f52e961548d 24 //Send the report
samux 3:8f52e961548d 25 hid.send(&send_report);
samux 6:3f614fb580cd 26
samux 5:b1d34990a376 27 //try to read a msg
samux 5:b1d34990a376 28 if(hid.readNB(&recv_report)) {
samux 5:b1d34990a376 29 l1 = !l1;
samux 5:b1d34990a376 30 for(int i = 1; i < recv_report.length; i++) {
samux 5:b1d34990a376 31 pc.printf("%d ", recv_report.data[i]);
samux 5:b1d34990a376 32 }
samux 5:b1d34990a376 33 pc.printf("\r\n");
samux 5:b1d34990a376 34 }
samux 3:8f52e961548d 35 }
samux 3:8f52e961548d 36 }