CaryCoders / Mbed 2 deprecated demo_SX1276_standalone

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

Files at this revision

API Documentation at this revision

Comitter:
ftagius
Date:
Wed Jul 29 12:14:42 2015 +0000
Parent:
31:2c813f321db7
Child:
33:319cbac3b6eb
Commit message:
cleanup

Changed in this revision

AdafruitGPS.lib Show annotated file Show diff for this revision Revisions of this file
LCDadafruit/lcdadafruit.cpp Show annotated file Show diff for this revision Revisions of this file
console_chat.cpp Show annotated file Show diff for this revision Revisions of this file
datetime.cpp Show annotated file Show diff for this revision Revisions of this file
datetime.h Show annotated file Show diff for this revision Revisions of this file
hello.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
utilities.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AdafruitGPS.lib	Tue Jul 14 14:58:23 2015 +0000
+++ b/AdafruitGPS.lib	Wed Jul 29 12:14:42 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/CaryCoders/code/AdafruitGPS/#35fcaa2209af
+http://developer.mbed.org/teams/CaryCoders/code/AdafruitGPS/#59bfc720698f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCDadafruit/lcdadafruit.cpp	Wed Jul 29 12:14:42 2015 +0000
@@ -0,0 +1,61 @@
+#include "mbed.h"
+#include "lcdadafruit.h"
+
+LCDadafruit::LCDadafruit(I2C & in_cI2C)
+    : LCD(in_cI2C)
+    , m_cMCP(in_cI2C, 0x20 << 1)
+    , m_cLCD(m_cMCP)
+{
+    m_cLCD.begin(16,2);
+}
+
+int LCDadafruit::_putc(int in_nValue)
+{
+    return m_cLCD._putc(in_nValue);
+}
+
+void LCDadafruit::clear()
+{
+    m_cLCD.clear();
+}
+
+uint8_t LCDadafruit::columns()
+{
+    return 16;
+}
+
+void LCDadafruit::createChar(uint8_t location, uint8_t charmap[])
+{
+    m_cLCD.createChar(location,charmap);
+}
+
+void LCDadafruit::home()
+{
+    m_cLCD.home();
+}
+
+uint8_t LCDadafruit::rows()
+{
+    return 2;
+}
+
+void LCDadafruit::setCursor(uint8_t in_nX, uint8_t in_nY)
+{ 
+    m_cLCD.setCursor(in_nX,in_nY);
+}
+
+void LCDadafruit::showBlink(bool in_bShow)
+{
+    
+    if (in_bShow) m_cLCD.blink(); else m_cLCD.noBlink();    
+}
+
+void LCDadafruit::showCursor(bool in_bShow)
+{
+    if (in_bShow) m_cLCD.cursor(); else m_cLCD.noCursor();
+}
+
+void LCDadafruit::showDisplay(bool in_bShow)
+{
+    if (in_bShow) m_cLCD.display(); else m_cLCD.noDisplay();
+}
--- a/console_chat.cpp	Tue Jul 14 14:58:23 2015 +0000
+++ b/console_chat.cpp	Wed Jul 29 12:14:42 2015 +0000
@@ -173,7 +173,7 @@
                 //radf Radio.Tx( TX_TIMEOUT_VALUE );
                 break;    
             default:
-                debug("Unsupported command\r\n");
+                debug("Unsupported command received\r\n");
                 break;     
           }
     } else if (pcbuf[0] == 'f' && pcbuf[1] == 'r' && pcbuf[2] == 'f') {
@@ -234,8 +234,11 @@
               
             print_cr();printf("\r\n");
 
-    } else
+    } 
+#if 0    
+    else
         debug("Unsupported command\r\n");
+#endif        
     
     if (app  == APP_CONSOLE)
     {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/datetime.cpp	Wed Jul 29 12:14:42 2015 +0000
@@ -0,0 +1,40 @@
+#include "datetime.h"
+#include "rtc_api.h"
+#include "mbed.h"
+
+void SetDateTime
+(int           year = 2015
+,int           mon = 5
+,int           day = 26
+,int           hour = 10
+,int           min = 0
+,int           sec = 0
+)
+{
+  //  struct     tm Clock;
+    Clock.tm_year = year - 1900;
+    Clock.tm_mon  = mon;
+    Clock.tm_mday = day;
+    Clock.tm_hour = hour;
+    Clock.tm_min  = min;
+    Clock.tm_sec  = sec;
+    time_t epoch = mktime(&Clock);
+    if (epoch == (time_t) -1) {
+        error("Error in clock setting\r\n");        
+    }
+    set_time(epoch);
+}   
+
+void ShowDateTime()
+{
+    char       str[32];
+    time_t     seconds = time(NULL);
+    struct tm *gpsd = localtime(&seconds);
+    struct tm *tminfo = localtime(&seconds);
+ 
+    strftime(str, 32, "%F,%T", tminfo);
+    //printf("RTC: %s\r\n", str);
+    printf("%02d/%02d/%02d,", gpsd->tm_mon, gpsd->tm_mday, gpsd->tm_year+1900);
+    printf("%02d:%02d:%02d ", gpsd->tm_hour, gpsd->tm_min, gpsd->tm_sec);  
+}
+ 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/datetime.h	Wed Jul 29 12:14:42 2015 +0000
@@ -0,0 +1,14 @@
+#include "rtc_api.h"
+
+void SetDateTime
+(int           year
+,int           mon
+,int           day
+,int           hour
+,int           min
+,int           sec
+);
+
+void ShowDateTime();
+
+extern struct tm Clock;
\ No newline at end of file
--- a/hello.cpp	Tue Jul 14 14:58:23 2015 +0000
+++ b/hello.cpp	Wed Jul 29 12:14:42 2015 +0000
@@ -2,16 +2,15 @@
 #include "sx1276-hal.h"
 #include "main.h"
 #include "debug.h"
-// radfta #include "vt100.h"
 #include "serial_api.h"
 #include "GPS.h"
 #include <math.h>
-#define PI 3.14159265
+
 
 void start_hello(void)
 {
     int len = get_kbd_str(pcbuf, PCBUF_SIZE);
-    printf("in start gps/hello\r\n");
+    //printf("in start gps/hello\r\n");
     if (len < 0) {
         fflush(stdout);
         Radio.Rx( RX_TIMEOUT_VALUE );
@@ -29,147 +28,22 @@
     }
     else
     {   
-        hello();
-    }
-}
-
-void find_distance()
-{
-    //float dist = 0;
-    float lat2 = 0;
-    float lon2 = 0;
-    float lat1 = 0;
-    float lon1 = 0;
-    float dLat = 0;
-    float dLon = 0;
-    float c = 0;
-    float a = 0;
-    float d = 0;
-    
-#ifdef radfta    
-    gpsd.distance = 0;
-    if (gpsd.l_latitude == 0.0)
-    {
-        gpsd.distance = -1;
-        return;
-    }
-    if (gpsd.l_longitude == 0.0)
-    {
-        gpsd.distance = -1;
-        return;
-    }
-   
-    //lat1 = 35.73244;
-    //lon1 = -78.79333;
-    
-    lat1 = gpsd.r_latitude;
-    lon1 = gpsd.r_longitude;
-        
-    lat2 = gpsd.l_latitude;
-    lon2 = gpsd.l_longitude;
-    //Calculate Distance
-    dLat = (lat2-lat1)*(PI/180);
-    dLon = (lon2-lon1)*(PI/180);
-    a = sin(dLat/2) * sin(dLat/2) +
-    sin(dLon/2) * sin(dLon/2) * cos(lat1*(PI/180)) * cos(lat2*(PI/180));
-    c = 2 * atan2(sqrt(a), sqrt(1-a));
-    d = 6371000 * c;
- 
-    //GPS is only precise to 5 meters, so throw out bad data
-    if ( d <= 5) {
-       d = 0;
+       hello();
     }
-    gpsd.distance = gpsd.distance + d;
-   // printf("distance: %f\r\n", gpsd.distance);
-#endif  
-}
- 
-void check_gps(void)
-{
-    static int gps_count=0;
-#ifdef radfta    
-    if (gps_count >= 0)
-    {
-        //printf("checking gps\r\n");
-        gps_count=0;  // reset for next cycle
-        if(gpsd.sample()) 
-        {
-            led = !led;   
-            static int swap=0;
-            //printf("gps sample is true \r\n");
-            if (gpsd.l_latitude != 0)
-            {
-                if (RADIO_INSTALLED)
-                {
-                    cLCD.setCursor(0,1);
-                    cLCD.printf("d:NA per:%d      ", per);
-                    //cLCD.printf("%0.4f %0.4f", gpsd.l_latitude, gpsd.l_longitude);
-                }    
-                else
-                {
-                    if (swap)
-                    {
-                    cLCD.setCursor(0,0);
-                    cLCD.printf("Lat: %0.6f ", gpsd.l_latitude);
-                    cLCD.setCursor(0,1);
-                    cLCD.printf("Lon: %0.6f ", gpsd.l_longitude);
-                    swap = 0;
-                    }
-                    else
-                    {
-                    cLCD.setCursor(0,1);
-                    cLCD.printf("Lat: %0.6f ", gpsd.l_latitude);
-                    cLCD.setCursor(0,0);
-                    cLCD.printf("Lon: %0.6f ", gpsd.l_longitude);
-                    swap = 1;
-                    }
-                }
-
-                if (gpsd.r_latitude != 0)
-                {
-                    find_distance();
-                    if (gpsd.distance < 50000 )
-                    {
-                        if (gpsd.distance >= 0)
-                            cLCD.setCursor(0,1);
-                            cLCD.printf("d:%0.1fm per:%d      ", gpsd.distance,per);
-                    }
-                }
-            }    
-            else
-            {
-                cLCD.setCursor(0,1);
-                cLCD.printf("No GPS. per:%d      ",per);
-            }
-        }
-        else      
-        {  
-            //printf("gps sample is false \r\n");
-            cLCD.setCursor(0,1);
-            cLCD.printf("No GPS. per:%d      ",per);
-        }
-    }
-    else
-    {
-        #if 0
-        if (gps_count % 5)
-            gpsd.flushSerialBuffer();
-        #endif
-        //printf("not checking gps\r\n");
-        gps_count++;
-    }
-#endif       
 }
 
 void hello(void)
 {
-    srand ( randomSeed() );
+ 
+    // srand ( randomSeed() );
     // generate a random wait time
-    int mywait = ((rand()%2 +1));
+    // int mywait = ((rand()%2 +1));
+    int mywait = 3;
     int i=0;
     int rcvd=0;
     int lost=0;
- 
+     
+    // check_gps();
     for (i=0;i<max_pkts;i++)
     {
         
@@ -201,8 +75,8 @@
 
             printf("rx timeout in gps/hello loop. ");
             printf("per=%d\r\n", per);
-            // radfta gpsd.r_latitude = 0;
-            // radfta gpsd.r_longitude = 0;
+            r_latitude = 0;
+            r_longitude = 0;
             // printf("rcv - rssi = %d snr = %d msg=%s\r\n", RssiValue, SnrValue, BufferRx);
             cLCD.setCursor(0,0);
             cLCD.printf("RI:NA SR:NA      ");
@@ -212,7 +86,7 @@
             {
                 if( strncmp( ( const char* )BufferRx, ( const char* )HelloMsg, 5 ) == 0 )
                 {
-                    led = !led;     
+                    // radfta led = !led;     
                     printf("rcv - rssi = %d snr = %d msg=%s ", RssiValue, SnrValue, BufferRx );
                     printf("per=%d\r\n", per);
                     cLCD.setCursor(0,0);
@@ -220,12 +94,11 @@
                 }
                 else
                 {
-                  // float latitude, longitude;
-#ifdef radfta                  
-                  if(sscanf((char *)BufferRx, "GP,%f,%f", &gpsd.r_latitude, &gpsd.r_longitude ) >= 1) 
+
+                  if(sscanf((char *)BufferRx, "GP,%f,%f", &r_latitude, &r_longitude ) >= 1) 
                   {                    
-                    led = !led;     
-                    printf("rssi=%d snr=%d rlat=%0.5f rlon=%0.5f llat=%0.5f llon=%0.5f dst=%0.0fm ", RssiValue, SnrValue, gpsd.r_latitude, gpsd.r_longitude, gpsd.l_latitude, gpsd.l_longitude, gpsd.distance);
+                    // radfta led = !led;     
+                    printf("rssi=%d snr=%d rlat=%0.5f rlon=%0.5f llat=%0.5f llon=%0.5f dst=%0.0fm ", RssiValue, SnrValue, r_latitude, r_longitude, gpsd.lat_deg, gpsd.lon_deg, distance);
                     printf("per=%d\r\n", per);
                     cLCD.setCursor(0,0);
                     cLCD.printf("RI:%d SR:%d    ", RssiValue,SnrValue);
@@ -233,28 +106,21 @@
                   }
                   else
                     printf("strange received message: %s\r\n", BufferRx);
-#else
-                  cLCD.setCursor(0,0);
-                  cLCD.printf("RI:%d SR:%d    ", RssiValue,SnrValue);
-                  printf("received message: %s\r\n", BufferRx);               
-#endif                    
+           
               }                  
             }
        case RX_ERROR:
-            if (gpsEnabled)
-            {
-                check_gps();
-            }   
-#ifdef radfta            
+         
             if (gpsEnabled)
-                sprintf((char *)BufferTx, "GP,%4.5f,%4.5f", gpsd.l_latitude, gpsd.l_longitude);
+                sprintf((char *)BufferTx, "GP,%4.5f,%4.5f", gpsd.lat_deg, gpsd.lon_deg);
             else
-#else            
                 strcpy( ( char* )BufferTx, ( char* )HelloMsg );
-#endif               
+#if 0
+radfta     
             BufferSize=strlen((char *)BufferTx);
             //printf("Sending new buffer\r\n");
             Radio.Send( BufferTx, BufferSize );
+#endif            
             State = LOWPOWER;
 
             break;
@@ -271,7 +137,8 @@
             State = LOWPOWER;
             break;
         case LOWPOWER:
-            wait_ms (5);
+            //printf("in wait\r\n");
+           // wait_ms (1);
             break;
         default:
             debug("state is set to low power\r\n");
--- a/main.cpp	Tue Jul 14 14:58:23 2015 +0000
+++ b/main.cpp	Wed Jul 29 12:14:42 2015 +0000
@@ -1,75 +1,47 @@
 #include "mbed.h"
 #include "lcdadafruit.h"
-//radfta #include "keyreaderadafruit.h"
-//radfta #include "keyreadernull.h"
-// #include "RTclock.h"
-//#include "DateModule.h"
-//#include "TempModule.h"
-//#include "TimeModule.h"
-//#include "TitleModule.h"
-//#include "SyncModule.h"
-//#include "GPSModule.h"
-//#include "LatitudeModule.h"
-//#include "RadioModule.h"
-//#include "MenuManager.h"
 #include "sx1276-hal.h"
 #include "GPS.h"
 #include "main.h"
 #include "debug.h"
-//#include "vt100.h"
 #include "serial_api.h"
+#include "datetime.h"
+#include "time.h"
+#include <string>
 
 
-/* Set this flag to '1' to display debug messages on the console */
-#define DEBUG_MESSAGE   1
 #define USE_MODEM_LORA  1
-
 //#define RF_FREQUENCY                                    868000000 // Hz
 //#define RF_FREQUENCY                                    880030000 
 #define RF_FREQUENCY                                    915000000.0 // Hz
 //#define RF_FREQUENCY                                    413000000.0 // Hz
-#define TX_OUTPUT_POWER                                 20        // 14 dBm
-#define LORA_BANDWIDTH                                  2         // [0: 125 kHz,
-                                                                  //  1: 250 kHz,
-                                                                  //  2: 500 kHz,
-                                                                  //  3: Reserved]
-// #define LORA_SPREADING_FACTOR                        7         // [SF7..SF12]
-#define LORA_SPREADING_FACTOR                           12        // [SF7..SF12]
-// #define LORA_CODINGRATE                              1         // [1: 4/5,
-#define LORA_CODINGRATE                                 2         // [1: 4/5,
-                                                                  //  2: 4/6,
-                                                                  //  3: 4/7,
-                                                                  //  4: 4/8]
-#define LORA_PREAMBLE_LENGTH                            8         // Same for Tx and Rx
-#define LORA_SYMBOL_TIMEOUT                             5         // Symbols
-#define LORA_FIX_LENGTH_PAYLOAD_ON                      false
-#define LORA_FHSS_ENABLED                               false  
-#define LORA_NB_SYMB_HOP                                4     
-#define LORA_IQ_INVERSION_ON                            false
-#define LORA_CRC_ENABLED                                true
+
 
 //#define BUFFER_SIZE                                     64        // Define the payload size here
 
 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
-DigitalOut led(LED2);
-//DigitalOut led(LED_RED);
+//DigitalOut led(LED2);
+DigitalOut led(LED_RED);
 #else
 DigitalOut led(LED1);
 #endif
 Serial pc(USBTX, USBRX);
 
 float Frequency = RF_FREQUENCY;
+float distance = -1;
+float r_latitude = 0;
+float r_longitude = 0;
 int TxPower = TX_OUTPUT_POWER;
 int Bandwidth = LORA_BANDWIDTH;
 int SpreadingFactor = LORA_SPREADING_FACTOR;
 int CodingRate = LORA_CODINGRATE;
+struct tm Clock;
 
 /*
  *  Global variables declarations
  */
 typedef RadioState States_t;
 volatile States_t State = LOWPOWER;
-// radfta vt100 ctrl;
 
 SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, NULL );
 // for hand wired I2C cI2C(PTC9, PTC8); 
@@ -95,6 +67,7 @@
 int max_pkts = 20;
 int pkt_data[20];
 int per = 0;
+int tx_rate_us = 100000;  // 1 second
 app_e app = APP_NONE;
 bool isMaster = true;
 bool AlwaysMaster = false;
@@ -102,243 +75,51 @@
 bool ackRcvd = true;
 bool rxTimeout = false;
 bool gpsEnabled = true;
+InterruptIn PPS(PTA1); //pps IRQ
+Ticker usec_tick;
+ 
+void tx_pkt() {
+    led = !led;
+     BufferSize=strlen((char *)BufferTx);
+  //   printf("Buffersize = %d\r\n",BufferSize);
+     Radio.Send( BufferTx, BufferSize );
+     //Radio.Rx( RX_TIMEOUT_VALUE / 1  );
+}
   
-char* itoa(int val, int base){
-    
-    static char buf[32] = {0};
-    
-    int i = 30;
-    
-    for(; val && i ; --i, val /= base)
-    
-        buf[i] = "0123456789abcdef"[val % base];
-    
-    return &buf[i+1];
-    
-}    
-
-unsigned int randomSeed(){
-    AnalogIn randIn(A0);  // analog input for random number seed
-    unsigned int rnum = 0;  // int = 4 bytes - 32 bits
-    // build 32 bit random number from the 2 lower bits from 16 analog reads
-    for (int i = 0; i<4;i++){
-        rnum = rnum | ((randIn.read_u16() & 15) << i*4);
-        wait_ms(5);
-    }
-    return rnum;
-}
-
-void configRxTx() {
-
-
-    Radio.SetTxConfig( MODEM_LORA, TxPower, 0, Bandwidth,
-                         SpreadingFactor, CodingRate,
-                         LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
-                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
-                         LORA_IQ_INVERSION_ON, 2000000 );
-    
-    Radio.SetRxConfig( MODEM_LORA, Bandwidth, SpreadingFactor,
-                         CodingRate, 0, LORA_PREAMBLE_LENGTH,
-                         LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
-                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
-                         LORA_IQ_INVERSION_ON, true );
-}
-
-void print_bandwidth()
+void ppsSync()
 {
-    printf("bandwidth: ");
-    switch (((Radio.Read(REG_LR_MODEMCONFIG1) & 0xf0) >> 4)) {
-        case 0: printf("7.8KHz "); break;
-        case 1: printf("10.4KHz "); break;
-        case 2: printf("15.6KHz "); break;
-        case 3: printf("20.8KHz "); break;
-        case 4: printf("31.25KHz "); break;
-        case 5: printf("41.7KHz "); break;
-        case 6: printf("62.5KHz "); break;
-        case 7: printf("125KHz "); break;
-        case 8: printf("250KHz "); break;
-        case 9: printf("500KHz "); break;
-        default: printf("%x ", Radio.Read(REG_LR_MODEMCONFIG1)); break;
-    }
-    //printf("\r\n");     
-}
-
-void print_cr()
-{
-    int cr = (Radio.Read(REG_LR_MODEMCONFIG1) & 0x0f)>>1;
-    printf("coding rate: ");
-    switch (cr)
+    static int pps_count=0;
+ 
+    ++pps_count;
+    if (pps_count == 1)
     {
-        case 1:
-            printf("4/5");
-            break;
-        case 2:
-            printf("4/6");
-            break;
-        case 3:
-            printf("4/7");
-            break;     
-        case 4:
-            printf("4/8");
-            break;                   
-        default:
-            printf("unknown");
-            break;
-    
-    }
-    printf(" ");
-}
-
-void print_power()
-{
-    uint8_t paConfig = 0;
-    uint8_t paDac = 0;
-     
-    paConfig = Radio.Read( REG_PACONFIG );
-    paDac = Radio.Read( REG_PADAC );
-
-    paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK ) | Radio.GetPaSelect( Radio.GetChannel()*1000000);
-    paConfig = ( paConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
-    printf("channel = %f getpa = %x, paConfig =%x \r\n",Radio.GetChannel(), Radio.GetPaSelect((uint32_t)Radio.GetChannel()*1000000), paConfig );
-    if( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
-    {
-        if( ( paDac & RF_PADAC_20DBM_ON ) == RF_PADAC_20DBM_ON )
-        {
-            printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f  ) + 5 );
-        }
-        else
-            printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) &0x0f) + 2 );
+        // detach the timer
+        usec_tick.detach();
+        printf("call attach\r\n");
+        //wait_ms(500);
+        usec_tick.attach_us(&tx_pkt, tx_rate_us);
     }
     else
     {
-        printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f ) - 1 );
+        if (pps_count > 10)
+        {
+            pps_count = 1;
+        }
     }
-  
-}
-
-void print_errata()
-{
-    
-        int seqconfig1 = (Radio.Read(REG_SEQCONFIG1));
-        int timer2 = (Radio.Read(REG_TIMER2COEF));
-        printf("seqconfig1 = 0x%02x\r\n", seqconfig1);
-        printf("timer2coef = 0x%02x\r\n", timer2);
-
-}
-void print_status()
-{
-
-    printf("Radio version: 0x%02x Channel: %.1f MHz ", \
-            Radio.Read(REG_VERSION),\
-            Radio.GetChannel()\
-            );
-    print_power();        
-    print_bandwidth(); printf("\r\n");
-    printf("Spreading Factor: %d ",SpreadingFactor);
-    print_cr();
-    if (isMaster)
-        printf("Mode: master ");
-    else
-        printf("Mode: slave ");
-        
- 
-     printf("\r\n");
-     print_errata();
-     printf("\r\n");
- 
-}
-
-
-void OnTxDone( void )
-{
-    Radio.Sleep( );
-    State = TX;
-    pkt_count++;
-
-   
-    //debug_if( DEBUG_MESSAGE, "> OnTxDone\r\n" );
-}
-
-void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
-{
-    Radio.Sleep( );
-    if (pkt_count >= max_pkts)
-    {
-        pkt_count=0;
-    } 
     
-    // mark the rolling avg that a pkt has been received
-    pkt_data[pkt_count]=1;
-    //printf("rx done, pkt_count = %d\r\n", pkt_count);
- 
-    BufferSize = size;
-    int i;
-    for (i=0; i< BUFFER_SIZE; i++)
-        BufferRx[ i ] = '\0';  // clear the buffer
- 
-    memcpy( BufferRx, payload, BufferSize );
-    if (rssi > 100)
-    {
-     rssi *= -1;
-    }
-    RssiValue = rssi;
-    SnrValue = snr;
-    State = RX;
-    rxTimeout = false;
-    if (app == APP_PING)  
-        debug_if( DEBUG_MESSAGE, "> OnRxDone size=%d rssi=%d snr=%d  \r\n", size, rssi, snr );
-  
-}
-
-void OnTxTimeout( void )
-{
-    Radio.Sleep( );
-    State = TX_TIMEOUT;
-    //debug_if( DEBUG_MESSAGE, "> OnTxTimeout\r\n" );
+    //printf("got a pps\r\n");
+    //ShowDateTime();
+   
 }
-
-void OnRxTimeout( void )
-{
-    Radio.Sleep( );
-    int i;
-    if (pkt_count >= max_pkts)
-    {
-        pkt_count=0;
-    } 
-    
-    // mark the rolling avg that a pkt has not been received
-    pkt_data[pkt_count]=2;
- 
-    for (i=0; i< BUFFER_SIZE; i++)
-        BufferRx[ i ] = '\0';  // clear the buffer
-    BufferSize = 0;
-    State = RX_TIMEOUT;
-    rxTimeout = false;
-    // debug_if( DEBUG_MESSAGE, "> OnRxTimeout\r\n" );
-}
-
-void OnRxError( void )
-{
-    Radio.Sleep( );
-    if (pkt_count >= max_pkts)
-    {
-        pkt_count=0;
-    } 
-    
-    // mark the rolling avg that a pkt has not been received
-    pkt_data[pkt_count]=2;
-    State = RX_ERROR;
-    debug_if( DEBUG_MESSAGE, "> OnRxError\r\n" );
-}
-
-
 int main()
 {
   
     int i;
-    // radfta gpsd.setBaud57600();
+    Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
+    const int refresh_Time = 1000; //refresh time in ms
+    bool gpsFix=false;
+    gps_setup();
     pc.baud(9600);
-    //pc.baud(57600);
     cI2C.frequency(400000);    // I2C can handle two different frequencies - switch to high speed if asked
     cLCD.clear();
     cLCD.home();
@@ -397,10 +178,55 @@
         pc.printf("Starting GPS App\r\n");
     }
     
- 
+    refresh_Timer.start();  //starts the clock on the timer
+    usec_tick.attach_us(&tx_pkt, tx_rate_us);  // attach a timer to periodically call tx.  this timer will be restarted in ppsSync to synronize timer accross systems
+    PPS.rise(&ppsSync);  // call ppsSync when the PPS irq is detected
+            
     while( 1 )
     {
-        // wait_ms( 50 ); 
+        check_gps();
+        if (refresh_Timer.read_ms() >= refresh_Time) 
+        {
+            // printf("reset timer popped\r\n");
+            if (gpsd.fix) {
+                // got a gps fix
+                if (gpsFix == false)
+                {
+                    // first time fix obtained
+                    gpsFix = true;
+                    // set the rtc with the latest gps time
+
+                    pc.printf("GPS fix obtained on %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds);
+                    // update rtc with the latest gps time stamp
+                    SetDateTime(gpsd.year+2000,
+                        gpsd.month,
+                        gpsd.day,
+                        gpsd.hour,
+                        gpsd.minute,
+                        gpsd.seconds);
+                }
+#if 0                
+                else
+                {
+                    printf("radfta - ");
+                    ShowDateTime();
+              //    pc.printf("GPS %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds);
+                }
+#endif
+  
+            }
+            else
+            {
+                gpsFix = false;
+                cLCD.setCursor(0,1);
+                cLCD.printf("No GPS. per:%d      ",per);
+                //pc.printf("Waiting for GPS FIX\r\n");
+              
+            }
+          
+            refresh_Timer.reset();    
+        }
+      
         switch (app) {
             case APP_PING:
                 start_ping_pong();
@@ -413,7 +239,6 @@
                 break;    
             case APP_HELLO:
                 start_hello();
-                wait(0.11);
                 break;
             case APP_CONSOLE:
                 // printf("-chat-> x");
--- a/main.h	Tue Jul 14 14:58:23 2015 +0000
+++ b/main.h	Wed Jul 29 12:14:42 2015 +0000
@@ -63,7 +63,6 @@
 void ping_pong(void);
 void start_hello(void);
 void hello(void);
-void check_gps(void);
 int get_kbd_str(char* buf, int size);
 void console_chat();
 void console();
@@ -74,6 +73,8 @@
 void print_bandwidth();
 void print_cr();
 void print_power();
+void gps_setup(void);
+int check_gps(void);
 
 /*
  *  Global variables declarations
@@ -85,6 +86,27 @@
 #define TX_TIMEOUT_VALUE                                4000000   // in us
 #define BUFFER_SIZE                                     256        // Define the payload size here
 #define RADIO_INSTALLED                                 true
+/* Set this flag to '1' to display debug messages on the console */
+#define DEBUG_MESSAGE   1
+#define TX_OUTPUT_POWER                                 20        // 14 dBm
+#define LORA_BANDWIDTH                                  2         // [0: 125 kHz,
+                                                                  //  1: 250 kHz,
+                                                                  //  2: 500 kHz,
+                                                                  //  3: Reserved]
+// #define LORA_SPREADING_FACTOR                        7         // [SF7..SF12]
+#define LORA_SPREADING_FACTOR                           12        // [SF7..SF12]
+// #define LORA_CODINGRATE                              1         // [1: 4/5,
+#define LORA_CODINGRATE                                 2         // [1: 4/5,
+                                                                  //  2: 4/6,
+                                                                  //  3: 4/7,
+                                                                  //  4: 4/8]
+#define LORA_PREAMBLE_LENGTH                            8         // Same for Tx and Rx
+#define LORA_SYMBOL_TIMEOUT                             5         // Symbols
+#define LORA_FIX_LENGTH_PAYLOAD_ON                      false
+#define LORA_FHSS_ENABLED                               false  
+#define LORA_NB_SYMB_HOP                                4     
+#define LORA_IQ_INVERSION_ON                            false
+#define LORA_CRC_ENABLED                                true
 
 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
 extern DigitalOut led;
@@ -105,6 +127,9 @@
 extern uint8_t BufferRx[];
 // radfta extern vt100 ctrl;
 extern float Frequency;
+extern float distance;
+extern float r_latitude;
+extern float r_longitude;
 extern int TxPower;
 extern int Bandwidth;
 extern int SpreadingFactor;
@@ -124,6 +149,7 @@
 extern int16_t RssiValue;
 extern int8_t SnrValue;
 #define PCBUF_SIZE 64
+#define PI 3.14159265
 
 typedef enum {
     APP_NONE = 0,
--- a/utilities.cpp	Tue Jul 14 14:58:23 2015 +0000
+++ b/utilities.cpp	Wed Jul 29 12:14:42 2015 +0000
@@ -2,9 +2,23 @@
 #include "sx1276-hal.h"
 #include "main.h"
 #include "debug.h"
-//radfta #include "vt100.h"
 #include "serial_api.h"
 
+void gps_setup(void)
+{
+    printf("setting up gps\r\n");
+    gpsd.setBaud(9600);
+    gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); 
+    gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
+    gpsd.sendCommand(PGCMD_NOANTENNA);
+    gpsd.day=01;
+    gpsd.month=01;
+    gpsd.year=15;
+    gpsd.hour=1;
+    gpsd.minute=1;
+    gpsd.seconds=1;
+}    
+
 int get_kbd_str(char* buf, int size)
 {
     char c;
@@ -37,11 +51,23 @@
                 buf[i++] = c;
                 pc.putc(c);
             }
-        }  else {
+        }  
+        
+        else {
+#if 0            
+            return 0;
+#else            
             if (app == APP_PING)
                 ping_pong();
             if (app == APP_HELLO)
-                hello();
+            {
+             //   if (check_gps() == 0)
+             //   {
+                    return 0;
+                    //hello();
+             //   }
+            }                
+            
             if (app == APP_GPS)
                 check_gps();    
             #if 1    
@@ -51,6 +77,346 @@
                 check_rx_chat();    
             }
             #endif
+            #endif
         }
+
     } // ...for()
 }
+void OnTxTimeout( void )
+{
+    Radio.Sleep( );
+    State = TX_TIMEOUT;
+    //debug_if( DEBUG_MESSAGE, "> OnTxTimeout\r\n" );
+}
+
+void OnRxTimeout( void )
+{
+    Radio.Sleep( );
+    int i;
+    if (pkt_count >= max_pkts)
+    {
+        pkt_count=0;
+    } 
+    
+    // mark the rolling avg that a pkt has not been received
+    pkt_data[pkt_count]=2;
+ 
+    for (i=0; i< BUFFER_SIZE; i++)
+        BufferRx[ i ] = '\0';  // clear the buffer
+    BufferSize = 0;
+    State = RX_TIMEOUT;
+    rxTimeout = false;
+    // debug_if( DEBUG_MESSAGE, "> OnRxTimeout\r\n" );
+}
+
+void OnRxError( void )
+{
+    Radio.Sleep( );
+    if (pkt_count >= max_pkts)
+    {
+        pkt_count=0;
+    } 
+    
+    // mark the rolling avg that a pkt has not been received
+    pkt_data[pkt_count]=2;
+    State = RX_ERROR;
+    debug_if( DEBUG_MESSAGE, "> OnRxError\r\n" );
+}
+
+  
+char* itoa(int val, int base){
+    
+    static char buf[32] = {0};
+    
+    int i = 30;
+    
+    for(; val && i ; --i, val /= base)
+    
+        buf[i] = "0123456789abcdef"[val % base];
+    
+    return &buf[i+1];
+    
+}    
+
+unsigned int randomSeed(){
+    AnalogIn randIn(A0);  // analog input for random number seed
+    unsigned int rnum = 0;  // int = 4 bytes - 32 bits
+    // build 32 bit random number from the 2 lower bits from 16 analog reads
+    for (int i = 0; i<4;i++){
+        rnum = rnum | ((randIn.read_u16() & 15) << i*4);
+        wait_ms(5);
+    }
+    return rnum;
+}
+
+void configRxTx() {
+
+
+    Radio.SetTxConfig( MODEM_LORA, TxPower, 0, Bandwidth,
+                         SpreadingFactor, CodingRate,
+                         LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
+                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
+                         LORA_IQ_INVERSION_ON, 2000000 );
+    
+    Radio.SetRxConfig( MODEM_LORA, Bandwidth, SpreadingFactor,
+                         CodingRate, 0, LORA_PREAMBLE_LENGTH,
+                         LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
+                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
+                         LORA_IQ_INVERSION_ON, true );
+}
+
+void print_bandwidth()
+{
+    printf("bandwidth: ");
+    switch (((Radio.Read(REG_LR_MODEMCONFIG1) & 0xf0) >> 4)) {
+        case 0: printf("7.8KHz "); break;
+        case 1: printf("10.4KHz "); break;
+        case 2: printf("15.6KHz "); break;
+        case 3: printf("20.8KHz "); break;
+        case 4: printf("31.25KHz "); break;
+        case 5: printf("41.7KHz "); break;
+        case 6: printf("62.5KHz "); break;
+        case 7: printf("125KHz "); break;
+        case 8: printf("250KHz "); break;
+        case 9: printf("500KHz "); break;
+        default: printf("%x ", Radio.Read(REG_LR_MODEMCONFIG1)); break;
+    }
+    //printf("\r\n");     
+}
+
+void print_cr()
+{
+    int cr = (Radio.Read(REG_LR_MODEMCONFIG1) & 0x0f)>>1;
+    printf("coding rate: ");
+    switch (cr)
+    {
+        case 1:
+            printf("4/5");
+            break;
+        case 2:
+            printf("4/6");
+            break;
+        case 3:
+            printf("4/7");
+            break;     
+        case 4:
+            printf("4/8");
+            break;                   
+        default:
+            printf("unknown");
+            break;
+    
+    }
+    printf(" ");
+}
+
+void print_power()
+{
+    uint8_t paConfig = 0;
+    uint8_t paDac = 0;
+     
+    paConfig = Radio.Read( REG_PACONFIG );
+    paDac = Radio.Read( REG_PADAC );
+
+    paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK ) | Radio.GetPaSelect( Radio.GetChannel()*1000000);
+    paConfig = ( paConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
+    printf("channel = %f getpa = %x, paConfig =%x \r\n",Radio.GetChannel(), Radio.GetPaSelect((uint32_t)Radio.GetChannel()*1000000), paConfig );
+    if( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
+    {
+        if( ( paDac & RF_PADAC_20DBM_ON ) == RF_PADAC_20DBM_ON )
+        {
+            printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f  ) + 5 );
+        }
+        else
+            printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) &0x0f) + 2 );
+    }
+    else
+    {
+        printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f ) - 1 );
+    }
+  
+}
+
+void print_errata()
+{
+    
+        int seqconfig1 = (Radio.Read(REG_SEQCONFIG1));
+        int timer2 = (Radio.Read(REG_TIMER2COEF));
+        printf("seqconfig1 = 0x%02x\r\n", seqconfig1);
+        printf("timer2coef = 0x%02x\r\n", timer2);
+
+}
+void print_status()
+{
+
+    printf("Radio version: 0x%02x Channel: %.1f MHz ", \
+            Radio.Read(REG_VERSION),\
+            Radio.GetChannel()\
+            );
+    print_power();        
+    print_bandwidth(); printf("\r\n");
+    printf("Spreading Factor: %d ",SpreadingFactor);
+    print_cr();
+    if (isMaster)
+        printf("Mode: master ");
+    else
+        printf("Mode: slave ");
+        
+ 
+     printf("\r\n");
+     print_errata();
+     printf("\r\n");
+ 
+}
+
+
+void OnTxDone( void )
+{
+    Radio.Sleep( );
+    State = TX;
+    pkt_count++;
+
+   
+    //debug_if( DEBUG_MESSAGE, "> OnTxDone\r\n" );
+}
+
+void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
+{
+    Radio.Sleep( );
+    if (pkt_count >= max_pkts)
+    {
+        pkt_count=0;
+    } 
+    
+    // mark the rolling avg that a pkt has been received
+    pkt_data[pkt_count]=1;
+    //printf("rx done, pkt_count = %d\r\n", pkt_count);
+ 
+    BufferSize = size;
+    int i;
+    for (i=0; i< BUFFER_SIZE; i++)
+        BufferRx[ i ] = '\0';  // clear the buffer
+ 
+    memcpy( BufferRx, payload, BufferSize );
+    if (rssi > 100)
+    {
+     rssi *= -1;
+    }
+    RssiValue = rssi;
+    SnrValue = snr;
+    State = RX;
+    rxTimeout = false;
+    if (app == APP_PING)  
+        debug_if( DEBUG_MESSAGE, "> OnRxDone size=%d rssi=%d snr=%d  \r\n", size, rssi, snr );
+  
+}
+
+void find_distance()
+{
+    //float dist = 0;
+    float lat2 = 0;
+    float lon2 = 0;
+    float lat1 = 0;
+    float lon1 = 0;
+    float dLat = 0;
+    float dLon = 0;
+    float c = 0;
+    float a = 0;
+    float d = 0;
+      
+    distance = 0;
+    if (gpsd.lat_deg == 0.0)
+    {
+        distance = -1;
+        return;
+    }
+    if (gpsd.lon_deg == 0.0)
+    {
+        distance = -1;
+        return;
+    }
+    
+    lat1 = r_latitude;
+    lon1 = r_longitude;
+        
+    lat2 = gpsd.lat_deg;
+    lon2 = gpsd.lon_deg;
+    //Calculate Distance
+    dLat = (lat2-lat1)*(PI/180);
+    dLon = (lon2-lon1)*(PI/180);
+    a = sin(dLat/2) * sin(dLat/2) +
+    sin(dLon/2) * sin(dLon/2) * cos(lat1*(PI/180)) * cos(lat2*(PI/180));
+    c = 2 * atan2(sqrt(a), sqrt(1-a));
+    d = 6371000 * c;
+ 
+    //GPS is only precise to 5 meters, so throw out bad data
+    if ( d <= 5) {
+       d = 0;
+    }
+    distance = distance + d;
+   // printf("distance: %f\r\n", gpsd.distance);
+   
+}
+ 
+int check_gps(void)
+{
+  
+        char c;
+        int debug=0;
+        c = gpsd.read();   //queries the GPS
+        if (debug)
+        {
+            if (c) { 
+                printf("%c", c);  //this line will echo the GPS data if not paused
+                return 1;
+                
+            }
+        }
+        
+        //check if we recieved a new message from GPS, if so, attempt to parse it,
+        if ( gpsd.newNMEAreceived() ) {
+              if (r_latitude != 0)
+              {
+                    find_distance();
+                    //printf("distance is %f\r\n",distance);
+                    if (distance < 50000 )
+                    {
+                        if (distance >= 0)
+                        {
+                            cLCD.setCursor(0,1);
+                            cLCD.printf("d:%0.1fm per:%d      ", distance,per);
+                        }
+                    }
+               }
+   
+            if ( !gpsd.parse(gpsd.lastNMEA()) ) 
+            {
+               // radfta led = !led;   
+               #if 0
+               if (gpsd.lat_deg != 0)
+               {
+                    cLCD.setCursor(0,1);
+                    cLCD.printf("d:Nx per:%d      ", per);
+                    cLCD.printf("%0.4f %0.4f", gpsd.lat_deg, gpsd.lon_deg);
+               }
+               #endif
+
+
+              
+               return 0;
+            }
+            else
+            {
+             // printf("GPS  %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds);
+               return 1;
+            }
+               
+        } 
+        else
+        {     
+           //printf("GPS  %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds);
+           return 0;   
+        }
+   
+}
+