Marc Plouhinec / Mbed 2 deprecated Adafruit_FONA_Library_FONAtest

Dependencies:   Adafruit_FONA_Library BLE_API SoftSerial mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*************************************************** 
00002   This is an example for our Adafruit FONA Cellular Module
00003 
00004   Designed specifically to work with the Adafruit FONA 
00005   ----> http://www.adafruit.com/products/1946
00006   ----> http://www.adafruit.com/products/1963
00007   ----> http://www.adafruit.com/products/2468
00008   ----> http://www.adafruit.com/products/2542
00009 
00010   These cellular modules use TTL Serial to communicate, 2 pins are 
00011   required to interface
00012   Adafruit invests time and resources providing this open source code, 
00013   please support Adafruit and open-source hardware by purchasing 
00014   products from Adafruit!
00015 
00016   Written by Limor Fried/Ladyada for Adafruit Industries.  
00017   BSD license, all text above must be included in any redistribution
00018  ****************************************************/
00019  
00020  /*
00021   *  Modified by Marc PLOUHINEC 27/06/2015 for use in mbed
00022   */
00023 
00024 /* 
00025 THIS CODE IS STILL IN PROGRESS!
00026 
00027 Open up the serial console on the Arduino at 4800 baud to interact with FONA
00028 
00029 Note that if you need to set a GPRS APN, username, and password scroll down to
00030 the commented section below just before the main "while (true)" loop.
00031 */
00032 
00033 #include <ctype.h>
00034 #include "SoftSerial.h"
00035 #include "Adafruit_FONA.h"
00036 
00037 #define FONA_RST p12
00038 #define FONA_TX p6
00039 #define FONA_RX p5
00040 #define FONA_RI p4
00041 
00042 // this is a large buffer for replies
00043 char replybuffer[255];
00044 
00045 // Note: there is only one hardware serial on the "nRF51 DK" board, so the SoftSerial is used instead. However it doesn't 100% work.
00046 SoftSerial pcSerial(USBTX, USBRX);
00047 Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
00048 
00049 // Turn on a LED when somebody call the FONA
00050 DigitalOut led1(LED1, 1); // 1 = LED OFF on the "nRF51 DK" board
00051 class FonaEventListener : public Adafruit_FONA::EventListener {
00052     virtual void onRing() {
00053         led1 = 0; // 0 = LED ON on the "nRF51 DK" board
00054     }
00055     
00056     virtual void onNoCarrier() {
00057         led1 = 1; // 1 = LED OFF on the "nRF51 DK" board
00058     }
00059 };
00060 FonaEventListener fonaEventListener;
00061 
00062 // Functions defined after main()
00063 uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
00064 void printMenu(void);
00065 void flushSerial();
00066 char readBlocking();
00067 uint16_t readnumber();
00068 long map(long x, long in_min, long in_max, long out_min, long out_max);
00069 
00070 int main() {
00071     pcSerial.baud(4800);
00072     wait(1);
00073     pcSerial.printf("\r\n");
00074     
00075     pcSerial.printf("FONA basic test\r\n");
00076     pcSerial.printf("Initializing....(May take 3 seconds)\r\n");
00077     
00078     // See if the FONA is responding
00079     if (! fona.begin(9600)) {
00080         pcSerial.printf("Couldn't find FONA\r\n");
00081         while (1);
00082     }
00083     fona.setEventListener(&fonaEventListener);
00084     pcSerial.printf("FONA is OK\r\n");
00085     
00086     // Print SIM card IMEI number.
00087     char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
00088     uint8_t imeiLen = fona.getIMEI(imei);
00089     if (imeiLen > 0) {
00090         pcSerial.printf("SIM card IMEI: %s\r\n", imei);
00091     }
00092     
00093     // Optionally configure a GPRS APN, username, and password.
00094     // You might need to do this to access your network's GPRS/data
00095     // network.  Contact your provider for the exact APN, username,
00096     // and password values.  Username and password are optional and
00097     // can be removed, but APN is required.
00098     //fona.setGPRSNetworkSettings("your APN", "your username", "your password");
00099     fona.setGPRSNetworkSettings("web.pt.lu", "", "");
00100     
00101     // Optionally configure HTTP gets to follow redirects over SSL.
00102     // Default is not to follow SSL redirects, however if you uncomment
00103     // the following line then redirects over SSL will be followed.
00104     //fona.setHTTPSRedirect(true);
00105 
00106     printMenu();
00107     
00108     while (true) {
00109         pcSerial.printf("FONA> ");
00110         while (! pcSerial.readable() ) {
00111             if (fona.readable()) {
00112                 pcSerial.putc(fona.getc());
00113             }
00114         }
00115         
00116         char command = pcSerial.getc();
00117         pcSerial.printf("%c\r\n", command);
00118         
00119         
00120         switch (command) {
00121             case '?': {
00122                 printMenu();
00123                 break;
00124             }
00125             
00126             case 'a': {
00127                 // read the ADC
00128                 uint16_t adc;
00129                 if (! fona.getADCVoltage(&adc)) {
00130                     pcSerial.printf("Failed to read ADC\r\n");
00131                 } else {
00132                     pcSerial.printf("ADC = %d mV\r\n", adc);
00133                 }
00134                 break;
00135             }
00136             
00137             case 'b': {
00138                 // read the battery voltage and percentage
00139                 uint16_t vbat;
00140                 if (! fona.getBattVoltage(&vbat)) {
00141                     pcSerial.printf("Failed to read Batt\r\n");
00142                 } else {
00143                     pcSerial.printf("VBat = %d mV\r\n", vbat);
00144                 }
00145                 
00146                 if (! fona.getBattPercent(&vbat)) {
00147                     pcSerial.printf("Failed to read Batt\r\n");
00148                 } else {
00149                     pcSerial.printf("VPct = %d%%\r\n", vbat);
00150                 }
00151                 
00152                 break;
00153             }
00154             
00155             case 'U': {
00156                 // Unlock the SIM with a PIN code
00157                 char PIN[5];
00158                 flushSerial();
00159                 pcSerial.printf("Enter 4-digit PIN\r\n");
00160                 readline(PIN, 3);
00161                 pcSerial.printf("%s\r\n", PIN);
00162                 pcSerial.printf("Unlocking SIM card: ");
00163                 if (! fona.unlockSIM(PIN)) {
00164                     pcSerial.printf("Failed\r\n");
00165                 } else {
00166                     pcSerial.printf("OK!\r\n");
00167                 }        
00168                 break;
00169             }
00170             
00171             case 'C': {
00172                 // read the CCID
00173                 fona.getSIMCCID(replybuffer);  // make sure replybuffer is at least 21 bytes!
00174                 pcSerial.printf("SIM CCID = %s\r\n", replybuffer);
00175                 break;
00176             }
00177             
00178             case 'i': {
00179                 // read the RSSI
00180                 uint8_t n = fona.getRSSI();
00181                 int8_t r = 0;
00182                 
00183                 pcSerial.printf("RSSI = %d: ", n);
00184                 if (n == 0) r = -115;
00185                 if (n == 1) r = -111;
00186                 if (n == 31) r = -52;
00187                 if ((n >= 2) && (n <= 30)) {
00188                     r = map(n, 2, 30, -110, -54);
00189                 }
00190                 pcSerial.printf("%d dBm\r\n", r);
00191                 
00192                 break;
00193             }
00194             
00195             case 'n': {
00196                 // read the network/cellular status
00197                 uint8_t n = fona.getNetworkStatus();
00198                 pcSerial.printf("Network status %d: ", n);
00199                 if (n == 0) pcSerial.printf("Not registered\r\n");
00200                 if (n == 1) pcSerial.printf("Registered (home)\r\n");
00201                 if (n == 2) pcSerial.printf("Not registered (searching)\r\n");
00202                 if (n == 3) pcSerial.printf("Denied\r\n");
00203                 if (n == 4) pcSerial.printf("Unknown\r\n");
00204                 if (n == 5) pcSerial.printf("Registered roaming\r\n");
00205                 break;
00206             }
00207             
00208             /*** Audio ***/
00209             case 'v': {
00210                 // set volume
00211                 flushSerial();
00212                 pcSerial.printf("Set Vol %%");
00213                 uint8_t vol = readnumber();
00214                 pcSerial.printf("\r\n");
00215                 if (! fona.setVolume(vol)) {
00216                     pcSerial.printf("Failed\r\n");
00217                 } else {
00218                     pcSerial.printf("OK!\r\n");
00219                 }
00220                 break;
00221             }
00222             
00223             case 'V': {
00224                 uint8_t v = fona.getVolume();
00225                 pcSerial.printf("%d%%\r\n", v);
00226                 
00227                 break; 
00228             }
00229             
00230             case 'H': {
00231                 // Set Headphone output
00232                 if (! fona.setAudio(FONA_HEADSETAUDIO)) {
00233                     pcSerial.printf("Failed\r\n");
00234                 } else {
00235                     pcSerial.printf("OK!\r\n");
00236                 }
00237                 fona.setMicVolume(FONA_HEADSETAUDIO, 15);
00238                 break;
00239             }
00240             case 'e': {
00241                 // Set External output
00242                 if (! fona.setAudio(FONA_EXTAUDIO)) {
00243                     pcSerial.printf("Failed\r\n");
00244                 } else {
00245                     pcSerial.printf("OK!\r\n");
00246                 }
00247                 
00248                 fona.setMicVolume(FONA_EXTAUDIO, 10);
00249                 break;
00250             }
00251             
00252             case 'T': {
00253                 // play tone
00254                 flushSerial();
00255                 pcSerial.printf("Play tone #");
00256                 uint8_t kittone = readnumber();
00257                 pcSerial.printf("\r\n");
00258                 // play for 1 second (1000 ms)
00259                 if (! fona.playToolkitTone(kittone, 1000)) {
00260                     pcSerial.printf("Failed\r\n");
00261                 } else {
00262                     pcSerial.printf("OK!\r\n");
00263                 }
00264                 break;
00265             }
00266             
00267             /*** FM Radio ***/
00268             
00269             case 'f': {
00270                 // get freq
00271                 flushSerial();
00272                 pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): ");
00273                 uint16_t station = readnumber();
00274                 pcSerial.printf("\r\n");
00275                 // FM radio ON using headset
00276                 if (fona.FMradio(true, FONA_HEADSETAUDIO)) {
00277                     pcSerial.printf("Opened\r\n");
00278                 }
00279                 if (! fona.tuneFMradio(station)) {
00280                     pcSerial.printf("Failed\r\n");
00281                 } else {
00282                     pcSerial.printf("Tuned\r\n");
00283                 }
00284                 break;
00285             }
00286             case 'F': {
00287                 // FM radio off
00288                 if (! fona.FMradio(false)) {
00289                     pcSerial.printf("Failed\r\n");
00290                 } else {
00291                     pcSerial.printf("OK!\r\n");
00292                 }
00293                 break;
00294             }
00295             case 'm': {
00296                 // Set FM volume.
00297                 flushSerial();
00298                 pcSerial.printf("Set FM Vol [0-6]:");
00299                 uint8_t vol = readnumber();
00300                 pcSerial.printf("\r\n");
00301                 if (!fona.setFMVolume(vol)) {
00302                     pcSerial.printf("Failed\r\n");
00303                 } else {
00304                     pcSerial.printf("OK!\r\n");
00305                 }   
00306                 break;
00307             }
00308             case 'M': {
00309                 // Get FM volume.
00310                 int8_t fmvol = fona.getFMVolume();
00311                 if (fmvol < 0) {
00312                     pcSerial.printf("Failed\r\n");
00313                 } else {
00314                     pcSerial.printf("FM volume: %d\r\n", fmvol);
00315                 }
00316                 break;
00317             }
00318             case 'q': {
00319                 // Get FM station signal level (in decibels).
00320                 flushSerial();
00321                 pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): ");
00322                 uint16_t station = readnumber();
00323                 pcSerial.printf("\r\n");
00324                 int8_t level = fona.getFMSignalLevel(station);
00325                 if (level < 0) {
00326                     pcSerial.printf("Failed! Make sure FM radio is on (tuned to station).\r\n");
00327                 } else {
00328                     pcSerial.printf("Signal level (dB): %d\r\n", level);
00329                 }
00330                 break;
00331             }
00332             
00333             /*** PWM ***/
00334             
00335             case 'P': {
00336                 // PWM Buzzer output @ 2KHz max
00337                 flushSerial();
00338                 pcSerial.printf("PWM Freq, 0 = Off, (1-2000): ");
00339                 uint16_t freq = readnumber();
00340                 pcSerial.printf("\r\n");
00341                 if (! fona.setPWM(freq)) {
00342                     pcSerial.printf("Failed\r\n");
00343                 } else {
00344                     pcSerial.printf("OK!\r\n");
00345                 }
00346                 break;
00347             }
00348             
00349             /*** Call ***/
00350             case 'c': {      
00351                 // call a phone!
00352                 char number[30];
00353                 flushSerial();
00354                 pcSerial.printf("Call #");
00355                 readline(number, 30);
00356                 pcSerial.printf("\r\n");
00357                 pcSerial.printf("Calling %s\r\n", number);
00358                 if (!fona.callPhone(number)) {
00359                     pcSerial.printf("Failed\r\n");
00360                 } else {
00361                     pcSerial.printf("Sent!\r\n");
00362                 }
00363                 
00364                 break;
00365             }
00366             case 'h': {
00367                 // hang up! 
00368                 if (! fona.hangUp()) {
00369                     pcSerial.printf("Failed\r\n");
00370                 } else {
00371                     pcSerial.printf("OK!\r\n");
00372                 }
00373                 break;     
00374             }
00375             
00376             case 'p': {
00377                 // pick up! 
00378                 if (! fona.pickUp()) {
00379                     pcSerial.printf("Failed\r\n");
00380                 } else {
00381                     pcSerial.printf("OK!\r\n");
00382                 }
00383                 break;     
00384             }
00385             
00386             /*** SMS ***/
00387             
00388             case 'N': {
00389                 // read the number of SMS's!
00390                 int8_t smsnum = fona.getNumSMS();
00391                 if (smsnum < 0) {
00392                     pcSerial.printf("Could not read # SMS\r\n");
00393                 } else {
00394                     pcSerial.printf("%d SMS's on SIM card!\r\n", smsnum); 
00395                 }
00396                 break;
00397             }
00398             case 'r': {
00399                 // read an SMS
00400                 flushSerial();
00401                 pcSerial.printf("Read #");
00402                 uint8_t smsn = readnumber();
00403                 pcSerial.printf("\r\nReading SMS #%d\r\n", smsn);
00404                 
00405                 // Retrieve SMS sender address/phone number.
00406                 if (! fona.getSMSSender(smsn, replybuffer, 250)) {
00407                     pcSerial.printf("Failed!\r\n");
00408                     break;
00409                 }
00410                 pcSerial.printf("FROM: %s\r\n", replybuffer);
00411                 
00412                 // Retrieve SMS value.
00413                 uint16_t smslen;
00414                 if (! fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len!
00415                     pcSerial.printf("Failed!\r\n");
00416                     break;
00417                 }
00418                 pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen); 
00419                 pcSerial.printf("%s\r\n", replybuffer);
00420                 pcSerial.printf("*****\r\n");
00421                 
00422                 break;
00423             }
00424             case 'R': {
00425                 // read all SMS
00426                 int8_t smsnum = fona.getNumSMS();
00427                 uint16_t smslen;
00428                 for (int8_t smsn=1; smsn<=smsnum; smsn++) {
00429                     pcSerial.printf("\r\nReading SMS #%d\r\n", smsn);
00430                     if (!fona.readSMS(smsn, replybuffer, 250, &smslen)) {  // pass in buffer and max len!
00431                         pcSerial.printf("Failed!\r\n");
00432                         break;
00433                     }
00434                     // if the length is zero, its a special case where the index number is higher
00435                     // so increase the max we'll look at!
00436                     if (smslen == 0) {
00437                         pcSerial.printf("[empty slot]\r\n");
00438                         smsnum++;
00439                         continue;
00440                     }
00441                     
00442                     pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen);
00443                     pcSerial.printf("%s\r\n", replybuffer);
00444                     pcSerial.printf("*****\r\n");
00445                 }
00446                 break;
00447             }
00448             
00449             case 'd': {
00450                 // delete an SMS
00451                 flushSerial();
00452                 pcSerial.printf("Delete #");
00453                 uint8_t smsn = readnumber();
00454                 
00455                 pcSerial.printf("\r\nDeleting SMS #%d\r\n", smsn);
00456                 if (fona.deleteSMS(smsn)) {
00457                     pcSerial.printf("OK!\r\n");
00458                 } else {
00459                     pcSerial.printf("Couldn't delete\r\n");
00460                 }
00461                 break;
00462             }
00463             
00464             case 's': {
00465                 // send an SMS!
00466                 char sendto[21], message[141];
00467                 flushSerial();
00468                 pcSerial.printf("Send to #");
00469                 readline(sendto, 20);
00470                 pcSerial.printf("%s\r\n", sendto);
00471                 pcSerial.printf("Type out one-line message (140 char): ");
00472                 readline(message, 140);
00473                 pcSerial.printf("%s\r\n", message);
00474                 if (!fona.sendSMS(sendto, message)) {
00475                     pcSerial.printf("Failed\r\n");
00476                 } else {
00477                     pcSerial.printf("Sent!\r\n");
00478                 }
00479                 
00480                 break;
00481             }
00482             
00483             /*** Time ***/
00484             
00485             case 'y': {
00486                 // enable network time sync
00487                 if (!fona.enableNetworkTimeSync(true))
00488                     pcSerial.printf("Failed to enable\r\n");
00489                 break;
00490             }
00491             
00492             case 'Y': {
00493                 // enable NTP time sync
00494                 if (!fona.enableNTPTimeSync(true, "pool.ntp.org"))
00495                     pcSerial.printf("Failed to enable\r\n");
00496                 break;
00497             }
00498             
00499             case 't': {
00500                 // read the time
00501                 char buffer[23];
00502                 
00503                 fona.getTime(buffer, 23);  // make sure replybuffer is at least 23 bytes!
00504                 pcSerial.printf("Time = %s\r\n", buffer);
00505                 break;
00506             }
00507             
00508             /*********************************** GPS (SIM808 only) */
00509             
00510             case 'o': {
00511                 // turn GPS off
00512                 if (!fona.enableGPS(false))  
00513                     pcSerial.printf("Failed to turn off\r\n");
00514                 break;
00515             }
00516             case 'O': {
00517                 // turn GPS on
00518                 if (!fona.enableGPS(true))  
00519                     pcSerial.printf("Failed to turn on\r\n");
00520                 break;
00521             }
00522             case 'x': {
00523                 int8_t stat;
00524                 // check GPS fix
00525                 stat = fona.GPSstatus();
00526                 if (stat < 0)  
00527                     pcSerial.printf("Failed to query\r\n");
00528                 if (stat == 0) pcSerial.printf("GPS off\r\n");
00529                 if (stat == 1) pcSerial.printf("No fix\r\n");
00530                 if (stat == 2) pcSerial.printf("2D fix\r\n");
00531                 if (stat == 3) pcSerial.printf("3D fix\r\n");
00532                 break;
00533             }
00534             
00535             case 'L': {
00536                 // check for GPS location
00537                 char gpsdata[80];
00538                 fona.getGPS(0, gpsdata, 80);
00539                 pcSerial.printf("Reply in format: mode,longitude,latitude,altitude,utctime(yyyymmddHHMMSS),ttff,satellites,speed,course\r\n");
00540                 pcSerial.printf("%s\r\n", gpsdata);
00541                 
00542                 break;
00543             }
00544             
00545             case 'E': {
00546                 flushSerial();
00547                 pcSerial.printf("GPS NMEA output sentences (0 = off, 34 = RMC+GGA, 255 = all)\r\n");
00548                 uint8_t nmeaout = readnumber();
00549                 
00550                 // turn on NMEA output
00551                 fona.enableGPSNMEA(nmeaout);
00552                 
00553                 break;
00554             }
00555             
00556             /*********************************** GPRS */
00557             
00558             case 'g': {
00559                 // turn GPRS off
00560                 if (!fona.enableGPRS(false))  
00561                     pcSerial.printf("Failed to turn off\r\n");
00562                 break;
00563             }
00564             case 'G': {
00565                 // turn GPRS on
00566                 if (!fona.enableGPRS(true))  
00567                     pcSerial.printf("Failed to turn on\r\n");
00568                 break;
00569             }
00570             case 'l': {
00571                 // check for GSMLOC (requires GPRS)
00572                 uint16_t returncode;
00573                 
00574                 if (!fona.getGSMLoc(&returncode, replybuffer, 250))
00575                     pcSerial.printf("Failed!\r\n");
00576                 if (returncode == 0) {
00577                     pcSerial.printf("%s\r\n", replybuffer);
00578                 } else {
00579                     pcSerial.printf("Fail code #%d\r\n", returncode);
00580                 }
00581                 
00582                 break;
00583             }
00584             case 'w': {
00585                 // read website URL
00586                 uint16_t statuscode;
00587                 int16_t length;
00588                 char url[80];
00589                 
00590                 flushSerial();
00591                 pcSerial.printf("NOTE: in beta! Use small webpages to read!\r\n");
00592                 pcSerial.printf("URL to read (e.g. www.adafruit.com/testwifi/index.html):\r\n");
00593                 pcSerial.printf("http://"); readline(url, 79);
00594                 pcSerial.printf("%s\r\n", url);
00595                 
00596                 pcSerial.printf("****\r\n");
00597                 if (!fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) {
00598                     pcSerial.printf("Failed!\r\n");
00599                     break;
00600                 }
00601                 while (length > 0) {
00602                     while (fona.readable()) {
00603                         char c = fona.getc();
00604                         pcSerial.putc(c);
00605                         length--;
00606                         if (! length) break;
00607                     }
00608                 }
00609                 pcSerial.printf("\r\n****\r\n");
00610                 fona.HTTP_GET_end();
00611                 break;
00612             }
00613             
00614             case 'W': {
00615                 // Post data to website
00616                 uint16_t statuscode;
00617                 int16_t length;
00618                 char url[80];
00619                 char data[80];
00620                 
00621                 flushSerial();
00622                 pcSerial.printf("NOTE: in beta! Use simple websites to post!\r\n");
00623                 pcSerial.printf("URL to post (e.g. httpbin.org/post):\r\n");
00624                 pcSerial.printf("http://"); readline(url, 79);
00625                 pcSerial.printf("%s\r\n", url);
00626                 pcSerial.printf("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):\r\n");
00627                 readline(data, 79);
00628                 pcSerial.printf("%s\r\n", data);
00629                 
00630                 pcSerial.printf("****\r\n");
00631                 if (!fona.HTTP_POST_start(url, "text/plain", (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
00632                     pcSerial.printf("Failed!\r\n");
00633                     break;
00634                 }
00635                 while (length > 0) {
00636                     while (fona.readable()) {
00637                         char c = fona.getc();
00638                         pcSerial.putc(c);
00639                         length--;
00640                         if (! length) break;
00641                     }
00642                 }
00643                 pcSerial.printf("\r\n****\r\n");
00644                 fona.HTTP_POST_end();
00645                 break;
00646             }
00647             /*****************************************/
00648             
00649             case 'S': {
00650                 pcSerial.printf("Creating SERIAL TUBE\r\n");
00651                 while (1) {
00652                     while (pcSerial.readable()) {
00653                         wait_ms(1);
00654                         fona.putc(pcSerial.getc());
00655                     }
00656                     if (fona.readable()) {
00657                         pcSerial.putc(fona.getc());
00658                     }
00659                 }
00660             }
00661             
00662             default: {
00663                 pcSerial.printf("Unknown command\r\n");
00664                 printMenu();
00665                 break;
00666             }
00667         }
00668         // flush input
00669         flushSerial();
00670         while (fona.readable()) {
00671             pcSerial.putc(fona.getc());
00672         }
00673     }
00674 }
00675 
00676 void printMenu(void) {
00677     pcSerial.printf("-------------------------------------\r\n");
00678     pcSerial.printf("[?] Print this menu\r\n");
00679     pcSerial.printf("[a] read the ADC (2.8V max)\r\n");
00680     pcSerial.printf("[b] read the Battery V and %% charged\r\n");
00681     pcSerial.printf("[C] read the SIM CCID\r\n");
00682     pcSerial.printf("[U] Unlock SIM with PIN code\r\n");
00683     pcSerial.printf("[i] read RSSI\r\n");
00684     pcSerial.printf("[n] get Network status\r\n");
00685     pcSerial.printf("[v] set audio Volume\r\n");
00686     pcSerial.printf("[V] get Volume\r\n");
00687     pcSerial.printf("[H] set Headphone audio\r\n");
00688     pcSerial.printf("[e] set External audio\r\n");
00689     pcSerial.printf("[T] play audio Tone\r\n");
00690     pcSerial.printf("[P] PWM/Buzzer out\r\n");
00691     
00692     // FM (SIM800 only)
00693     pcSerial.printf("[f] tune FM radio\r\n");
00694     pcSerial.printf("[F] turn off FM\r\n");
00695     pcSerial.printf("[m] set FM volume\r\n");
00696     pcSerial.printf("[M] get FM volume\r\n");
00697     pcSerial.printf("[q] get FM station signal level\r\n");
00698     
00699     // Phone
00700     pcSerial.printf("[c] make phone Call\r\n");
00701     pcSerial.printf("[h] Hang up phone\r\n");
00702     pcSerial.printf("[p] Pick up phone\r\n");
00703     
00704     // SMS
00705     pcSerial.printf("[N] Number of SMSs\r\n");
00706     pcSerial.printf("[r] Read SMS #\r\n");
00707     pcSerial.printf("[R] Read All SMS\r\n");
00708     pcSerial.printf("[d] Delete SMS #\r\n");
00709     pcSerial.printf("[s] Send SMS\r\n");
00710     
00711     // Time
00712     pcSerial.printf("[y] Enable network time sync\r\n");   
00713     pcSerial.printf("[Y] Enable NTP time sync (GPRS)\r\n");   
00714     pcSerial.printf("[t] Get network time\r\n");
00715     
00716     // GPRS
00717     pcSerial.printf("[G] Enable GPRS\r\n");
00718     pcSerial.printf("[g] Disable GPRS\r\n");
00719     pcSerial.printf("[l] Query GSMLOC (GPRS)\r\n");
00720     pcSerial.printf("[w] Read webpage (GPRS)\r\n");
00721     pcSerial.printf("[W] Post to website (GPRS)\r\n");
00722     
00723     // GPS
00724     pcSerial.printf("[O] Turn GPS on (SIM808)\r\n");
00725     pcSerial.printf("[o] Turn GPS off (SIM808)\r\n");
00726     pcSerial.printf("[x] GPS fix status (SIM808)\r\n");
00727     pcSerial.printf("[L] Query GPS location (SIM808)\r\n");
00728     pcSerial.printf("[E] Raw NMEA out (SIM808)\r\n");
00729     
00730     pcSerial.printf("[S] create Serial passthru tunnel\r\n");
00731     pcSerial.printf("-------------------------------------\r\n");
00732     pcSerial.printf("\r\n");
00733 }
00734 
00735 void flushSerial() {
00736     while (pcSerial.readable()) 
00737         pcSerial.getc();
00738 }
00739 
00740 char readBlocking() {
00741     while (!pcSerial.readable());
00742     return pcSerial.getc();
00743 }
00744 
00745 uint16_t readnumber() {
00746     uint16_t x = 0;
00747     char c;
00748     while (! isdigit(c = readBlocking())) {
00749         //pcSerial.putc(c);
00750     }
00751     pcSerial.putc(c);
00752     x = c - '0';
00753     while (isdigit(c = readBlocking())) {
00754         pcSerial.putc(c);
00755         x *= 10;
00756         x += c - '0';
00757     }
00758     return x;
00759 }
00760   
00761 uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
00762     uint16_t buffidx = 0;
00763     bool timeoutvalid = true;
00764     if (timeout == 0) timeoutvalid = false;
00765     
00766     while (true) {
00767         if (buffidx > maxbuff) {
00768             //pcSerial.printf("SPACE\r\n");
00769             break;
00770         }
00771         
00772         while(pcSerial.readable()) {
00773             char c =  pcSerial.getc();
00774             
00775             //pcSerial.printf("%02x#%c\r\n", c, c);
00776             
00777             if (c == '\r') continue;
00778             if (c == 0xA) {
00779                 if (buffidx == 0)   // the first 0x0A is ignored
00780                     continue;
00781                 
00782                 timeout = 0;         // the second 0x0A is the end of the line
00783                 timeoutvalid = true;
00784                 break;
00785             }
00786             buff[buffidx] = c;
00787             buffidx++;
00788         }
00789         
00790         if (timeoutvalid && timeout == 0) {
00791             //pcSerial.printf("TIMEOUT\r\n");
00792             break;
00793         }
00794         wait_ms(1);
00795     }
00796     buff[buffidx] = 0;  // null term
00797     return buffidx;
00798 }
00799 
00800 long map(long x, long in_min, long in_max, long out_min, long out_max)
00801 {
00802     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00803 }