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

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Thu Sep 26 07:59:11 2013 +0000
Revision:
0:79a419828f86
Child:
2:661a997f0ec7
First version

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