Chris Womack / sampleGUI

Dependencies:   DMBasicGUI DMSupport

Fork of lpc4088_displaymodule_shipped_demo by Embedded Artists

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002  * Includes
00003  *****************************************************************************/
00004  
00005 #include "mbed.h"
00006 #include "mbed_interface.h"
00007 #include "rtos.h"
00008 #include "EthernetInterface.h"
00009 #include "HTTPServer.h"
00010 #include "mbed_rpc.h"
00011 #include "USBHostMSD.h"
00012 #include "USBHostMouse.h"
00013 #include "USBHostKeyboard.h"
00014 
00015 #include "DMBoard.h"
00016 
00017 #include "AppLauncherSpecial.h"
00018 #include "meas.h"
00019 
00020 #include "AppNetworkSettings.h"
00021 #include "AppStatus.h"
00022 #include "AppTouchCalibration.h"
00023 #include "AppColorPicker.h"
00024 #include "AppImageViewer.h"
00025 #include "AppSlideShow.h"
00026 #include "AppRTCSettings.h"
00027 #include "AppUSBStatus.h"
00028 #include "AppDraw.h"
00029 #include "image_data.h"
00030 #include "Resource.h"
00031 
00032 #include "lpc_swim.h"
00033 #include "lpc_helvr10.h"
00034 #include "lpc_winfreesystem14x16.h"
00035 
00036 /******************************************************************************
00037  * Typedefs and defines
00038  *****************************************************************************/
00039 
00040 typedef enum {
00041     Res_ok,
00042     Res_cancel,
00043     Res_arrow_up,
00044     Res_arrow_down,
00045     Res_arrow_left,
00046     Res_arrow_right,
00047     Res_repeat,
00048     Res_digits,
00049     Res_background,
00050     Res_about,
00051     Res_ColorPickerApp,
00052     Res_ImageViewerApp,
00053     Res_SlideshowApp,
00054     Res_SettingsApp, 
00055     Res_StatusApp, 
00056     //Res_TouchTestApp,
00057     Res_RtcApp,
00058     Res_USBStatusApp,
00059     Res_DrawingApp,
00060     //Res_CalibrationApp,
00061     NumResources,
00062 } resorces_t;
00063 
00064 /******************************************************************************
00065  * Local variables
00066  *****************************************************************************/
00067 
00068 static Resource* RESOURCES[NumResources] = {0};
00069 
00070 /******************************************************************************
00071  * Global variables
00072  *****************************************************************************/
00073 
00074 EthernetInterface eth;
00075 bool ethInitialized = false;
00076 bool ethUsingDHCP = true;
00077 bool haveUSBMSD = false;
00078 
00079 /******************************************************************************
00080  * Local functions
00081  *****************************************************************************/
00082 
00083 static void prepareResources()
00084 {
00085   Display* disp = DMBoard::instance().display();
00086   
00087   // select which images to use based on display size
00088   if (disp->width() == 480) {
00089     RESOURCES[Res_ok         ] = new Resource(img_480x272_ok,          img_size_480x272_ok, 40, 40);
00090     RESOURCES[Res_cancel     ] = new Resource(img_480x272_cancel,      img_size_480x272_cancel, 40, 40);
00091     RESOURCES[Res_arrow_up   ] = new Resource(img_480x272_arrow_up,    img_size_480x272_arrow_up,    52, 52);
00092     RESOURCES[Res_arrow_down ] = new Resource(img_480x272_arrow_down,  img_size_480x272_arrow_down,  52, 52);
00093     RESOURCES[Res_arrow_left ] = new Resource(img_480x272_arrow_left,  img_size_480x272_arrow_left,  52, 52);
00094     RESOURCES[Res_arrow_right] = new Resource(img_480x272_arrow_right, img_size_480x272_arrow_right, 52, 52);
00095     RESOURCES[Res_repeat     ] = new Resource(img_480x272_repeat,      img_size_480x272_repeat, 40, 40);
00096     RESOURCES[Res_digits     ] = new Resource(img_numbers_16b,         img_size_numbers_16b, 13, 242);
00097       
00098     RESOURCES[Res_ColorPickerApp] = new Resource(img_480x272_super_mono_3d_part2_19, img_size_480x272_super_mono_3d_part2_19, 64, 64);
00099     RESOURCES[Res_ImageViewerApp] = new Resource(img_480x272_super_mono_3d_48,       img_size_480x272_super_mono_3d_48,       64, 64);
00100     RESOURCES[Res_SlideshowApp  ] = new Resource(img_480x272_super_mono_3d_87,       img_size_480x272_super_mono_3d_87,       64, 64);
00101     RESOURCES[Res_SettingsApp   ] = new Resource(img_480x272_super_mono_3d_93,       img_size_480x272_super_mono_3d_93,       64, 64);
00102     RESOURCES[Res_StatusApp     ] = new Resource(img_480x272_super_mono_3d_part2_68, img_size_480x272_super_mono_3d_part2_68, 64, 64);
00103     RESOURCES[Res_RtcApp        ] = new Resource(img_480x272_super_mono_3d_02,       img_size_480x272_super_mono_3d_02,       64, 64);
00104     RESOURCES[Res_USBStatusApp  ] = new Resource(img_480x272_super_mono_3d_57,       img_size_480x272_super_mono_3d_57,       64, 64);
00105     RESOURCES[Res_DrawingApp    ] = new Resource(img_480x272_super_mono_3d_22,       img_size_480x272_super_mono_3d_22,       64, 64);
00106     RESOURCES[Res_background    ] = new Resource(img_480x272_background,             img_size_480x272_background, 364, 57);
00107     RESOURCES[Res_about         ] = new Resource(img_480x272_about,                  img_size_480x272_about, 240, 245);
00108     
00109     swim_set_default_font(&font_helvr10);
00110   } else {
00111     RESOURCES[Res_ok         ] = new Resource(img_800x480_ok,          img_size_800x480_ok,       64, 64);
00112     RESOURCES[Res_cancel     ] = new Resource(img_800x480_cancel,      img_size_800x480_cancel,   64, 64);
00113     RESOURCES[Res_arrow_up   ] = new Resource("/qspi/800x480_arrow_up.raw",                     128, 128);
00114     RESOURCES[Res_arrow_down ] = new Resource("/qspi/800x480_arrow_down.raw",                   128, 128);
00115     RESOURCES[Res_arrow_left ] = new Resource("/qspi/800x480_arrow_left.raw",                   128, 128);
00116     RESOURCES[Res_arrow_right] = new Resource("/qspi/800x480_arrow_right.raw",                  128, 128);
00117     RESOURCES[Res_repeat     ] = new Resource(img_800x480_repeat,      img_size_800x480_repeat,  64,  64);
00118     RESOURCES[Res_digits     ] = new Resource(img_numbers_20b,         img_size_numbers_20b,     16, 322);
00119       
00120     RESOURCES[Res_ColorPickerApp] = new Resource("/qspi/800x480_super-mono-3d-part2-19.raw",  64,  64);
00121     RESOURCES[Res_ImageViewerApp] = new Resource("/qspi/800x480_super-mono-3d-48.raw",        64,  64);
00122     RESOURCES[Res_SlideshowApp  ] = new Resource("/qspi/800x480_super-mono-3d-87.raw",        64,  64);
00123     RESOURCES[Res_SettingsApp   ] = new Resource("/qspi/800x480_super-mono-3d-93.raw",        64,  64);
00124     RESOURCES[Res_StatusApp     ] = new Resource("/qspi/800x480_super-mono-3d-part2-68.raw",  64,  64);
00125     RESOURCES[Res_RtcApp        ] = new Resource("/qspi/800x480_super-mono-3d-02.raw",        64,  64);
00126     RESOURCES[Res_USBStatusApp  ] = new Resource("/qspi/800x480_super-mono-3d-57.raw",        64,  64);
00127     RESOURCES[Res_DrawingApp    ] = new Resource("/qspi/800x480_super-mono-3d-22.raw",        64,  64);
00128     RESOURCES[Res_background    ] = new Resource("/qspi/800x480_background.raw",             548,  80);
00129     RESOURCES[Res_about         ] = new Resource("/qspi/800x480_about.raw",                  420, 429);
00130       
00131     swim_set_default_font(&font_winfreesys14x16);
00132   }
00133 }
00134 
00135 void aliveTask(void const* args)
00136 {
00137   DMBoard* board = &DMBoard::instance();
00138     
00139   while(true)
00140   {
00141     board->setLED(DMBoard::Led4, false);
00142     board->setLED(DMBoard::Led1, true);
00143     Thread::wait(300);
00144     board->setLED(DMBoard::Led1, false);
00145     board->setLED(DMBoard::Led2, true);
00146     Thread::wait(300);
00147     board->setLED(DMBoard::Led2, false);
00148     board->setLED(DMBoard::Led3, true);
00149     Thread::wait(300);
00150     board->setLED(DMBoard::Led3, false);
00151     board->setLED(DMBoard::Led4, true);
00152     Thread::wait(300);
00153   }
00154 }
00155 
00156 #if defined(DM_BOARD_USE_DISPLAY)
00157 
00158 typedef enum {
00159     ColorPickerApp,
00160     ImageViewerApp,
00161     SlideshowApp,
00162     SettingsApp, 
00163     StatusApp, 
00164     TouchTestApp,
00165     RtcApp,
00166     USBStatusApp,
00167     DrawingApp,
00168     CalibrationApp =  AppLauncherSpecial::CalibrationApp,
00169     Placeholder,
00170 } myapps_t;
00171 
00172 static App* launchApp(uint32_t id)
00173 {
00174   App* a = NULL;
00175   switch ((myapps_t)id) {
00176       case CalibrationApp:
00177           a = new AppTouchCalibration();
00178          ((AppTouchCalibration*)a)->addResource(AppTouchCalibration::Resource_Ok_button, RESOURCES[Res_ok]);
00179           break;
00180       case SettingsApp:
00181           a = new AppNetworkSettings();
00182          ((AppNetworkSettings*)a)->addResource(AppNetworkSettings::Resource_Ok_button, RESOURCES[Res_ok]);
00183          ((AppNetworkSettings*)a)->addResource(AppNetworkSettings::Resource_Cancel_button, RESOURCES[Res_cancel]);
00184          ((AppNetworkSettings*)a)->addResource(AppNetworkSettings::Resource_ArrowUp_button, RESOURCES[Res_arrow_up]);
00185          ((AppNetworkSettings*)a)->addResource(AppNetworkSettings::Resource_ArrowDown_button, RESOURCES[Res_arrow_down]);
00186          ((AppNetworkSettings*)a)->addResource(AppNetworkSettings::Resource_ArrowLeft_button, RESOURCES[Res_arrow_left]);
00187          ((AppNetworkSettings*)a)->addResource(AppNetworkSettings::Resource_ArrowRight_button, RESOURCES[Res_arrow_right]);
00188          ((AppNetworkSettings*)a)->addResource(AppNetworkSettings::Resource_Digits, RESOURCES[Res_digits]);
00189           break;
00190       case ColorPickerApp:
00191           a = new AppColorPicker();
00192          ((AppColorPicker*)a)->addResource(AppColorPicker::Resource_Ok_button, RESOURCES[Res_ok]);
00193           break;
00194       case ImageViewerApp:
00195           a = new AppImageViewer();
00196          ((AppImageViewer*)a)->addResource(AppImageViewer::Resource_Ok_button, RESOURCES[Res_ok]);
00197           break;
00198       case SlideshowApp:
00199           {
00200               // Select folder for the slideshow based on display size
00201               Display* d = DMBoard::instance().display();
00202               if (d->width() == 480) {
00203                   a = new AppSlideShow("/qspi/slides.txt", "/qspi/480x272", 0, 0);
00204               } else {
00205                   a = new AppSlideShow("/qspi/slides.txt", "/qspi/800x480", 0, 0);
00206               }
00207               
00208              ((AppSlideShow*)a)->addResource(AppSlideShow::Resource_Ok_button, RESOURCES[Res_ok]);
00209              ((AppSlideShow*)a)->addResource(AppSlideShow::Resource_Repeat_button, RESOURCES[Res_repeat]);
00210           }
00211           break;
00212       case StatusApp:
00213           a = new AppStatus();
00214          ((AppStatus*)a)->addResource(AppStatus::Resource_Ok_button, RESOURCES[Res_ok]);
00215          ((AppStatus*)a)->addResource(AppStatus::Resource_About_image, RESOURCES[Res_about]);
00216           break;
00217       case RtcApp:
00218           a = new AppRTCSettings();
00219          ((AppRTCSettings*)a)->addResource(AppRTCSettings::Resource_Ok_button, RESOURCES[Res_ok]);
00220          ((AppRTCSettings*)a)->addResource(AppRTCSettings::Resource_Cancel_button, RESOURCES[Res_cancel]);
00221          ((AppRTCSettings*)a)->addResource(AppRTCSettings::Resource_ArrowUp_button, RESOURCES[Res_arrow_up]);
00222          ((AppRTCSettings*)a)->addResource(AppRTCSettings::Resource_ArrowDown_button, RESOURCES[Res_arrow_down]);
00223          ((AppRTCSettings*)a)->addResource(AppRTCSettings::Resource_ArrowLeft_button, RESOURCES[Res_arrow_left]);
00224          ((AppRTCSettings*)a)->addResource(AppRTCSettings::Resource_ArrowRight_button, RESOURCES[Res_arrow_right]);
00225          ((AppRTCSettings*)a)->addResource(AppRTCSettings::Resource_Digits, RESOURCES[Res_digits]);
00226           break;
00227       case USBStatusApp:
00228           a = new AppUSBStatus();
00229          ((AppUSBStatus*)a)->addResource(AppUSBStatus::Resource_Ok_button, RESOURCES[Res_ok]);
00230           break;
00231       case DrawingApp:
00232           a = new AppDraw();
00233          ((AppDraw*)a)->addResource(AppDraw::Resource_Ok_button, RESOURCES[Res_ok]);
00234           break;
00235       default:
00236           break;
00237   }
00238   return a;
00239 }
00240 
00241 #define SWIM_TASK_PREFIX  "[SWIM] "
00242 void swimTask(void const* args)
00243 {
00244   RtosLog* log = DMBoard::instance().logger();
00245   Display* disp = DMBoard::instance().display();
00246     
00247   log->printf(SWIM_TASK_PREFIX"swimTask started\n");
00248   
00249   int iconsize = (disp->width()==480) ? 64 : 128;
00250   AppLauncherSpecial launcher(iconsize, iconsize);
00251   launcher.addResource(AppLauncherSpecial::Resource_Background, RESOURCES[Res_background]);
00252     
00253   if (launcher.setup()) {
00254     launcher.addImageButton(SettingsApp,    "Network",      0x2d5c, RESOURCES[Res_SettingsApp   ]);
00255     launcher.addImageButton(DrawingApp,     "Drawing",      0x2d5c, RESOURCES[Res_DrawingApp    ]);
00256     launcher.addImageButton(SlideshowApp,   "Slideshow",    0x2d5c, RESOURCES[Res_SlideshowApp  ]);
00257     launcher.addImageButton(ColorPickerApp, "Color Picker", 0x2d5c, RESOURCES[Res_ColorPickerApp]);
00258     launcher.addImageButton(ImageViewerApp, "Image Viewer", 0x2d5c, RESOURCES[Res_ImageViewerApp]);
00259     launcher.addImageButton(StatusApp,      "About",        0x2d5c, RESOURCES[Res_StatusApp     ]);
00260     launcher.addImageButton(USBStatusApp,   "USB Status",   0x2d5c, RESOURCES[Res_USBStatusApp  ]);
00261     launcher.addImageButton(RtcApp,         "Clock",        0x2d5c, RESOURCES[Res_RtcApp        ]);
00262       
00263     launcher.setAppCreatorFunc(launchApp);
00264     log->printf(SWIM_TASK_PREFIX"Starting menu system\n");
00265     launcher.runToCompletion();
00266     launcher.teardown();
00267   } else {
00268     log->printf(SWIM_TASK_PREFIX"Failed to prepare menu system\n");
00269   }
00270   
00271   // Should never end up here  
00272   mbed_die();
00273 }
00274 
00275 #endif //DM_BOARD_USE_DISPLAY
00276 
00277 
00278 #define NET_TASK_PREFIX  "[NET] "
00279 
00280 void netTask(void const* args)
00281 {
00282   RtosLog* log = DMBoard::instance().logger();
00283   log->printf(NET_TASK_PREFIX"EthernetInterface Setting up...\r\n");
00284   if(eth.init()!=0) {                             //for DHCP Server
00285      //if(eth.init(IPAddress,NetMasks,Gateway)!=0) { //for Static IP Address
00286      log->printf(NET_TASK_PREFIX"EthernetInterface Initialize Error \r\n");
00287      mbed_die();
00288   }
00289   while (eth.connect() != 0) {
00290      Thread::wait(1000);
00291   }
00292   
00293   ethInitialized = true;
00294   ethUsingDHCP = true;
00295   
00296   log->printf(NET_TASK_PREFIX"IP Address is %s\r\n", eth.getIPAddress());
00297   log->printf(NET_TASK_PREFIX"NetMask is %s\r\n", eth.getNetworkMask());
00298   log->printf(NET_TASK_PREFIX"Gateway Address is %s\r\n", eth.getGateway());
00299   log->printf(NET_TASK_PREFIX"Ethernet Setup OK\r\n");
00300 
00301   HTTPServerAddHandler<SimpleHandler>("/hello"); //Default handler
00302   FSHandler::mount("/usb", "/");
00303   HTTPServerAddHandler<FSHandler>("/");
00304   //HTTPServerAddHandler<RPCHandler>("/rpc");
00305   //lcd.locate(0,0);
00306   //lcd.printf("%s",eth.getIPAddress());
00307   HTTPServerStart(80);
00308 }
00309 
00310 static volatile int8_t mouse_button, mouse_x, mouse_y, mouse_z;
00311 static uint16_t cursor_x=0, cursor_y=0, mouse_max_x = 0, mouse_max_y = 0;
00312 void mouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z)
00313 {
00314     mouse_button = buttons;
00315     mouse_x = x;
00316     mouse_y = y;
00317     mouse_z = z;
00318     
00319     if (mouse_max_x == 0) {
00320         mouse_max_x = DMBoard::instance().display()->width() - 1;
00321     }
00322     if (mouse_max_y == 0) {
00323         mouse_max_y = DMBoard::instance().display()->height() - 1;
00324     }
00325     
00326     if (x < 0) {
00327         if (cursor_x > -x) {
00328             cursor_x += x;
00329         } else {
00330             cursor_x = 0;
00331         }
00332     } else {
00333         if ((cursor_x + x) > mouse_max_x) {
00334             cursor_x = mouse_max_x;
00335         } else {
00336             cursor_x += x;
00337         }
00338     }
00339     y = y/8;
00340     if (y < 0) {
00341         if (cursor_y > -y) {
00342             cursor_y += y;
00343         } else {
00344             cursor_y = 0;
00345         }
00346     } else {
00347         if ((cursor_y + y) > mouse_max_y) {
00348             cursor_y = mouse_max_y;
00349         } else {
00350             cursor_y += y;
00351         }
00352     }
00353     
00354     //Chip_LCD_Cursor_SetPos(LPC_LCD, cursor_x, cursor_y);
00355     LPC_LCD->CRSR_XY = (cursor_x & 0x3FF) | ((cursor_y & 0x3FF) << 16);
00356 }
00357 
00358 #define LCD_CURSOR_32x32 0
00359 #define LCD_CURSOR_64x64 1
00360 #define CURSOR_SIZE  LCD_CURSOR_32x32
00361 #define CURSOR_H_SIZE 32
00362 #define CURSOR_V_SIZE 32
00363 #define CURSOR_NUM   0    
00364 #define CURSOR_H_OFS (10)
00365 #define CURSOR_V_OFS (6)
00366 
00367 const unsigned char __attribute__ ((aligned(4))) Cursor[(CURSOR_H_SIZE / 4) * CURSOR_V_SIZE] =
00368 {
00369     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00370     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00371     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00372     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00373     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00374     0xAA, 0xAA, 0xAA, 0xFA, 0xAA, 0xAA, 0xAA, 0xAA,
00375     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00376     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00377     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00378     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00379     0xAA, 0xAA, 0xAB, 0xFF, 0xEA, 0xAA, 0xAA, 0xAA,
00380     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xAA, 0xAA, 0xAA,
00381     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00382     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00383     0xAA, 0xAB, 0xFB, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00384     0xAA, 0xAB, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00385     0xAA, 0xAB, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00386     0xAA, 0xAA, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00387     0xAA, 0xAA, 0xBF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00388     0xAA, 0xAA, 0xBF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00389     0xAA, 0xAA, 0xAF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00390     0xAA, 0xAA, 0xAF, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00391     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00392     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00393     0xAA, 0xAA, 0xAA, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00394     0xAA, 0xAA, 0xAA, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00395     0xAA, 0xAA, 0xAA, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00396     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00397     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00398     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00399     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00400     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
00401 };
00402 
00403 void prepareCursor(bool enable) {
00404     //Chip_LCD_Cursor_Disable(LPC_LCD, 0);
00405     LPC_LCD->CRSR_CTRL = (CURSOR_NUM << 4);
00406     
00407     if (enable) {
00408         //Chip_LCD_Cursor_Config(LPC_LCD, LCD_CURSOR_32x32, true);
00409         LPC_LCD->CRSR_CFG = ((true ? 1 : 0) << 1) | CURSOR_SIZE;
00410         
00411         //Chip_LCD_Cursor_WriteImage(LPC_LCD, 0, (void *) Cursor);
00412         {
00413             int i, j;
00414             uint32_t *fifoptr, *crsr_ptr = (uint32_t *) Cursor;
00415 
00416             /* Check if Cursor Size was configured as 32x32 or 64x64*/
00417             if (CURSOR_SIZE == LCD_CURSOR_32x32) {
00418                 i = CURSOR_NUM * 64;
00419                 j = i + 64;
00420             }
00421             else {
00422                 i = 0;
00423                 j = 256;
00424             }
00425             fifoptr = (uint32_t *) &(LPC_LCD->CRSR_IMG[0]);
00426 
00427             /* Copy Cursor Image content to FIFO */
00428             for (; i < j; i++) {
00429 
00430                 *fifoptr = *crsr_ptr;
00431                 crsr_ptr++;
00432                 fifoptr++;
00433             }
00434         }
00435         
00436         //Chip_LCD_Cursor_SetClip(LPC_LCD, CURSOR_H_OFS, CURSOR_V_OFS);
00437         LPC_LCD->CRSR_CLIP = (CURSOR_H_OFS & 0x3F) | ((CURSOR_V_OFS & 0x3F) << 8);
00438         
00439         //Chip_LCD_Cursor_SetPos(LPC_LCD, cursor_x, cursor_y);
00440         
00441         //Chip_LCD_Cursor_Enable(LPC_LCD, 0);
00442         LPC_LCD->CRSR_CTRL = (CURSOR_NUM << 4) | 1;
00443     }
00444 }
00445 
00446 
00447 #define CIRCBUFF_SIZE 256
00448 static volatile uint8_t circbuff[CIRCBUFF_SIZE];
00449 static volatile uint32_t circbuff_read = 0;
00450 static uint32_t circbuff_write = 0;
00451 void keyEvent(uint8_t key)
00452 {
00453     circbuff[circbuff_write%CIRCBUFF_SIZE] = key;
00454     circbuff_write++;
00455 }
00456 
00457 bool msdConnected = false;
00458 bool keyboardConnected = false;
00459 bool mouseConnected = false;
00460 
00461 #define USB_TASK_PREFIX  "[USB] "
00462 #define USB_CONNECTION_EVENT   (1<<4)
00463 void usbTask(void const* args)
00464 {
00465   USBHostMSD* msd = new USBHostMSD("usb");
00466   USBHostKeyboard* keyboard = new USBHostKeyboard();
00467   USBHostMouse* mouse = new USBHostMouse();
00468   USBHost* host = USBHost::getHostInst();
00469   host->signalOnConnections(Thread::gettid(), USB_CONNECTION_EVENT);
00470 
00471   RtosLog* log = DMBoard::instance().logger();
00472     
00473   log->printf(USB_TASK_PREFIX"usbTask started\n");
00474     
00475   prepareCursor(false);
00476 
00477   while (true) {
00478     // wait for connect/disconnect message from USBHost
00479     Thread::signal_wait(USB_CONNECTION_EVENT);
00480     log->printf(USB_TASK_PREFIX"got USB event\n");
00481       
00482     if (msd->connected()) {
00483       if (!msdConnected) {
00484         msdConnected = true;
00485         haveUSBMSD = true;
00486         log->printf(USB_TASK_PREFIX"USB MassStorage Device - Connected\n");
00487       }
00488     } else {
00489       if (msdConnected) {
00490         msdConnected = false;
00491         haveUSBMSD = false;
00492         log->printf(USB_TASK_PREFIX"USB MassStorage Device - Ejected\n");
00493       }
00494       
00495       if (msd->connect()) {
00496         msdConnected = true;
00497         haveUSBMSD = true;
00498         log->printf(USB_TASK_PREFIX"USB MassStorage Device - Connected\n");
00499       }      
00500     }
00501     
00502     if (keyboard->connected()) {
00503       if (!keyboardConnected) {
00504         keyboardConnected = true;
00505         log->printf(USB_TASK_PREFIX"USB Keyboard - Connected\n");
00506         keyboard->attach(keyEvent);
00507       }
00508     } else {
00509       if (keyboardConnected) {
00510         keyboardConnected = false;
00511         log->printf(USB_TASK_PREFIX"USB Keyboard - Ejected\n");
00512       }
00513       if (keyboard->connect()) {
00514         keyboardConnected = true;
00515         log->printf(USB_TASK_PREFIX"USB Keyboard - Connected\n");
00516         keyboard->attach(keyEvent);
00517       }
00518     }
00519     
00520     if (mouse->connected()) {
00521       if (!mouseConnected) {
00522         mouseConnected = true;
00523         log->printf(USB_TASK_PREFIX"USB Mouse - Connected\n");
00524         mouse->attachEvent(mouseEvent);
00525         prepareCursor(true);
00526       }
00527     } else {
00528       if (mouseConnected) {
00529         prepareCursor(false);
00530         mouseConnected = false;
00531         log->printf(USB_TASK_PREFIX"USB Mouse - Ejected\n");
00532       }
00533       if (mouse->connect()) {
00534         mouseConnected = true;
00535         log->printf(USB_TASK_PREFIX"USB Mouse - Connected\n");
00536         mouse->attachEvent(mouseEvent);
00537         prepareCursor(true);
00538       }
00539     }
00540   }
00541 }
00542 
00543 
00544 
00545 /******************************************************************************
00546  * Main function
00547  *****************************************************************************/
00548 int main()
00549 {
00550   DMBoard::BoardError err;
00551   DMBoard* board = &DMBoard::instance();
00552   RtosLog* log = board->logger();
00553   err = board->init();
00554   if (err != DMBoard::Ok) {
00555     log->printf("Failed to initialize the board, got error %d\r\n", err);
00556     log->printf("\nTERMINATING\n");
00557     wait_ms(2000); // allow RtosLog to flush messages
00558     mbed_die();
00559   }
00560   
00561   // select which images to use based on display size
00562   prepareResources();
00563   
00564   board->buzzer(440,50);
00565   wait_ms(30);
00566   board->buzzer(660,50);
00567   wait_ms(30);
00568   board->buzzer(440,50);
00569   
00570   log->printf("\n\n---\nDemo flashed on new Display Modules.\nBuilt: " __DATE__ " at " __TIME__ "\n\n");
00571   
00572  
00573   Thread tAlive(aliveTask);
00574 #if defined(DM_BOARD_USE_DISPLAY)
00575   Thread tSwim(swimTask, NULL, osPriorityNormal, 8192);
00576 #endif  
00577   Thread tNetwork(netTask, NULL, osPriorityNormal, 8192);
00578   Thread tUSBHandler(usbTask, NULL, osPriorityNormal, 8192);
00579   
00580   while(1) { 
00581     // Wait forever (in 1h increments) to prevent the tAlive, tSwim,
00582     // tNetwork and tUSBHandler thread from being garbage collected.
00583     Thread::wait(3600*1000);
00584   }
00585 }