standalone sx1276 demo program

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

Committer:
ftagius
Date:
Thu Sep 03 14:43:30 2015 +0000
Revision:
34:75e4c3600d2f
Parent:
33:319cbac3b6eb
fix lib dependencies

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ftagius 29:0ea07cc7124b 1 #include "mbed.h"
ftagius 29:0ea07cc7124b 2 #include "sx1276-hal.h"
ftagius 29:0ea07cc7124b 3 #include "main.h"
ftagius 29:0ea07cc7124b 4 #include "debug.h"
ftagius 29:0ea07cc7124b 5 #include "serial_api.h"
ftagius 33:319cbac3b6eb 6 #ifndef STANDALONE
ftagius 29:0ea07cc7124b 7 #include "GPS.h"
ftagius 33:319cbac3b6eb 8 #endif
ftagius 29:0ea07cc7124b 9 #include <math.h>
ftagius 32:a2472bbe7c92 10
ftagius 29:0ea07cc7124b 11
ftagius 29:0ea07cc7124b 12 void start_hello(void)
ftagius 29:0ea07cc7124b 13 {
ftagius 29:0ea07cc7124b 14 int len = get_kbd_str(pcbuf, PCBUF_SIZE);
ftagius 33:319cbac3b6eb 15 //printf("in start gps/hello len=%d\r\n",len);
ftagius 29:0ea07cc7124b 16 if (len < 0) {
ftagius 29:0ea07cc7124b 17 fflush(stdout);
ftagius 29:0ea07cc7124b 18 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 19 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 20 Radio.Sleep( );
ftagius 29:0ea07cc7124b 21 if (gpsEnabled)
ftagius 29:0ea07cc7124b 22 debug("GPS exchange ended\r\n");
ftagius 29:0ea07cc7124b 23 else
ftagius 29:0ea07cc7124b 24 debug("Hello exchange ended\r\n");
ftagius 29:0ea07cc7124b 25 app = APP_CONSOLE;
ftagius 29:0ea07cc7124b 26 print_help();
ftagius 29:0ea07cc7124b 27 printf("> ");
ftagius 29:0ea07cc7124b 28 fflush(stdout);
ftagius 29:0ea07cc7124b 29 return;
ftagius 29:0ea07cc7124b 30 }
ftagius 29:0ea07cc7124b 31 else
ftagius 29:0ea07cc7124b 32 {
ftagius 33:319cbac3b6eb 33 Radio.Rx( RX_TIMEOUT_VALUE / 3 );
ftagius 32:a2472bbe7c92 34 hello();
ftagius 29:0ea07cc7124b 35 }
ftagius 29:0ea07cc7124b 36 }
ftagius 29:0ea07cc7124b 37
ftagius 29:0ea07cc7124b 38 void hello(void)
ftagius 29:0ea07cc7124b 39 {
ftagius 32:a2472bbe7c92 40
ftagius 32:a2472bbe7c92 41 // srand ( randomSeed() );
ftagius 29:0ea07cc7124b 42 // generate a random wait time
ftagius 33:319cbac3b6eb 43 int mywait = ((rand()%2 +1));
ftagius 33:319cbac3b6eb 44 // int mywait = 3;
ftagius 29:0ea07cc7124b 45 int i=0;
ftagius 29:0ea07cc7124b 46 int rcvd=0;
ftagius 29:0ea07cc7124b 47 int lost=0;
ftagius 32:a2472bbe7c92 48
ftagius 32:a2472bbe7c92 49 // check_gps();
ftagius 29:0ea07cc7124b 50 for (i=0;i<max_pkts;i++)
ftagius 29:0ea07cc7124b 51 {
ftagius 29:0ea07cc7124b 52
ftagius 29:0ea07cc7124b 53 if (pkt_data[i]==1)
ftagius 29:0ea07cc7124b 54 {
ftagius 29:0ea07cc7124b 55 //printf("rcvd, pkt count=%d\r\n",i);
ftagius 29:0ea07cc7124b 56 rcvd++;
ftagius 29:0ea07cc7124b 57 }
ftagius 29:0ea07cc7124b 58 else if (pkt_data[i] == 2)
ftagius 29:0ea07cc7124b 59 {
ftagius 29:0ea07cc7124b 60 //printf("lose, pkt count=%d\r\n",i);
ftagius 29:0ea07cc7124b 61 lost++;
ftagius 29:0ea07cc7124b 62 }
ftagius 29:0ea07cc7124b 63
ftagius 29:0ea07cc7124b 64 }
ftagius 29:0ea07cc7124b 65 if ((rcvd + lost) > 0)
ftagius 29:0ea07cc7124b 66 {
ftagius 29:0ea07cc7124b 67 per = 100 - (rcvd*100 / (rcvd + lost));
ftagius 29:0ea07cc7124b 68 //printf("rcvd = %d, lost = %d per = %d\r\n",rcvd, lost, per);
ftagius 29:0ea07cc7124b 69 }
ftagius 29:0ea07cc7124b 70 else
ftagius 29:0ea07cc7124b 71 per = 0;
ftagius 29:0ea07cc7124b 72
ftagius 29:0ea07cc7124b 73
ftagius 29:0ea07cc7124b 74 switch( State )
ftagius 29:0ea07cc7124b 75 {
ftagius 29:0ea07cc7124b 76
ftagius 29:0ea07cc7124b 77 case RX_TIMEOUT:
ftagius 29:0ea07cc7124b 78
ftagius 29:0ea07cc7124b 79 printf("rx timeout in gps/hello loop. ");
ftagius 29:0ea07cc7124b 80 printf("per=%d\r\n", per);
ftagius 32:a2472bbe7c92 81 r_latitude = 0;
ftagius 32:a2472bbe7c92 82 r_longitude = 0;
ftagius 29:0ea07cc7124b 83 // printf("rcv - rssi = %d snr = %d msg=%s\r\n", RssiValue, SnrValue, BufferRx);
ftagius 33:319cbac3b6eb 84 #ifndef STANDALONE
ftagius 29:0ea07cc7124b 85 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 86 cLCD.printf("RI:NA SR:NA ");
ftagius 33:319cbac3b6eb 87 #endif
ftagius 29:0ea07cc7124b 88 case RX:
ftagius 29:0ea07cc7124b 89
ftagius 29:0ea07cc7124b 90 if( BufferSize > 0 )
ftagius 29:0ea07cc7124b 91 {
ftagius 33:319cbac3b6eb 92 RXin = !RXin;
ftagius 29:0ea07cc7124b 93 if( strncmp( ( const char* )BufferRx, ( const char* )HelloMsg, 5 ) == 0 )
ftagius 29:0ea07cc7124b 94 {
ftagius 32:a2472bbe7c92 95 // radfta led = !led;
ftagius 29:0ea07cc7124b 96 printf("rcv - rssi = %d snr = %d msg=%s ", RssiValue, SnrValue, BufferRx );
ftagius 29:0ea07cc7124b 97 printf("per=%d\r\n", per);
ftagius 33:319cbac3b6eb 98 #ifndef STANDALONE
ftagius 29:0ea07cc7124b 99 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 100 cLCD.printf("RI:%d SR:%d ", RssiValue, SnrValue);
ftagius 33:319cbac3b6eb 101 #endif
ftagius 29:0ea07cc7124b 102 }
ftagius 33:319cbac3b6eb 103 #ifndef STANDALONE
ftagius 29:0ea07cc7124b 104 else
ftagius 29:0ea07cc7124b 105 {
ftagius 32:a2472bbe7c92 106
ftagius 32:a2472bbe7c92 107 if(sscanf((char *)BufferRx, "GP,%f,%f", &r_latitude, &r_longitude ) >= 1)
ftagius 33:319cbac3b6eb 108 {
ftagius 33:319cbac3b6eb 109 r_latitude_last = r_latitude;
ftagius 33:319cbac3b6eb 110 r_longitude_last = r_longitude;
ftagius 32:a2472bbe7c92 111 // radfta led = !led;
ftagius 32:a2472bbe7c92 112 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);
ftagius 29:0ea07cc7124b 113 printf("per=%d\r\n", per);
ftagius 29:0ea07cc7124b 114 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 115 cLCD.printf("RI:%d SR:%d ", RssiValue,SnrValue);
ftagius 29:0ea07cc7124b 116 //printf("Msg received - Lat: %0.4f Lon: %0.4f\r\n", latitude, longitude);
ftagius 29:0ea07cc7124b 117 }
ftagius 29:0ea07cc7124b 118 else
ftagius 29:0ea07cc7124b 119 printf("strange received message: %s\r\n", BufferRx);
ftagius 32:a2472bbe7c92 120
ftagius 33:319cbac3b6eb 121 }
ftagius 33:319cbac3b6eb 122 #else
ftagius 34:75e4c3600d2f 123 if(sscanf((char *)BufferRx, "GP,%f,%f", &r_latitude, &r_longitude ) >= 1)
ftagius 33:319cbac3b6eb 124 printf("rssi=%d \tsnr=%d \tper=%d \tmsg=%s\r\n", RssiValue, SnrValue, per, BufferRx);
ftagius 33:319cbac3b6eb 125 #endif
ftagius 29:0ea07cc7124b 126 }
ftagius 29:0ea07cc7124b 127 case RX_ERROR:
ftagius 33:319cbac3b6eb 128 #ifndef STANDALONE
ftagius 29:0ea07cc7124b 129 if (gpsEnabled)
ftagius 32:a2472bbe7c92 130 sprintf((char *)BufferTx, "GP,%4.5f,%4.5f", gpsd.lat_deg, gpsd.lon_deg);
ftagius 29:0ea07cc7124b 131 else
ftagius 33:319cbac3b6eb 132 #endif
ftagius 29:0ea07cc7124b 133 strcpy( ( char* )BufferTx, ( char* )HelloMsg );
ftagius 33:319cbac3b6eb 134 #if 1
ftagius 29:0ea07cc7124b 135 BufferSize=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 136 //printf("Sending new buffer\r\n");
ftagius 29:0ea07cc7124b 137 Radio.Send( BufferTx, BufferSize );
ftagius 33:319cbac3b6eb 138 State = LOWPOWER;
ftagius 33:319cbac3b6eb 139 #else
ftagius 33:319cbac3b6eb 140 State = CAD;
ftagius 32:a2472bbe7c92 141 #endif
ftagius 29:0ea07cc7124b 142
ftagius 29:0ea07cc7124b 143 break;
ftagius 29:0ea07cc7124b 144 case TX:
ftagius 33:319cbac3b6eb 145 // printf("tx state\r\n");
ftagius 33:319cbac3b6eb 146 // wait_ms (10);
ftagius 29:0ea07cc7124b 147 Radio.Rx( RX_TIMEOUT_VALUE / mywait );
ftagius 29:0ea07cc7124b 148 State = LOWPOWER;
ftagius 29:0ea07cc7124b 149 break;
ftagius 29:0ea07cc7124b 150 case TX_TIMEOUT:
ftagius 29:0ea07cc7124b 151 debug("tx timeout\r\n");
ftagius 29:0ea07cc7124b 152 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 153 State = LOWPOWER;
ftagius 29:0ea07cc7124b 154 break;
ftagius 29:0ea07cc7124b 155 case LOWPOWER:
ftagius 32:a2472bbe7c92 156 //printf("in wait\r\n");
ftagius 33:319cbac3b6eb 157 //wait_ms (1);
ftagius 33:319cbac3b6eb 158 //wait_us(200);
ftagius 33:319cbac3b6eb 159 #ifndef STANDALONE
ftagius 33:319cbac3b6eb 160 check_gps();
ftagius 33:319cbac3b6eb 161 #endif
ftagius 33:319cbac3b6eb 162 break;
ftagius 33:319cbac3b6eb 163 case CAD:
ftagius 33:319cbac3b6eb 164 printf("in cad\r\n");
ftagius 33:319cbac3b6eb 165 //State = LOWPOWER;
ftagius 29:0ea07cc7124b 166 break;
ftagius 29:0ea07cc7124b 167 default:
ftagius 33:319cbac3b6eb 168 //debug("state is set to CAD\r\n");
ftagius 29:0ea07cc7124b 169 State = LOWPOWER;
ftagius 29:0ea07cc7124b 170 break;
ftagius 29:0ea07cc7124b 171 }
ftagius 29:0ea07cc7124b 172 }