EmbeddedArtists AB
/
app_gfx
Example using the GFX graphical library, EaLcdBoard, TSC2046 touch screen and SDRAM initialization
main.cpp
- Committer:
- embeddedartists
- Date:
- 2013-09-26
- Revision:
- 0:79a419828f86
- Child:
- 2:661a997f0ec7
File content as of revision 0:79a419828f86:
/****************************************************************************** * Includes *****************************************************************************/ #include "mbed.h" #include "LcdController.h" #include "EaLcdBoard.h" #include "TSC2046.h" #include "sdram.h" #include "wchar.h" #include "GFXFb.h" /****************************************************************************** * Typedefs and defines *****************************************************************************/ /****************************************************************************** * Local variables *****************************************************************************/ // 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, DARKGRAY, WHITE, RED, GREEN, BLUE, MAGENTA, CYAN, YELLOW, LIGHTRED, LIGHTGREEN, LIGHTBLUE, LIGHTMAGENTA, LIGHTCYAN, LIGHTYELLOW }; /****************************************************************************** * Local functions *****************************************************************************/ static uint16_t random(uint16_t max) { uint16_t temp; temp = rand(); temp = temp % max; return temp; } static void demo1(GFXFb &gfx) { int16_t x0 = 0; int16_t y0 = 0; int16_t radius = 0; int color = 0; int fill = 0; gfx.fillScreen(BLACK); for (int i = 0; i < 100; i++) { x0 = random(gfx.width()); y0 = random(gfx.height()); color = random(16); radius = random(50); fill = random(2); if (!fill) { gfx.drawCircle(x0, y0, radius, colors[color]); } else { gfx.fillCircle(x0, y0, radius, colors[color]); } wait_ms(10); } } static void demo2(GFXFb &gfx) { int32_t margin = 5; int32_t rowHeight = gfx.height() / 3; int32_t colWidth = gfx.width() / 3; int32_t graphPosX = gfx.getStringWidth("drawRoundRect"); int32_t maxGraphW = colWidth - graphPosX; gfx.fillScreen(BLACK); // ############## // C O L U M N 1 // ############## // drawLine gfx.setCursor(0, rowHeight/2); gfx.writeString("drawLine"); gfx.drawLine(0+graphPosX+margin, margin, 0+graphPosX+maxGraphW-margin, rowHeight-margin, colors[1]); // drawRect gfx.setCursor(0, rowHeight+rowHeight/2); gfx.writeString("drawRect"); gfx.drawRect(0+graphPosX+margin, rowHeight+margin, maxGraphW-2*margin, rowHeight-margin, colors[2]); // fillRect gfx.setCursor(0, rowHeight*2+rowHeight/2); gfx.writeString("fillRect"); gfx.fillRect(0+graphPosX+margin, rowHeight*2+margin, maxGraphW-2*margin, rowHeight-margin, colors[3]); // ############## // C O L U M N 2 // ############## // drawCircle gfx.setCursor(colWidth, rowHeight/2); gfx.writeString("drawCircle"); gfx.drawCircle(colWidth+graphPosX+maxGraphW/2, rowHeight/2, rowHeight/2-2*margin, colors[4]); // fillCircle gfx.setCursor(colWidth, rowHeight+rowHeight/2); gfx.writeString("fillCircle"); gfx.fillCircle(colWidth+graphPosX+maxGraphW/2, rowHeight+rowHeight/2, rowHeight/2-2*margin, colors[5]); // drawTriangle gfx.setCursor(colWidth, rowHeight*2+rowHeight/2); gfx.writeString("drawTriangle"); gfx.drawTriangle(colWidth+graphPosX+margin, rowHeight*3-margin, colWidth+graphPosX+maxGraphW/2, rowHeight*2+margin, colWidth+graphPosX+maxGraphW-margin, rowHeight*3-margin, colors[6]); // ############## // C O L U M N 3 // ############## // fillTriangle gfx.setCursor(colWidth*2, rowHeight/2); gfx.writeString("fillTriangle"); gfx.fillTriangle(colWidth*2+graphPosX+margin, rowHeight-margin, colWidth*2+graphPosX+maxGraphW/2, margin, colWidth*2+graphPosX+maxGraphW-margin, rowHeight-margin, colors[7]); // drawRoundRect gfx.setCursor(colWidth*2, rowHeight+rowHeight/2); gfx.writeString("drawRoundRect"); gfx.drawRoundRect(colWidth*2+graphPosX+margin, rowHeight+margin, maxGraphW-2*margin, rowHeight-margin, 10, colors[8]); // fillRoundRect gfx.setCursor(colWidth*2, rowHeight*2+rowHeight/2); gfx.writeString("fillRoundRect"); gfx.fillRoundRect(colWidth*2+graphPosX+margin, rowHeight*2+margin, maxGraphW-2*margin, rowHeight-margin, 10, colors[9]); } 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 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}; 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; } if (releaseNeeded && !touchReleased) { continue; } 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; touch.calibrate(ref1, ref2, ref3, scr1, scr2, scr3); calibrated = true; break; } } gfx.fillScreen(BLACK); } static void demo3(GFXFb &gfx) { TSC2046::touchCoordinate_t coord; touchCalibrate(gfx); while(1) { touch.read(coord); if (coord.z > 0) { gfx.drawPixel(coord.x, coord.y, WHITE); } } } /****************************************************************************** * Main function *****************************************************************************/ int main (void) { bool initSuccessful = false; EaLcdBoard::Result result; LcdController::Config lcdCfg; uint32_t frameBuf1 = (uint32_t) SDRAM_BASE; // framebuffer is put in SDRAM if (sdram_init() == 1) { printf("Failed to initialize SDRAM\n"); return 1; } do { result = lcdBoard.open(NULL, NULL); if (result != EaLcdBoard::Ok) { printf("Failed to open display: %d\n", result); 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) { printf("Failed to get LCD configuration: %d\n", result); break; } memset((void*)frameBuf1, 0x0, lcdCfg.width*lcdCfg.height*2); initSuccessful = true; } while(0); if (initSuccessful) { GFXFb gfx(lcdCfg.width, lcdCfg.height, (uint16_t*)frameBuf1); while (1) { demo1(gfx); wait_ms(5000); demo2(gfx); wait_ms(15000); demo3(gfx); } } else { printf("Couldn't start demo -> Initialization failed\n"); } return 0; }