Jonathan Osei-Owusu / Mbed 2 deprecated Alexa_Text

Dependencies:   emic2 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // By: Jonathan Osei-Owusu, Alex Mussa
00002 // 4180 Final Project
00003 
00004 // Goal: ESP8266 static page WEB server to send text queries to Alexa via Mbed and TExt-to-Speech module.
00005 //       WEB server accessible from any network.
00006 //       Also uses FONA chip to allow Alexa text queries via SMS.
00007 
00008 
00009 #include "mbed.h"
00010 #include "emic2.h"
00011 #include <string.h>
00012 #include <ctype.h>
00013 #include "Adafruit_FONA.h"
00014 
00015 /////// ------ Begin FONA Global Region ----- ////////
00016 
00017 #define FONA_RST p12
00018 #define FONA_TX p13
00019 #define FONA_RX p14
00020 #define FONA_RI p11
00021 
00022 // this is a large buffer for replies
00023 char replybuffer[255];
00024 char smsText[255], smsPhoneNo[11]; ///// By Jonathan: Text to send to Text -> Speech chip & phone # to send to 
00025 int count; // by Jonathan
00026 Serial pcSerial(USBTX, USBRX);
00027 Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
00028 void replySMS(char*);
00029 
00030 // Turn on a LED when somebody call the FONA
00031 emic2 myTTS(p9, p10);
00032 DigitalOut  led1(LED1);
00033 DigitalOut  led2(LED2);
00034 DigitalOut  led3(LED3);
00035 DigitalOut  led4(LED4);
00036 
00037 class FonaEventListener : public Adafruit_FONA::EventListener {
00038     virtual void onRing() {
00039         led1 = 1;
00040     }
00041     
00042     virtual void onNoCarrier() {
00043         led1 = 0; 
00044     }
00045 };
00046 FonaEventListener fonaEventListener;
00047 
00048 // Functions defined after main()
00049 uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
00050 void printMenu(void);
00051 void flushSerial();
00052 char readBlocking();
00053 uint16_t readnumber();
00054 long map(long x, long in_min, long in_max, long out_min, long out_max);
00055 
00056 /////// ------ End FONA Global Region ----- ////////
00057 
00058 
00059 ////// ------- Begin ESP Global Region -------- //////
00060 
00061 Serial esp(p28, p27); // tx, rx
00062 
00063 // some test values to show on web page
00064 AnalogIn   Ain1(p18);
00065 AnalogIn   Ain2(p19);
00066 
00067 /*
00068 char ssid[32] = "hsd";     // enter WiFi router ssid inside the quotes
00069 char pwd [32] = "austin123"; // enter WiFi router password inside the quotes
00070 */
00071 float temperature, AdcIn, Ht;
00072 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
00073 char Vcc[10];
00074 char Temp[10];
00075 
00076 // things for sending/receiving data over serial
00077 volatile int tx_in=0;
00078 volatile int tx_out=0;
00079 volatile int rx_in=0;
00080 volatile int rx_out=0;
00081 const int buffer_size = 4095;
00082 char tx_buffer[buffer_size+1];
00083 char rx_buffer[buffer_size+1];
00084 void Tx_interrupt();
00085 void Rx_interrupt();
00086 void send_line();
00087 void read_line();
00088 
00089 int DataRX;
00090 int update;
00091 int espCount;
00092 char cmdbuff[1024];
00093 char replybuff[4096];
00094 char webdata[4096]; // This may need to be bigger depending on WEB browser used
00095 char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
00096 char timebuf[30];
00097 void SendCMD(),getreply(),ReadWebData(),startserver();
00098 void gettime(),setRTC(),gettemp(),getbattery();
00099 char rx_line[1024];
00100 int port        =80;  // set server port
00101 int SERVtimeout =5;    // set server timeout in seconds in case link breaks.
00102 struct tm t;
00103 // manual set RTC values
00104 int minute      =00;    // 0-59
00105 int hour        =12;    // 2-23
00106 int dayofmonth  =26;    // 1-31
00107 int month       =8;     // 1-12
00108 int year        =15;    // last 2 digits
00109 
00110 
00111 //// -------- End ESP Global region -------- /////
00112 
00113 int main()
00114 {
00115     myTTS.volume(5);
00116     myTTS.voice(0);
00117     
00118     pcSerial.baud(9600);
00119     esp.baud(9600);
00120     wait(1);
00121     
00122     ////---------------- Begin FONA init setup Region -------// 
00123     
00124     pcSerial.printf("\r\n");
00125     
00126     pcSerial.printf("FONA basic test\r\n");
00127     pcSerial.printf("Initializing....(May take 3 seconds)\r\n");
00128     
00129     // See if the FONA is responding
00130     if (! fona.begin(9600)) {
00131         pcSerial.printf("Couldn't find FONA\r\n");
00132         while (1);
00133     }
00134     fona.setEventListener(&fonaEventListener);
00135     pcSerial.printf("FONA is OK\r\n");
00136     
00137     // Print SIM card IMEI number.
00138     char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
00139     uint8_t imeiLen = fona.getIMEI(imei);
00140     if (imeiLen > 0) {
00141         pcSerial.printf("SIM card IMEI: %s\r\n", imei);
00142     }
00143     
00144     ////----- End FONA init setup region -----////
00145     
00146     
00147     ////----- Begin ESP init setup region ----////
00148     
00149     led1 = 0, led2 = 0, led3 = 0, led4 = 0;
00150     // Setup a serial interrupt function to receive data
00151     esp.attach(&Rx_interrupt, Serial::RxIrq);
00152     // Setup a serial interrupt function to transmit data
00153     esp.attach(&Tx_interrupt, Serial::TxIrq);
00154     if (time(NULL) < 1420070400) {
00155         setRTC();
00156     }
00157     startserver();
00158     DataRX=0;
00159     espCount=0;
00160     wait(3);
00161     
00162     ////----- End ESP init setup region ----////
00163     
00164     while(1) {        
00165         ////---- Begin ESP loop ----////
00166         if(DataRX==1) {
00167             ReadWebData();
00168             esp.attach(&Rx_interrupt, Serial::RxIrq);
00169         }
00170         if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
00171         {
00172             // get new values
00173             gettime();
00174             gettemp();
00175             getbattery();
00176             espCount++;
00177             // send new values
00178             sprintf(cmdbuff, "espCount,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",espCount,timebuf,Temp,Vcc); 
00179             SendCMD();
00180             getreply();
00181             update=0;   
00182         }
00183         ////---- End ESP loop ----////
00184         
00185         ////---- Begin FONA loop ----////
00186         
00187         //pcSerial.printf("FONA waiting for SMS...\r\n");
00188         
00189         //while (! pcSerial.readable()) {
00190             //if (fona.readable()) {
00191           while (fona.readable() && ! pcSerial.readable()) {
00192                 //// Printf that incoming SMS detected
00193                 pcSerial.putc(fona.getc()); 
00194 
00195                     
00196                 // ----------------------------------------------------------------//
00197                 
00198                 // read an SMS + send -> TTS chip
00199                 flushSerial();
00200                 pcSerial.printf("\r\nReading SMS #1\r\n");
00201                 
00202                 // Retrieve SMS sender address/phone number.
00203                 if (! fona.getSMSSender(1, replybuffer, 250)) {
00204                     pcSerial.printf("Failed!\r\n");
00205                     break;
00206                 }
00207                 pcSerial.printf("FROM: %s\r\n", replybuffer);
00208                 for (int i = 0; i < 10; i++) { /////// BY JONATHAN to store phone # to respond to
00209                     smsPhoneNo[i] = replybuffer[i + 2]; // chop off the "+1" from phone # returned by replybuffer
00210                 }
00211                 smsPhoneNo[10] = '\0'; // null terminate.... NECESSARY??
00212                 pcSerial.printf("FROM: %s\r\n", smsPhoneNo);
00213                 
00214                 // Retrieve SMS value.
00215                 uint16_t smslen;
00216                 if (! fona.readSMS(1, replybuffer, 250, &smslen)) { // pass in buffer and max len!
00217                     pcSerial.printf("Failed!\r\n");
00218                     break;
00219                 }
00220                 pcSerial.printf("***** SMS #1 (%d) bytes *****\r\n", smslen); 
00221                 pcSerial.printf("%s\r\n", replybuffer);
00222                 pcSerial.printf("*****\r\n");
00223                 
00224                 
00225                 
00226                 //char message[255]; // CHANGED BY JONATHAN. 141 -> 255
00227                 char myString[255];
00228                 
00229                 for (int i = 0; i < 255; i++) {
00230                     myString[i] = replybuffer[i + 5];   // chop of the "Alexa string"    
00231                 }
00232          
00233                 strcpy(smsText, "Uh lex uh "); //Pronunciation Correction.
00234                 strcat(smsText, myString);
00235                 pcSerial.printf("DEBUG::%s\r\n", smsText);
00236                 myTTS.speakf("S%s\r", smsText);  // Send text message to the text to speech module.
00237                 
00238               
00239                  // ----------------------------------------------------------------//
00240                  
00241                  // Send SMS back!
00242                  replySMS(replybuffer);
00243                  
00244                  // ----------------------------------------------------------------//
00245                 
00246                 // delete an SMS after response sent back
00247                 flushSerial();
00248                 pcSerial.printf("\r\nDeleting SMS #1\r\n");
00249                 if (fona.deleteSMS(1)) // delete SMS #1
00250                     pcSerial.printf("OK!\r\n");
00251                 else 
00252                     pcSerial.printf("Couldn't delete\r\n");
00253                 
00254            // } // end if
00255         } // end inner fona.readable() while
00256         
00257         ////---- End FONA loop ----////
00258         
00259     } // end outer loop
00260 } // end main
00261 
00262 
00263 // 
00264 void replySMS(char* inMsg) 
00265 {
00266     // send an SMS back!
00267     char message[255];
00268      
00269     strcpy(message, "");
00270     
00271     flushSerial();
00272     
00273     pcSerial.printf("Replying to %s\r\n", smsPhoneNo);
00274     
00275     
00276     if(strstr(inMsg, "how old are you")) { // if reply buffer contains that string
00277         strcpy(message, "I'm 2 in human years, 14 in dog years, and 25 in cat years. I think AI years are marked in nanoseconds, so that makes me, like, a scrillion!");
00278     }
00279     else if(strstr(inMsg, "what is the world population")) {
00280         strcpy(message, "The population of the world is about 7 Billion 400 Million.");
00281     }
00282     else if(strstr(inMsg, "integral of x squared")) {
00283         strcpy(message, "The integral of x squared is one third x cubed plus a constant.");
00284     }
00285     else if(strstr(inMsg, "discrete co sine transform")) {
00286         strcpy(message, "The discrete cosine transform expresses a finite sequence of data points in terms of a sum of cosine function oscillating at different frequencies.");
00287     } 
00288     else {
00289         strcpy(message, "Sorry, command not recognized.");   
00290     }
00291     
00292     pcSerial.printf("%s\r\n", message);
00293     if (!fona.sendSMS(smsPhoneNo, message))
00294         pcSerial.printf("Failed\r\n");
00295      else 
00296         pcSerial.printf("Sent!\r\n");
00297                     
00298 }
00299 
00300 // Reads and processes GET and POST web data
00301 void ReadWebData()
00302 {
00303     wait_ms(200);
00304     esp.attach(NULL,Serial::RxIrq);
00305     DataRX=0;
00306     memset(webdata, '\0', sizeof(webdata));
00307     strcpy(webdata, rx_buffer);
00308     memset(rx_buffer, '\0', sizeof(rx_buffer));
00309     rx_in = 0;
00310     rx_out = 0;
00311     // check web data for form information
00312     if( strstr(webdata, "check=age") != NULL ) { ///////// JONATHAN ::: 'led1v' -> 'age'
00313         strcpy(smsText, "Uh lex uh, how old are you?");
00314         led1=!led1;
00315         myTTS.speakf("S%s\r", smsText);  // Send text message to the text to speech module.
00316     }
00317     if( strstr(webdata, "check=population") != NULL ) {
00318         strcpy(smsText, "Uh lex uh, what is the world population?");
00319         led2=!led2;
00320         myTTS.speakf("S%s\r", smsText);
00321     }
00322     if( strstr(webdata, "check=integral") != NULL ) {
00323         strcpy(smsText, "Uh lex uh, what is the integral of x squared?");
00324         led3=!led3;
00325         myTTS.speakf("S%s\r", smsText);
00326     }
00327     if( strstr(webdata, "check=dct") != NULL ) {
00328         strcpy(smsText, "Uh lex uh, what is the discrete co sine transform?");
00329         led4=!led4;
00330         myTTS.speakf("S%s\r", smsText);
00331     }
00332     if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
00333         update=1;
00334     }
00335     if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
00336         update=1;
00337     }
00338 }
00339 // Starts webserver
00340 void startserver()
00341 {
00342     gettime();
00343     gettemp();
00344     getbattery();
00345     pcSerial.printf("++++++++++ Resetting ESP ++++++++++\r\n");
00346     strcpy(cmdbuff,"node.restart()\r\n");
00347     SendCMD();
00348     wait(2);
00349     getreply();
00350     
00351     pcSerial.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
00352 
00353     // initial values
00354     sprintf(cmdbuff, "espCount,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
00355     SendCMD();
00356     getreply();
00357     wait(0.5);
00358 
00359     //create server
00360     sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
00361     SendCMD();
00362     getreply();
00363     wait(0.5);
00364     strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
00365     SendCMD();
00366     getreply();
00367     wait(0.3);
00368         strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
00369         SendCMD();
00370         getreply();
00371         wait(0.3);
00372         
00373         //print data to mbed
00374         strcpy(cmdbuff,"print(payload)\r\n");
00375         SendCMD();
00376         getreply();
00377         wait(0.2);
00378        
00379         //web page data
00380         strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>Alexa Server</h1>')\r\n");
00381         SendCMD();
00382         getreply();
00383         wait(0.4);
00384         strcpy(cmdbuff,"conn:send('Hit count: '..espCount..'')\r\n");
00385         SendCMD();
00386         getreply();
00387         wait(0.2);
00388         strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n");
00389         SendCMD();
00390         getreply();
00391         wait(0.4);
00392         strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
00393         SendCMD();
00394         getreply();
00395         wait(0.3);
00396         strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"x\"> Data Sent')\r\n");
00397         SendCMD();
00398         getreply();
00399         wait(0.3);
00400 
00401         // end web page data
00402         strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
00403         SendCMD();
00404         getreply();
00405         wait(0.3);
00406         strcpy(cmdbuff, "end)\r\n");
00407         SendCMD();
00408         getreply();
00409         wait(0.2);
00410     strcpy(cmdbuff, "end)\r\n");
00411     SendCMD();
00412     getreply();
00413     wait(0.2);
00414 
00415     strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
00416     SendCMD();
00417     getreply();
00418     wait(0.2);
00419     strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
00420     SendCMD();
00421     getreply();
00422     wait(0.2);
00423     strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
00424     SendCMD();
00425     getreply();
00426     wait(0.2);
00427     strcpy(cmdbuff, "else\r\n");
00428     SendCMD();
00429     getreply();
00430     wait(0.2);
00431     strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
00432     SendCMD();
00433     getreply();
00434     wait(0.2);
00435     strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
00436     SendCMD();
00437     getreply();
00438     wait(0.2);
00439     strcpy(cmdbuff,"tmr.stop(0)\r\n");
00440     SendCMD();
00441     getreply();
00442     wait(0.2);
00443     strcpy(cmdbuff,"end\r\n");
00444     SendCMD();
00445     getreply();
00446     wait(0.2);
00447     strcpy(cmdbuff,"end)\r\n");
00448     SendCMD();
00449     getreply();
00450     wait(0.2);
00451     
00452     pcSerial.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
00453 }
00454 
00455 ////------- Begin ESP Helper Functions Region ------////
00456 // ESP Command data send
00457 void SendCMD()
00458 {
00459     int i;
00460     char temp_char;
00461     bool empty;
00462     i = 0;
00463 // Start Critical Section - don't interrupt while changing global buffer variables
00464     NVIC_DisableIRQ(UART1_IRQn);
00465     empty = (tx_in == tx_out);
00466     while ((i==0) || (cmdbuff[i-1] != '\n')) {
00467 // Wait if buffer full
00468         if (((tx_in + 1) % buffer_size) == tx_out) {
00469 // End Critical Section - need to let interrupt routine empty buffer by sending
00470             NVIC_EnableIRQ(UART1_IRQn);
00471             while (((tx_in + 1) % buffer_size) == tx_out) {
00472             }
00473 // Start Critical Section - don't interrupt while changing global buffer variables
00474             NVIC_DisableIRQ(UART1_IRQn);
00475         }
00476         tx_buffer[tx_in] = cmdbuff[i];
00477         i++;
00478         tx_in = (tx_in + 1) % buffer_size;
00479     }
00480     if (esp.writeable() && (empty)) {
00481         temp_char = tx_buffer[tx_out];
00482         tx_out = (tx_out + 1) % buffer_size;
00483 // Send first character to start tx interrupts, if stopped
00484         esp.putc(temp_char);
00485     }
00486 // End Critical Section
00487     NVIC_EnableIRQ(UART1_IRQn);
00488     return;
00489 }
00490 
00491 // Get Command and ESP status replies
00492 void getreply()
00493 {
00494     read_line();
00495     sscanf(rx_line,replybuff);
00496 }
00497  
00498 // Read a line from the large rx buffer from rx interrupt routine
00499 void read_line() {
00500     int i;
00501     i = 0;
00502 // Start Critical Section - don't interrupt while changing global buffer variables
00503     NVIC_DisableIRQ(UART1_IRQn);
00504 // Loop reading rx buffer characters until end of line character
00505     while ((i==0) || (rx_line[i-1] != '\r')) {
00506 // Wait if buffer empty
00507         if (rx_in == rx_out) {
00508 // End Critical Section - need to allow rx interrupt to get new characters for buffer
00509             NVIC_EnableIRQ(UART1_IRQn);
00510             while (rx_in == rx_out) {
00511             }
00512 // Start Critical Section - don't interrupt while changing global buffer variables
00513             NVIC_DisableIRQ(UART1_IRQn);
00514         }
00515         rx_line[i] = rx_buffer[rx_out];
00516         i++;
00517         rx_out = (rx_out + 1) % buffer_size;
00518     }
00519 // End Critical Section
00520     NVIC_EnableIRQ(UART1_IRQn);
00521     rx_line[i-1] = 0;
00522     return;
00523 }
00524  
00525  
00526 // Interupt Routine to read in data from serial port
00527 void Rx_interrupt() {
00528     DataRX=1;
00529     //led3=1;
00530 // Loop just in case more than one character is in UART's receive FIFO buffer
00531 // Stop if buffer full
00532     while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
00533         rx_buffer[rx_in] = esp.getc();
00534 // Uncomment to Echo to USB serial to watch data flow
00535         pcSerial.putc(rx_buffer[rx_in]);
00536         rx_in = (rx_in + 1) % buffer_size;
00537     }
00538     //led3=0;
00539     return;
00540 }
00541  
00542  
00543 // Interupt Routine to write out data to serial port
00544 void Tx_interrupt() {
00545     //led2=1;
00546 // Loop to fill more than one character in UART's transmit FIFO buffer
00547 // Stop if buffer empty
00548     while ((esp.writeable()) && (tx_in != tx_out)) {
00549         esp.putc(tx_buffer[tx_out]);
00550         tx_out = (tx_out + 1) % buffer_size;
00551     }
00552     //led2=0;
00553     return;
00554 }
00555 
00556 void gettime()
00557 {
00558     time_t seconds = time(NULL);
00559     strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
00560 }
00561 
00562 void setRTC()
00563 {
00564     t.tm_sec = (0);             // 0-59
00565     t.tm_min = (minute);        // 0-59
00566     t.tm_hour = (hour);         // 0-23
00567     t.tm_mday = (dayofmonth);   // 1-31
00568     t.tm_mon = (month-1);       // 0-11  "0" = Jan, -1 added for Mbed RCT clock format
00569     t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
00570     set_time(mktime(&t));       // set RTC clock
00571 }
00572 // Analog in example
00573 void getbattery()
00574 {
00575     AdcIn=Ain1.read();
00576     Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
00577     sprintf(Vcc,"%2.3f",Ht);
00578 }
00579 // Temperature example
00580 void gettemp()
00581 {
00582  
00583     AdcIn=Ain2.read();
00584     Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy  
00585     sprintf(Temp,"%2.3f",Ht);
00586 }
00587 
00588 ////------ End ESP Helper Functions Region ------////
00589 
00590 ////------ Begin FONA Helper Functions Region ------////
00591 
00592 void flushSerial() {
00593     while (pcSerial.readable()) 
00594         pcSerial.getc();
00595 }
00596 
00597 char readBlocking() {
00598     while (!pcSerial.readable());
00599     return pcSerial.getc();
00600 }
00601 
00602 uint16_t readnumber() {
00603     uint16_t x = 0;
00604     char c;
00605     while (! isdigit(c = readBlocking())) {
00606         //pcSerial.putc(c);
00607     }
00608     pcSerial.putc(c);
00609     x = c - '0';
00610     while (isdigit(c = readBlocking())) {
00611         pcSerial.putc(c);
00612         x *= 10;
00613         x += c - '0';
00614     }
00615     return x;
00616 }
00617   
00618 uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
00619     uint16_t buffidx = 0;
00620     bool timeoutvalid = true;
00621     if (timeout == 0) timeoutvalid = false;
00622     
00623     while (true) {
00624         if (buffidx > maxbuff) {
00625             //pcSerial.printf("SPACE\r\n");
00626             break;
00627         }
00628         
00629         while(pcSerial.readable()) {
00630             char c =  pcSerial.getc();
00631             
00632             //pcSerial.printf("%02x#%c\r\n", c, c);
00633             
00634             if (c == '\r') 
00635                     continue;
00636                 
00637             
00638             if (c == '\r') { /////////////////////////////////////////// CHANGED BY JONATHAN: (0XA -> '\r')
00639                 if (buffidx == 0)   // the first 0x0A is ignored
00640                     continue;
00641                 
00642                 timeout = 0;         // the second 0x0A is the end of the line
00643                 timeoutvalid = true;
00644                 break;
00645             }
00646             buff[buffidx] = c;
00647             buffidx++;
00648             
00649             count++; ///////////////////////// ADED BY JONATHAN
00650         }
00651         
00652         if (timeoutvalid && timeout == 0) {
00653             //pcSerial.printf("TIMEOUT\r\n");
00654             break;
00655         }
00656         wait_ms(1);
00657     }
00658     buff[buffidx] = 0;  // null term
00659     return buffidx;
00660 }
00661 
00662 long map(long x, long in_min, long in_max, long out_min, long out_max)
00663 {
00664     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00665 }
00666 
00667 ////------ End FONA Helper Functions Region ------////
00668