Example using the support package for LPC4088 DisplayModule

Dependencies:   DMBasicGUI DMSupport

Example using a lot of the features in the software package for the LPC4088 Display Module.

This project can be selected as a template when creating a new project based on the LPC4088 Display Module.

Information

This project works on the 4.3" display modules.

Some of the apps works on the 5" display modules. The ImageViewer and Slideshow app will show the images distorted as it does not take the resolution into consideration.

Information

The USB Status app is disabled. The Image viewer looks for images in the root of SD cards, USB memory sticks or the file system on the QSPI flash. The Slideshow app expects to find a slideshow script in /mci/elec14/ea_logo.txt.

This is what it looks like on the 4.3" display:

/media/uploads/embeddedartists/everything_cap_000.png /media/uploads/embeddedartists/everything_cap_001.png /media/uploads/embeddedartists/everything_cap_003.png /media/uploads/embeddedartists/everything_cap_004.png /media/uploads/embeddedartists/everything_cap_006.png /media/uploads/embeddedartists/everything_cap_008.png

Revision:
26:f07df116f3c9
Parent:
24:768f5958c308
Child:
27:4fe24decabf9
--- a/main.cpp	Thu Feb 19 12:27:42 2015 +0000
+++ b/main.cpp	Mon Mar 09 16:21:26 2015 +0100
@@ -5,13 +5,25 @@
 #include "mbed.h"
 #include "mbed_interface.h"
 #include "rtos.h"
-#include "EthernetInterface.h"
-#include "HTTPServer.h"
-#include "mbed_rpc.h"
-#include "USBHostMSD.h"
-#include "USBHostMouse.h"
-#include "USBHostKeyboard.h"
+
+#include "dm_board_config.h"
 
+#if defined(DM_BOARD_USE_ETHERNET)
+  #include "EthernetInterface.h"
+  #include "HTTPServer.h"
+  #include "mbed_rpc.h"
+#endif
+#if defined(DM_BOARD_USE_USB_DEVICE)
+  #include "USBMSD_RAMFS.h"
+  #include "USBSerial.h"
+  #include "USBKeyboard.h"
+  #include "AppUSBMouse.h"
+#endif
+#if defined(DM_BOARD_USE_USB_HOST)
+  #include "USBHostMSD.h"
+  #include "USBHostMouse.h"
+  #include "USBHostKeyboard.h"
+#endif
 #include "DMBoard.h"
 
 #include "AppLauncher.h"
@@ -33,31 +45,31 @@
  * Typedefs and defines
  *****************************************************************************/
 
-/* Number of colors in 565 mode */
-#define NUM_COLORS    65536
-/* Number of red colors in 565 mode */
-#define RED_COLORS    0x20
-/* Number of green colors in 565 mode */
-#define GREEN_COLORS  0x40
-/* Number of blue colors in 565 mode */
-#define BLUE_COLORS   0x20
-/* Black color, 565 mode */
-#define BLACK         0x0000
+/* Size of RAM file system exposed to PC as USB MassStorage */
+#define RAM_FS_SIZE (20*1024*1024)
 
 /******************************************************************************
  * Local variables
  *****************************************************************************/
 
-Mutex usbInitGuard;
-
 /******************************************************************************
  * Global variables
  *****************************************************************************/
 
-EthernetInterface eth;
-bool ethInitialized = false;
-bool ethUsingDHCP = true;
-bool haveUSBMSD = false;
+#if defined(DM_BOARD_USE_ETHERNET)
+  EthernetInterface eth;
+  bool ethInitialized = false;
+  bool ethUsingDHCP = true;
+#endif
+#if defined(DM_BOARD_USE_USB_HOST)
+  bool haveUSBMSD = false;
+#endif
+
+#if defined(DM_BOARD_USE_USB_DEVICE)
+  //#define DEMO_USB_DEVICE_MOUSE
+  //#define DEMO_USB_DEVICE_SERIAL
+  #define DEMO_USB_DEVICE_MSD
+#endif
 
 /******************************************************************************
  * Local functions
@@ -96,6 +108,7 @@
     RtcApp,
     USBStatusApp,
     DrawingApp,
+    USBMouseApp,
     CalibrationApp =  AppLauncher::CalibrationApp,
     Placeholder,
 } myapps_t;
@@ -131,6 +144,11 @@
       case DrawingApp:
           a = new AppDraw();
           break;
+#if defined(DM_BOARD_USE_USB_DEVICE) && defined(DEMO_USB_DEVICE_MOUSE)
+      case USBMouseApp:
+          a = new AppUSBMouse();
+          break;
+#endif
       default:
           break;
   }
@@ -156,7 +174,11 @@
     launcher.addImageButton(ColorPickerApp, "Color Picker", WHITE,  img_preferences_color, img_size_preferences_color);
     launcher.addImageButton(ImageViewerApp, "Image Viewer", WHITE,  img_multimedia_photo_manager, img_size_multimedia_photo_manager);
     launcher.addImageButton(StatusApp, "About", WHITE,  img_utilities_system_monitor, img_size_utilities_system_monitor);
+#if defined(DM_BOARD_USE_USB_DEVICE) && defined(DEMO_USB_DEVICE_MOUSE)
+    launcher.addImageButton(USBMouseApp, "USB Mouse", WHITE,  img_unetbootin, img_size_unetbootin);
+#else
     launcher.addImageButton(Placeholder, "USB Status", WHITE,  img_unetbootin, img_size_unetbootin);
+#endif
     launcher.addImageButton(RtcApp, "Clock", WHITE,  img_preferences_system_time, img_size_preferences_system_time);
       
     launcher.setAppCreatorFunc(launchApp);
@@ -173,40 +195,42 @@
 
 #endif //DM_BOARD_USE_DISPLAY
 
-
-#define NET_TASK_PREFIX  "[NET] "
+#if defined(DM_BOARD_USE_ETHERNET)
 
-void netTask(void const* args)
-{
-  RtosLog* log = DMBoard::instance().logger();
-  log->printf(NET_TASK_PREFIX"EthernetInterface Setting up...\r\n");
-  if(eth.init()!=0) {                             //for DHCP Server
-     //if(eth.init(IPAddress,NetMasks,Gateway)!=0) { //for Static IP Address
-     log->printf(NET_TASK_PREFIX"EthernetInterface Initialize Error \r\n");
-     mbed_die();
-  }
-  while (eth.connect() != 0) {
-     Thread::wait(1000);
-  }
+  #define NET_TASK_PREFIX  "[NET] "  
+  void netTask(void const* args)
+  {
+    RtosLog* log = DMBoard::instance().logger();
+    log->printf(NET_TASK_PREFIX"EthernetInterface Setting up...\r\n");
+    if(eth.init()!=0) {                             //for DHCP Server
+       //if(eth.init(IPAddress,NetMasks,Gateway)!=0) { //for Static IP Address
+       log->printf(NET_TASK_PREFIX"EthernetInterface Initialize Error \r\n");
+       mbed_die();
+    }
+    while (eth.connect() != 0) {
+       Thread::wait(1000);
+    }
+    
+    ethInitialized = true;
+    ethUsingDHCP = true;
+    
+    log->printf(NET_TASK_PREFIX"IP Address is %s\r\n", eth.getIPAddress());
+    log->printf(NET_TASK_PREFIX"NetMask is %s\r\n", eth.getNetworkMask());
+    log->printf(NET_TASK_PREFIX"Gateway Address is %s\r\n", eth.getGateway());
+    log->printf(NET_TASK_PREFIX"Ethernet Setup OK\r\n");
   
-  ethInitialized = true;
-  ethUsingDHCP = true;
-  
-  log->printf(NET_TASK_PREFIX"IP Address is %s\r\n", eth.getIPAddress());
-  log->printf(NET_TASK_PREFIX"NetMask is %s\r\n", eth.getNetworkMask());
-  log->printf(NET_TASK_PREFIX"Gateway Address is %s\r\n", eth.getGateway());
-  log->printf(NET_TASK_PREFIX"Ethernet Setup OK\r\n");
+    HTTPServerAddHandler<SimpleHandler>("/hello"); //Default handler
+    FSHandler::mount("/usb", "/");
+    HTTPServerAddHandler<FSHandler>("/");
+    //HTTPServerAddHandler<RPCHandler>("/rpc");
+    //lcd.locate(0,0);
+    //lcd.printf("%s",eth.getIPAddress());
+    HTTPServerStart(80);
+  }
+#endif //DM_BOARD_USE_ETHERNET
 
-  HTTPServerAddHandler<SimpleHandler>("/hello"); //Default handler
-  FSHandler::mount("/usb", "/");
-  HTTPServerAddHandler<FSHandler>("/");
-  //HTTPServerAddHandler<RPCHandler>("/rpc");
-  //lcd.locate(0,0);
-  //lcd.printf("%s",eth.getIPAddress());
-  HTTPServerStart(80);
-}
-
-static volatile int8_t mouse_button, mouse_x, mouse_y, mouse_z;
+#if defined(DM_BOARD_USE_USB_HOST)
+static volatile int mouse_button, mouse_x, mouse_y, mouse_z;
 static uint16_t cursor_x=0, cursor_y=0;
 void mouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z)
 {
@@ -228,7 +252,7 @@
             cursor_x += x;
         }
     }
-    y = y/8;
+
     if (y < 0) {
         if (cursor_y > -y) {
             cursor_y += y;
@@ -293,7 +317,7 @@
 };
 
 void prepareCursor(bool enable) {
-	//Chip_LCD_Cursor_Disable(LPC_LCD, 0);
+    //Chip_LCD_Cursor_Disable(LPC_LCD, 0);
     LPC_LCD->CRSR_CTRL = (CURSOR_NUM << 4);
     
     if (enable) {
@@ -347,9 +371,9 @@
 }
 
 
-#define USB_TASK_PREFIX  "[USB] "
-#define USB_CONNECTION_EVENT   (1<<4)
-void usbTask(void const* args)
+#define USBH_TASK_PREFIX  "[USBH] "
+#define USBH_CONNECTION_EVENT   (1<<4)
+void usbHostTask(void const* args)
 {
   bool msdConnected = false;
   bool keyboardConnected = false;
@@ -359,53 +383,53 @@
   USBHostKeyboard* keyboard = new USBHostKeyboard();
   USBHostMouse* mouse = new USBHostMouse();
   USBHost* host = USBHost::getHostInst();
-  host->signalOnConnections(Thread::gettid(), USB_CONNECTION_EVENT);
+  host->signalOnConnections(Thread::gettid(), USBH_CONNECTION_EVENT);
 
   RtosLog* log = DMBoard::instance().logger();
     
-  log->printf(USB_TASK_PREFIX"usbTask started\n");
+  log->printf(USBH_TASK_PREFIX"Started\n");
     
   prepareCursor(false);
 
   while (true) {
     // wait for connect/disconnect message from USBHost
-    Thread::signal_wait(USB_CONNECTION_EVENT);
-    log->printf(USB_TASK_PREFIX"got USB event\n");
+    Thread::signal_wait(USBH_CONNECTION_EVENT);
+    log->printf(USBH_TASK_PREFIX"got USB event\n");
       
     if (msd->connected()) {
       if (!msdConnected) {
         msdConnected = true;
         haveUSBMSD = true;
-        log->printf(USB_TASK_PREFIX"USB MassStorage Device - Connected\n");
+        log->printf(USBH_TASK_PREFIX"USB MassStorage Device - Connected\n");
       }
     } else {
       if (msdConnected) {
         msdConnected = false;
         haveUSBMSD = false;
-        log->printf(USB_TASK_PREFIX"USB MassStorage Device - Ejected\n");
+        log->printf(USBH_TASK_PREFIX"USB MassStorage Device - Ejected\n");
       }
       
       if (msd->connect()) {
         msdConnected = true;
         haveUSBMSD = true;
-        log->printf(USB_TASK_PREFIX"USB MassStorage Device - Connected\n");
+        log->printf(USBH_TASK_PREFIX"USB MassStorage Device - Connected\n");
       }      
     }
     
     if (keyboard->connected()) {
       if (!keyboardConnected) {
         keyboardConnected = true;
-        log->printf(USB_TASK_PREFIX"USB Keyboard - Connected\n");
+        log->printf(USBH_TASK_PREFIX"USB Keyboard - Connected\n");
         keyboard->attach(keyEvent);
       }
     } else {
       if (keyboardConnected) {
         keyboardConnected = false;
-        log->printf(USB_TASK_PREFIX"USB Keyboard - Ejected\n");
+        log->printf(USBH_TASK_PREFIX"USB Keyboard - Ejected\n");
       }
       if (keyboard->connect()) {
         keyboardConnected = true;
-        log->printf(USB_TASK_PREFIX"USB Keyboard - Connected\n");
+        log->printf(USBH_TASK_PREFIX"USB Keyboard - Connected\n");
         keyboard->attach(keyEvent);
       }
     }
@@ -413,7 +437,7 @@
     if (mouse->connected()) {
       if (!mouseConnected) {
         mouseConnected = true;
-        log->printf(USB_TASK_PREFIX"USB Mouse - Connected\n");
+        log->printf(USBH_TASK_PREFIX"USB Mouse - Connected\n");
         mouse->attachEvent(mouseEvent);
         prepareCursor(true);
       }
@@ -421,11 +445,11 @@
       if (mouseConnected) {
         prepareCursor(false);
         mouseConnected = false;
-        log->printf(USB_TASK_PREFIX"USB Mouse - Ejected\n");
+        log->printf(USBH_TASK_PREFIX"USB Mouse - Ejected\n");
       }
       if (mouse->connect()) {
         mouseConnected = true;
-        log->printf(USB_TASK_PREFIX"USB Mouse - Connected\n");
+        log->printf(USBH_TASK_PREFIX"USB Mouse - Connected\n");
         mouse->attachEvent(mouseEvent);
         prepareCursor(true);
       }
@@ -433,6 +457,170 @@
   }
 }
 
+#endif //DM_BOARD_USE_USB_HOST
+
+#if defined(DM_BOARD_USE_USB_DEVICE)
+
+  #define USBD_TASK_PREFIX  "[USBD] "
+  //#define USBD_CONNECTION_EVENT   (1<<4)
+  void usbDeviceTask(void const* args)
+  {
+    DMBoard* board = &DMBoard::instance();
+    RtosLog* log = board->logger();
+      
+    log->printf(USBD_TASK_PREFIX"Started\n");
+  
+    // Possibilities:
+    // - MassStorage
+    //    * RAM file system
+    //        a) For editing of settings or registry
+    //        b) To give user an html file with information
+    //        c) For BIOS updating
+    //    * Expose MCI file system
+    // - Serial
+    //    * Let RtosLogger use the USBDevice serial port instead to 
+    //      allow prints during debugging
+    // - Mouse
+    //    * Use the display as a "trackpad"
+    // - Keyboard
+    //    * Could be used by the RtosLogger (for fun)
+    //    * Write our logo in ASCII art (for fun)
+    // - Audio?
+    // - CDC?
+      
+#if defined(DEMO_USB_DEVICE_MSD)
+    // USBDevice: MassStorage - Expose a 5MB RAM file system
+    //
+    // Notes: 
+    //    * Copying the large file to the PC at ca 512Kbyte per second
+    //    * Copying a large file to the board at ca 800Kbyte per second
+    void* fsmem = malloc(RAM_FS_SIZE);
+    if (fsmem == NULL) {
+      log->printf("Failed to allocate memory for RAM file system\n");
+      mbed_die();
+    }
+    RAMFileSystem ramfs((uint32_t)fsmem, RAM_FS_SIZE, "ram");
+    USBMSD_RAMFS usbmsd(&ramfs);  
+    ramfs.format();
+    
+    // Create test file that user can modify
+    FILE* f = fopen("/ram/message.txt", "w");
+    if (f != NULL) {
+      fwrite("Hello World!\n", 1, 13, f);
+      fclose(f);
+    }
+    
+    // Create benchmark file
+    uint32_t benchSize = RAM_FS_SIZE - 1*1024*1024;
+    log->printf(USBD_TASK_PREFIX"Generating %dMB benchmarking file...\n", benchSize/(1024*1024));
+    char buff[512+1] = {0};
+    for (int i = 0; i < 512; i++) {
+      buff[i] = i;
+    }
+    f = fopen("/ram/bench.bin", "w");
+    if (f != NULL) {
+      for (int i = 0; i < (benchSize/512); i++) {
+        fwrite(buff, 1, 512, f);
+      }
+      fclose(f);
+    }
+    log->printf(USBD_TASK_PREFIX"Finished generating /ram/bench.bin\n");
+    
+    while (true) {
+      if (usbmsd.connect()) {
+        log->printf(USBD_TASK_PREFIX"USB MassStorage - Connected\n");
+        log->printf(USBD_TASK_PREFIX"Press USER button to disconnect USB MassStorage!\n");
+        while (!board->buttonPressed()) {
+          Thread::wait(50);
+        }
+        log->printf(USBD_TASK_PREFIX"User requested USB disconnect\n");
+        usbmsd.disconnect();
+        while (board->buttonPressed()) {
+          Thread::wait(50);
+        }
+        log->printf(USBD_TASK_PREFIX"USB disconnected\n");
+        
+        {
+          log->printf("Reading /ram/message.txt:\n");
+          f = fopen("/ram/message.txt", "r");
+          if (f != NULL) {
+            while (true) {
+              size_t num = fread(buff, 1, 512, f);
+              if (num > 0) {
+                buff[num] = '\0';
+                log->printf(buff);
+              } else {
+                break;
+              }
+            }
+            fclose(f);
+            log->printf("\nEOF\n");
+          } else {
+            log->printf("Failed to read /ram/message.txt\n");
+          }
+        }
+        
+        log->printf("\n"USBD_TASK_PREFIX"Press USER button to connect USB MassStorage again!\n");
+        while (!board->buttonPressed()) {
+          Thread::wait(50);
+        }
+        while (board->buttonPressed()) {
+          Thread::wait(50);
+        }
+        log->printf(USBD_TASK_PREFIX"User requested to connect USB again\n");
+      } 
+    }
+#endif // #if defined(DEMO_USB_DEVICE_MSD)
+    
+#if defined(DEMO_USB_DEVICE_SERIAL)
+    // USBDevice: Serial  (see http://developer.mbed.org/handbook/USBSerial)
+    // http://developer.mbed.org/questions/5872/User-Friendly-Installation-of-USBSerial-/
+    //
+    // Notes: 
+    //    * Works, but a reset (e.g. Alt-B) requires the USB cable to be ejected+inserted 
+    //      for the PC to see the COM port again. It does not help to restart the Terminal
+    //      program
+    //    * Unplugging the USB cable after the terminal program on the PC has connected
+    //      and then plugging it in again does not work. The terminal program will not see
+    //      the port until after a bord reset. 
+    //      Update: This seems to be the Terminal Program's fault. Restarting the terminal
+    //      program will make the port appear again.
+    //    * It is possible to have the cable connected before powering up as well as connecting
+    //      the cable after powering up.
+  #if defined(DM_BOARD_USE_USBSERIAL_IN_RTOSLOG)
+    int counter = 0;
+    while(true) {
+        Thread::wait(1000);
+        log->printf(USBD_TASK_PREFIX"Counter %3d\n", counter++);
+    }
+  #else
+    USBSerial s;
+    int counter = 0;
+    while(true) {
+        Thread::wait(1000);
+        s.printf(USBD_TASK_PREFIX"Counter %3d\n", counter++);
+    }
+  #endif
+#endif // #if defined(DEMO_USB_DEVICE_SERIAL)
+    
+#if defined(DEMO_USB_DEVICE_MOUSE)
+    // Notes: 
+    //    * If the USBMouse class is used after this then the USBKeyboard stops working.
+    //      if both are to be used then the USBMouseKeyboard class must be used instead.
+    USBKeyboard keyb;
+    while(true) {
+      while (!board->buttonPressed()) {
+        Thread::wait(50);
+      }
+      keyb.printf("Hello World!\n");
+      while (board->buttonPressed()) {
+        Thread::wait(50);
+      }
+    }
+#endif // #if defined(DEMO_USB_DEVICE_MOUSE)    
+  }
+#endif
+
 
 #define REGTEST "[REG] "
 static void testRegistry()
@@ -549,12 +737,18 @@
   Thread tAlive(aliveTask);
 #if defined(DM_BOARD_USE_DISPLAY)
   Thread tSwim(swimTask, NULL, osPriorityNormal, 8192);
-#endif  
+#endif
+#if defined(DM_BOARD_USE_ETHERNET)  
   Thread tNetwork(netTask, NULL, osPriorityNormal, 8192);
-  Thread tUSBHandler(usbTask, NULL, osPriorityNormal, 8192);
-  
+#endif
+#if defined(DM_BOARD_USE_USB_HOST)
+  Thread tUSBHandler(usbHostTask, NULL, osPriorityNormal, 8192);
+#elif defined(DM_BOARD_USE_USB_DEVICE)
+  Thread tUSBDev(usbDeviceTask, NULL, osPriorityNormal, 8192);
+#endif
+
   while(1) { 
-    Thread::wait(5000); 
+    Thread::wait(5000);
     time_t seconds = time(NULL);
     log->printf("Time: %s\n", ctime(&seconds));
   }