This is a very simple Generic HID demo, which is based on the example program shown in the Cypress AN82072 Application Note. Due to the similarities, you can use the graphical PC application from the published in the downloadable software package of the above mentioned Application Note, if you set the Vid/PId values of our device (VID = 0x1234, PID = 0x0006). Note, that besides the Generic Hid Ui.exe executable, you also need the dynamic library cyUSB.dll as well (from the same ZIP package)

Dependencies:   mbed USBDevice

Committer:
cspista
Date:
Thu Jun 16 12:31:17 2022 +0000
Revision:
0:7ae040134a91
Final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cspista 0:7ae040134a91 1 /* LAB10_USBHID_demo
cspista 0:7ae040134a91 2 *
cspista 0:7ae040134a91 3 * This is a very simple Generic HID demo, which is based on the
cspista 0:7ae040134a91 4 * example program shown in the Cypress AN82072 Application Note.
cspista 0:7ae040134a91 5 * Due to the similarities, you can use the graphical PC application
cspista 0:7ae040134a91 6 * from the published in the downloadable software package of the above
cspista 0:7ae040134a91 7 * mentioned Application Note, if you set the Vid/PId valuaes of aur device
cspista 0:7ae040134a91 8 * (VID = 0x1234, PID = 0x0006).
cspista 0:7ae040134a91 9 * Note, that besides the Generic Hid Ui.exe executable, you also need
cspista 0:7ae040134a91 10 * the dynamic library cyUSB.dll as well (freom the same ZIP package)
cspista 0:7ae040134a91 11 */
cspista 0:7ae040134a91 12
cspista 0:7ae040134a91 13 #include "mbed.h"
cspista 0:7ae040134a91 14 #include "USBHID.h"
cspista 0:7ae040134a91 15
cspista 0:7ae040134a91 16 //We declare a USBHID device. By default input and output reports are 8 bytes long.
cspista 0:7ae040134a91 17 USBHID hid(8, 8);
cspista 0:7ae040134a91 18
cspista 0:7ae040134a91 19 HID_REPORT send_report; //This report will contain data to be sent
cspista 0:7ae040134a91 20 HID_REPORT recv_report; //This report will contain data received
cspista 0:7ae040134a91 21
cspista 0:7ae040134a91 22 DigitalOut LED_1(LED1); //Buitin KED at PA5
cspista 0:7ae040134a91 23 PwmOut LED_2(D3); //External LED at D3 (PB3)
cspista 0:7ae040134a91 24 DigitalOut myGND(D4);
cspista 0:7ae040134a91 25 DigitalIn SW1(BUTTON1,PullUp); //Builtin button at PC13
cspista 0:7ae040134a91 26 AnalogIn adc(A0); //Analog input at A0 (PA0)
cspista 0:7ae040134a91 27
cspista 0:7ae040134a91 28 int main(void) {
cspista 0:7ae040134a91 29 send_report.length = 8;
cspista 0:7ae040134a91 30 LED_1 = 0;
cspista 0:7ae040134a91 31 LED_2.period_ms(20);
cspista 0:7ae040134a91 32 myGND = 0;
cspista 0:7ae040134a91 33 while (1) {
cspista 0:7ae040134a91 34 uint16_t raw = adc.read_u16(); //Read ADC (A0 chan)
cspista 0:7ae040134a91 35 for (int i = 0; i < send_report.length; i++) //Fill the report
cspista 0:7ae040134a91 36 send_report.data[i] = 0x00;
cspista 0:7ae040134a91 37 send_report.data[0] = !SW1.read();
cspista 0:7ae040134a91 38 send_report.data[3] = (raw>>8);
cspista 0:7ae040134a91 39 send_report.data[4] = (raw & 0xff);
cspista 0:7ae040134a91 40 hid.send(&send_report); //Send the report
cspista 0:7ae040134a91 41
cspista 0:7ae040134a91 42 if(hid.readNB(&recv_report)) { //try to read a msg
cspista 0:7ae040134a91 43 LED_1 = recv_report.data[0];
cspista 0:7ae040134a91 44 LED_2.write(recv_report.data[1]*0.01f);
cspista 0:7ae040134a91 45 }
cspista 0:7ae040134a91 46 }
cspista 0:7ae040134a91 47 }