The out-of-the-box demo application flashed on all display modules before they are shipped.

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers <title>AppStatus.cpp Source File</title>

AppStatus.cpp

00001 /*
00002  *  Copyright 2014 Embedded Artists AB
00003  *
00004  *  Licensed under the Apache License, Version 2.0 (the "License");
00005  *  you may not use this file except in compliance with the License.
00006  *  You may obtain a copy of the License at
00007  *
00008  *    http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  *  Unless required by applicable law or agreed to in writing, software
00011  *  distributed under the License is distributed on an "AS IS" BASIS,
00012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *  See the License for the specific language governing permissions and
00014  *  limitations under the License.
00015  */
00016 
00017 
00018 #include "mbed.h"
00019 #include "EthernetInterface.h"
00020 #include "AppStatus.h"
00021 #include "lpc_swim_image.h"
00022 #include "lpc_swim_font.h"
00023 #include "BiosLoader.h"
00024 
00025 /******************************************************************************
00026  * Defines and typedefs
00027  *****************************************************************************/
00028  
00029 #define HEADER_X_OFF            (20-5)
00030 #define HEADER_Y_SPACING        (20-5)
00031 #define ITEM_CAPTION_X_OFF      (40-15)
00032 #define ITEM_CAPTION_Y_SPACING  (swim_get_font_height(_win) + 5)
00033 #define ITEM_VALUE_X_OFF        (140-25)
00034 #define COL3_OFF                (230+70)
00035 #define COL4                    (COL3_OFF + ITEM_VALUE_X_OFF - 5)
00036 
00037 #define BIG_HEADER_X_OFF            (25)
00038 #define BIG_HEADER_Y_SPACING        (25)
00039 #define BIG_ITEM_CAPTION_X_OFF      (BIG_HEADER_X_OFF + 10)
00040 #define BIG_ITEM_CAPTION_Y_SPACING  (swim_get_font_height(_win) + 10)
00041 #define BIG_ITEM_VALUE_X_OFF        (160)
00042 #define BIG_COL3_OFF                (520)
00043 #define BIG_COL4                    (BIG_COL3_OFF + BIG_ITEM_VALUE_X_OFF - 5)
00044 
00045 #define BTN_OFF    20
00046  
00047 #define DEMO_VERSION     "v1.0"
00048 #define DEMO_BUILD_INFO  __DATE__ " " __TIME__
00049 
00050 #define DESCR(__res, __disp) \
00051   (((__res) == ((__disp)->currentResolution())) ? ("Active") : (((__disp)->isSupported(__res)) ? ("Supported") : ("N/A")))
00052 
00053 
00054 /******************************************************************************
00055  * Global variables
00056  *****************************************************************************/
00057 
00058 extern EthernetInterface eth;
00059 extern bool ethInitialized;
00060 extern bool ethUsingDHCP;
00061 
00062 /******************************************************************************
00063  * Private functions
00064  *****************************************************************************/
00065 
00066 static void buttonClicked(uint32_t x)
00067 {
00068   bool* done = (bool*)x;
00069   *done = true;
00070 }
00071 
00072 void AppStatus::draw()
00073 {
00074     swim_window_open(_win, 
00075                      _disp->width(), _disp->height(),         // full size
00076                      (COLOR_T*)_fb,
00077                      0,0,_disp->width()-1, _disp->height()-1, // window position and size
00078                      1,                                       // border
00079                      BLACK, WHITE, BLACK);                    // colors: pen, backgr, forgr
00080     
00081 
00082     swim_put_image_xy(_win, _bgImg.pixels, _bgImg.width, _bgImg.height, 
00083                       (_disp->width()-_bgImg.width)/2, (_disp->height()-_bgImg.height)/2);
00084     
00085     if (_disp->width() == 480) {
00086       draw480x272();
00087     } else {
00088       draw800x480();
00089     }
00090 
00091     _btn = new ImageButton(_win->fb, _win->xpmax - BTN_OFF - _resOk->width(), _win->ypmax - BTN_OFF - _resOk->height(), _resOk->width(), _resOk->height());
00092     _btn->loadImages(_resOk);
00093     _btn->draw();
00094 }
00095     
00096 void AppStatus::draw480x272()
00097 {
00098     char buff[120];
00099     
00100     // Column 1
00101     swim_put_text_xy(_win, "Demo:", HEADER_X_OFF, HEADER_Y_SPACING);
00102     swim_put_text_xy(_win, "Version:", ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 1);
00103     swim_put_text_xy(_win, "Build:", ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 2);
00104     
00105     swim_put_text_xy(_win, "Network:", HEADER_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 4);
00106     swim_put_text_xy(_win, "DHCP/Static:", ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 5);
00107     swim_put_text_xy(_win, "IP Address:", ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 6);
00108     swim_put_text_xy(_win, "NetMask:", ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 7);
00109     swim_put_text_xy(_win, "Gateway:", ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 8);
00110     swim_put_text_xy(_win, "MAC Address:", ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 9);
00111 
00112     char mac[6];
00113     mbed_mac_address(mac);
00114     snprintf(buff, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00115     swim_put_text_xy(_win, buff, ITEM_CAPTION_X_OFF+10, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 10);
00116 
00117     // Column 2
00118     swim_put_text_xy(_win, DEMO_VERSION, ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 1);
00119     swim_put_text_xy(_win, DEMO_BUILD_INFO, ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 2);
00120 
00121     swim_put_text_xy(_win, ethUsingDHCP?"DHCP":"Static IP", ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 5);
00122     if (ethInitialized) {
00123         swim_put_text_xy(_win, eth.get_ip_address(), ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 6);
00124         swim_put_text_xy(_win, eth.get_netmask(), ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 7);
00125         swim_put_text_xy(_win, eth.get_gateway(), ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 8);
00126         //swim_put_text_xy(_win, eth.getMACAddress(), ITEM_VALUE_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 4);
00127     } else {
00128         swim_put_text_xy(_win, "Not Initialized", ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 6);
00129         swim_put_text_xy(_win, "Not Initialized", ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 7);
00130         swim_put_text_xy(_win, "Not Initialized", ITEM_VALUE_X_OFF-20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 8);    
00131     }
00132 
00133     // Column 3
00134     swim_put_text_xy(_win, "Display:", COL3_OFF+HEADER_X_OFF, HEADER_Y_SPACING);
00135     swim_put_text_xy(_win, "Size:", COL3_OFF+ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 1);
00136     swim_put_text_xy(_win, "Landscape:", COL3_OFF+ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 2);
00137     swim_put_text_xy(_win, "Resolutions:", COL3_OFF+ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 3);
00138     swim_put_text_xy(_win, "16bit rgb565:", COL3_OFF+ITEM_CAPTION_X_OFF+10, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 4);
00139     swim_put_text_xy(_win, "18bit rgb666:", COL3_OFF+ITEM_CAPTION_X_OFF+10, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 5);
00140     swim_put_text_xy(_win, "24bit rgb888:", COL3_OFF+ITEM_CAPTION_X_OFF+10, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 6);
00141 
00142     swim_put_text_xy(_win, "BIOS:", COL3_OFF+HEADER_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 8);
00143     {
00144         uint8_t type, maj, min, rev;
00145         BiosLoader::instance().getBiosStats(type, maj, min, rev);
00146         snprintf(buff, 25, "v%u.%u.%u  (board type %u)", maj, min, rev, type);
00147         swim_put_text_xy(_win, buff, COL3_OFF+ITEM_CAPTION_X_OFF, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 9);
00148     }
00149 
00150     // Column 4
00151     sprintf(buff, "%d x %d pixels", _disp->width(), _disp->height());
00152     swim_put_text_xy(_win, buff, COL4 -20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 1);
00153     
00154     sprintf(buff, "%s", _disp->landscape()?"YES":"NO");
00155     swim_put_text_xy(_win, buff, COL4 -20, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 2);
00156 
00157     sprintf(buff, "%s", DESCR(Display::Resolution_16bit_rgb565, _disp));
00158     swim_put_text_xy(_win, buff, COL4, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 4);
00159     
00160     sprintf(buff, "%s", DESCR(Display::Resolution_18bit_rgb666, _disp));
00161     swim_put_text_xy(_win, buff, COL4, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 5);
00162     
00163     sprintf(buff, "%s", DESCR(Display::Resolution_24bit_rgb888, _disp));
00164     swim_put_text_xy(_win, buff, COL4, HEADER_Y_SPACING + ITEM_CAPTION_Y_SPACING * 6);
00165 }
00166 
00167 void AppStatus::draw800x480()
00168 {
00169     char buff[120];
00170     
00171     // Column 1
00172     swim_put_text_xy(_win, "Demo:", BIG_HEADER_X_OFF, BIG_HEADER_Y_SPACING);
00173     swim_put_text_xy(_win, "Version:", BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 1);
00174     swim_put_text_xy(_win, "Build:", BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 2);
00175     
00176     swim_put_text_xy(_win, "Network:", BIG_HEADER_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 4);
00177     swim_put_text_xy(_win, "DHCP/Static:", BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 5);
00178     swim_put_text_xy(_win, "IP Address:", BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 6);
00179     swim_put_text_xy(_win, "NetMask:", BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 7);
00180     swim_put_text_xy(_win, "Gateway:", BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 8);
00181     swim_put_text_xy(_win, "MAC Address:", BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 9);
00182 
00183     // Column 2
00184     swim_put_text_xy(_win, DEMO_VERSION, BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 1);
00185     swim_put_text_xy(_win, DEMO_BUILD_INFO, BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 2);
00186 
00187     swim_put_text_xy(_win, ethUsingDHCP?"DHCP":"Static IP", BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 5);
00188     if (ethInitialized) {
00189         swim_put_text_xy(_win, eth.get_ip_address(), BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 6);
00190         swim_put_text_xy(_win, eth.get_netmask(), BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 7);
00191         swim_put_text_xy(_win, eth.get_gateway(), BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 8);
00192     } else {
00193         swim_put_text_xy(_win, "Not Initialized", BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 6);
00194         swim_put_text_xy(_win, "Not Initialized", BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 7);
00195         swim_put_text_xy(_win, "Not Initialized", BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 8);    
00196     }
00197     char mac[6];
00198     mbed_mac_address(mac);
00199     snprintf(buff, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00200     swim_put_text_xy(_win, buff, BIG_ITEM_VALUE_X_OFF-20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 9);
00201 
00202     // Column 3
00203     swim_put_text_xy(_win, "Display:", BIG_COL3_OFF+BIG_HEADER_X_OFF, BIG_HEADER_Y_SPACING);
00204     swim_put_text_xy(_win, "Size:", BIG_COL3_OFF+BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 1);
00205     swim_put_text_xy(_win, "Landscape:", BIG_COL3_OFF+BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 2);
00206     swim_put_text_xy(_win, "Resolutions:", BIG_COL3_OFF+BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 3);
00207     swim_put_text_xy(_win, "16bit rgb565:", BIG_COL3_OFF+BIG_ITEM_CAPTION_X_OFF+10, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 4);
00208     swim_put_text_xy(_win, "18bit rgb666:", BIG_COL3_OFF+BIG_ITEM_CAPTION_X_OFF+10, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 5);
00209     swim_put_text_xy(_win, "24bit rgb888:", BIG_COL3_OFF+BIG_ITEM_CAPTION_X_OFF+10, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 6);
00210 
00211     swim_put_text_xy(_win, "BIOS:", BIG_COL3_OFF+BIG_HEADER_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 8);
00212     {
00213         uint8_t type, maj, min, rev;
00214         BiosLoader::instance().getBiosStats(type, maj, min, rev);
00215         snprintf(buff, 25, "v%u.%u.%u  (board type %u)", maj, min, rev, type);
00216         swim_put_text_xy(_win, buff, BIG_COL3_OFF+BIG_ITEM_CAPTION_X_OFF, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 9);
00217     }
00218 
00219     // Column 4
00220     sprintf(buff, "%d x %d pixels", _disp->width(), _disp->height());
00221     swim_put_text_xy(_win, buff, BIG_COL4 -20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 1);
00222     
00223     sprintf(buff, "%s", _disp->landscape()?"YES":"NO");
00224     swim_put_text_xy(_win, buff, BIG_COL4 -20, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 2);
00225 
00226     sprintf(buff, "%s", DESCR(Display::Resolution_16bit_rgb565, _disp));
00227     swim_put_text_xy(_win, buff, BIG_COL4, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 4);
00228     
00229     sprintf(buff, "%s", DESCR(Display::Resolution_18bit_rgb666, _disp));
00230     swim_put_text_xy(_win, buff, BIG_COL4, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 5);
00231     
00232     sprintf(buff, "%s", DESCR(Display::Resolution_24bit_rgb888, _disp));
00233     swim_put_text_xy(_win, buff, BIG_COL4, BIG_HEADER_Y_SPACING + BIG_ITEM_CAPTION_Y_SPACING * 6);
00234 }
00235 
00236 /******************************************************************************
00237  * Public functions
00238  *****************************************************************************/
00239 
00240 AppStatus::AppStatus() : _disp(NULL), _win(NULL), _fb(NULL), _btn(NULL)
00241 {
00242   _bgImg.pointerToFree = NULL;
00243   _bgImg.pixels = NULL;
00244 }
00245 
00246 AppStatus::~AppStatus()
00247 {
00248     teardown();
00249 }
00250 
00251 bool AppStatus::setup()
00252 {
00253     RtosLog* log = DMBoard::instance().logger();
00254 
00255     _disp = DMBoard::instance().display();
00256     _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
00257     _fb = _disp->allocateFramebuffer();
00258 
00259     if (_win == NULL || _fb == NULL) {
00260         log->printf("Failed to allocate memory for framebuffer\r\n");
00261         return false;
00262     }
00263     
00264     if (Image::decode(_resAbout, Image::RES_16BIT, &_bgImg) != 0) {
00265         log->printf("Failed to load background image\n");
00266         return false;
00267     }
00268     
00269     return true;
00270 }
00271 
00272 void AppStatus::runToCompletion()
00273 {
00274     bool done = false;
00275     draw();
00276     _btn->setAction(buttonClicked, (uint32_t)&done);
00277     void* oldFB = _disp->swapFramebuffer(_fb);
00278     
00279     // Wait for touches, but the AppLauncher is already listening
00280     // for new touch event and sends a signal to its thread which
00281     // is the same as runs this function so it is enough to wait
00282     // for that signal.
00283     TouchPanel* touch = DMBoard::instance().touchPanel();
00284     touch_coordinate_t coord;
00285     while(!done) {
00286       ThisThread::flags_wait_all(0x1);
00287       if (touch->read(coord) == TouchPanel::TouchError_Ok) {
00288         if (_btn->handle(coord.x, coord.y, coord.z > 0)) {
00289             _btn->draw();
00290         }
00291       }
00292     }
00293     
00294     // User has clicked the button, restore the original FB
00295     _disp->swapFramebuffer(oldFB);
00296     swim_window_close(_win);
00297 }
00298 
00299 bool AppStatus::teardown()
00300 {
00301     if (_win != NULL) {
00302         free(_win);
00303         _win = NULL;
00304     }
00305     if (_fb != NULL) {
00306         free(_fb);
00307         _fb = NULL;
00308     }
00309     if (_btn != NULL) {
00310         delete _btn;
00311         _btn = NULL;
00312     }
00313     if (_bgImg.pointerToFree != NULL) {
00314         free(_bgImg.pointerToFree);
00315         _bgImg.pointerToFree = NULL;
00316     }    
00317     return true;
00318 }
00319 
00320 void AppStatus::addResource(Resources id, Resource* res)
00321 {
00322     if (id == Resource_Ok_button) {
00323         _resOk = res;
00324     } else {
00325         _resAbout = res;
00326     }
00327 }
00328 
00329 
00330 
00331