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

main.cpp

Committer:
embeddedartists
Date:
2015-01-07
Revision:
11:4830c7689843
Parent:
7:c8bef6a4b019
Child:
13:57e65aba9802

File content as of revision 11:4830c7689843:

/******************************************************************************
 * Includes
 *****************************************************************************/
 
#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 "DMBoard.h"

#include "AppLauncher.h"
#include "meas.h"

#include "AppSettings.h"
#include "AppTouchCalibration.h"
#include "AppColorPicker.h"
#include "AppImageViewer.h"
#include "AppSlideShow.h"
#include "image_data.h"


/******************************************************************************
 * 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

/******************************************************************************
 * Local variables
 *****************************************************************************/

Mutex usbInitGuard;

/******************************************************************************
 * Global variables
 *****************************************************************************/

EthernetInterface eth;
bool ethInitialized = false;
bool ethUsingDHCP = true;
bool haveUSBMSD = false;

/******************************************************************************
 * Local functions
 *****************************************************************************/

void aliveTask(void const* args)
{
  DMBoard* board = &DMBoard::instance();
    
  while(true)
  {
    board->setLED(DMBoard::Led4, false);
    board->setLED(DMBoard::Led1, true);
    Thread::wait(300);
    board->setLED(DMBoard::Led1, false);
    board->setLED(DMBoard::Led2, true);
    Thread::wait(300);
    board->setLED(DMBoard::Led2, false);
    board->setLED(DMBoard::Led3, true);
    Thread::wait(300);
    board->setLED(DMBoard::Led3, false);
    board->setLED(DMBoard::Led4, true);
    Thread::wait(300);
  }
}

/*
 * Reads the /message.txt file from the file system and prints the content
 */
void readMessageFile(const char* prefix)
{
  RtosLog* log = DMBoard::instance().logger();
  log->printf("%sTesting to read from USB\n", prefix);
    
  FILE * fp = fopen("/usb/message.txt", "r");
  
  if (fp != NULL) {
    uint8_t* buff = (uint8_t*)malloc(1024+1);
    if (buff == NULL) {
      log->printf("%sFailed to allocate memory for test\n", prefix);
    } else {
      int num = fread(buff, 1, 1024, fp);
      if (num <= 0) {
        log->printf("%sUnable to read file, got %d as result\n", prefix, num);
      } else if (num < 1024) {
        log->printf("%sHave read all %d bytes in the file:\n---\n", prefix, num);
        buff[num] = '\0';
        log->printf((const char*)buff);
        log->printf("\n---\n");
      } else {
        log->printf("%sHave read %d bytes with more to read\n", prefix, num);
      }
      free(buff);
    }
    fclose(fp);
  } else {
    log->printf("%sFILE == NULL\r\n", prefix);
  }
}


#if defined(DM_BOARD_USE_DISPLAY)

typedef enum {
    ColorPickerApp,
    ImageViewerApp,
    SlideshowApp,
    SettingsApp, 
    TouchTestApp,
    CalibrationApp =  AppLauncher::CalibrationApp,
    Placeholder,
} myapps_t;

static App* launchApp(uint32_t id)
{
  App* a = NULL;
  switch ((myapps_t)id) {
      case CalibrationApp:
          a = new AppTouchCalibration();
          break;
      case SettingsApp:
          a = new AppSettings();
          break;
      case ColorPickerApp:
          a = new AppColorPicker();
          break;
      case ImageViewerApp:
          a = new AppImageViewer();
          break;
      case SlideshowApp:
          a = new AppSlideShow();
          break;
      default:
          break;
  }
  return a;
}

#define SWIM_TASK_PREFIX  "[SWIM] "
void swimTask(void const* args)
{
  RtosLog* log = DMBoard::instance().logger();
    
  log->printf(SWIM_TASK_PREFIX"swimTask started\n");
  
  AppLauncher launcher;
    
    
  if (launcher.setup()) {
    launcher.addImageButton(SettingsApp,  img_preferences_desktop_applications, img_size_preferences_desktop_applications);
    launcher.addImageButton(Placeholder, img_bijiben, img_size_bijiben);
    launcher.addImageButton(SlideshowApp, img_multimedia_photo_manager, img_size_multimedia_photo_manager);
    //launcher.addImageButton(TouchGFXApp,  "TouchGFX");
    //launcher.addImageButton(EmWinApp,     "emWin");
    launcher.addImageButton(ColorPickerApp,  img_preferences_color, img_size_preferences_color);
    launcher.addImageButton(ImageViewerApp,  img_multimedia_photo_manager, img_size_multimedia_photo_manager);
    launcher.addImageButton(Placeholder,  img_help_info, img_size_help_info);
    launcher.addImageButton(Placeholder,  img_unetbootin, img_size_unetbootin);
      
    launcher.setAppCreatorFunc(launchApp);
    log->printf(SWIM_TASK_PREFIX"Starting menu system\n");
    launcher.runToCompletion();
    launcher.teardown();
  } else {
    log->printf(SWIM_TASK_PREFIX"Failed to prepare menu system\n");
  }
  
  // Should never end up here  
  mbed_die();
}

#endif //DM_BOARD_USE_DISPLAY

#define MSD_TASK_PREFIX  "[MSD] "

void msdTask(void const* args)
{    
  usbInitGuard.lock();
  USBHostMSD* msd = new USBHostMSD("usb");
  usbInitGuard.unlock();
  USBHost* host = USBHost::getHostInst();
  RtosLog* log = DMBoard::instance().logger();
    
  log->printf(MSD_TASK_PREFIX"msdTask started\n");
  
    while(1) {
        
        log->printf(MSD_TASK_PREFIX"Attempting to connect...\n");
      
        // try to connect a MSD device
        bool connected = false;
        do {
            usbInitGuard.lock();
            connected = msd->connect();
            usbInitGuard.unlock();
            if (!connected) {
                //log->printf(MSD_TASK_PREFIX"Failed to connect, waiting and trying again!\n");
                Thread::wait(500);
                //DMBoard::instance().buzzer(440, 100);
            }
        } while(!connected);
        
        log->printf(MSD_TASK_PREFIX"Connected!\n");
        haveUSBMSD = true;

        // read a file
        //readMessageFile(MSD_TASK_PREFIX);
        
        // if/when the device is disconnected, we try to connect it again
        while(1) {
            
            Thread::wait(500);
        
            // if device disconnected, try to connect again
            if (!msd->connected()) {
                break;
            }
        }
        haveUSBMSD = false;
        log->printf(MSD_TASK_PREFIX"Disconnected\n");
    }
}

#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");

  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 uint8_t mouse_button, mouse_x, mouse_y, mouse_z;
void mouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z)
{
    mouse_button = buttons;
    mouse_x = x;
    mouse_y = y;
    mouse_z = z;
}

#define MOUSE_TASK_PREFIX  "[MOUSE] "

void mouseTask(void const* args)
{
    usbInitGuard.lock();
    USBHostMouse* mouse = new USBHostMouse();
    usbInitGuard.unlock();
    RtosLog* log = DMBoard::instance().logger();
    
    log->printf(MOUSE_TASK_PREFIX"mouseTask started\n");
  
    while(1) {
        
        log->printf(MOUSE_TASK_PREFIX"Attempting to connect...\n");
      
        // try to connect a mouse
        bool connected = false;
        do {
            usbInitGuard.lock();
            connected = mouse->connect();
            usbInitGuard.unlock();
            if (!connected) {
                //log->printf(MOUSE_TASK_PREFIX"Failed to connect, waiting and trying again!\n");
                Thread::wait(500);
            }
        } while(!connected);
        
        log->printf(MOUSE_TASK_PREFIX"Connected!\n");
        mouse->attachEvent(mouseEvent);

        while(mouse->connected()) {
            log->printf(MOUSE_TASK_PREFIX"Buttons: 0x%02x, X %3d, Y %3d, Z %3d\n", mouse_button, mouse_x, mouse_y, mouse_z);
            Thread::wait(500);
        }

        log->printf(MOUSE_TASK_PREFIX"Disconnected\n");
    }
}

#define CIRCBUFF_SIZE 256
static uint8_t circbuff[CIRCBUFF_SIZE];
static uint32_t circbuff_read = 0;
static uint32_t circbuff_write = 0;
void keyEvent(uint8_t key)
{
    circbuff[circbuff_write%CIRCBUFF_SIZE] = key;
    circbuff_write++;
}

#define KEY_TASK_PREFIX  "[KEY] "

void keyTask(void const* args)
{
    usbInitGuard.lock();
    USBHostKeyboard* keyboard = new USBHostKeyboard();
    usbInitGuard.unlock();
 
    RtosLog* log = DMBoard::instance().logger();
    uint8_t buff[10+1] = {0};
    int pos;
    
    log->printf(KEY_TASK_PREFIX"keyTask started\n");
  
    while(1) {
        
        log->printf(KEY_TASK_PREFIX"Attempting to connect...\n");
      
        // try to connect a keyboard
        bool connected = false;
        do {
            usbInitGuard.lock();
            connected = keyboard->connect();
            usbInitGuard.unlock();
            if (!connected) {
                //log->printf(KEY_TASK_PREFIX"Failed to connect, waiting and trying again!\n");
                Thread::wait(1000);
            }
        } while(!connected);
        
        log->printf(KEY_TASK_PREFIX"Connected!\n");
        keyboard->attach(keyEvent);

        while(keyboard->connected()) {            
            for (pos = 0; pos < 10; pos++) {
                if (circbuff_read < circbuff_write) {
                    buff[pos++] = circbuff[circbuff_read%CIRCBUFF_SIZE];
                    circbuff_read++;
                } else {
                    break;
                }
            }
            if (pos > 0) {
                log->printf(KEY_TASK_PREFIX"%s\n", buff);
            }
            Thread::wait(20);
        }

        log->printf(KEY_TASK_PREFIX"Disconnected\n");
    }
}

/******************************************************************************
 * Main function
 *****************************************************************************/
int main()
{
  DMBoard::BoardError err;
  DMBoard* board = &DMBoard::instance();
  RtosLog* log = board->logger();
  err = board->init();
  if (err != DMBoard::Ok) {
    log->printf("Failed to initialize the board, got error %d\r\n", err);
    mbed_die();
  }
  
  board->buzzer(440,50);
  wait_ms(30);
  board->buzzer(660,50);
  wait_ms(30);
  board->buzzer(440,50);
  
  log->printf("\n\n---\nMulti-threaded demo\nBuilt: " __DATE__ " at " __TIME__ "\n\n");
  
  //log->printf("Press button to start...\r\n");
  //while(!board->buttonPressed());
  //while(board->buttonPressed());
  
  Thread tAlive(aliveTask);
#if defined(DM_BOARD_USE_DISPLAY)
  Thread tSwim(swimTask, NULL, osPriorityNormal, 8192);
#endif  
  Thread tMemstick(msdTask, NULL, osPriorityNormal, 8192);
  Thread tNetwork(netTask, NULL, osPriorityNormal, 8192);
  Thread tMouse(mouseTask);
  Thread tKeyboard(keyTask);
  
  while(1) { wait(5); }
}