Demo for Embedded World 2015.

Dependencies:   DMBasicGUI DMSupport

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 "USBHostMSD.h"
00011 #include "USBHostMouse.h"
00012 #include "USBHostKeyboard.h"
00013 
00014 #include "DMBoard.h"
00015 
00016 #include "AppLauncherSpecial.h"
00017 #include "meas.h"
00018 
00019 #include "AppNetworkSettings.h"
00020 #include "AppStatus.h"
00021 #include "AppTouchCalibration.h"
00022 #include "AppColorPicker.h"
00023 #include "AppImageViewer.h"
00024 #include "AppSlideShow.h"
00025 #include "AppRTCSettings.h"
00026 #include "AppUSBStatus.h"
00027 #include "AppDraw.h"
00028 #include "image_data.h"
00029 
00030 
00031 /******************************************************************************
00032  * Typedefs and defines
00033  *****************************************************************************/
00034 
00035 /******************************************************************************
00036  * Local variables
00037  *****************************************************************************/
00038 
00039 /******************************************************************************
00040  * Global variables
00041  *****************************************************************************/
00042 
00043 EthernetInterface eth;
00044 bool ethInitialized = false;
00045 bool ethUsingDHCP = true;
00046 bool haveUSBMSD = false;
00047 
00048 /******************************************************************************
00049  * Local functions
00050  *****************************************************************************/
00051 
00052 void aliveTask(void)
00053 {
00054   DMBoard* board = &DMBoard::instance();
00055     
00056   while(true)
00057   {
00058     board->setLED(DMBoard::Led4, false);
00059     board->setLED(DMBoard::Led1, true);
00060     ThisThread::sleep_for(300);
00061     board->setLED(DMBoard::Led1, false);
00062     board->setLED(DMBoard::Led2, true);
00063     ThisThread::sleep_for(300);
00064     board->setLED(DMBoard::Led2, false);
00065     board->setLED(DMBoard::Led3, true);
00066     ThisThread::sleep_for(300);
00067     board->setLED(DMBoard::Led3, false);
00068     board->setLED(DMBoard::Led4, true);
00069     ThisThread::sleep_for(300);
00070   }
00071 }
00072 
00073 #if defined(DM_BOARD_USE_DISPLAY)
00074 
00075 typedef enum {
00076     ColorPickerApp,
00077     ImageViewerApp,
00078     SlideshowApp,
00079     SettingsApp, 
00080     StatusApp, 
00081     TouchTestApp,
00082     RtcApp,
00083     USBStatusApp,
00084     DrawingApp,
00085     CalibrationApp =  AppLauncherSpecial::CalibrationApp,
00086     Placeholder,
00087 } myapps_t;
00088 
00089 static App* launchApp(uint32_t id)
00090 {
00091   App* a = NULL;
00092   switch ((myapps_t)id) {
00093       case CalibrationApp:
00094           a = new AppTouchCalibration();
00095           break;
00096       case SettingsApp:
00097           a = new AppNetworkSettings();
00098           break;
00099       case ColorPickerApp:
00100           a = new AppColorPicker();
00101           break;
00102       case ImageViewerApp:
00103           a = new AppImageViewer();
00104           break;
00105       case SlideshowApp:
00106           a = new AppSlideShow("/qspi/slides.txt", "/qspi", 0, 0);
00107           break;
00108       case StatusApp:
00109           a = new AppStatus();
00110           break;
00111       case RtcApp:
00112           a = new AppRTCSettings();
00113           break;
00114       case USBStatusApp:
00115           a = new AppUSBStatus();
00116           break;
00117       case DrawingApp:
00118           a = new AppDraw();
00119           break;
00120       default:
00121           break;
00122   }
00123   return a;
00124 }
00125 
00126 #define SWIM_TASK_PREFIX  "[SWIM] "
00127 void swimTask(void)
00128 {
00129   RtosLog* log = DMBoard::instance().logger();
00130     
00131   log->printf(SWIM_TASK_PREFIX"swimTask started\n");
00132   
00133   AppLauncherSpecial launcher;
00134     
00135     
00136   if (launcher.setup()) {
00137     launcher.addImageButton(SettingsApp, "Network", 0x2d5c, img_super_mono_3d_93, img_size_super_mono_3d_93);
00138     //launcher.addImageButton(Placeholder, "Logo Splash", 0x2d5c, img_super_mono_3d_part2_83, img_size_super_mono_3d_part2_83);
00139     launcher.addImageButton(DrawingApp, "Drawing", 0x2d5c, img_super_mono_3d_22, img_size_super_mono_3d_22);
00140     launcher.addImageButton(SlideshowApp, "Slideshow", 0x2d5c, img_super_mono_3d_87, img_size_super_mono_3d_87);
00141     launcher.addImageButton(ColorPickerApp, "Color Picker", 0x2d5c, img_super_mono_3d_part2_19, img_size_super_mono_3d_part2_19);
00142     launcher.addImageButton(ImageViewerApp, "Image Viewer", 0x2d5c, img_super_mono_3d_48, img_size_super_mono_3d_48);
00143     launcher.addImageButton(StatusApp, "About", 0x2d5c, img_super_mono_3d_part2_68, img_size_super_mono_3d_part2_68);
00144     launcher.addImageButton(USBStatusApp, "USB Status", 0x2d5c, img_super_mono_3d_57, img_size_super_mono_3d_57);
00145     launcher.addImageButton(RtcApp, "Clock", 0x2d5c, img_super_mono_3d_02, img_size_super_mono_3d_02);
00146       
00147     launcher.setAppCreatorFunc(launchApp);
00148     log->printf(SWIM_TASK_PREFIX"Starting menu system\n");
00149     launcher.runToCompletion();
00150     launcher.teardown();
00151   } else {
00152     log->printf(SWIM_TASK_PREFIX"Failed to prepare menu system\n");
00153   }
00154   
00155   // Should never end up here  
00156   mbed_die();
00157 }
00158 
00159 #endif //DM_BOARD_USE_DISPLAY
00160 
00161 
00162 #define NET_TASK_PREFIX  "[NET] "
00163 
00164 void netTask(void)
00165 {
00166   RtosLog* log = DMBoard::instance().logger();
00167   log->printf(NET_TASK_PREFIX"EthernetInterface Setting up...\r\n");
00168   nsapi_error_t net_err = eth.connect();
00169 
00170   if (net_err != NSAPI_ERROR_OK) {
00171     log->printf(NET_TASK_PREFIX"EthernetInterface connect Error %d\r\n", net_err);
00172   }
00173   else {
00174   
00175     ethInitialized = true;
00176     ethUsingDHCP = true;
00177   
00178     log->printf(NET_TASK_PREFIX"IP Address is %s\r\n", eth.get_ip_address());
00179     log->printf(NET_TASK_PREFIX"NetMask is %s\r\n", eth.get_netmask());
00180     log->printf(NET_TASK_PREFIX"Gateway Address is %s\r\n", eth.get_gateway());
00181     log->printf(NET_TASK_PREFIX"Ethernet Setup OK\r\n");
00182 
00183     HTTPServerAddHandler<SimpleHandler>("/hello"); //Default handler
00184     FSHandler::mount("/mci", "/");
00185     HTTPServerAddHandler<FSHandler>("/");
00186     HTTPServerStart(80);
00187   }
00188 }
00189 
00190 static volatile int8_t mouse_button, mouse_x, mouse_y, mouse_z;
00191 static uint16_t cursor_x=0, cursor_y=0;
00192 void mouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z)
00193 {
00194     mouse_button = buttons;
00195     mouse_x = x;
00196     mouse_y = y;
00197     mouse_z = z;
00198     
00199     if (x < 0) {
00200         if (cursor_x > -x) {
00201             cursor_x += x;
00202         } else {
00203             cursor_x = 0;
00204         }
00205     } else {
00206         if ((cursor_x + x) >= 480) {
00207             cursor_x = 479;
00208         } else {
00209             cursor_x += x;
00210         }
00211     }
00212     y = y/8;
00213     if (y < 0) {
00214         if (cursor_y > -y) {
00215             cursor_y += y;
00216         } else {
00217             cursor_y = 0;
00218         }
00219     } else {
00220         if ((cursor_y + y) >= 272) {
00221             cursor_y = 271;
00222         } else {
00223             cursor_y += y;
00224         }
00225     }
00226     
00227     //Chip_LCD_Cursor_SetPos(LPC_LCD, cursor_x, cursor_y);
00228     LPC_LCD->CRSR_XY = (cursor_x & 0x3FF) | ((cursor_y & 0x3FF) << 16);
00229 }
00230 
00231 #define LCD_CURSOR_32x32 0
00232 #define LCD_CURSOR_64x64 1
00233 #define CURSOR_SIZE  LCD_CURSOR_32x32
00234 #define CURSOR_H_SIZE 32
00235 #define CURSOR_V_SIZE 32
00236 #define CURSOR_NUM   0    
00237 #define CURSOR_H_OFS (10)
00238 #define CURSOR_V_OFS (6)
00239 
00240 const unsigned char __attribute__ ((aligned(4))) Cursor[(CURSOR_H_SIZE / 4) * CURSOR_V_SIZE] =
00241 {
00242     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00243     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00244     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00245     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00246     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00247     0xAA, 0xAA, 0xAA, 0xFA, 0xAA, 0xAA, 0xAA, 0xAA,
00248     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00249     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00250     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00251     0xAA, 0xAA, 0xAB, 0xFE, 0xAA, 0xAA, 0xAA, 0xAA,
00252     0xAA, 0xAA, 0xAB, 0xFF, 0xEA, 0xAA, 0xAA, 0xAA,
00253     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xAA, 0xAA, 0xAA,
00254     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00255     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00256     0xAA, 0xAB, 0xFB, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00257     0xAA, 0xAB, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00258     0xAA, 0xAB, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00259     0xAA, 0xAA, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00260     0xAA, 0xAA, 0xBF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00261     0xAA, 0xAA, 0xBF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00262     0xAA, 0xAA, 0xAF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA,
00263     0xAA, 0xAA, 0xAF, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00264     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00265     0xAA, 0xAA, 0xAB, 0xFF, 0xFF, 0xFE, 0xAA, 0xAA,
00266     0xAA, 0xAA, 0xAA, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00267     0xAA, 0xAA, 0xAA, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00268     0xAA, 0xAA, 0xAA, 0xFF, 0xFF, 0xFA, 0xAA, 0xAA,
00269     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00270     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00271     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00272     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
00273     0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
00274 };
00275 
00276 void prepareCursor(bool enable) {
00277     //Chip_LCD_Cursor_Disable(LPC_LCD, 0);
00278     LPC_LCD->CRSR_CTRL = (CURSOR_NUM << 4);
00279     
00280     if (enable) {
00281         //Chip_LCD_Cursor_Config(LPC_LCD, LCD_CURSOR_32x32, true);
00282         LPC_LCD->CRSR_CFG = ((true ? 1 : 0) << 1) | CURSOR_SIZE;
00283         
00284         //Chip_LCD_Cursor_WriteImage(LPC_LCD, 0, (void *) Cursor);
00285         {
00286             int i, j;
00287             uint32_t *fifoptr, *crsr_ptr = (uint32_t *) Cursor;
00288 
00289             /* Check if Cursor Size was configured as 32x32 or 64x64*/
00290             if (CURSOR_SIZE == LCD_CURSOR_32x32) {
00291                 i = CURSOR_NUM * 64;
00292                 j = i + 64;
00293             }
00294             else {
00295                 i = 0;
00296                 j = 256;
00297             }
00298             fifoptr = (uint32_t *) &(LPC_LCD->CRSR_IMG[0]);
00299 
00300             /* Copy Cursor Image content to FIFO */
00301             for (; i < j; i++) {
00302 
00303                 *fifoptr = *crsr_ptr;
00304                 crsr_ptr++;
00305                 fifoptr++;
00306             }
00307         }
00308         
00309         //Chip_LCD_Cursor_SetClip(LPC_LCD, CURSOR_H_OFS, CURSOR_V_OFS);
00310         LPC_LCD->CRSR_CLIP = (CURSOR_H_OFS & 0x3F) | ((CURSOR_V_OFS & 0x3F) << 8);
00311         
00312         //Chip_LCD_Cursor_SetPos(LPC_LCD, cursor_x, cursor_y);
00313         
00314         //Chip_LCD_Cursor_Enable(LPC_LCD, 0);
00315         LPC_LCD->CRSR_CTRL = (CURSOR_NUM << 4) | 1;
00316     }
00317 }
00318 
00319 
00320 #define CIRCBUFF_SIZE 256
00321 static volatile uint8_t circbuff[CIRCBUFF_SIZE];
00322 static volatile uint32_t circbuff_read = 0;
00323 static uint32_t circbuff_write = 0;
00324 void keyEvent(uint8_t key)
00325 {
00326     circbuff[circbuff_write%CIRCBUFF_SIZE] = key;
00327     circbuff_write++;
00328 }
00329 
00330 bool msdConnected = false;
00331 bool keyboardConnected = false;
00332 bool mouseConnected = false;
00333 
00334 #define USB_TASK_PREFIX  "[USB] "
00335 #define USB_CONNECTION_EVENT   (1<<4)
00336 void usbTask(void)
00337 {
00338   USBHostMSD* msd = new USBHostMSD("usb");
00339   USBHostKeyboard* keyboard = new USBHostKeyboard();
00340   USBHostMouse* mouse = new USBHostMouse();
00341   USBHost* host = USBHost::getHostInst();
00342   EventFlags connectionEvent;
00343   host->signalOnConnections(&connectionEvent, USB_CONNECTION_EVENT);
00344 
00345   RtosLog* log = DMBoard::instance().logger();
00346     
00347   log->printf(USB_TASK_PREFIX"usbTask started\n");
00348     
00349   prepareCursor(false);
00350 
00351   while (true) {
00352     // wait for connect/disconnect message from USBHost
00353     connectionEvent.wait_any(USB_CONNECTION_EVENT);
00354     log->printf(USB_TASK_PREFIX"got USB event\n");
00355       
00356     if (msd->connected()) {
00357       if (!msdConnected) {
00358         msdConnected = true;
00359         haveUSBMSD = true;
00360         log->printf(USB_TASK_PREFIX"USB MassStorage Device - Connected\n");
00361       }
00362     } else {
00363       if (msdConnected) {
00364         msdConnected = false;
00365         haveUSBMSD = false;
00366         log->printf(USB_TASK_PREFIX"USB MassStorage Device - Ejected\n");
00367       }
00368       
00369       if (msd->connect()) {
00370         msdConnected = true;
00371         haveUSBMSD = true;
00372         log->printf(USB_TASK_PREFIX"USB MassStorage Device - Connected\n");
00373       }      
00374     }
00375     
00376     if (keyboard->connected()) {
00377       if (!keyboardConnected) {
00378         keyboardConnected = true;
00379         log->printf(USB_TASK_PREFIX"USB Keyboard - Connected\n");
00380         keyboard->attach(keyEvent);
00381       }
00382     } else {
00383       if (keyboardConnected) {
00384         keyboardConnected = false;
00385         log->printf(USB_TASK_PREFIX"USB Keyboard - Ejected\n");
00386       }
00387       if (keyboard->connect()) {
00388         keyboardConnected = true;
00389         log->printf(USB_TASK_PREFIX"USB Keyboard - Connected\n");
00390         keyboard->attach(keyEvent);
00391       }
00392     }
00393     
00394     if (mouse->connected()) {
00395       if (!mouseConnected) {
00396         mouseConnected = true;
00397         log->printf(USB_TASK_PREFIX"USB Mouse - Connected\n");
00398         mouse->attachEvent(mouseEvent);
00399         prepareCursor(true);
00400       }
00401     } else {
00402       if (mouseConnected) {
00403         prepareCursor(false);
00404         mouseConnected = false;
00405         log->printf(USB_TASK_PREFIX"USB Mouse - Ejected\n");
00406       }
00407       if (mouse->connect()) {
00408         mouseConnected = true;
00409         log->printf(USB_TASK_PREFIX"USB Mouse - Connected\n");
00410         mouse->attachEvent(mouseEvent);
00411         prepareCursor(true);
00412       }
00413     }
00414   }
00415 }
00416 
00417 
00418 
00419 /******************************************************************************
00420  * Main function
00421  *****************************************************************************/
00422 int main()
00423 {
00424   DMBoard::BoardError err;
00425   DMBoard* board = &DMBoard::instance();
00426   RtosLog* log = board->logger();
00427   err = board->init();
00428   if (err != DMBoard::Ok) {
00429     log->printf("Failed to initialize the board, got error %d\r\n", err);
00430     log->printf("\nTERMINATING\n");
00431     ThisThread::sleep_for(2000); // allow RtosLog to flush messages
00432     mbed_die();
00433   }
00434   
00435   board->buzzer(440,50);
00436   ThisThread::sleep_for(30);
00437   board->buzzer(660,50);
00438   ThisThread::sleep_for(30);
00439   board->buzzer(440,50);
00440   
00441   log->printf("\n\n---\nDemo for Embedded World 2015\nBuilt: " __DATE__ " at " __TIME__ "\n\n");
00442   
00443  
00444   Thread tAlive;
00445   tAlive.start(aliveTask);
00446 #if defined(DM_BOARD_USE_DISPLAY)
00447   Thread tSwim(osPriorityNormal, 8192);
00448   tSwim.start(swimTask);
00449 #endif  
00450   Thread tNetwork(osPriorityNormal, 8192);
00451   tNetwork.start(netTask);
00452   Thread tUSBHandler(osPriorityNormal, 8192);
00453   tUSBHandler.start(usbTask);
00454   
00455   while(1) { 
00456     // Wait forever (in 1h increments) to prevent the tAlive, tSwim,
00457     // tNetwork and tUSBHandler thread from being garbage collected.
00458     ThisThread::sleep_for(3600*1000);
00459   }
00460 }
00461