aaaa

Dependencies:   SeeedStudioTFTv2 TFT_fonts mbed

Fork of Seeed_TFT_Touch_Shield by Bhavik Bhuta

Files at this revision

API Documentation at this revision

Comitter:
uswickra
Date:
Sat Oct 25 20:21:17 2014 +0000
Parent:
4:5b088ba4f1f1
Child:
6:2a920ec91d2b
Commit message:
new changes

Changed in this revision

Modem.cpp Show annotated file Show diff for this revision Revisions of this file
Modem.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Modem.cpp	Sat Oct 25 20:21:17 2014 +0000
@@ -0,0 +1,401 @@
+#include "Modem.h"
+
+Modem::Modem(PinName tx, PinName rx, int baudRate,char *default_number):serial_modem(tx,rx)
+{
+    this->default_number = default_number;
+//    serial_modem.baud(baudRate);
+    buffer_p = 0;
+    read_trail = false;
+    read_buffer[BUF_SIZE]='\0';
+}
+
+
+/***********************************************
+Utility/debug Functions
+***********************************************/
+void cleanBuffer(char *buffer, int count)
+{
+    for(int i=0; i < count; i++) {
+        buffer[i] = '\0';
+    }
+}
+
+int Modem::loopHandle(void)
+{
+    char gprsBuffer[100];
+    int i;
+    char *s = NULL;
+    while(serial_modem.readable()) {
+        char c = serial_modem.getc();
+    }
+    wait(0.5);
+START:
+    cleanBuffer(gprsBuffer,100);
+    i = 0;
+    while(1) {
+        if(serial_modem.readable()) {
+            timeCnt.start();  // start timer
+            while(1) {
+                while (serial_modem.readable()) {
+                    char c = serial_modem.getc();
+                    if (c == '\r' || c == '\n') c = '$';
+                    gprsBuffer[i] = c;
+                    i++;
+                    if(i > 100) {
+                        i = 0;
+                        break;
+                    }
+                }
+                if(timeCnt.read() > 2) {          // time out
+                    timeCnt.stop();
+                    timeCnt.reset();
+                    break;
+                }
+            }
+            break;
+        }
+    }
+    if(NULL != strstr(gprsBuffer,"RING")) {
+        return MESSAGE_RING;
+    } else if(NULL != (s = strstr(gprsBuffer,"+CMT"))) { //SMS: $$+CMTI: "SM",24$$
+        if(NULL != (s = strstr(gprsBuffer,"+32"))) {
+            s += 6;
+            int i = 0;
+            cleanBuffer(messageBuffer,SMS_MAX_LENGTH);
+            while((*s != '$')&&(i < SMS_MAX_LENGTH-1)) {
+                messageBuffer[i++] = *(s++);
+            }
+            messageBuffer[i] = '\0';
+            return MESSAGE_SMS;
+        } else {
+            goto START;
+        }
+    } else {
+        goto START;
+    }
+}
+
+
+void Modem::clean_buffer()
+{
+    int i = 0;
+    for(i = 0 ; i < BUF_SIZE ; i ++) {
+        read_buffer[i] = '\0';
+    }
+}
+
+void Modem::reset_buffer()
+{
+    buffer_p = 0;
+    clean_buffer();
+}
+
+void Modem::debug_cmd_buffer()
+{
+//    TFT.printf("[DEBUG] ");
+    int i = 0 ;
+    for(i = 0 ; i < BUF_SIZE ; i ++) {
+//        TFT.putc(read_buffer[i]);
+    }
+//    TFT.printf("\r\n");
+}
+
+int Modem::find_pattern_end(char *pattern, char *buffer, int size)
+{
+    int i = 0;
+    int final_state = strlen(pattern);
+    int state = 0;
+    for (i = 0; i < size; i++) {
+        char current = buffer[i];
+        if (pattern[state] == current) {
+            state += 1;
+            continue;
+        }
+        if (state == final_state) {
+            return i;
+        }
+        state = 0;
+    }
+
+    return -1;
+}
+
+/*******************************************************
+Control/Init/Setup Functions for SeedsStudto GPRS modem
+********************************************************/
+
+int Modem::init()
+{
+    int i = 0;
+    int ret = 0 ;
+    for(i = 0 ; i < 10 ; i++) {
+        ret = check_AT();
+        wait(1);
+    }
+
+    if(ret) {
+        ret = check_PIN();
+    }
+    if(ret) {
+        ret = setup_SMS();
+    }
+    return ret;
+};
+
+int Modem::check_AT()
+{
+    int ret = sendCmdWaitResp("AT\r\n","OK", 5);
+    if(ret == 0) {
+        return -1 ;
+    }
+
+    return 1;
+}
+
+int Modem::check_PIN()
+{
+    int ret = sendCmdWaitResp("AT+CPIN?\r\n","+CPIN: READY", 5);
+
+    //debug code
+    debug_cmd_buffer();
+
+    if(ret == 0) {
+        return -1 ;
+    }
+
+    return 1;
+}
+
+int Modem::setup_SMS()
+{
+//    sendCmdWaitResp("AT+CPMS=\"SM\",\"BM\"\r\n", "OK", 5);
+//    int ret = sendCmdWaitResp("AT+CNMI=2,2\r\n", "OK", 5);
+    int ret = 1 ;
+    if(ret) {
+        ret = sendCmdWaitResp("AT+CMGF=1\r\n","OK", 5);
+    }
+    //debug code
+    debug_cmd_buffer();
+    if(ret == 0) {
+        return -1 ;
+    }
+
+    return 1;
+}
+
+/***********************************************
+Start of Primary Interface Functions
+***********************************************/
+
+int Modem::call_phone(char* number)
+{
+    //enable optional caller presentation
+    sendCmdWaitResp("AT+COLP=1\r\n","OK", 5);
+    //send actual call
+    wait(1);
+    debug_cmd_buffer();
+    int ret = 0 ;
+    char temp[30];
+
+    if(number == NULL) {
+        snprintf(temp, 30, "\r\nATD%s;\r\n",default_number);
+    } else {
+        snprintf(temp, 30, "\r\nATD%s;\r\n",number);
+    }
+    ret = sendCmdWaitResp(temp,"OK", 5);
+//    ret = sendCmdWaitResp("\r\nATD+13174806512;\r\n","OK", 5);
+    //debug code
+    debug_cmd_buffer();
+
+    if(ret == 0) {
+        return -1 ;
+    }
+    return ret ;
+};
+
+void Modem::hangup_phone()
+{
+    int ret = sendCmdWaitResp("ATH\r\n","OK", 5);
+    wait(1);
+}
+
+void Modem::recv_phone()
+{
+    int ret = sendCmdWaitResp("ATA\r\n","OK", 5);
+}
+
+void Modem::get_clck(char* msg, int size)
+{
+    int ret = sendCmdWaitResp("AT+CCLK?\r\n","OK", 5);
+    //if returned OK
+    debug_cmd_buffer();
+    if(ret == 1) {
+        char *pattern = "+CCLK:";
+        int index = find_pattern_end(pattern, read_buffer, BUF_SIZE);
+        int state = 0;
+        int curr = index + 2;
+        int end = curr + 17 ;
+        while(curr < end) {
+            msg[state]=read_buffer[curr];
+            state++;
+            curr++;
+        }
+        msg[state] = '\0';
+    }
+}
+
+int Modem::send_sms(char* number, char* msg)
+{
+    char temp[60];
+    while(read_device_ready()) {
+        char c = read_device();
+    }
+
+    if(number == NULL) {
+        snprintf(temp, 60, "AT+CMGS=\"%s\"\r\n",default_number);
+    } else {
+        snprintf(temp, 60, "AT+CMGS=\"%s\"\r\n",number);
+    }
+
+    sendCmdWaitResp(temp, ">", 5);
+    debug_cmd_buffer();
+    wait(3);
+    //now write the actual message
+    write_device_str(msg);
+    //final ctrl+z character in ascii
+    write_device((char)0x1a);
+
+    return 1 ;
+}
+
+void Modem::recv_sms(char* msg, int msg_size ,int index)
+{
+    char temp[30];
+    snprintf(temp, 30, "AT+CMGR=%d\r\n",index);
+    read_trail = true;
+    int ret = sendCmdWaitResp("AT+CMGL=\"ALL\"\r\n","OK", 30);
+    read_trail = false;
+    wait(3);
+
+    debug_cmd_buffer();
+    //if returned OK
+//    if(ret == 1) {
+    char *pattern = "+CMT:";
+    int in = find_pattern_end(pattern, read_buffer, BUF_SIZE);
+    int state = 0;
+    int curr = in ;
+    int end = BUF_SIZE ;
+    while(curr < end) {
+        msg[state]=read_buffer[curr];
+        state++;
+        curr++;
+    }
+    msg[state] = '\0';
+
+//    }
+    wait(1);
+//    ret = sendCmdWaitResp("AT+CMGD=1,4\r\n","OK", 5);
+}
+
+/***********************************************
+Start of Low Level Functions
+***********************************************/
+
+bool Modem::read_device_ready()
+{
+    return serial_modem.readable();
+}
+
+void Modem::cls()
+{
+//    TFT.locate(0,0);
+//    TFT.cls();
+
+}
+
+char Modem::read_device()
+{
+    return serial_modem.getc();
+}
+
+int Modem::write_device(char c)
+{
+    int ret = 0;
+    while(!serial_modem.writeable()) {
+        //wait until writeable
+    }
+    ret = serial_modem.putc(c);
+    if(ret != -1)
+        return 0;
+    return ret;
+};
+
+
+int Modem::write_device_str(char* str)
+{
+    int ret = 0;
+    while(!serial_modem.writeable()) {
+        //wait until writeable
+    }
+
+    ret = serial_modem.puts(str);
+    if(ret != -1)
+        return 0;
+
+    return ret;
+}
+
+int Modem::sendCmdWaitResp(char* cmd, char* response, int timeout)
+{
+    //reset buffer pointer
+    reset_buffer();
+    //send command and wait until respose
+    write_device_str(cmd);
+
+    //wait
+    Timer my_timer;
+    my_timer.start();
+    float start = my_timer.read();
+    float elapsed = 0 ;
+
+    int expected_resp_len = strlen(response);
+    int curr_len = 0 ;
+    while(1) {
+        //ok start waiting game ;)
+        if(read_device_ready()) {
+            char rec_char = read_device();
+//            pc.putc(rec_char);
+            curr_len++;
+            if(rec_char != response[curr_len - 1]) {
+                //we haven't recieved expected response
+                //reset
+                curr_len = 0 ;
+            }
+            //update characters read so far in to the buffer (circular)
+            read_buffer[buffer_p] = rec_char;
+            buffer_p = (buffer_p + 1) % BUF_SIZE;
+
+            if(!read_trail && curr_len == expected_resp_len) {
+                //at this point response is matched and we have waited for the length of response
+                return 1 ;
+            }
+        }
+
+        elapsed = my_timer.read() - start;
+        if(elapsed > timeout) {
+            //we have waited long enough to succeed, but failed
+            break;
+        }
+    }
+
+
+    while(serial_modem.readable()) {      // display the other thing..
+        char c = serial_modem.getc();
+        //if we enabled reading trailing characters -> read them onto buffer
+        if(read_trail) {
+            read_buffer[buffer_p] = c;
+            buffer_p = (buffer_p + 1) % BUF_SIZE;
+        }
+    }
+    return 0 ;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Modem.h	Sat Oct 25 20:21:17 2014 +0000
@@ -0,0 +1,57 @@
+#pragma once
+#include "mbed.h"
+#include "SeeedStudioTFTv2.h"
+
+extern SeeedStudioTFTv2 TFT;
+//extern Serial pc;
+enum GSM_MESSAGE {
+    MESSAGE_RING = 0,
+    MESSAGE_SMS  = 1,
+    MESSAGE_ERROR
+};
+
+#define BUF_SIZE 100
+#define SMS_MAX_LENGTH          16
+class Modem{
+
+public:
+    Modem(PinName tx, PinName rx, int baudRate,char *default_number);
+    int init();
+    int call_phone(char* number);
+    void recv_phone();
+    void hangup_phone();
+    int send_sms(char* number, char* msg);
+    void recv_sms(char* msg, int msg_size,  int index);
+    void get_clck(char* msg, int size);
+    void cls();
+    Serial serial_modem;
+    Timer timeCnt;
+    int loopHandle(void);
+    char messageBuffer[SMS_MAX_LENGTH];
+
+protected:
+    int sendCmdWaitResp(char* cmd, char* response, int timeout);    
+    int write_device(char c);
+    int write_device_str(char* str);
+    bool read_device_ready();
+    char read_device();
+    
+    int check_AT();
+    int check_PIN();
+    int setup_SMS();
+    
+    char read_buffer[BUF_SIZE+1];
+    int buffer_p;
+    void clean_buffer();
+    void reset_buffer();
+
+    
+private:
+    
+    char* default_number;
+    bool read_trail;
+    void debug_cmd_buffer();
+    int find_pattern_end(char *pattern, char *buffer, int size);
+    
+    
+};
\ No newline at end of file
--- a/main.cpp	Thu Oct 23 23:13:57 2014 +0000
+++ b/main.cpp	Sat Oct 25 20:21:17 2014 +0000
@@ -42,6 +42,33 @@
 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);
+#define PHONE_NUMBER  "+13174806512"
+#include "Modem.h"
+
+Modem gsm (D10, D2, 19200, PHONE_NUMBER);
+DigitalOut modem_power(D9);
+char* cmd = NULL;
+
+void messageHandle(void)
+{        
+     __disable_irq();
+    int messageType = gsm.loopHandle();
+    if(MESSAGE_RING == messageType) {
+//        gsm.answer();
+        cmd = "Ring";
+        pc.printf("recieved call.. \r\n");
+    } else if(MESSAGE_SMS == messageType) {
+        char smsMessage[SMS_MAX_LENGTH];
+//        gsm.getSMS(smsMessage);
+        pc.printf("recieved sms.. \r\n");
+        int i = 0 ;
+        for(i = 0 ; i < SMS_MAX_LENGTH ; i++){
+            pc.putc(smsMessage[i]);
+        }
+    }
+    __enable_irq();
+}
+
 
 void printKey(int x, int y, char* digit, char* alphas)
 {
@@ -153,141 +180,22 @@
     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;
-}
+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'};
 
-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;
+    if(cursor.x + 12 <= 228)
+        cursor.x += 12;
     else
     {
         cursor.x = 0;
@@ -296,29 +204,160 @@
         else
             return false;
     }
-    TFT.set_font((unsigned char*) Arial24x23);
+    TFT.set_font((unsigned char*) Arial12x12);
     TFT.printf("%c",letter);
     return true;
 }
 
+int getAlpha(point& x, char** pt)
+{
+    int touchCount = 1, count = 1;
+    int key, begin, end;
+    int prevKey;
+    do  {
+        prevKey = key = getKey();
+    }while(prevKey == -1);
+        
+    point cursor = x;
+    char* ptr = *pt;
+    t.reset();
+    t.start();
+    begin = t.read_ms();
+    end = t.read_ms();
+    while((end - begin) < 2000)
+    { 
+        cursor = x;
+        ptr = *pt;
+        touchCount = count;
+START:
+        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:
+                    pc.printf("AAYA\r\n");
+                        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;
+                }
+        
+        key = getKey();
+        end = t.read_ms();
+        if(key == -1 && ((end - begin) < 2000)) goto START;
+ 
+        if(!(prevKey == key)){
+            break;
+        }
+        count++;
+        
+        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);
+    
+    x = cursor;
+    *pt = ptr;
+    return prevKey;
+}
+ 
+
+void init_modem(){
+    modem_power = 1 ;  
+    wait(1);
+//    gsm.serial_modem.attach(&messageHandle);
+    while(gsm.init() != 1) {
+//        TFT.printf("waiting... \r\n");
+        wait(1);
+    }  
+}
+
+
 int main()
 {
     point p;
-    char* cmd = NULL;
+
     //Configure the display driver
     TFT.background(Black);
     TFT.foreground(White);
     TFT.cls();
-    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'};
-
+    //initialize the modem - may take few seconds
+    init_modem();
+    
     while(true)
     {
         //Print a welcome message
@@ -326,6 +365,7 @@
         TFT.set_font((unsigned char*) Neu42x35);
         TFT.foreground(Maroon);
         TFT.cls();
+        
         TFT.locate(100,60);
         TFT.printf("IU");
         
@@ -453,6 +493,7 @@
                         break;
                     case 43:
                         cmd = "Make";
+                        *ptr= '\0';
                         break;
                     case 60:
                         cmd = "Back";
@@ -461,11 +502,11 @@
                         if(ptr > buffer)
                         {
                             ptr--;
-                            if(cursor.x - 16 >= 0)
-                                cursor.x -= 16;
+                            if(cursor.x - 12 >= 0)
+                                cursor.x -= 12;
                             else
                             {
-                                cursor.x = 224;
+                                cursor.x = 240;
                                 if(cursor.y - 21 >= 5 )
                                 cursor.y -= 21;
                             }
@@ -484,6 +525,7 @@
                 cursor.x = 0;
                 cursor.y = 5;
                 TFT.locate(0,0);
+                int ret = gsm.call_phone(buffer);
                 while(&buffer[i] < ptr)
                     displayChar(cursor, buffer[i++]);
                 TFT.line(0,270,240,270,Red);
@@ -492,8 +534,11 @@
                 TFT.printf("Hang Up");
                 while(strcmp(cmd, "Make") == 0)
                 {
-                    if(TFT.getTouch(p) == TFT.YES && p.x >= 6300 && p.x <= 7250)
+                    if(TFT.getTouch(p) == TFT.YES && p.x >= 6300 && p.x <= 7250){
+                        gsm.hangup_phone();
                         cmd = "Back";
+                        
+                    }
                 }
             }
             if(strcmp(cmd, "Back") == 0)
@@ -519,91 +564,10 @@
 //            TFT.line(0,270,240,270,Black);
             while(strcmp(cmd, "Text") == 0)
             {
-                int touchCount=0;
-                int key = getAlpha(touchCount);
+                int key = getAlpha(cursor, &ptr);
+
                 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;
@@ -614,11 +578,11 @@
                         if(ptr > buffer)
                         {
                             ptr--;
-                            if(cursor.x - 16 >= 0)
-                                cursor.x -= 16;
+                            if(cursor.x - 12 >= 0)
+                                cursor.x -= 12;
                             else
                             {
-                                cursor.x = 224;
+                                cursor.x = 240;
                                 if(cursor.y - 21 >= 5 )
                                 cursor.y -= 21;
                             }
@@ -640,6 +604,34 @@
                 wait(2.5);
                 cmd = "Back";
             }
+        }else if (strcmp(cmd, "Ring") == 0)
+        {
+            TFT.background(Black);
+            TFT.foreground(White);
+            TFT.cls();
+//            int i=0;
+            //point cursor;
+//            cursor.x = 0;
+//            cursor.y = 5;
+            TFT.locate(0,0);
+            TFT.printf("Incoming Call...");
+            TFT.foreground(Green);
+            TFT.set_font((unsigned char*) Arial24x23);
+            TFT.locate(28,230);
+            TFT.printf("ANS");
+            TFT.circle(60,240,50,Maroon);
+        
+            TFT.foreground(Red);
+            TFT.locate(140,230);
+            TFT.printf("HANG UP");
+            TFT.circle(180,240,50,Maroon);
+            while(strcmp(cmd, "Make") == 0)
+            {
+                if(TFT.getTouch(p) == TFT.YES && p.x >= 6300 && p.x <= 7250)
+                    cmd = "Back";
+            }
         }
+        
+        
     }
 }