standalone sx1276 demo program

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

Revision:
33:319cbac3b6eb
Parent:
32:a2472bbe7c92
diff -r a2472bbe7c92 -r 319cbac3b6eb utilities.cpp
--- a/utilities.cpp	Wed Jul 29 12:14:42 2015 +0000
+++ b/utilities.cpp	Thu Sep 03 14:33:55 2015 +0000
@@ -4,6 +4,7 @@
 #include "debug.h"
 #include "serial_api.h"
 
+#ifndef STANDALONE
 void gps_setup(void)
 {
     printf("setting up gps\r\n");
@@ -18,8 +19,8 @@
     gpsd.minute=1;
     gpsd.seconds=1;
 }    
-
-int get_kbd_str(char* buf, int size)
+#endif
+int get_kbd_str2(char* buf, int size)
 {
     char c;
     int i;
@@ -51,25 +52,82 @@
                 buf[i++] = c;
                 pc.putc(c);
             }
+        }  else {
+            if (app == APP_PING)
+                ping_pong();
+            if (app == APP_HELLO)
+                hello();
+#ifndef STANDALONE
+            if (app == APP_GPS)
+                check_gps();    
+#endif          
+            #if 1    
+            if (app == APP_CHAT)
+            {
+                // printf("utility call to console chat\r\n");
+                check_rx_chat();    
+            }
+            #endif
+        }
+    } // ...for()
+}
+
+int get_kbd_str(char* buf, int size)
+{
+    char c;
+    int i;
+    static int prev_len;
+    
+    for (i = 0;;) {
+        if (pc.readable()) {
+            c = pc.getc();
+            if (c == 8 && i > 0) {
+                pc.putc(8);
+                pc.putc(' ');
+                pc.putc(8);
+                i--;
+            } else if (c == '\r') {
+                if (i == 0) {
+                    if (app == APP_CHAT)
+                        return 0;
+                    else
+                        return prev_len; // repeat previous
+                } else {
+                    buf[i] = 0; // null terminate
+                    prev_len = i;
+                    return i;
+                }
+            } else if (c == 3) {
+                {
+                // ctrl-C abort
+                //printf("ctrl c\r\n");
+                app = APP_CONSOLE;
+                return -1;
+                }
+            } else if (i < size) {
+                buf[i++] = c;
+                pc.putc(c);
+            }
         }  
         
         else {
 #if 0            
-            return 0;
+            return 1;
 #else            
             if (app == APP_PING)
                 ping_pong();
             if (app == APP_HELLO)
             {
-             //   if (check_gps() == 0)
-             //   {
-                    return 0;
-                    //hello();
-             //   }
+#ifndef STANDALONE   
+                    check_gps();
+#endif              
+                    hello();
+     
             }                
-            
+#ifndef STANDALONE
             if (app == APP_GPS)
                 check_gps();    
+#endif
             #if 1    
             if (app == APP_CHAT)
             {
@@ -311,6 +369,24 @@
   
 }
 
+void OnCadDone( bool activity )
+{
+    //Radio.Sleep();
+    State = CAD;
+    if (activity)
+    {
+    debug_if( DEBUG_MESSAGE, "> OnCadDone\n\r" );
+    printf("on cad done\r\n");
+    Radio.StartCad();
+    }
+    else
+    {
+        // printf("false\r\n");
+        Radio.StartCad();
+    }
+}
+
+#ifndef STANDALONE
 void find_distance()
 {
     //float dist = 0;
@@ -325,6 +401,7 @@
     float d = 0;
       
     distance = 0;
+ 
     if (gpsd.lat_deg == 0.0)
     {
         distance = -1;
@@ -336,8 +413,8 @@
         return;
     }
     
-    lat1 = r_latitude;
-    lon1 = r_longitude;
+    lat1 = r_latitude_last;
+    lon1 = r_longitude_last;
         
     lat2 = gpsd.lat_deg;
     lon2 = gpsd.lon_deg;
@@ -354,7 +431,13 @@
        d = 0;
     }
     distance = distance + d;
-   // printf("distance: %f\r\n", gpsd.distance);
+#if 0    
+    if (distance > 10000)
+    {
+       printf("distance: %f\r\n", distance);
+       printf("lat1=%f lon1=%f lat2=%f lon2=%f\r\n",lat1,lon1,lat2,lon2);
+    }
+#endif    
    
 }
  
@@ -375,7 +458,8 @@
         
         //check if we recieved a new message from GPS, if so, attempt to parse it,
         if ( gpsd.newNMEAreceived() ) {
-              if (r_latitude != 0)
+              //printf("nmea received \r\n");
+              if (r_latitude_last != 0)
               {
                     find_distance();
                     //printf("distance is %f\r\n",distance);
@@ -386,10 +470,22 @@
                             cLCD.setCursor(0,1);
                             cLCD.printf("d:%0.1fm per:%d      ", distance,per);
                         }
+
                     }
+                    //printf("distance = %f, %f\r\n",distance, r_latitude_last);
                }
+               else
+               {
+                    cLCD.setCursor(0,1);
+                    if (gpsd.lat_deg != 0)
+                        cLCD.printf("%0.4f %0.4f", gpsd.lat_deg, gpsd.lon_deg);
+                    else
+                        cLCD.printf("No GPS. per:%d      ",per);
+                }
+               
+ 
    
-            if ( !gpsd.parse(gpsd.lastNMEA()) ) 
+            if ( !gpsd.parse(gpsd.lastNMEA()) )  
             {
                // radfta led = !led;   
                #if 0
@@ -419,4 +515,4 @@
         }
    
 }
-
+#endif