A fork of the original interface for OS/2. Features a correctly-implemented recv (but retains the old behavior via recv2).

Dependencies:   BufferedSerial

Dependents:   weather_clock weather_clock

Revision:
49:cac09a34102c
Parent:
47:04632d22a723
Child:
50:f484783b8a34
diff -r 6031f70e3914 -r cac09a34102c ESP8266/ESP8266.cpp
--- a/ESP8266/ESP8266.cpp	Thu Jun 04 20:45:00 2015 +0000
+++ b/ESP8266/ESP8266.cpp	Sun Mar 31 00:53:46 2019 +0000
@@ -23,7 +23,7 @@
 #include <algorithm>
 
 //Debug is disabled by default
-#if 0
+#if 1
 #define DBG(x, ...)  printf("[ESP8266 : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 
 #define WARN(x, ...) printf("[ESP8266 : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 
 #define ERR(x, ...)  printf("[ESP8266 : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 
@@ -38,7 +38,9 @@
 #define INFO(x, ...)
 #endif
 
-
+int tmp_buf_ind = 0;
+char tmp_buf[8192];
+RawSerial dev1(p28,p27);
 ESP8266 *ESP8266::_inst;
 
 ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, int baud, int timeout) :
@@ -70,11 +72,11 @@
 bool ESP8266::connect(const char *ssid, const char *phrase) {
     // Configure as station with passed ssid and passphrase
     if (!(command("wifi.setmode(wifi.STATION);") &&
-          command("wifi.sta.config('") &&
+          command("wifi.sta.config(\"") &&
           command(ssid) &&
-          command("','") &&
+          command("\",\"") &&
           command(phrase) &&
-          command("')") &&
+          command("\")") &&
           execute()))
         return false;
         
@@ -82,7 +84,7 @@
     // TODO WISHLIST make this a seperate check so it can run asynch?
     Timer timer;
     timer.start();
-    
+
     while (true) {
         int ip_len = 15;
         
@@ -136,16 +138,18 @@
     
     // Setup functions for sending and recieving characters on the esp side
     if (!(command("cm='';") &&
-          command("function cs(n) c:send(n) end;") &&
+          command("function cs(n) c:send(n); end;") &&
           command("function ca() print(#cm) end;") &&
           command("function cr(n) " 
                     "d=cm:sub(1,n):gsub('.', function(s) "
                       "return s.format('\\\\%03d', s:byte(1)) "
                     "end);"
-                    "cm=cm:sub(n+1,-1);" 
+                    "cm=cm:sub(n+1,-1); " 
                     "print(d) "
                   "end;") && 
-          command("c:on('receive',function(c,n) cm=cm..n end)") && 
+          command("c:on('receive',function(c,n) "
+                    "cm=cm..n "
+                    "end)") && 
           execute()))
         return false;
     
@@ -174,8 +178,9 @@
         if (!(command("print(cc)") && execute(con_buf, &con_len)))
             return false;
         
-        if (memcmp(con_buf, "true", con_len) == 0)
+        if (memcmp(con_buf, "true", con_len) == 0) {
             return true;
+        }
         else if (memcmp(con_buf, "false", con_len) == 0)
             return false;
         
@@ -204,23 +209,146 @@
                 serialputc(c + '0') < 0)
                 return false;
         }
-        
         if (!(command("')") && execute()))
             return false;
+        
     }
-    
     return true;
 }
 
-bool ESP8266::recv(char *buffer, int *len) {
+int ESP8266::recv2(char* buffer, int len, int offset=0) {
+    // start at offset, read at most LEN bytes
+    // first make sure we have that many bytes to read!
+    
+    // Get total number of bytes
+    char num_buf[16];
+    int num_i = 0;
+    char cmd[128];
+    
+    sprintf(cmd, "print(#cm)\r\n");
+    command(cmd);
+    
+    // cleanup
+    while ((serialgetc() != '\n'));
+    
+    // Get number
+    while (char tmp = serialgetc()) {
+        if (tmp == '\r') {
+            serialgetc();
+            break;
+        }
+        num_buf[num_i++] = tmp;
+    }
+    num_buf[num_i] = '\0';
+    
+    // now we can get number
+    int resp_len = atoi(num_buf);
+    if (offset >= resp_len) {
+        // Can't read anything!
+        return -1;
+    }
+    if ((offset + len) > resp_len) {
+        len = resp_len - offset;
+    }
+    // we're good
+    sprintf(cmd, "print(cm:sub(%d, %d))\r\n", offset + 1, offset + len);
+    command(cmd);
+    
+    // cleanup
+    while ((serialgetc() != '\n'));
+    
+    // Read Data
+    for (int i = 0; i < len; i++) {
+        buffer[i] = serialgetc();
+    }
+    // Will be two more characters to clean up, but NOT PART OF DATA
+    serialgetc();
+    serialgetc();
+    
+    return len;
+}
+bool ESP8266::recv2(char *buffer, int *len) {
     char len_buf[16];
     sprintf(len_buf, "%d", *len);
     
-    if (!(command("cr(") &&
-          command(len_buf) &&
-          command(")")))
-        return false;
+    //if (!(command("cr(") &&
+    //      command(len_buf) &&
+    //      command(")")))
+    //    return false;
+   //if (!(command("\r\n") && discardEcho()))
+    //    return false;
+    //if (!(command("print(cm)")))
+    //    return false;
+    char buf[4096];
+    int buf_len = 4095;
+    int ind = 0;
+    while (true) {
+        dev1.printf("\r\nIterating\r\n");
+        int res = recv2(buf + ind, buf_len, ind);
+        dev1.printf("Result: %d\n", res);
+        if (res < 0) {
+            break;
+        } else {
+            ind += res;
+        }
         
+    }
+    dev1.printf("\r\n** WHAT WE HAVE **\r\n");
+    dev1.printf(buf);
+    dev1.printf("\r\n** DONE ** \r\n");
+    dev1.getc();
+    
+    int len2 = 127;
+    int n = 0;
+    char* CMD = "print(cm:sub(%d, %d))\r\n";
+    char cmd[1024];
+    char cmd_buf_f[8192];
+    int cmd_buf_i = 0;
+    while (true) {
+        tmp_buf_ind = 0;
+        //command("print(#cm)\r\n");
+        //sprintf(cmd, "print(#cm)\r\n");
+        //command(cmd);
+        //command(cmd);
+        sprintf(cmd, CMD, n + 1, n + 1024);
+        command(cmd);
+        // cleanup the command
+        for (int l = 0; l <= strlen(cmd); l++) {
+            serialgetc();
+        }
+        /*
+        // While we have numbers, get
+        char num_buf[16];
+        int ind2 = 0;
+        while (char tmp = serialgetc()) {
+            if (tmp < '0' || tmp > '9') {
+                break;
+            }
+            num_buf[ind2++] = tmp;
+        }
+        num_buf[ind2] = '\0';
+        dev1.printf("NUM: %s = %d\r\n", num_buf, atoi(num_buf));
+        */
+        for (int l = 0; l < 3; l++) {
+            cmd_buf_f[cmd_buf_i++] = serialgetc();
+        }
+        cmd_buf_f[cmd_buf_i] = '\0';
+        n += 1024;
+//        tmp_buf[tmp_buf_ind % 8192] = '\0';
+ //       dev1.printf("Raw output\r\n%s\r\n", tmp_buf);
+        //dev1.printf("Length: %d\n", len2);
+//        for (int k = 0; k < 4000; k++) {
+//            dev1.putc(buf[k]);
+//        }
+        dev1.printf("** WHAT WE HAVE SO FAR **\r\n");
+        dev1.printf(cmd_buf_f);
+        dev1.printf("\r\n** END WHAT WE HAVE **\r\n");
+        dev1.printf("Waiting...\r\n");
+        dev1.getc();
+        dev1.printf("Continuing...\r\n");
+    }
+    printf("\r\n**EXECUTION**\r\n%s", buf);
+    while(true);
    if (!(command("\r\n") && discardEcho()))
         return false;
         
@@ -244,7 +372,6 @@
             
         buffer[i] = (a-'0')*100 + (b-'0')*10 + (c-'0');
     }
-    
     // Flush to next prompt
     return flush();
 }
@@ -331,12 +458,14 @@
 
 int ESP8266::serialgetc() {
     Timer timer;
+
     timer.start();
     
     while (true) {
         if (_serial.readable()) {
             char c = _serial.getc();
 #ifdef ESP8266_ECHO
+  //          tmp_buf[tmp_buf_ind++ % 8192] = c;
             printf("%c", c);
 #endif
             return c;
@@ -383,7 +512,7 @@
 }
 
 bool ESP8266::command(const char *cmd) {
-    DBG("command sent:\t %s", cmd);
+    //DBG("command sent:\t %s", cmd);
     
     for (int i = 0; cmd[i]; i++) {
         if (serialputc(cmd[i]) < 0)
@@ -416,7 +545,7 @@
             resp_buf[i] = c;
         }
         
-        DBG("command response:\t %.*s", *resp_len, resp_buf);
+        //DBG("command response:\t %.*s", *resp_len, resp_buf);
     }
     
     return flush();