Example using the GFX graphical library, EaLcdBoard, TSC2046 touch screen and SDRAM initialization

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Wed Apr 09 09:40:08 2014 +0000
Revision:
3:be2af824cdf6
Parent:
2:661a997f0ec7
Updated to latest version of EALib.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:79a419828f86 1
embeddedartists 0:79a419828f86 2 /******************************************************************************
embeddedartists 0:79a419828f86 3 * Includes
embeddedartists 0:79a419828f86 4 *****************************************************************************/
embeddedartists 0:79a419828f86 5
embeddedartists 0:79a419828f86 6 #include "mbed.h"
embeddedartists 0:79a419828f86 7
embeddedartists 0:79a419828f86 8
embeddedartists 0:79a419828f86 9 #include "LcdController.h"
embeddedartists 0:79a419828f86 10 #include "EaLcdBoard.h"
embeddedartists 0:79a419828f86 11 #include "TSC2046.h"
embeddedartists 2:661a997f0ec7 12 #include "AR1021.h"
embeddedartists 0:79a419828f86 13 #include "sdram.h"
embeddedartists 0:79a419828f86 14
embeddedartists 0:79a419828f86 15 #include "wchar.h"
embeddedartists 0:79a419828f86 16 #include "GFXFb.h"
embeddedartists 0:79a419828f86 17
embeddedartists 0:79a419828f86 18
embeddedartists 0:79a419828f86 19
embeddedartists 0:79a419828f86 20 /******************************************************************************
embeddedartists 0:79a419828f86 21 * Typedefs and defines
embeddedartists 0:79a419828f86 22 *****************************************************************************/
embeddedartists 0:79a419828f86 23
embeddedartists 0:79a419828f86 24
embeddedartists 0:79a419828f86 25 /******************************************************************************
embeddedartists 0:79a419828f86 26 * Local variables
embeddedartists 0:79a419828f86 27 *****************************************************************************/
embeddedartists 0:79a419828f86 28
embeddedartists 0:79a419828f86 29 // EA LCD Board interface
embeddedartists 0:79a419828f86 30 static EaLcdBoard lcdBoard(P0_27, P0_28);
embeddedartists 0:79a419828f86 31
embeddedartists 0:79a419828f86 32 static uint16_t const colors[16] = {
embeddedartists 0:79a419828f86 33 BLACK,
embeddedartists 0:79a419828f86 34 LIGHTGRAY,
embeddedartists 0:79a419828f86 35 DARKGRAY,
embeddedartists 0:79a419828f86 36 WHITE,
embeddedartists 0:79a419828f86 37 RED,
embeddedartists 0:79a419828f86 38 GREEN,
embeddedartists 0:79a419828f86 39 BLUE,
embeddedartists 0:79a419828f86 40 MAGENTA,
embeddedartists 0:79a419828f86 41 CYAN,
embeddedartists 0:79a419828f86 42 YELLOW,
embeddedartists 0:79a419828f86 43 LIGHTRED,
embeddedartists 0:79a419828f86 44 LIGHTGREEN,
embeddedartists 0:79a419828f86 45 LIGHTBLUE,
embeddedartists 0:79a419828f86 46 LIGHTMAGENTA,
embeddedartists 0:79a419828f86 47 LIGHTCYAN,
embeddedartists 0:79a419828f86 48 LIGHTYELLOW
embeddedartists 0:79a419828f86 49 };
embeddedartists 0:79a419828f86 50
embeddedartists 0:79a419828f86 51 /******************************************************************************
embeddedartists 0:79a419828f86 52 * Local functions
embeddedartists 0:79a419828f86 53 *****************************************************************************/
embeddedartists 0:79a419828f86 54
embeddedartists 0:79a419828f86 55 static uint16_t random(uint16_t max)
embeddedartists 0:79a419828f86 56 {
embeddedartists 0:79a419828f86 57 uint16_t temp;
embeddedartists 0:79a419828f86 58
embeddedartists 0:79a419828f86 59 temp = rand();
embeddedartists 0:79a419828f86 60 temp = temp % max;
embeddedartists 0:79a419828f86 61 return temp;
embeddedartists 0:79a419828f86 62 }
embeddedartists 0:79a419828f86 63
embeddedartists 0:79a419828f86 64 static void demo1(GFXFb &gfx) {
embeddedartists 0:79a419828f86 65
embeddedartists 0:79a419828f86 66 int16_t x0 = 0;
embeddedartists 0:79a419828f86 67 int16_t y0 = 0;
embeddedartists 0:79a419828f86 68 int16_t radius = 0;
embeddedartists 0:79a419828f86 69 int color = 0;
embeddedartists 0:79a419828f86 70 int fill = 0;
embeddedartists 0:79a419828f86 71
embeddedartists 0:79a419828f86 72
embeddedartists 0:79a419828f86 73 gfx.fillScreen(BLACK);
embeddedartists 0:79a419828f86 74
embeddedartists 0:79a419828f86 75 for (int i = 0; i < 100; i++) {
embeddedartists 0:79a419828f86 76 x0 = random(gfx.width());
embeddedartists 0:79a419828f86 77 y0 = random(gfx.height());
embeddedartists 0:79a419828f86 78 color = random(16);
embeddedartists 0:79a419828f86 79 radius = random(50);
embeddedartists 0:79a419828f86 80 fill = random(2);
embeddedartists 0:79a419828f86 81
embeddedartists 0:79a419828f86 82 if (!fill) {
embeddedartists 0:79a419828f86 83 gfx.drawCircle(x0, y0, radius, colors[color]);
embeddedartists 0:79a419828f86 84 } else {
embeddedartists 0:79a419828f86 85 gfx.fillCircle(x0, y0, radius, colors[color]);
embeddedartists 0:79a419828f86 86 }
embeddedartists 0:79a419828f86 87
embeddedartists 0:79a419828f86 88 wait_ms(10);
embeddedartists 0:79a419828f86 89 }
embeddedartists 0:79a419828f86 90
embeddedartists 0:79a419828f86 91 }
embeddedartists 0:79a419828f86 92
embeddedartists 0:79a419828f86 93
embeddedartists 0:79a419828f86 94 static void demo2(GFXFb &gfx) {
embeddedartists 0:79a419828f86 95 int32_t margin = 5;
embeddedartists 0:79a419828f86 96 int32_t rowHeight = gfx.height() / 3;
embeddedartists 0:79a419828f86 97 int32_t colWidth = gfx.width() / 3;
embeddedartists 0:79a419828f86 98 int32_t graphPosX = gfx.getStringWidth("drawRoundRect");
embeddedartists 0:79a419828f86 99 int32_t maxGraphW = colWidth - graphPosX;
embeddedartists 0:79a419828f86 100
embeddedartists 0:79a419828f86 101 gfx.fillScreen(BLACK);
embeddedartists 0:79a419828f86 102
embeddedartists 0:79a419828f86 103 // ##############
embeddedartists 0:79a419828f86 104 // C O L U M N 1
embeddedartists 0:79a419828f86 105 // ##############
embeddedartists 0:79a419828f86 106
embeddedartists 0:79a419828f86 107 // drawLine
embeddedartists 0:79a419828f86 108 gfx.setCursor(0, rowHeight/2);
embeddedartists 0:79a419828f86 109 gfx.writeString("drawLine");
embeddedartists 0:79a419828f86 110 gfx.drawLine(0+graphPosX+margin, margin,
embeddedartists 0:79a419828f86 111 0+graphPosX+maxGraphW-margin, rowHeight-margin, colors[1]);
embeddedartists 0:79a419828f86 112
embeddedartists 0:79a419828f86 113 // drawRect
embeddedartists 0:79a419828f86 114 gfx.setCursor(0, rowHeight+rowHeight/2);
embeddedartists 0:79a419828f86 115 gfx.writeString("drawRect");
embeddedartists 0:79a419828f86 116 gfx.drawRect(0+graphPosX+margin, rowHeight+margin,
embeddedartists 0:79a419828f86 117 maxGraphW-2*margin, rowHeight-margin, colors[2]);
embeddedartists 0:79a419828f86 118
embeddedartists 0:79a419828f86 119 // fillRect
embeddedartists 0:79a419828f86 120 gfx.setCursor(0, rowHeight*2+rowHeight/2);
embeddedartists 0:79a419828f86 121 gfx.writeString("fillRect");
embeddedartists 0:79a419828f86 122 gfx.fillRect(0+graphPosX+margin, rowHeight*2+margin,
embeddedartists 0:79a419828f86 123 maxGraphW-2*margin, rowHeight-margin, colors[3]);
embeddedartists 0:79a419828f86 124
embeddedartists 0:79a419828f86 125 // ##############
embeddedartists 0:79a419828f86 126 // C O L U M N 2
embeddedartists 0:79a419828f86 127 // ##############
embeddedartists 0:79a419828f86 128
embeddedartists 0:79a419828f86 129 // drawCircle
embeddedartists 0:79a419828f86 130 gfx.setCursor(colWidth, rowHeight/2);
embeddedartists 0:79a419828f86 131 gfx.writeString("drawCircle");
embeddedartists 0:79a419828f86 132 gfx.drawCircle(colWidth+graphPosX+maxGraphW/2, rowHeight/2,
embeddedartists 0:79a419828f86 133 rowHeight/2-2*margin, colors[4]);
embeddedartists 0:79a419828f86 134
embeddedartists 0:79a419828f86 135 // fillCircle
embeddedartists 0:79a419828f86 136 gfx.setCursor(colWidth, rowHeight+rowHeight/2);
embeddedartists 0:79a419828f86 137 gfx.writeString("fillCircle");
embeddedartists 0:79a419828f86 138 gfx.fillCircle(colWidth+graphPosX+maxGraphW/2, rowHeight+rowHeight/2,
embeddedartists 0:79a419828f86 139 rowHeight/2-2*margin, colors[5]);
embeddedartists 0:79a419828f86 140
embeddedartists 0:79a419828f86 141 // drawTriangle
embeddedartists 0:79a419828f86 142 gfx.setCursor(colWidth, rowHeight*2+rowHeight/2);
embeddedartists 0:79a419828f86 143 gfx.writeString("drawTriangle");
embeddedartists 0:79a419828f86 144 gfx.drawTriangle(colWidth+graphPosX+margin, rowHeight*3-margin,
embeddedartists 0:79a419828f86 145 colWidth+graphPosX+maxGraphW/2, rowHeight*2+margin,
embeddedartists 0:79a419828f86 146 colWidth+graphPosX+maxGraphW-margin, rowHeight*3-margin, colors[6]);
embeddedartists 0:79a419828f86 147
embeddedartists 0:79a419828f86 148 // ##############
embeddedartists 0:79a419828f86 149 // C O L U M N 3
embeddedartists 0:79a419828f86 150 // ##############
embeddedartists 0:79a419828f86 151
embeddedartists 0:79a419828f86 152 // fillTriangle
embeddedartists 0:79a419828f86 153 gfx.setCursor(colWidth*2, rowHeight/2);
embeddedartists 0:79a419828f86 154 gfx.writeString("fillTriangle");
embeddedartists 0:79a419828f86 155 gfx.fillTriangle(colWidth*2+graphPosX+margin, rowHeight-margin,
embeddedartists 0:79a419828f86 156 colWidth*2+graphPosX+maxGraphW/2, margin,
embeddedartists 0:79a419828f86 157 colWidth*2+graphPosX+maxGraphW-margin, rowHeight-margin, colors[7]);
embeddedartists 0:79a419828f86 158
embeddedartists 0:79a419828f86 159
embeddedartists 0:79a419828f86 160 // drawRoundRect
embeddedartists 0:79a419828f86 161 gfx.setCursor(colWidth*2, rowHeight+rowHeight/2);
embeddedartists 0:79a419828f86 162 gfx.writeString("drawRoundRect");
embeddedartists 0:79a419828f86 163 gfx.drawRoundRect(colWidth*2+graphPosX+margin, rowHeight+margin,
embeddedartists 0:79a419828f86 164 maxGraphW-2*margin, rowHeight-margin, 10, colors[8]);
embeddedartists 0:79a419828f86 165
embeddedartists 0:79a419828f86 166 // fillRoundRect
embeddedartists 0:79a419828f86 167 gfx.setCursor(colWidth*2, rowHeight*2+rowHeight/2);
embeddedartists 0:79a419828f86 168 gfx.writeString("fillRoundRect");
embeddedartists 0:79a419828f86 169 gfx.fillRoundRect(colWidth*2+graphPosX+margin, rowHeight*2+margin,
embeddedartists 0:79a419828f86 170 maxGraphW-2*margin, rowHeight-margin, 10, colors[9]);
embeddedartists 0:79a419828f86 171
embeddedartists 0:79a419828f86 172
embeddedartists 0:79a419828f86 173 }
embeddedartists 0:79a419828f86 174
embeddedartists 2:661a997f0ec7 175 static void demo3(GFXFb &gfx, TouchPanel* touchPanel) {
embeddedartists 0:79a419828f86 176
embeddedartists 2:661a997f0ec7 177 uint16_t x = 0;
embeddedartists 2:661a997f0ec7 178 uint16_t y = 0;
embeddedartists 2:661a997f0ec7 179 bool hasMorePoints = false;
embeddedartists 2:661a997f0ec7 180 TouchPanel::touchCoordinate_t coord;
embeddedartists 0:79a419828f86 181
embeddedartists 0:79a419828f86 182
embeddedartists 2:661a997f0ec7 183 do {
embeddedartists 2:661a997f0ec7 184 if (!touchPanel->init(gfx.width(), gfx.height())) {
embeddedartists 2:661a997f0ec7 185 printf("TouchPanel.init failed\n");
embeddedartists 2:661a997f0ec7 186 break;
embeddedartists 0:79a419828f86 187 }
embeddedartists 0:79a419828f86 188
embeddedartists 2:661a997f0ec7 189 printf("Starting calibration\n");
embeddedartists 2:661a997f0ec7 190 if (!touchPanel->calibrateStart()) {
embeddedartists 2:661a997f0ec7 191 printf("Failed to start calibration\n");
embeddedartists 2:661a997f0ec7 192 break;
embeddedartists 0:79a419828f86 193 }
embeddedartists 0:79a419828f86 194
embeddedartists 0:79a419828f86 195
embeddedartists 2:661a997f0ec7 196 do {
embeddedartists 2:661a997f0ec7 197 if (!touchPanel->getNextCalibratePoint(&x, &y)) {
embeddedartists 2:661a997f0ec7 198 printf("Failed to get next calibrate point\n");
embeddedartists 2:661a997f0ec7 199 break;
embeddedartists 2:661a997f0ec7 200 }
embeddedartists 0:79a419828f86 201
embeddedartists 2:661a997f0ec7 202 printf("calib: x=%d, y=%d\n", x, y);
embeddedartists 2:661a997f0ec7 203 gfx.fillScreen(BLACK);
embeddedartists 2:661a997f0ec7 204 gfx.setCursor(0, 0);
embeddedartists 2:661a997f0ec7 205 gfx.writeString("Calibrate Touch Screen");
embeddedartists 2:661a997f0ec7 206 gfx.drawRect(x-5, y-5, 10, 10, WHITE);
embeddedartists 0:79a419828f86 207
embeddedartists 0:79a419828f86 208
embeddedartists 2:661a997f0ec7 209 if (!touchPanel->waitForCalibratePoint(&hasMorePoints, 0)) {
embeddedartists 2:661a997f0ec7 210 printf("Failed waiting for calibration point\n");
embeddedartists 2:661a997f0ec7 211 break;
embeddedartists 2:661a997f0ec7 212 }
embeddedartists 0:79a419828f86 213
embeddedartists 2:661a997f0ec7 214 } while(hasMorePoints);
embeddedartists 2:661a997f0ec7 215
embeddedartists 2:661a997f0ec7 216 printf("Calibration done\n");
embeddedartists 2:661a997f0ec7 217
embeddedartists 2:661a997f0ec7 218 gfx.fillScreen(BLACK);
embeddedartists 2:661a997f0ec7 219
embeddedartists 0:79a419828f86 220
embeddedartists 2:661a997f0ec7 221 while(1) {
embeddedartists 2:661a997f0ec7 222 touchPanel->read(coord);
embeddedartists 2:661a997f0ec7 223 if (coord.z > 0) {
embeddedartists 2:661a997f0ec7 224 gfx.drawPixel(coord.x, coord.y, WHITE);
embeddedartists 2:661a997f0ec7 225 }
embeddedartists 3:be2af824cdf6 226 wait_ms(2);
embeddedartists 0:79a419828f86 227 }
embeddedartists 2:661a997f0ec7 228
embeddedartists 2:661a997f0ec7 229 } while (0);
embeddedartists 2:661a997f0ec7 230
embeddedartists 2:661a997f0ec7 231
embeddedartists 2:661a997f0ec7 232
embeddedartists 2:661a997f0ec7 233 while(1);
embeddedartists 0:79a419828f86 234 }
embeddedartists 0:79a419828f86 235
embeddedartists 0:79a419828f86 236 /******************************************************************************
embeddedartists 0:79a419828f86 237 * Main function
embeddedartists 0:79a419828f86 238 *****************************************************************************/
embeddedartists 0:79a419828f86 239
embeddedartists 0:79a419828f86 240
embeddedartists 0:79a419828f86 241 int main (void) {
embeddedartists 0:79a419828f86 242 bool initSuccessful = false;
embeddedartists 0:79a419828f86 243
embeddedartists 0:79a419828f86 244 EaLcdBoard::Result result;
embeddedartists 0:79a419828f86 245 LcdController::Config lcdCfg;
embeddedartists 2:661a997f0ec7 246 EaLcdBoard::TouchParams_t touchParams;
embeddedartists 2:661a997f0ec7 247 uint32_t frameBuf1 = 0;
embeddedartists 2:661a997f0ec7 248 TouchPanel* tp = NULL;
embeddedartists 0:79a419828f86 249
embeddedartists 2:661a997f0ec7 250 // frame buffer is put in SDRAM
embeddedartists 0:79a419828f86 251 if (sdram_init() == 1) {
embeddedartists 2:661a997f0ec7 252 printf("Failed to initialize SDRAM\n");
embeddedartists 2:661a997f0ec7 253 return 1;
embeddedartists 0:79a419828f86 254 }
embeddedartists 0:79a419828f86 255
embeddedartists 0:79a419828f86 256 do {
embeddedartists 0:79a419828f86 257
embeddedartists 0:79a419828f86 258 result = lcdBoard.open(NULL, NULL);
embeddedartists 0:79a419828f86 259 if (result != EaLcdBoard::Ok) {
embeddedartists 0:79a419828f86 260 printf("Failed to open display: %d\n", result);
embeddedartists 0:79a419828f86 261 break;
embeddedartists 0:79a419828f86 262 }
embeddedartists 0:79a419828f86 263
embeddedartists 0:79a419828f86 264
embeddedartists 0:79a419828f86 265 result = lcdBoard.getLcdConfig(&lcdCfg);
embeddedartists 0:79a419828f86 266 if (result != EaLcdBoard::Ok) {
embeddedartists 0:79a419828f86 267 printf("Failed to get LCD configuration: %d\n", result);
embeddedartists 0:79a419828f86 268 break;
embeddedartists 0:79a419828f86 269 }
embeddedartists 0:79a419828f86 270
embeddedartists 2:661a997f0ec7 271
embeddedartists 2:661a997f0ec7 272 // allocate framebuffer, width x height x 2 (2 bytes = 16 bit color data)
embeddedartists 2:661a997f0ec7 273 frameBuf1 = (uint32_t)malloc(lcdCfg.width*lcdCfg.height*2);
embeddedartists 2:661a997f0ec7 274 if (frameBuf1 == 0) {
embeddedartists 2:661a997f0ec7 275 printf("Failed to allocate frame buffer\n");
embeddedartists 2:661a997f0ec7 276 break;
embeddedartists 2:661a997f0ec7 277 }
embeddedartists 2:661a997f0ec7 278
embeddedartists 2:661a997f0ec7 279
embeddedartists 2:661a997f0ec7 280 result = lcdBoard.setFrameBuffer(frameBuf1);
embeddedartists 2:661a997f0ec7 281 if (result != EaLcdBoard::Ok) {
embeddedartists 2:661a997f0ec7 282 printf("Failed to activate frameBuffer: %d\n", result);
embeddedartists 2:661a997f0ec7 283 break;
embeddedartists 2:661a997f0ec7 284 }
embeddedartists 2:661a997f0ec7 285
embeddedartists 2:661a997f0ec7 286
embeddedartists 0:79a419828f86 287 memset((void*)frameBuf1, 0x0, lcdCfg.width*lcdCfg.height*2);
embeddedartists 0:79a419828f86 288
embeddedartists 2:661a997f0ec7 289 result = lcdBoard.getTouchParameters(&touchParams);
embeddedartists 2:661a997f0ec7 290 if (result != EaLcdBoard::Ok) {
embeddedartists 2:661a997f0ec7 291 printf("Failed to get touch panel parameters: %d\n", result);
embeddedartists 2:661a997f0ec7 292 break;
embeddedartists 2:661a997f0ec7 293 }
embeddedartists 2:661a997f0ec7 294
embeddedartists 2:661a997f0ec7 295 // create touch panel
embeddedartists 2:661a997f0ec7 296 switch(touchParams.panelId) {
embeddedartists 2:661a997f0ec7 297 case EaLcdBoard::TouchPanel_AR1021:
embeddedartists 2:661a997f0ec7 298 printf("creating AR1021 touch panel\n");
embeddedartists 2:661a997f0ec7 299 tp = new AR1021(P2_27, P2_26, P2_22, P2_21, P2_25);
embeddedartists 2:661a997f0ec7 300 break;
embeddedartists 2:661a997f0ec7 301
embeddedartists 2:661a997f0ec7 302 case EaLcdBoard::TouchPanel_TSC2046:
embeddedartists 2:661a997f0ec7 303 case EaLcdBoard::TouchPanelUnknown:
embeddedartists 2:661a997f0ec7 304 default:
embeddedartists 2:661a997f0ec7 305 // we always default to TSC2046 even if we cannot
embeddedartists 2:661a997f0ec7 306 // detect which panel is used
embeddedartists 2:661a997f0ec7 307
embeddedartists 2:661a997f0ec7 308 printf("creating TSC2046 touch panel\n");
embeddedartists 2:661a997f0ec7 309
embeddedartists 2:661a997f0ec7 310 tp = new TSC2046(P2_27, P2_26, P2_22, P2_21);
embeddedartists 2:661a997f0ec7 311 break;
embeddedartists 2:661a997f0ec7 312
embeddedartists 2:661a997f0ec7 313 }
embeddedartists 2:661a997f0ec7 314
embeddedartists 2:661a997f0ec7 315
embeddedartists 0:79a419828f86 316 initSuccessful = true;
embeddedartists 0:79a419828f86 317
embeddedartists 0:79a419828f86 318
embeddedartists 0:79a419828f86 319 } while(0);
embeddedartists 0:79a419828f86 320
embeddedartists 0:79a419828f86 321
embeddedartists 0:79a419828f86 322
embeddedartists 0:79a419828f86 323
embeddedartists 0:79a419828f86 324 if (initSuccessful) {
embeddedartists 0:79a419828f86 325
embeddedartists 0:79a419828f86 326 GFXFb gfx(lcdCfg.width, lcdCfg.height, (uint16_t*)frameBuf1);
embeddedartists 0:79a419828f86 327
embeddedartists 0:79a419828f86 328 while (1) {
embeddedartists 0:79a419828f86 329 demo1(gfx);
embeddedartists 0:79a419828f86 330 wait_ms(5000);
embeddedartists 0:79a419828f86 331 demo2(gfx);
embeddedartists 2:661a997f0ec7 332 wait_ms(5000);
embeddedartists 2:661a997f0ec7 333 demo3(gfx, tp);
embeddedartists 0:79a419828f86 334 }
embeddedartists 0:79a419828f86 335
embeddedartists 0:79a419828f86 336 }
embeddedartists 0:79a419828f86 337 else {
embeddedartists 0:79a419828f86 338 printf("Couldn't start demo -> Initialization failed\n");
embeddedartists 0:79a419828f86 339 }
embeddedartists 0:79a419828f86 340
embeddedartists 0:79a419828f86 341
embeddedartists 0:79a419828f86 342 return 0;
embeddedartists 0:79a419828f86 343 }
embeddedartists 0:79a419828f86 344