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

Dependencies:   EALib ewgui mbed

Committer:
embeddedartists
Date:
Mon Apr 14 08:39:47 2014 +0000
Revision:
2:789b4879e22f
Parent:
0:6b81fd4666bb
Updated ewgui to latest version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:6b81fd4666bb 1 #include "EwGuiImpl.h"
embeddedartists 0:6b81fd4666bb 2
embeddedartists 0:6b81fd4666bb 3 #include "LcdController.h"
embeddedartists 0:6b81fd4666bb 4 #include "EaLcdBoard.h"
embeddedartists 0:6b81fd4666bb 5 #include "sdram.h"
embeddedartists 0:6b81fd4666bb 6 #include "AR1021.h"
embeddedartists 0:6b81fd4666bb 7 #include "TSC2046.h"
embeddedartists 0:6b81fd4666bb 8
embeddedartists 0:6b81fd4666bb 9
embeddedartists 0:6b81fd4666bb 10 #define PIXEL_SIZE (2)
embeddedartists 0:6b81fd4666bb 11
embeddedartists 0:6b81fd4666bb 12 EwGuiImpl::EwGuiImpl() {
embeddedartists 0:6b81fd4666bb 13
embeddedartists 0:6b81fd4666bb 14 EaLcdBoard lcdBoard(P0_27, P0_28);
embeddedartists 0:6b81fd4666bb 15 EaLcdBoard::Result result;
embeddedartists 0:6b81fd4666bb 16 LcdController::Config lcdCfg;
embeddedartists 0:6b81fd4666bb 17 EaLcdBoard::TouchParams_t touchParams;
embeddedartists 0:6b81fd4666bb 18
embeddedartists 0:6b81fd4666bb 19 do {
embeddedartists 0:6b81fd4666bb 20
embeddedartists 0:6b81fd4666bb 21 result = lcdBoard.open(NULL, NULL);
embeddedartists 0:6b81fd4666bb 22 if (result != EaLcdBoard::Ok) {
embeddedartists 0:6b81fd4666bb 23 printf("Failed to open display: %d\n", result);
embeddedartists 0:6b81fd4666bb 24 break;
embeddedartists 0:6b81fd4666bb 25 }
embeddedartists 0:6b81fd4666bb 26
embeddedartists 0:6b81fd4666bb 27 result = lcdBoard.getLcdConfig(&lcdCfg);
embeddedartists 0:6b81fd4666bb 28 if (result != EaLcdBoard::Ok) {
embeddedartists 0:6b81fd4666bb 29 printf("Failed to get LCD configuration: %d\n", result);
embeddedartists 0:6b81fd4666bb 30 break;
embeddedartists 0:6b81fd4666bb 31 }
embeddedartists 0:6b81fd4666bb 32
embeddedartists 0:6b81fd4666bb 33 _width = lcdCfg.width;
embeddedartists 0:6b81fd4666bb 34 _height = lcdCfg.height;
embeddedartists 0:6b81fd4666bb 35
embeddedartists 0:6b81fd4666bb 36 // allocate buffer, width x height x 2 (2 bytes = 16 bit color data)
embeddedartists 0:6b81fd4666bb 37 _fb = (uint32_t)malloc(_width*_height*PIXEL_SIZE);
embeddedartists 0:6b81fd4666bb 38 if (_fb == 0) {
embeddedartists 0:6b81fd4666bb 39 printf("Failed to allocate frame buffer\n");
embeddedartists 0:6b81fd4666bb 40 break;
embeddedartists 0:6b81fd4666bb 41 }
embeddedartists 0:6b81fd4666bb 42
embeddedartists 0:6b81fd4666bb 43 result = lcdBoard.setFrameBuffer(_fb);
embeddedartists 0:6b81fd4666bb 44 if (result != EaLcdBoard::Ok) {
embeddedartists 0:6b81fd4666bb 45 printf("Failed to activate frameBuffer: %d\n", result);
embeddedartists 0:6b81fd4666bb 46 break;
embeddedartists 0:6b81fd4666bb 47 }
embeddedartists 0:6b81fd4666bb 48
embeddedartists 0:6b81fd4666bb 49 _memSz = 1024*1024*5;
embeddedartists 0:6b81fd4666bb 50 _mem = (uint32_t)malloc(_memSz);
embeddedartists 0:6b81fd4666bb 51 if (_mem == 0) {
embeddedartists 0:6b81fd4666bb 52 printf("Failed to allocate memory block for emwin\n");
embeddedartists 0:6b81fd4666bb 53 break;
embeddedartists 0:6b81fd4666bb 54 }
embeddedartists 0:6b81fd4666bb 55
embeddedartists 0:6b81fd4666bb 56
embeddedartists 0:6b81fd4666bb 57 memset((void*)_fb, 0x0, _width*_height*PIXEL_SIZE);
embeddedartists 0:6b81fd4666bb 58
embeddedartists 0:6b81fd4666bb 59 result = lcdBoard.getTouchParameters(&touchParams);
embeddedartists 0:6b81fd4666bb 60 if (result != EaLcdBoard::Ok) {
embeddedartists 0:6b81fd4666bb 61 printf("Failed to get touch panel parameters: %d\n", result);
embeddedartists 0:6b81fd4666bb 62 break;
embeddedartists 0:6b81fd4666bb 63 }
embeddedartists 0:6b81fd4666bb 64
embeddedartists 0:6b81fd4666bb 65 // create touch panel
embeddedartists 0:6b81fd4666bb 66 switch(touchParams.panelId) {
embeddedartists 0:6b81fd4666bb 67 case EaLcdBoard::TouchPanel_AR1021:
embeddedartists 0:6b81fd4666bb 68 printf("creating AR1021 touch panel\n");
embeddedartists 0:6b81fd4666bb 69 _touch = new AR1021(P2_27, P2_26, P2_22, P2_21, P2_25);
embeddedartists 0:6b81fd4666bb 70 break;
embeddedartists 0:6b81fd4666bb 71
embeddedartists 0:6b81fd4666bb 72 case EaLcdBoard::TouchPanel_TSC2046:
embeddedartists 0:6b81fd4666bb 73 case EaLcdBoard::TouchPanelUnknown:
embeddedartists 0:6b81fd4666bb 74 default:
embeddedartists 0:6b81fd4666bb 75 // we always default to TSC2046 even if we cannot
embeddedartists 0:6b81fd4666bb 76 // detect which panel is used
embeddedartists 0:6b81fd4666bb 77
embeddedartists 0:6b81fd4666bb 78 printf("creating TSC2046 touch panel\n");
embeddedartists 0:6b81fd4666bb 79
embeddedartists 0:6b81fd4666bb 80 _touch = new TSC2046(P2_27, P2_26, P2_22, P2_21);
embeddedartists 0:6b81fd4666bb 81 break;
embeddedartists 0:6b81fd4666bb 82
embeddedartists 0:6b81fd4666bb 83 }
embeddedartists 0:6b81fd4666bb 84
embeddedartists 0:6b81fd4666bb 85 if (!_touch->init(_width, _height)) {
embeddedartists 0:6b81fd4666bb 86 printf("TouchPanel.init failed\n");
embeddedartists 0:6b81fd4666bb 87 break;
embeddedartists 0:6b81fd4666bb 88 }
embeddedartists 0:6b81fd4666bb 89
embeddedartists 0:6b81fd4666bb 90 init();
embeddedartists 0:6b81fd4666bb 91
embeddedartists 0:6b81fd4666bb 92 } while(0);
embeddedartists 0:6b81fd4666bb 93
embeddedartists 0:6b81fd4666bb 94
embeddedartists 0:6b81fd4666bb 95 }
embeddedartists 0:6b81fd4666bb 96
embeddedartists 0:6b81fd4666bb 97 void* EwGuiImpl::getMemoryBlockAddress() {
embeddedartists 0:6b81fd4666bb 98 return (void*)_mem;
embeddedartists 0:6b81fd4666bb 99 }
embeddedartists 0:6b81fd4666bb 100
embeddedartists 0:6b81fd4666bb 101 uint32_t EwGuiImpl::getMemoryBlockSize() {
embeddedartists 0:6b81fd4666bb 102 return _memSz;
embeddedartists 0:6b81fd4666bb 103 }
embeddedartists 0:6b81fd4666bb 104
embeddedartists 0:6b81fd4666bb 105 uint32_t EwGuiImpl::getDisplayWidth() {
embeddedartists 0:6b81fd4666bb 106 return _width;
embeddedartists 0:6b81fd4666bb 107 }
embeddedartists 0:6b81fd4666bb 108
embeddedartists 0:6b81fd4666bb 109 uint32_t EwGuiImpl::getDisplayHeight() {
embeddedartists 0:6b81fd4666bb 110 return _height;
embeddedartists 0:6b81fd4666bb 111 }
embeddedartists 0:6b81fd4666bb 112
embeddedartists 0:6b81fd4666bb 113 void* EwGuiImpl::getFrameBufferAddress() {
embeddedartists 0:6b81fd4666bb 114 return (void*)_fb;
embeddedartists 0:6b81fd4666bb 115 }
embeddedartists 0:6b81fd4666bb 116
embeddedartists 0:6b81fd4666bb 117 void EwGuiImpl::getTouchValues(int32_t* x, int32_t* y, int32_t* z) {
embeddedartists 0:6b81fd4666bb 118
embeddedartists 0:6b81fd4666bb 119 if (!x || !y || !z) return;
embeddedartists 0:6b81fd4666bb 120
embeddedartists 0:6b81fd4666bb 121 TouchPanel::touchCoordinate_t coord;
embeddedartists 0:6b81fd4666bb 122 _touch->read(coord);
embeddedartists 0:6b81fd4666bb 123
embeddedartists 0:6b81fd4666bb 124 *x = coord.x;
embeddedartists 0:6b81fd4666bb 125 *y = coord.y;
embeddedartists 0:6b81fd4666bb 126 *z = coord.z;
embeddedartists 0:6b81fd4666bb 127 }
embeddedartists 0:6b81fd4666bb 128
embeddedartists 0:6b81fd4666bb 129 void EwGuiImpl::calibrate() {
embeddedartists 0:6b81fd4666bb 130
embeddedartists 0:6b81fd4666bb 131 uint16_t x = 0;
embeddedartists 0:6b81fd4666bb 132 uint16_t y = 0;
embeddedartists 0:6b81fd4666bb 133 bool hasMorePoints = false;
embeddedartists 0:6b81fd4666bb 134
embeddedartists 0:6b81fd4666bb 135 EwPainter painter;
embeddedartists 0:6b81fd4666bb 136 painter.saveContext();
embeddedartists 0:6b81fd4666bb 137
embeddedartists 0:6b81fd4666bb 138 do {
embeddedartists 0:6b81fd4666bb 139
embeddedartists 0:6b81fd4666bb 140 printf("Starting calibration\n");
embeddedartists 0:6b81fd4666bb 141 if (!_touch->calibrateStart()) {
embeddedartists 0:6b81fd4666bb 142 printf("Failed to start calibration\n");
embeddedartists 0:6b81fd4666bb 143 break;
embeddedartists 0:6b81fd4666bb 144 }
embeddedartists 0:6b81fd4666bb 145
embeddedartists 0:6b81fd4666bb 146 do {
embeddedartists 0:6b81fd4666bb 147 if (!_touch->getNextCalibratePoint(&x, &y)) {
embeddedartists 0:6b81fd4666bb 148 printf("Failed to get next calibrate point\n");
embeddedartists 0:6b81fd4666bb 149 break;
embeddedartists 0:6b81fd4666bb 150 }
embeddedartists 0:6b81fd4666bb 151
embeddedartists 0:6b81fd4666bb 152 printf("calib: x=%d, y=%d\n", x, y);
embeddedartists 0:6b81fd4666bb 153 drawCalibPoint(painter, x, y);
embeddedartists 0:6b81fd4666bb 154
embeddedartists 0:6b81fd4666bb 155 if (!_touch->waitForCalibratePoint(&hasMorePoints, 0)) {
embeddedartists 0:6b81fd4666bb 156 printf("Failed waiting for calibration point\n");
embeddedartists 0:6b81fd4666bb 157 break;
embeddedartists 0:6b81fd4666bb 158 }
embeddedartists 0:6b81fd4666bb 159
embeddedartists 0:6b81fd4666bb 160 } while(hasMorePoints);
embeddedartists 0:6b81fd4666bb 161
embeddedartists 0:6b81fd4666bb 162 printf("Calibration done\n");
embeddedartists 0:6b81fd4666bb 163
embeddedartists 0:6b81fd4666bb 164
embeddedartists 0:6b81fd4666bb 165 } while (0);
embeddedartists 0:6b81fd4666bb 166
embeddedartists 0:6b81fd4666bb 167
embeddedartists 0:6b81fd4666bb 168 painter.clear();
embeddedartists 0:6b81fd4666bb 169 painter.restoreContext();
embeddedartists 0:6b81fd4666bb 170 }
embeddedartists 0:6b81fd4666bb 171
embeddedartists 0:6b81fd4666bb 172 void EwGuiImpl::drawCalibPoint(EwPainter &painter, int32_t x, int32_t y) {
embeddedartists 0:6b81fd4666bb 173
embeddedartists 0:6b81fd4666bb 174 painter.clear();
embeddedartists 0:6b81fd4666bb 175 painter.setColor(EW_WHITE);
embeddedartists 0:6b81fd4666bb 176 painter.drawStringHorizCenter("Touch circle to calibrate",
embeddedartists 0:6b81fd4666bb 177 getDisplayWidth()/2, getDisplayHeight()/2);
embeddedartists 0:6b81fd4666bb 178
embeddedartists 0:6b81fd4666bb 179 painter.fillCircle(x, y, 10);
embeddedartists 0:6b81fd4666bb 180 painter.setColor(EW_BLACK);
embeddedartists 0:6b81fd4666bb 181 painter.fillCircle(x, y, 5);
embeddedartists 0:6b81fd4666bb 182 }