Example that shows how to use the ewgui wrapper classes in combination with Segger's emwin library.

Dependencies:   EALib ewgui mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EwGuiImpl.cpp Source File

EwGuiImpl.cpp

00001 #include "EwGuiImpl.h"
00002 
00003 #include "LcdController.h"
00004 #include "EaLcdBoard.h"
00005 #include "sdram.h"
00006 #include "AR1021.h"
00007 #include "TSC2046.h"
00008 
00009 
00010 #define PIXEL_SIZE (2)
00011 
00012 EwGuiImpl::EwGuiImpl() {
00013 
00014     EaLcdBoard lcdBoard(P0_27, P0_28);
00015     EaLcdBoard::Result result;
00016     LcdController::Config lcdCfg;
00017     EaLcdBoard::TouchParams_t touchParams;
00018 
00019     do {
00020 
00021         result = lcdBoard.open(NULL, NULL);
00022         if (result != EaLcdBoard::Ok) {
00023             printf("Failed to open display: %d\n", result);
00024             break;
00025         }
00026 
00027         result = lcdBoard.getLcdConfig(&lcdCfg);
00028         if (result != EaLcdBoard::Ok) {
00029             printf("Failed to get LCD configuration: %d\n", result);
00030             break;
00031         }
00032 
00033         _width = lcdCfg.width;
00034         _height = lcdCfg.height;
00035 
00036         // allocate buffer, width x height x 2 (2 bytes = 16 bit color data)
00037         _fb = (uint32_t)malloc(_width*_height*PIXEL_SIZE);
00038         if (_fb == 0) {
00039             printf("Failed to allocate frame buffer\n");
00040             break;
00041         }
00042 
00043         result = lcdBoard.setFrameBuffer(_fb);
00044         if (result != EaLcdBoard::Ok) {
00045             printf("Failed to activate frameBuffer: %d\n", result);
00046             break;
00047         }
00048 
00049         _memSz = 1024*1024*5;
00050         _mem = (uint32_t)malloc(_memSz);
00051         if (_mem == 0) {
00052             printf("Failed to allocate memory block for emwin\n");
00053             break;
00054         }
00055 
00056 
00057         memset((void*)_fb, 0x0, _width*_height*PIXEL_SIZE);
00058 
00059         result = lcdBoard.getTouchParameters(&touchParams);
00060         if (result != EaLcdBoard::Ok) {
00061             printf("Failed to get touch panel parameters: %d\n", result);
00062             break;
00063         }
00064 
00065         // create touch panel
00066         switch(touchParams.panelId) {
00067         case EaLcdBoard::TouchPanel_AR1021:
00068             printf("creating AR1021 touch panel\n");
00069             _touch = new AR1021(P2_27, P2_26, P2_22, P2_21, P2_25);
00070             break;
00071 
00072         case EaLcdBoard::TouchPanel_TSC2046:
00073         case EaLcdBoard::TouchPanelUnknown:
00074         default:
00075             // we always default to TSC2046 even if we cannot
00076             // detect which panel is used
00077 
00078             printf("creating TSC2046 touch panel\n");
00079 
00080             _touch = new TSC2046(P2_27, P2_26, P2_22, P2_21);
00081             break;
00082 
00083         }
00084 
00085         if (!_touch->init(_width, _height)) {
00086             printf("TouchPanel.init failed\n");
00087             break;
00088         }
00089 
00090         init();
00091 
00092     } while(0);
00093 
00094 
00095 }
00096 
00097 void* EwGuiImpl::getMemoryBlockAddress() {
00098     return (void*)_mem;
00099 }
00100 
00101 uint32_t EwGuiImpl::getMemoryBlockSize() {
00102     return _memSz;
00103 }
00104 
00105 uint32_t EwGuiImpl::getDisplayWidth() {
00106     return _width;
00107 }
00108 
00109 uint32_t EwGuiImpl::getDisplayHeight() {
00110     return _height;
00111 }
00112 
00113 void* EwGuiImpl::getFrameBufferAddress() {
00114     return (void*)_fb;
00115 }
00116 
00117 void EwGuiImpl::getTouchValues(int32_t* x, int32_t* y, int32_t* z) {
00118 
00119     if (!x || !y || !z) return;
00120 
00121     TouchPanel::touchCoordinate_t coord;
00122     _touch->read(coord);
00123 
00124     *x = coord.x;
00125     *y = coord.y;
00126     *z = coord.z;
00127 }
00128 
00129 void EwGuiImpl::calibrate() {
00130 
00131     uint16_t x = 0;
00132     uint16_t y = 0;
00133     bool hasMorePoints = false;
00134 
00135     EwPainter painter;
00136     painter.saveContext();
00137 
00138     do {
00139 
00140         printf("Starting calibration\n");
00141         if (!_touch->calibrateStart()) {
00142             printf("Failed to start calibration\n");
00143             break;
00144         }
00145 
00146         do {
00147             if (!_touch->getNextCalibratePoint(&x, &y)) {
00148                 printf("Failed to get next calibrate point\n");
00149                 break;
00150             }
00151 
00152             printf("calib: x=%d, y=%d\n", x, y);
00153             drawCalibPoint(painter, x, y);
00154 
00155             if (!_touch->waitForCalibratePoint(&hasMorePoints, 0)) {
00156                 printf("Failed waiting for calibration point\n");
00157                 break;
00158             }
00159 
00160         } while(hasMorePoints);
00161 
00162         printf("Calibration done\n");
00163 
00164 
00165     } while (0);
00166 
00167 
00168     painter.clear();
00169     painter.restoreContext();
00170 }
00171 
00172 void EwGuiImpl::drawCalibPoint(EwPainter &painter, int32_t x, int32_t y) {
00173 
00174     painter.clear();
00175     painter.setColor(EW_WHITE);
00176     painter.drawStringHorizCenter("Touch circle to calibrate",
00177             getDisplayWidth()/2, getDisplayHeight()/2);
00178 
00179     painter.fillCircle(x, y, 10);
00180     painter.setColor(EW_BLACK);
00181     painter.fillCircle(x, y, 5);
00182 }