This code will display two unsigned shorts (representing health & remaining ammo) on a TextLCD display. The mbed receives them via the USB HID interface.
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include "USBHID.h" 00004 00005 //for debugging 00006 Serial pc(USBTX, USBRX); 00007 00008 //an LED to display activity 00009 DigitalOut hid_activity_led(LED2); 00010 DigitalOut alive_led(LED1); 00011 00012 //The TextLCD 00013 TextLCD lcd(p21, p22, p17, p18, p19, p20); // rs, e, d4-d7 00014 00015 //the mbed doesn't return the report ID. 00016 union hid_report_t { 00017 uint8_t bytes[sizeof(int16_t)*2]; 00018 struct { 00019 uint16_t health; 00020 uint16_t ammo; 00021 } data; 00022 }; 00023 00024 //We declare a USBHID device. 00025 // output report size = 1 (not needed here) 00026 // input report size matches the one defined above 00027 USBHID hid(sizeof(int16_t)*2, 1); 00028 00029 //This report will contain data to be received 00030 HID_REPORT recv_report; 00031 00032 int main() { 00033 //initialise the LCD 00034 lcd.printf(" CRYSIS\n Maximum mbed"); 00035 pc.printf("ready!\r\n"); 00036 uint16_t health, ammo; 00037 00038 while(1) { 00039 if(hid.readNB(&recv_report)) { 00040 pc.printf("recv[%d]: ", recv_report.length); 00041 for(int i = 0; i < recv_report.length; i++) { 00042 pc.printf("%d ", recv_report.data[i]); 00043 } 00044 pc.printf("\r\n"); 00045 00046 //receive data 00047 hid_report_t* pHidData = (hid_report_t*)(recv_report.data); 00048 00049 //for some reason, the byte order has been changed! 00050 //negative number support will need sign extension 00051 health = __REV16(pHidData->data.health); 00052 ammo = __REV16(pHidData->data.ammo); 00053 00054 //for debugging... 00055 //pc.printf("Health: %d, Ammo: %d\r\n", health, ammo); 00056 //pc.printf("HEX: Health: %x, Ammo: %x\r\n", health, ammo); 00057 00058 //write new values on screen. 00059 lcd.cls(); 00060 lcd.printf("Health: %d\nAmmo: %d", health, ammo); 00061 hid_activity_led = hid_activity_led == 0 ? 1 : 0;//show HID activity 00062 } 00063 00064 //toggle an LED to show us, that the mbed is alive. 00065 wait(0.1); 00066 alive_led = alive_led == 0 ? 1 : 0; 00067 } 00068 }
Generated on Sun Jul 17 2022 04:30:32 by 1.7.2