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

Dependencies:   EALib mbed

Revision:
2:661a997f0ec7
Parent:
0:79a419828f86
Child:
3:be2af824cdf6
--- a/main.cpp	Thu Sep 26 08:31:14 2013 +0000
+++ b/main.cpp	Fri Oct 18 13:09:40 2013 +0200
@@ -9,6 +9,7 @@
 #include "LcdController.h"
 #include "EaLcdBoard.h"
 #include "TSC2046.h"
+#include "AR1021.h"
 #include "sdram.h"
 
 #include "wchar.h"
@@ -28,9 +29,6 @@
 // EA LCD Board interface
 static EaLcdBoard lcdBoard(P0_27, P0_28);
 
-// touch interface
-static TSC2046 touch(P2_27, P2_26, P2_22, P2_21);
-
 static uint16_t const colors[16] = {
         BLACK,
         LIGHTGRAY,
@@ -174,90 +172,64 @@
 
 }
 
-static void drawCalibPoint(GFXFb &gfx, TSC2046::touchCoordinate_t &coord)
-{
-    gfx.fillScreen(BLACK);
-    gfx.setCursor(0, 0);
-    gfx.writeString("Calibrate Touch Screen");
-    gfx.drawRect(coord.x-5, coord.y-5, 10, 10, WHITE);
-}
+static void demo3(GFXFb &gfx, TouchPanel* touchPanel) {
 
-static void touchCalibrate(GFXFb &gfx)
-{
-    TSC2046::touchCoordinate_t coord;
-    TSC2046::touchCoordinate_t ref1 = {15, gfx.height()-15};
-    TSC2046::touchCoordinate_t ref2 = {gfx.width()/2, 80};
-    TSC2046::touchCoordinate_t ref3 = {gfx.width()-15, gfx.height()-15};
-    TSC2046::touchCoordinate_t scr1 = {0,0};
-    TSC2046::touchCoordinate_t scr2 = {0,0};
-    TSC2046::touchCoordinate_t scr3 = {0,0};
+    uint16_t x = 0;
+    uint16_t y = 0;
+    bool hasMorePoints = false;
+    TouchPanel::touchCoordinate_t coord;
 
-    bool calibrated = false;
-    bool releaseNeeded = false;
-    bool touchReleased = false;
 
-    int calibPoint = 0;
-
-    drawCalibPoint(gfx, ref1);
-
-    while(!calibrated) {
-        wait_ms(100);
-
-        touch.read(coord);
-
-        if (coord.z == 0) {
-            touchReleased = true;
-            continue;
+    do {
+        if (!touchPanel->init(gfx.width(), gfx.height())) {
+            printf("TouchPanel.init failed\n");
+            break;
         }
 
-        if (releaseNeeded && !touchReleased) {
-            continue;
+        printf("Starting calibration\n");
+        if (!touchPanel->calibrateStart()) {
+            printf("Failed to start calibration\n");
+            break;
         }
 
-        touchReleased = false;
-
-        switch(calibPoint++) {
-        case 0:
-            scr1.x = coord.x;
-            scr1.y = coord.y;
-            drawCalibPoint(gfx, ref2);
-
-            releaseNeeded = true;
-            break;
-        case 1:
-            scr2.x = coord.x;
-            scr2.y = coord.y;
-            drawCalibPoint(gfx, ref3);
 
-            releaseNeeded = true;
-            break;
-        case 2:
-            scr3.x = coord.x;
-            scr3.y = coord.y;
-
-            releaseNeeded = true;
+        do {
+            if (!touchPanel->getNextCalibratePoint(&x, &y)) {
+                printf("Failed to get next calibrate point\n");
+                break;
+            }
 
-            touch.calibrate(ref1, ref2, ref3, scr1, scr2, scr3);
-            calibrated = true;
-            break;
-        }
-    }
-
-    gfx.fillScreen(BLACK);
-}
+            printf("calib: x=%d, y=%d\n", x, y);
+            gfx.fillScreen(BLACK);
+            gfx.setCursor(0, 0);
+            gfx.writeString("Calibrate Touch Screen");
+            gfx.drawRect(x-5, y-5, 10, 10, WHITE);
 
 
-static void demo3(GFXFb &gfx) {
-    TSC2046::touchCoordinate_t coord;
+            if (!touchPanel->waitForCalibratePoint(&hasMorePoints, 0)) {
+                printf("Failed waiting for calibration point\n");
+                break;
+            }
 
-    touchCalibrate(gfx);
+        } while(hasMorePoints);
+
+        printf("Calibration done\n");
+
+        gfx.fillScreen(BLACK);
+
 
-    while(1) {
-        touch.read(coord);
-        if (coord.z > 0) {
-            gfx.drawPixel(coord.x, coord.y, WHITE);
+        while(1) {
+            touchPanel->read(coord);
+            if (coord.z > 0) {
+                gfx.drawPixel(coord.x, coord.y, WHITE);
+            }
         }
-    }
+
+    } while (0);
+
+
+
+    while(1);
 }
 
 /******************************************************************************
@@ -270,16 +242,16 @@
 
     EaLcdBoard::Result result;
     LcdController::Config lcdCfg;
-    uint32_t frameBuf1 = (uint32_t) SDRAM_BASE;
+    EaLcdBoard::TouchParams_t touchParams;
+    uint32_t frameBuf1 = 0;
+    TouchPanel* tp = NULL;
 
-
-    // framebuffer is put in SDRAM
+    // frame buffer is put in SDRAM
     if (sdram_init() == 1) {
-        printf("Failed to initialize SDRAM\n");
-        return 1;
+    	printf("Failed to initialize SDRAM\n");
+    	return 1;
     }
 
-
     do {
 
         result = lcdBoard.open(NULL, NULL);
@@ -288,11 +260,6 @@
             break;
         }
 
-        result = lcdBoard.setFrameBuffer(frameBuf1);
-        if (result != EaLcdBoard::Ok) {
-            printf("Failed to activate frameBuffer: %d\n", result);
-            break;
-        }
 
         result = lcdBoard.getLcdConfig(&lcdCfg);
         if (result != EaLcdBoard::Ok) {
@@ -300,8 +267,51 @@
             break;
         }
 
+
+        // allocate framebuffer, width x height x 2 (2 bytes = 16 bit color data)
+        frameBuf1 = (uint32_t)malloc(lcdCfg.width*lcdCfg.height*2);
+        if (frameBuf1 == 0) {
+            printf("Failed to allocate frame buffer\n");
+            break;
+        }
+
+
+        result = lcdBoard.setFrameBuffer(frameBuf1);
+        if (result != EaLcdBoard::Ok) {
+            printf("Failed to activate frameBuffer: %d\n", result);
+            break;
+        }
+
+
         memset((void*)frameBuf1, 0x0, lcdCfg.width*lcdCfg.height*2);
 
+        result = lcdBoard.getTouchParameters(&touchParams);
+        if (result != EaLcdBoard::Ok) {
+            printf("Failed to get touch panel parameters: %d\n", result);
+            break;
+        }
+
+        // create touch panel
+        switch(touchParams.panelId) {
+        case EaLcdBoard::TouchPanel_AR1021:
+            printf("creating AR1021 touch panel\n");
+            tp = new AR1021(P2_27, P2_26, P2_22, P2_21, P2_25);
+            break;
+
+        case EaLcdBoard::TouchPanel_TSC2046:
+        case EaLcdBoard::TouchPanelUnknown:
+        default:
+            // we always default to TSC2046 even if we cannot
+            // detect which panel is used
+
+            printf("creating TSC2046 touch panel\n");
+
+            tp = new TSC2046(P2_27, P2_26, P2_22, P2_21);
+            break;
+
+        }
+
+
         initSuccessful = true;
 
 
@@ -318,8 +328,8 @@
             demo1(gfx);
             wait_ms(5000);
             demo2(gfx);
-            wait_ms(15000);
-            demo3(gfx);
+            wait_ms(5000);
+            demo3(gfx, tp);
         }
 
     }