Version working with mbed-os

Dependents:  

Fork of Adafruit_FONA_Library by Jesse van Rhijn

Revision:
1:89299f09929c
Parent:
0:d567815b7a5f
Child:
2:a47b72e3654e
--- a/Adafruit_FONA.cpp	Sat Jun 27 09:25:38 2015 +0000
+++ b/Adafruit_FONA.cpp	Sat Jun 27 15:03:10 2015 +0000
@@ -24,8 +24,9 @@
 #define HIGH 1
 #define LOW 0
 
-bool Adafruit_FONA::begin(Serial &port) {
-    mySerial = &port;
+bool Adafruit_FONA::begin(int baudrate) {
+    mySerial.baud(baudrate);
+    mySerial.attach(this, &Adafruit_FONA::onSerialDataReceived, Serial::RxIrq);
     
     _rstpin = HIGH;
     wait_ms(10);
@@ -36,7 +37,7 @@
     // give 3 seconds to reboot
     wait_ms(3000);
     
-    while (mySerial->readable()) mySerial->getc();
+    while (readable()) getc();
     
     sendCheckReply("AT", "OK");
     wait_ms(100);
@@ -56,6 +57,75 @@
     return true;
 }
 
+void Adafruit_FONA::setEventListener(EventListener *eventListener) {
+    this->eventListener = eventListener;
+}
+
+/********* Stream ********************************************/
+
+int Adafruit_FONA::_putc(int value) {
+    return mySerial.putc(value);
+}
+
+int Adafruit_FONA::_getc() {
+    __disable_irq(); // Start Critical Section - don't interrupt while changing global buffer variables
+    
+    // Wait for data if the buffer is empty
+    if (isRxBufferEmpty()) {
+        __enable_irq(); // End Critical Section - need to allow rx interrupt to get new characters for buffer
+        
+        while(isRxBufferEmpty());
+        
+        __disable_irq(); // Start Critical Section - don't interrupt while changing global buffer variables
+    }
+    
+    int data = rxBuffer[rxBufferOutIndex];
+    incrementRxBufferOutIndex();
+    
+    __enable_irq(); // End Critical Section
+    
+    return data;
+}
+
+int Adafruit_FONA::readable() {
+    return !isRxBufferEmpty();
+}
+
+void Adafruit_FONA::onSerialDataReceived() {
+    while (mySerial.readable() && !isRxBufferFull()) {
+        int data = mySerial.getc();
+        rxBuffer[rxBufferInIndex] = data;
+        
+        //
+        // Analyze the received data in order to detect events like RING or NO CARRIER
+        //
+        
+        // Copy the data in the current line
+        if (currentReceivedLineSize < RX_BUFFER_SIZE && data != '\r' && data != '\n') {
+            currentReceivedLine[currentReceivedLineSize] = (char) data;
+            currentReceivedLineSize++;
+        }
+        
+        // Check if the line is complete
+        if (data == '\n') {
+            currentReceivedLine[currentReceivedLineSize] = 0;
+            
+            if (eventListener != NULL) {
+                // Check if we have a special event
+                if (strcmp(currentReceivedLine, "RING") == 0) {
+                    eventListener->onRing();
+                } else if (strcmp(currentReceivedLine, "NO CARRIER") == 0) {
+                    eventListener->onNoCarrier();
+                }
+            }
+            
+            currentReceivedLineSize = 0;
+        }
+        
+        incrementRxBufferInIndex();
+    }
+}
+
 /********* Real Time Clock ********************************************/
 
 bool Adafruit_FONA::enableRTC(uint8_t i) {
@@ -348,7 +418,7 @@
     uint16_t thesmslen = 0;
     
     //getReply(F("AT+CMGR="), i, 1000);  //  do not print debug!
-    mySerial->printf("AT+CMGR=%d\r\n", i);
+    mySerial.printf("AT+CMGR=%d\r\n", i);
     readline(1000); // timeout
     
     // parse it out...
@@ -382,7 +452,7 @@
     if (! sendCheckReply("AT+CMGF=1", "OK")) return false;
     if (! sendCheckReply("AT+CSDH=1", "OK")) return false;
     // Send command to retrieve SMS message and parse a line of response.
-    mySerial->printf("AT+CMGR=%d\r\n", i);
+    mySerial.printf("AT+CMGR=%d\r\n", i);
     readline(1000);
     // Parse the second field in the response.
     bool result = parseReplyQuoted("+CMGR:", sender, senderlen, ',', 1);
@@ -402,8 +472,8 @@
 #ifdef ADAFRUIT_FONA_DEBUG
     printf("> %s\r\n", smsmsg);
 #endif
-    mySerial->printf("%s\r\n\r\n", smsmsg);
-    mySerial->putc(0x1A);
+    mySerial.printf("%s\r\n\r\n", smsmsg);
+    mySerial.putc(0x1A);
 #ifdef ADAFRUIT_FONA_DEBUG
     printf("^Z\r\n");
 #endif
@@ -457,13 +527,13 @@
         if (! sendCheckReply("AT+CNTPCID=1", "OK"))
             return false;
         
-        mySerial->printf("AT+CNTP=\"");
+        mySerial.printf("AT+CNTP=\"");
         if (ntpserver != 0) {
-            mySerial->printf(ntpserver);
+            mySerial.printf(ntpserver);
         } else {
-            mySerial->printf("pool.ntp.org");
+            mySerial.printf("pool.ntp.org");
         }
-        mySerial->printf("\",0\r\n");
+        mySerial.printf("\",0\r\n");
         readline(FONA_DEFAULT_TIMEOUT_MS);
         if (strcmp(replybuffer, "OK") != 0)
             return false;
@@ -821,7 +891,7 @@
     printf("AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", server, port);
 #endif
     
-    mySerial->printf("AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", server, port);
+    mySerial.printf("AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", server, port);
     
     if (! expectReply("OK")) return false;
     if (! expectReply("CONNECT OK")) return false;
@@ -852,7 +922,7 @@
 #endif
     
     
-    mySerial->printf("AT+CIPSEND=%d\r\n", len);
+    mySerial.printf("AT+CIPSEND=%d\r\n", len);
     readline();
 #ifdef ADAFRUIT_FONA_DEBUG
     printf("\t<--- %s\r\n", replybuffer);
@@ -860,7 +930,7 @@
     if (replybuffer[0] != '>') return false;
     
     for (uint16_t i=0; i<len; i++) {
-        mySerial->putc(packet[i]);
+        mySerial.putc(packet[i]);
     }
     readline(3000); // wait up to 3 seconds to send the data
 #ifdef ADAFRUIT_FONA_DEBUG
@@ -886,7 +956,7 @@
 uint16_t Adafruit_FONA::TCPread(uint8_t *buff, uint8_t len) {
     uint16_t avail;
     
-    mySerial->printf("AT+CIPRXGET=2,%d\r\n", len);
+    mySerial.printf("AT+CIPRXGET=2,%d\r\n", len);
     readline();
     if (! parseReply("+CIPRXGET: 2,", &avail, ',', 0)) return false;
     
@@ -922,31 +992,31 @@
     printf("\t---> AT+HTTPPARA=\"%s\"\r\n", parameter);
 #endif
     
-    mySerial->printf("AT+HTTPPARA=\"%s", parameter);
+    mySerial.printf("AT+HTTPPARA=\"%s", parameter);
     if (quoted)
-        mySerial->printf("\",\"");
+        mySerial.printf("\",\"");
     else
-        mySerial->printf("\",");
+        mySerial.printf("\",");
 }
 
 bool Adafruit_FONA::HTTP_para_end(bool quoted) {
     if (quoted)
-        mySerial->printf("\"\r\n");
+        mySerial.printf("\"\r\n");
     else
-        mySerial->printf("\r\n");
+        mySerial.printf("\r\n");
     
     return expectReply("OK");
 }
 
 bool Adafruit_FONA::HTTP_para(const char* parameter, const char* value) {
     HTTP_para_start(parameter, true);
-    mySerial->printf(value);
+    mySerial.printf(value);
     return HTTP_para_end(true);
 }
 
 bool Adafruit_FONA::HTTP_para(const char* parameter, int32_t value) {
     HTTP_para_start(parameter, false);
-    mySerial->printf("%d", value);
+    mySerial.printf("%d", value);
     return HTTP_para_end(false);
 }
 
@@ -957,7 +1027,7 @@
     printf("\t---> AT+HTTPDATA=%d,%d\r\n", size, maxTime);
 #endif
     
-    mySerial->printf("AT+HTTPDATA=%d,%d\r\n", size, maxTime);
+    mySerial.printf("AT+HTTPDATA=%d,%d\r\n", size, maxTime);
     
     return expectReply("DOWNLOAD");
 }
@@ -1027,7 +1097,7 @@
     if (! HTTP_data(postdatalen, 10000))
         return false;
     for (uint16_t i = 0; i < postdatalen; i++) {
-        mySerial->putc(postdata[i]);
+        mySerial.putc(postdata[i]);
     }
     if (! expectReply("OK"))
         return false;
@@ -1105,8 +1175,8 @@
     // Read all available serial input to flush pending data.
     uint16_t timeoutloop = 0;
     while (timeoutloop++ < 40) {
-        while(mySerial->readable()) {
-            mySerial->getc();
+        while(readable()) {
+            getc();
             timeoutloop = 0;  // If char was received reset the timer
         }
         wait_ms(1);
@@ -1117,8 +1187,8 @@
     uint16_t idx = 0;
     
     while (b && (idx < sizeof(replybuffer)-1)) {
-        if (mySerial->readable()) {
-            replybuffer[idx] = mySerial->getc();
+        if (readable()) {
+            replybuffer[idx] = getc();
             idx++;
             b--;
         }
@@ -1136,8 +1206,8 @@
             break;
         }
     
-        while(mySerial->readable()) {
-            char c =  mySerial->getc();
+        while(readable()) {
+            char c =  getc();
             if (c == '\r') continue;
             if (c == 0xA) {
                 if (replyidx == 0)   // the first 0x0A is ignored
@@ -1168,7 +1238,7 @@
     printf("\t---> %s\r\n", send);
 #endif
 
-    mySerial->printf("%s\r\n",send);
+    mySerial.printf("%s\r\n",send);
 
     uint8_t l = readline(timeout);
 #ifdef ADAFRUIT_FONA_DEBUG
@@ -1185,7 +1255,7 @@
     printf("\t---> %s%s\r\n", prefix, suffix);
 #endif
     
-    mySerial->printf("%s%s\r\n", prefix, suffix);
+    mySerial.printf("%s%s\r\n", prefix, suffix);
     
     uint8_t l = readline(timeout);
 #ifdef ADAFRUIT_FONA_DEBUG
@@ -1202,7 +1272,7 @@
     printf("\t---> %s%d\r\n", prefix, suffix);
 #endif
     
-    mySerial->printf("%s%d\r\n", prefix, suffix);
+    mySerial.printf("%s%d\r\n", prefix, suffix);
     
     uint8_t l = readline(timeout);
 #ifdef ADAFRUIT_FONA_DEBUG
@@ -1219,7 +1289,7 @@
     printf("\t---> %s%d,%d\r\n", prefix, suffix1, suffix2);
 #endif
     
-    mySerial->printf("%s%d,%d\r\n", prefix, suffix1, suffix2);
+    mySerial.printf("%s%d,%d\r\n", prefix, suffix1, suffix2);
     
     uint8_t l = readline(timeout);
 #ifdef ADAFRUIT_FONA_DEBUG
@@ -1236,7 +1306,7 @@
     printf("\t---> %s\"%s\"\r\n", prefix, suffix);
 #endif
     
-    mySerial->printf("%s\"%s\"\r\n", prefix, suffix);
+    mySerial.printf("%s\"%s\"\r\n", prefix, suffix);
     
     uint8_t l = readline(timeout);
 #ifdef ADAFRUIT_FONA_DEBUG