aaa

Dependencies:   SeeedStudioTFTv2 TFT_fonts mbed

Fork of Seeed_TFT_Touch_Shield by Shields

Revision:
4:5b088ba4f1f1
Parent:
2:5c2f6ff36ff1
Child:
5:4add240cef16
--- a/main.cpp	Fri Jul 25 12:26:33 2014 +0000
+++ b/main.cpp	Thu Oct 23 23:13:57 2014 +0000
@@ -39,62 +39,607 @@
 #define PIN_BL_TFT      D7
 #define PIN_CS_SD       D4
 
+Timer t;
+Serial pc(USBTX,USBRX);
 SeeedStudioTFTv2 TFT(PIN_XP, PIN_XM, PIN_YP, PIN_YM, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_DC_TFT, PIN_BL_TFT, PIN_CS_SD);
 
+void printKey(int x, int y, char* digit, char* alphas)
+{
+    TFT.foreground(Blue);
+    TFT.set_font((unsigned char*) Arial24x23);
+    TFT.locate(x,y);
+    TFT.printf(digit);
+    TFT.foreground(Black);
+    TFT.set_font((unsigned char*) Arial12x12);
+    TFT.locate(x+15,y+10);
+    TFT.printf(alphas);
+}
+
+void initKeypad()
+{
+    TFT.background(White);
+    TFT.cls();
+    TFT.foreground(Black);
+    TFT.set_font((unsigned char*) Arial12x12);
+    TFT.locate(210,84);
+    TFT.printf("Del");
+    TFT.line(0,100,240,100,Black);
+    TFT.rect(205,80,235,95,Black);
+    TFT.line(205,80,195,95,White);
+    TFT.line(192,87,205,80,Black);
+    TFT.line(192,87,205,95,Black);
+    printKey(20,120,"1","   ");
+    printKey(95,120,"2","ABC");
+    printKey(170,120,"3","DEF");
+    printKey(20,160,"4","GHI");
+    printKey(95,160,"5","JKL");
+    printKey(170,160,"6","MNO");
+    printKey(20,200,"7","PQRS");
+    printKey(95,200,"8","TUV");
+    printKey(170,200,"9","WXYZ");
+    printKey(20,240,"*","");
+    printKey(95,240,"0","+");
+    printKey(170,240,"#","");
+    printKey(20,280,"<--","");
+}
+
+int getKey()
+{
+    int key = -1;
+    point p;
+    int count = 0;
+    if(TFT.getTouch(p) == TFT.NO || TFT.getTouch(p) == TFT.MAYBE)
+    { return key;
+        }
+    while(TFT.getTouch(p) == TFT.YES)
+    {
+        count++;
+    }
+    if(count > 300)
+    {
+        //TFT.foreground(Black);
+//        TFT.set_font((unsigned char*) Arial12x12);
+//        TFT.locate(0,0);
+//        TFT.printf("X:%d, Y:%d\n\r",p.x,p.y);
+        if (p.x>2700 && p.x<7250)
+        {
+            if(p.x<3750)
+            {
+                if(p.y<3000)
+                    key = 1;    //1
+                else if(p.y<5250)
+                    key = 2;    //2
+                else
+                    key = 3;    //3
+            }
+            else if(p.x<4600)
+            {
+                if(p.y<3000)
+                    key = 4;    //4
+                else if(p.y<5250)
+                    key = 5;    //5
+                else
+                    key = 6;    //6
+            }
+            else if(p.x<5400)
+            {
+                if(p.y<3000)
+                    key = 7;    //7
+                else if(p.y<5250)
+                    key = 8;    //8
+                else
+                    key = 9;    //9
+            }
+            else if(p.x<6300)
+            {
+                if(p.y<3000)
+                    key = 42;   //*
+                else if(p.y<5250)
+                    key = 0;    //0
+                else
+                    key = 35;   //#
+            }
+            else
+            {
+                if(p.y<3000)
+                    key = 60;  // back
+                else
+                    key = 43;   // go (call/text)
+            }
+        }
+        if(p.x > 2200 && p.x < 2500 && p.y > 6200 && p.y < 7000)
+            key = 127;
+    }
+    return key;
+}
+
+int getAlpha(int &touchCount)
+{
+    touchCount = 1;
+    int key, begin, end;
+    int prevKey;
+    do  {
+        prevKey = key = getKey();
+    }while(prevKey == -1);
+        
+    
+    t.reset();
+    t.start();
+    begin = t.read_ms();
+    end = t.read_ms();
+    while((end - begin) < 2000)
+    { 
+        key = getKey();
+        end = t.read_ms();
+        if(key == -1) continue;
+        
+        touchCount++;
+        if(!(prevKey == key)){
+            break;
+        }
+        pc.printf("ok curr count :%d key : %d  prev_key : %d \r\n", touchCount,key,  prevKey);
+    }
+    t.stop();
+    pc.printf("ok count is :%d \r\n", touchCount);
+    return prevKey;
+}
+
+int getText(int &touchCount)
+{
+    int key = -1, prevKey = -1;
+    point p;
+    int count = 0;
+    touchCount = 0;
+    //int start=0,end=0;
+    bool firstTouch = false;
+    if(TFT.getTouch(p) == TFT.YES)
+    {
+        t.start();
+        while( t.read()< 2 && prevKey == key)
+        {
+            //while(TFT.getTouch(p) == TFT.NO || TFT.getTouch(p) == TFT.MAYBE)
+    //        {}
+            while(TFT.getTouch(p) == TFT.YES  || TFT.getTouch(p) == TFT.MAYBE)
+            {
+                count++;
+            }
+            //TFT.printf("count %d\r\n",count);
+            if(count > 400)
+            {
+                //TFT.foreground(Black);
+    //            TFT.set_font((unsigned char*) Arial12x12);
+    //            TFT.locate(0,0);
+    //            TFT.printf("X:%d, Y:%d\n\r",p.x,p.y);
+    //            TFT.printf("count %d\r\n",count);
+                if(p.x>2700 && p.x<7250)
+                {
+                    if(p.x<3750)
+                    {
+                        if(p.y<3000)
+                            key = 1;    //1
+                        else if(p.y<5250)
+                            key = 2;    //2
+                        else
+                            key = 3;    //3
+                    }
+                    else if(p.x<4600)
+                    {
+                        if(p.y<3000)
+                            key = 4;    //4
+                        else if(p.y<5250)
+                            key = 5;    //5
+                        else
+                            key = 6;    //6
+                    }
+                    else if(p.x<5400)
+                    {
+                        if(p.y<3000)
+                            key = 7;    //7
+                        else if(p.y<5250)
+                            key = 8;    //8
+                        else
+                            key = 9;    //9
+                    }
+                    else if(p.x<6300)
+                    {
+                        if(p.y<3000)
+                            key = 42;   //*
+                        else if(p.y<5250)
+                            key = 0;    //0
+                        else
+                            key = 35;   //#
+                    }
+                    else
+                    {
+                        if(p.y<3000)
+                            key = 60;  // back
+                        else
+                            key = 43;   // go (call/text)
+                    }
+                }
+                if(p.x > 2200 && p.x < 2500 && p.y > 6200 && p.y < 7000)
+                    key = 127;
+                if(prevKey != -1 && prevKey == key) {
+                    touchCount++;
+                    TFT.printf("\r\ntouchCount %d %d",touchCount,prevKey);
+                    }
+                if(!firstTouch)
+                {
+                    firstTouch = true;
+                    prevKey = key;
+                    touchCount++;
+                }
+            }
+            /*if(t.read()>2) {
+            TFT.locate(20,20);
+            TFT.printf("Khatam\r\n");
+            break;
+            }*/
+            count = 0;
+    //        TFT.printf("jyada ho gaya\r\n");
+        }
+    t.stop();
+    }
+    return prevKey;
+}
+
+bool displayChar(point& cursor, char letter)
+{
+    TFT.locate(cursor.x,cursor.y);
+    if(cursor.x + 16 <= 208)
+        cursor.x += 16;
+    else
+    {
+        cursor.x = 0;
+        if(cursor.y + 21 <= 65 )
+            cursor.y += 21;
+        else
+            return false;
+    }
+    TFT.set_font((unsigned char*) Arial24x23);
+    TFT.printf("%c",letter);
+    return true;
+}
+
 int main()
 {
+    point p;
+    char* cmd = NULL;
     //Configure the display driver
     TFT.background(Black);
     TFT.foreground(White);
     TFT.cls();
-
-    //Print a welcome message
-    TFT.set_font((unsigned char*) Arial12x12);
-    TFT.locate(0,0);
-    TFT.printf("Hello Mbed");
-
-    //Wait for 5 seconds
-    wait(5.0);
-
-    //Draw some graphics
-    TFT.cls();
-    TFT.set_font((unsigned char*) Arial24x23);
-    TFT.locate(100,100);
-    TFT.printf("Graphic");
-
-    TFT.line(0,0,100,0,Green);
-    TFT.line(0,0,0,200,Green);
-    TFT.line(0,0,100,200,Green);
-
-    TFT.rect(100,50,150,100,Red);
-    TFT.fillrect(180,25,220,70,Blue);
-
-    TFT.circle(80,150,33,White);
-    TFT.fillcircle(160,190,20,Yellow);
+    char ip0[] = {' ','0','+'};
+    char ip2[] = {'a','b','c','2'};
+    char ip3[] = {'d','e','f','3'};
+    char ip4[] = {'g','h','i','4'};
+    char ip5[] = {'j','k','l','5'};
+    char ip6[] = {'m','o','p','6'};
+    char ip7[] = {'p','q','r','s','7'};
+    char ip8[] = {'t','u','v','8'};
+    char ip9[] = {'w','x','y','z','9'};
 
-    double s;
-    for (int i = 0; i < 320; i++) {
-        s = 20 * sin((long double)i / 10);
-        TFT.pixel(i, 100 + (int)s, Red);
+    while(true)
+    {
+        //Print a welcome message
+        TFT.background(White);
+        TFT.set_font((unsigned char*) Neu42x35);
+        TFT.foreground(Maroon);
+        TFT.cls();
+        TFT.locate(100,60);
+        TFT.printf("IU");
+        
+        TFT.set_font((unsigned char*) Arial24x23);
+        TFT.locate(28,230);
+        TFT.printf("CALL");
+        TFT.circle(60,240,50,Maroon);
+        
+        TFT.locate(140,230);
+        TFT.printf("TEXT");
+        TFT.circle(180,240,50,Maroon);
+    
+        while(true)
+        {
+            if (TFT.getTouch(p) == TFT.YES)
+            {
+                if(p.x >= 4500 && p.x <= 6600)
+                {
+                    if(p.y >= 1500 && p.y <= 3800)
+                    {
+                        //call
+                        cmd = "Call";
+                        break;
+                    }
+                    else if(p.y >= 4500 && p.y <= 7100)
+                    {
+                        //text
+                        cmd = "Text";
+                        break;
+                    }
+                    else
+                    {}
+                    
+                }
+            }else if(cmd == "RING"){
+                }
+        }
+        
+        if(strcmp(cmd, "Call") == 0) 
+        {
+            initKeypad();
+            printKey(130,280,"CALL","" );
+            point cursor;
+            cursor.x = 0;
+            cursor.y = 5;
+            char buffer[140];
+            char *ptr = buffer;
+            while(strcmp(cmd, "Call") == 0)
+            {
+                TFT.foreground(Black);
+                int key = getKey();
+                switch(key)
+                {
+                    case 0:
+                        if(displayChar(cursor, '0')) {
+                            *ptr = '0';
+                            ptr++;
+                        }
+                        break;
+                    case 1:
+                        if(displayChar(cursor, '1')) {
+                            *ptr = '1';
+                            ptr++;
+                        }
+                        break;
+                    case 2:
+                        if(displayChar(cursor, '2')) {
+                            *ptr = '2';
+                            ptr++;
+                        }
+                        break;
+                    case 3:
+                        if(displayChar(cursor, '3')) {
+                            *ptr = '3';
+                            ptr++;
+                        }
+                        break;
+                    case 4:
+                        if(displayChar(cursor, '4')) {
+                            *ptr = '4';
+                            ptr++;
+                        }
+                        break;
+                    case 5:
+                        if(displayChar(cursor, '5')) {
+                            *ptr = '5';
+                            ptr++;
+                        }
+                        break;
+                    case 6:
+                        if(displayChar(cursor, '6')) {
+                            *ptr = '6';
+                            ptr++;
+                        }
+                        break;
+                    case 7:
+                        if(displayChar(cursor, '7')) {
+                            *ptr = '7';
+                            ptr++;
+                        }
+                        break;
+                    case 8:
+                        if(displayChar(cursor, '8')) {
+                            *ptr = '8';
+                            ptr++;
+                        }
+                        break;
+                    case 9:
+                        if(displayChar(cursor, '9')) {
+                            *ptr = '9';
+                            ptr++;
+                        }
+                        break;
+                    case 35:
+                        if(displayChar(cursor, '#')) {
+                            *ptr = '#';
+                            ptr++;
+                        }
+                        break;
+                    case 42:
+                        if(displayChar(cursor, '*')) {
+                            *ptr = '*';
+                            ptr++;
+                        }
+                        break;
+                    case 43:
+                        cmd = "Make";
+                        break;
+                    case 60:
+                        cmd = "Back";
+                        break;
+                    case 127:
+                        if(ptr > buffer)
+                        {
+                            ptr--;
+                            if(cursor.x - 16 >= 0)
+                                cursor.x -= 16;
+                            else
+                            {
+                                cursor.x = 224;
+                                if(cursor.y - 21 >= 5 )
+                                cursor.y -= 21;
+                            }
+                            TFT.locate(cursor.x,cursor.y);
+                            TFT.printf(" ");
+                        }
+                        break;
+                }
+            }
+            if(strcmp(cmd, "Make") == 0)
+            {
+                TFT.background(Black);
+                TFT.foreground(White);
+                TFT.cls();
+                int i=0;
+                cursor.x = 0;
+                cursor.y = 5;
+                TFT.locate(0,0);
+                while(&buffer[i] < ptr)
+                    displayChar(cursor, buffer[i++]);
+                TFT.line(0,270,240,270,Red);
+                TFT.foreground(Red);
+                TFT.locate(60,280);
+                TFT.printf("Hang Up");
+                while(strcmp(cmd, "Make") == 0)
+                {
+                    if(TFT.getTouch(p) == TFT.YES && p.x >= 6300 && p.x <= 7250)
+                        cmd = "Back";
+                }
+            }
+            if(strcmp(cmd, "Back") == 0)
+            {
+                //Home screen
+            }    
+        }
+        else if(strcmp(cmd, "Text") == 0)
+        {
+            initKeypad();
+            printKey(130,280,"SEND","" );
+            point cursor;
+            cursor.x = 0;
+            cursor.y = 5;
+            char buffer[140];
+            char *ptr = buffer;
+            //TFT.line(80,100,80,320,Black);
+//            TFT.line(160,100,160,320,Black);
+//            TFT.line(0,100,240,100,Black);
+//            TFT.line(0,150,240,150,Black);
+//            TFT.line(0,190,240,190,Black);
+//            TFT.line(0,230,240,230,Black);
+//            TFT.line(0,270,240,270,Black);
+            while(strcmp(cmd, "Text") == 0)
+            {
+                int touchCount=0;
+                int key = getAlpha(touchCount);
+                switch(key)
+                {
+                    case 0:
+                        touchCount = (touchCount-1)%(sizeof(ip0)/sizeof(ip0[0]));
+                        if(displayChar(cursor, ip0[touchCount])) {
+                            *ptr = ip0[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 1:
+                        if(displayChar(cursor, '1')) {
+                            *ptr = '1';
+                            ptr++;
+                        }
+                        break;
+                    case 2:
+                        touchCount = (touchCount-1)%(sizeof(ip2)/sizeof(ip2[0]));
+                        if(displayChar(cursor, ip2[touchCount])) {
+                            *ptr = ip2[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 3:
+                        touchCount = (touchCount-1)%(sizeof(ip3)/sizeof(ip3[0]));
+                        if(displayChar(cursor, ip3[touchCount])) {
+                            *ptr = ip3[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 4:
+                        touchCount = (touchCount-1)%(sizeof(ip4)/sizeof(ip4[0]));
+                        if(displayChar(cursor, ip4[touchCount])) {
+                            *ptr = ip4[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 5:
+                        touchCount = (touchCount-1)%(sizeof(ip5)/sizeof(ip5[0]));
+                        if(displayChar(cursor, ip5[touchCount])) {
+                            *ptr = ip5[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 6:
+                        touchCount = (touchCount-1)%(sizeof(ip6)/sizeof(ip6[0]));
+                        if(displayChar(cursor, ip6[touchCount])) {
+                            *ptr = ip6[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 7:
+                        touchCount = (touchCount-1)%(sizeof(ip7)/sizeof(ip7[0]));
+                        if(displayChar(cursor, ip7[touchCount])) {
+                            *ptr = ip7[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 8:
+                        touchCount = (touchCount-1)%(sizeof(ip8)/sizeof(ip8[0]));
+                        if(displayChar(cursor, ip8[touchCount])) {
+                            *ptr = ip8[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 9:
+                        touchCount = (touchCount-1)%(sizeof(ip9)/sizeof(ip9[0]));
+                        if(displayChar(cursor, ip9[touchCount])) {
+                            *ptr = ip9[touchCount];
+                            ptr++;
+                        }
+                        break;
+                    case 35:
+                        if(displayChar(cursor, '#')) {
+                            *ptr = '#';
+                            ptr++;
+                        }
+                        break;
+                    case 42:
+                        if(displayChar(cursor, '*')) {
+                            *ptr = '*';
+                            ptr++;
+                        }
+                        break;
+                    case 43:
+                        cmd = "Send";
+                        break;
+                    case 60:
+                        cmd = "Back";
+                        break;
+                    case 127:
+                        if(ptr > buffer)
+                        {
+                            ptr--;
+                            if(cursor.x - 16 >= 0)
+                                cursor.x -= 16;
+                            else
+                            {
+                                cursor.x = 224;
+                                if(cursor.y - 21 >= 5 )
+                                cursor.y -= 21;
+                            }
+                            TFT.locate(cursor.x,cursor.y);
+                            TFT.printf(" ");
+                        }
+                        break; 
+                }
+            }
+            if(strcmp(cmd,"Send") == 0)
+            {
+                TFT.background(Black);
+                TFT.foreground(White);
+                TFT.cls();
+                TFT.locate(90,60);
+                TFT.printf("Text");
+                TFT.locate(90,95);
+                TFT.printf("Sent!");
+                wait(2.5);
+                cmd = "Back";
+            }
+        }
     }
-
-    //Wait for 5 seconds
-    wait(5.0);
-
-    //Multiple fonts
-    TFT.foreground(White);
-    TFT.background(Blue);
-    TFT.cls();
-    TFT.set_font((unsigned char*) Arial24x23);
-    TFT.locate(0,0);
-    TFT.printf("Different Fonts:");
-    TFT.set_font((unsigned char*) Neu42x35);
-    TFT.locate(0,30);
-    TFT.printf("Hello Mbed 1");
-    TFT.set_font((unsigned char*) Arial24x23);
-    TFT.locate(20,80);
-    TFT.printf("Hello Mbed 2");
-    TFT.set_font((unsigned char*) Arial12x12);
-    TFT.locate(35,120);
-    TFT.printf("Hello Mbed 3");
 }