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:
31:2c813f321db7
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 31:2c813f321db7 5 // radfta #include "vt100.h"
ftagius 29:0ea07cc7124b 6 #include "serial_api.h"
ftagius 29:0ea07cc7124b 7
ftagius 29:0ea07cc7124b 8 void start_ping_pong(void)
ftagius 29:0ea07cc7124b 9 {
ftagius 29:0ea07cc7124b 10 //char pcbuf[64];
ftagius 29:0ea07cc7124b 11 int len = get_kbd_str(pcbuf, PCBUF_SIZE);
ftagius 29:0ea07cc7124b 12 if (len < 0) {
ftagius 29:0ea07cc7124b 13 //xctrl.cls();
ftagius 29:0ea07cc7124b 14 fflush(stdout);
ftagius 29:0ea07cc7124b 15 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 16 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 17 Radio.Sleep( );
ftagius 29:0ea07cc7124b 18 debug("Ping pong app ended\r\n");
ftagius 29:0ea07cc7124b 19 app = APP_CONSOLE;
ftagius 29:0ea07cc7124b 20 // red = 1; green = 1; blue = 1;
ftagius 29:0ea07cc7124b 21 print_help();
ftagius 29:0ea07cc7124b 22 printf("> ");
ftagius 29:0ea07cc7124b 23 fflush(stdout);
ftagius 29:0ea07cc7124b 24 return;
ftagius 29:0ea07cc7124b 25 }
ftagius 29:0ea07cc7124b 26 else
ftagius 29:0ea07cc7124b 27 {
ftagius 29:0ea07cc7124b 28 ping_pong();
ftagius 29:0ea07cc7124b 29 }
ftagius 29:0ea07cc7124b 30 }
ftagius 29:0ea07cc7124b 31
ftagius 29:0ea07cc7124b 32 void ping_pong(void)
ftagius 29:0ea07cc7124b 33 {
ftagius 29:0ea07cc7124b 34 static uint8_t i;
ftagius 29:0ea07cc7124b 35 // static bool isMaster = true;
ftagius 31:2c813f321db7 36 // radfta vt100 ctrl;
ftagius 29:0ea07cc7124b 37 static int xmit_count = 0;
ftagius 29:0ea07cc7124b 38
ftagius 29:0ea07cc7124b 39 //ctrl.cls();
ftagius 29:0ea07cc7124b 40
ftagius 29:0ea07cc7124b 41 strcpy((char *)PingMsg,"PING");
ftagius 29:0ea07cc7124b 42 strcpy((char *)PongMsg,"PONG");
ftagius 29:0ea07cc7124b 43
ftagius 29:0ea07cc7124b 44
ftagius 29:0ea07cc7124b 45 switch( State )
ftagius 29:0ea07cc7124b 46 {
ftagius 29:0ea07cc7124b 47 case RX:
ftagius 29:0ea07cc7124b 48 //ctrl.cls();
ftagius 29:0ea07cc7124b 49 led = !led;
ftagius 29:0ea07cc7124b 50 if( isMaster == true )
ftagius 29:0ea07cc7124b 51 {
ftagius 29:0ea07cc7124b 52 //debug("master is true \r\n");
ftagius 29:0ea07cc7124b 53 if( BufferSize > 0 )
ftagius 29:0ea07cc7124b 54 {
ftagius 29:0ea07cc7124b 55 if( strncmp( ( const char* )BufferRx, ( const char* )PongMsg, 4 ) == 0 )
ftagius 29:0ea07cc7124b 56 {
ftagius 29:0ea07cc7124b 57 //debug( "...Pong\r\n" );
ftagius 29:0ea07cc7124b 58 debug( "master mode, ...received %s, ", BufferRx );
ftagius 29:0ea07cc7124b 59 // Send the next PING frame
ftagius 29:0ea07cc7124b 60 ++xmit_count;
ftagius 29:0ea07cc7124b 61 strcpy((char *)PingMsg, "PING_");
ftagius 29:0ea07cc7124b 62 strcat((char *)PingMsg, itoa(xmit_count,10));
ftagius 29:0ea07cc7124b 63 strcpy( ( char* )BufferTx, ( char* )PingMsg );
ftagius 29:0ea07cc7124b 64 debug( "send %s\r\n", BufferTx );
ftagius 29:0ea07cc7124b 65 // We fill the buffer with numbers for the payload
ftagius 29:0ea07cc7124b 66 int msgLen=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 67 for( i = msgLen; i < BufferSize; i++ )
ftagius 29:0ea07cc7124b 68 {
ftagius 29:0ea07cc7124b 69 BufferTx[i] = i - msgLen;
ftagius 29:0ea07cc7124b 70 }
ftagius 29:0ea07cc7124b 71 // wait_ms( 10 );
ftagius 29:0ea07cc7124b 72 wait_ms(100);
ftagius 29:0ea07cc7124b 73 // ctrl.cls();
ftagius 29:0ea07cc7124b 74 Radio.Send( BufferTx, BufferSize );
ftagius 29:0ea07cc7124b 75 }
ftagius 29:0ea07cc7124b 76 else if( strncmp( ( const char* )BufferRx, ( const char* )PingMsg, 4 ) == 0 )
ftagius 29:0ea07cc7124b 77 { // A master already exists then become a slave
ftagius 29:0ea07cc7124b 78
ftagius 29:0ea07cc7124b 79 if (AlwaysMaster)
ftagius 29:0ea07cc7124b 80 return;
ftagius 29:0ea07cc7124b 81 isMaster = false;
ftagius 29:0ea07cc7124b 82 // Send the next PONG frame
ftagius 29:0ea07cc7124b 83 ++xmit_count;
ftagius 29:0ea07cc7124b 84 debug( "master mode, become a slave, ...received %s, ", BufferRx );
ftagius 29:0ea07cc7124b 85 strcpy((char *)PongMsg, "PONG_");
ftagius 29:0ea07cc7124b 86 strcat((char *)PongMsg, itoa(xmit_count,10));
ftagius 29:0ea07cc7124b 87 strcpy( ( char* )BufferTx, ( char* )PongMsg );
ftagius 29:0ea07cc7124b 88 debug( "send %s\r\n", BufferTx );
ftagius 29:0ea07cc7124b 89 // We fill the buffer with numbers for the payload
ftagius 29:0ea07cc7124b 90 int msgLen=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 91 for( i = msgLen; i < BufferSize; i++ )
ftagius 29:0ea07cc7124b 92 {
ftagius 29:0ea07cc7124b 93 BufferTx[i] = i - msgLen;
ftagius 29:0ea07cc7124b 94 }
ftagius 29:0ea07cc7124b 95 wait_ms( 10 );
ftagius 29:0ea07cc7124b 96 Radio.Send( BufferTx, BufferSize );
ftagius 29:0ea07cc7124b 97 }
ftagius 29:0ea07cc7124b 98 else // valid reception but neither a PING or a PONG message
ftagius 29:0ea07cc7124b 99 { // Set device as master an start again
ftagius 29:0ea07cc7124b 100 if (AlwaysSlave == false)
ftagius 29:0ea07cc7124b 101 isMaster = true;
ftagius 29:0ea07cc7124b 102 debug( "...received %s, but it was not a PING or a PONG\r\n", BufferRx );
ftagius 29:0ea07cc7124b 103 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 104 }
ftagius 29:0ea07cc7124b 105 }
ftagius 29:0ea07cc7124b 106 }
ftagius 29:0ea07cc7124b 107 else
ftagius 29:0ea07cc7124b 108 {
ftagius 29:0ea07cc7124b 109 //debug("master is false \r\n");
ftagius 29:0ea07cc7124b 110
ftagius 29:0ea07cc7124b 111 if( BufferSize > 0 )
ftagius 29:0ea07cc7124b 112 {
ftagius 29:0ea07cc7124b 113 if( strncmp( ( const char* )BufferRx, ( const char* )PingMsg, 4 ) == 0 )
ftagius 29:0ea07cc7124b 114 {
ftagius 29:0ea07cc7124b 115 //red = !red;
ftagius 29:0ea07cc7124b 116 // red = !red; green = 1; blue = 1;
ftagius 29:0ea07cc7124b 117 // ctrl.cls();
ftagius 29:0ea07cc7124b 118 debug( "slave mode, ...received %s, ", BufferRx );
ftagius 29:0ea07cc7124b 119
ftagius 29:0ea07cc7124b 120 // Send the reply to the PING string
ftagius 29:0ea07cc7124b 121 ++xmit_count;
ftagius 29:0ea07cc7124b 122 strcpy((char *)PongMsg, "PONG_");
ftagius 29:0ea07cc7124b 123 strcat((char *)PongMsg, itoa(xmit_count,10));
ftagius 29:0ea07cc7124b 124 strcpy( ( char* )BufferTx, ( char* )PongMsg );
ftagius 29:0ea07cc7124b 125 debug( "send %s \r\n", BufferTx );
ftagius 29:0ea07cc7124b 126 // We fill the buffer with numbers for the payload
ftagius 29:0ea07cc7124b 127 int msgLen=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 128 for( i = msgLen; i < BufferSize; i++ )
ftagius 29:0ea07cc7124b 129 {
ftagius 29:0ea07cc7124b 130 BufferTx[i] = i - msgLen;
ftagius 29:0ea07cc7124b 131 }
ftagius 29:0ea07cc7124b 132 wait_ms( 10 );
ftagius 29:0ea07cc7124b 133 Radio.Send( BufferTx, BufferSize );
ftagius 29:0ea07cc7124b 134 }
ftagius 29:0ea07cc7124b 135 else // valid reception but not a PING as expected
ftagius 29:0ea07cc7124b 136 { // Set device as master and start again
ftagius 29:0ea07cc7124b 137 debug( "...received %s, but was expecting %s\r\n", BufferRx, (const char *)PingMsg );
ftagius 29:0ea07cc7124b 138 if (AlwaysSlave == false)
ftagius 29:0ea07cc7124b 139 isMaster = true;
ftagius 29:0ea07cc7124b 140 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 141 }
ftagius 29:0ea07cc7124b 142 }
ftagius 29:0ea07cc7124b 143 }
ftagius 29:0ea07cc7124b 144 State = LOWPOWER;
ftagius 29:0ea07cc7124b 145 break;
ftagius 29:0ea07cc7124b 146 case TX:
ftagius 29:0ea07cc7124b 147 // red = !red;
ftagius 29:0ea07cc7124b 148 // red = !red; green = 1; blue = 1;
ftagius 29:0ea07cc7124b 149 int i;
ftagius 29:0ea07cc7124b 150
ftagius 29:0ea07cc7124b 151 if( isMaster == true )
ftagius 29:0ea07cc7124b 152 {
ftagius 29:0ea07cc7124b 153 //x ctrl.cls();
ftagius 29:0ea07cc7124b 154 //x ctrl.putStr( 1,1,"Ping...\r\n" );
ftagius 29:0ea07cc7124b 155 printf("Ping..\r\n");
ftagius 29:0ea07cc7124b 156 }
ftagius 29:0ea07cc7124b 157 else
ftagius 29:0ea07cc7124b 158 {
ftagius 29:0ea07cc7124b 159
ftagius 29:0ea07cc7124b 160 //x ctrl.cls();
ftagius 29:0ea07cc7124b 161 // ctrl.putStr( 1,1,"Pong...\r\n" );
ftagius 29:0ea07cc7124b 162 printf("Pong..\r\n");
ftagius 29:0ea07cc7124b 163 }
ftagius 29:0ea07cc7124b 164 // Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 165 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 166 State = LOWPOWER;
ftagius 29:0ea07cc7124b 167 break;
ftagius 29:0ea07cc7124b 168 case RX_TIMEOUT:
ftagius 29:0ea07cc7124b 169 if( isMaster == true )
ftagius 29:0ea07cc7124b 170 {
ftagius 29:0ea07cc7124b 171 // Send the next PING frame
ftagius 29:0ea07cc7124b 172 ++xmit_count;
ftagius 29:0ea07cc7124b 173 strcpy((char *)PingMsg, "PING_");
ftagius 29:0ea07cc7124b 174 strcat((char *)PingMsg, itoa(xmit_count,10));
ftagius 29:0ea07cc7124b 175 strcpy( ( char* )BufferTx, ( char* )PingMsg );
ftagius 29:0ea07cc7124b 176 debug( "master mode, rx timeout, send %s\r\n", BufferTx );
ftagius 29:0ea07cc7124b 177 int msgLen=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 178 #if 0
ftagius 29:0ea07cc7124b 179 for( i = msgLen; i < BufferSize; i++ )
ftagius 29:0ea07cc7124b 180 {
ftagius 29:0ea07cc7124b 181 BufferTx[i] = i - msgLen;
ftagius 29:0ea07cc7124b 182 }
ftagius 29:0ea07cc7124b 183 #else
ftagius 29:0ea07cc7124b 184 BufferSize = msgLen;
ftagius 29:0ea07cc7124b 185 #endif
ftagius 29:0ea07cc7124b 186
ftagius 29:0ea07cc7124b 187 /* initialize random seed: */
ftagius 29:0ea07cc7124b 188 srand ( randomSeed() );
ftagius 29:0ea07cc7124b 189 // generate a random wait time
ftagius 29:0ea07cc7124b 190 int mywait = (5*(rand()%10 +1));
ftagius 29:0ea07cc7124b 191 debug("wait %d ms before sending\r\n", mywait);
ftagius 29:0ea07cc7124b 192 wait_ms(mywait);
ftagius 29:0ea07cc7124b 193 Radio.Send( BufferTx, BufferSize );
ftagius 29:0ea07cc7124b 194 // use the random wait time to occasionaly switch modes.
ftagius 29:0ea07cc7124b 195 // this helps break out of endless cycles when both sides are
ftagius 29:0ea07cc7124b 196 // in master mode
ftagius 29:0ea07cc7124b 197 if ((mywait % 10) == 0)
ftagius 29:0ea07cc7124b 198 {
ftagius 29:0ea07cc7124b 199 if (AlwaysMaster == false)
ftagius 29:0ea07cc7124b 200 isMaster = false;
ftagius 29:0ea07cc7124b 201 }
ftagius 29:0ea07cc7124b 202
ftagius 29:0ea07cc7124b 203 }
ftagius 29:0ea07cc7124b 204 else
ftagius 29:0ea07cc7124b 205 { /* initialize random seed: */
ftagius 29:0ea07cc7124b 206 debug("rx timeout in slave mode\r\n");
ftagius 29:0ea07cc7124b 207 srand ( randomSeed() );
ftagius 29:0ea07cc7124b 208 // generate a random wait time
ftagius 29:0ea07cc7124b 209 int mywait = (5*(rand()%10 +1));
ftagius 29:0ea07cc7124b 210 if ((mywait % 10) == 0)
ftagius 29:0ea07cc7124b 211 {
ftagius 29:0ea07cc7124b 212 if (AlwaysSlave == false)
ftagius 29:0ea07cc7124b 213 {
ftagius 29:0ea07cc7124b 214 isMaster = true;
ftagius 29:0ea07cc7124b 215 debug("switch to master mode\r\n");
ftagius 29:0ea07cc7124b 216 }
ftagius 29:0ea07cc7124b 217 }
ftagius 29:0ea07cc7124b 218 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 219 }
ftagius 29:0ea07cc7124b 220 State = LOWPOWER;
ftagius 29:0ea07cc7124b 221 break;
ftagius 29:0ea07cc7124b 222 case RX_ERROR:
ftagius 29:0ea07cc7124b 223 // We have received a Packet with a CRC error, send reply as if packet was correct
ftagius 29:0ea07cc7124b 224 if( isMaster == true )
ftagius 29:0ea07cc7124b 225 {
ftagius 29:0ea07cc7124b 226 // Send the next PING frame
ftagius 29:0ea07cc7124b 227 ++xmit_count;
ftagius 29:0ea07cc7124b 228 strcpy((char *)PingMsg, "PING_");
ftagius 29:0ea07cc7124b 229 strcat((char *)PingMsg, itoa(xmit_count,10));
ftagius 29:0ea07cc7124b 230 strcpy( ( char* )BufferTx, ( char* )PingMsg );
ftagius 29:0ea07cc7124b 231 debug( "master mode, rx error, send %s\r\n", BufferTx );
ftagius 29:0ea07cc7124b 232 int msgLen=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 233 for( i = msgLen; i < BufferSize; i++ )
ftagius 29:0ea07cc7124b 234 {
ftagius 29:0ea07cc7124b 235 BufferTx[i] = i - msgLen;
ftagius 29:0ea07cc7124b 236 }
ftagius 29:0ea07cc7124b 237 wait_ms( 10 );
ftagius 29:0ea07cc7124b 238 Radio.Send( BufferTx, BufferSize );
ftagius 29:0ea07cc7124b 239 }
ftagius 29:0ea07cc7124b 240 else
ftagius 29:0ea07cc7124b 241 {
ftagius 29:0ea07cc7124b 242 // Send the next PONG frame
ftagius 29:0ea07cc7124b 243 ++xmit_count;
ftagius 29:0ea07cc7124b 244 strcpy((char *)PongMsg, "PONG_");
ftagius 29:0ea07cc7124b 245 strcat((char *)PongMsg, itoa(xmit_count,10));
ftagius 29:0ea07cc7124b 246 strcpy( ( char* )BufferTx, ( char* )PongMsg );
ftagius 29:0ea07cc7124b 247 debug( "slave mode, rx error, send %s\r\n", BufferTx );
ftagius 29:0ea07cc7124b 248 int msgLen=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 249 for( i = msgLen; i < BufferSize; i++ )
ftagius 29:0ea07cc7124b 250 {
ftagius 29:0ea07cc7124b 251 BufferTx[i] = i - msgLen;
ftagius 29:0ea07cc7124b 252 }
ftagius 29:0ea07cc7124b 253 //wait_ms( 10 );
ftagius 29:0ea07cc7124b 254 wait_ms(100);
ftagius 29:0ea07cc7124b 255 // ctrl.cls();
ftagius 29:0ea07cc7124b 256 Radio.Send( BufferTx, BufferSize );
ftagius 29:0ea07cc7124b 257 }
ftagius 29:0ea07cc7124b 258 State = LOWPOWER;
ftagius 29:0ea07cc7124b 259 break;
ftagius 29:0ea07cc7124b 260 case TX_TIMEOUT:
ftagius 29:0ea07cc7124b 261 debug("tx timeout\r\n");
ftagius 29:0ea07cc7124b 262 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 263 State = LOWPOWER;
ftagius 29:0ea07cc7124b 264 break;
ftagius 29:0ea07cc7124b 265 case LOWPOWER:
ftagius 29:0ea07cc7124b 266 static int j=0;
ftagius 29:0ea07cc7124b 267 //if ((j%100000) == 0)
ftagius 29:0ea07cc7124b 268 if ((j%10000) == 0)
ftagius 29:0ea07cc7124b 269 {
ftagius 29:0ea07cc7124b 270 debug("low power state %d\r\n", j);
ftagius 29:0ea07cc7124b 271 j=1;
ftagius 29:0ea07cc7124b 272 }
ftagius 29:0ea07cc7124b 273 else
ftagius 29:0ea07cc7124b 274 j++;
ftagius 29:0ea07cc7124b 275 // wait_ms( 250 );
ftagius 29:0ea07cc7124b 276 wait_ms (5);
ftagius 29:0ea07cc7124b 277 break;
ftagius 29:0ea07cc7124b 278 default:
ftagius 29:0ea07cc7124b 279 debug("state is set to low power\r\n");
ftagius 29:0ea07cc7124b 280 State = LOWPOWER;
ftagius 29:0ea07cc7124b 281 break;
ftagius 29:0ea07cc7124b 282 }
ftagius 29:0ea07cc7124b 283 }