Example that shows how to use emWin. This example is based on NXP's POS demo available at lpcware.com

Dependencies:   EALib ewgui mbed

Committer:
embeddedartists
Date:
Mon Apr 14 08:40:59 2014 +0000
Revision:
2:cbf00ac63f35
Parent:
0:2052561807c5
Updated to use latest ewgui

Who changed what in which revision?

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