CaryCoders
/
demo_SX1276_standalone
standalone sx1276 demo program
Fork of SX1276_GPS by
hello.cpp@32:a2472bbe7c92, 2015-07-29 (annotated)
- Committer:
- ftagius
- Date:
- Wed Jul 29 12:14:42 2015 +0000
- Revision:
- 32:a2472bbe7c92
- Parent:
- 31:2c813f321db7
- Child:
- 33:319cbac3b6eb
cleanup
Who changed what in which revision?
User | Revision | Line number | New 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 | 29:0ea07cc7124b | 6 | #include "GPS.h" |
ftagius | 29:0ea07cc7124b | 7 | #include <math.h> |
ftagius | 32:a2472bbe7c92 | 8 | |
ftagius | 29:0ea07cc7124b | 9 | |
ftagius | 29:0ea07cc7124b | 10 | void start_hello(void) |
ftagius | 29:0ea07cc7124b | 11 | { |
ftagius | 29:0ea07cc7124b | 12 | int len = get_kbd_str(pcbuf, PCBUF_SIZE); |
ftagius | 32:a2472bbe7c92 | 13 | //printf("in start gps/hello\r\n"); |
ftagius | 29:0ea07cc7124b | 14 | if (len < 0) { |
ftagius | 29:0ea07cc7124b | 15 | fflush(stdout); |
ftagius | 29:0ea07cc7124b | 16 | Radio.Rx( RX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 17 | Radio.Tx( TX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 18 | Radio.Sleep( ); |
ftagius | 29:0ea07cc7124b | 19 | if (gpsEnabled) |
ftagius | 29:0ea07cc7124b | 20 | debug("GPS exchange ended\r\n"); |
ftagius | 29:0ea07cc7124b | 21 | else |
ftagius | 29:0ea07cc7124b | 22 | debug("Hello exchange ended\r\n"); |
ftagius | 29:0ea07cc7124b | 23 | app = APP_CONSOLE; |
ftagius | 29:0ea07cc7124b | 24 | print_help(); |
ftagius | 29:0ea07cc7124b | 25 | printf("> "); |
ftagius | 29:0ea07cc7124b | 26 | fflush(stdout); |
ftagius | 29:0ea07cc7124b | 27 | return; |
ftagius | 29:0ea07cc7124b | 28 | } |
ftagius | 29:0ea07cc7124b | 29 | else |
ftagius | 29:0ea07cc7124b | 30 | { |
ftagius | 32:a2472bbe7c92 | 31 | hello(); |
ftagius | 29:0ea07cc7124b | 32 | } |
ftagius | 29:0ea07cc7124b | 33 | } |
ftagius | 29:0ea07cc7124b | 34 | |
ftagius | 29:0ea07cc7124b | 35 | void hello(void) |
ftagius | 29:0ea07cc7124b | 36 | { |
ftagius | 32:a2472bbe7c92 | 37 | |
ftagius | 32:a2472bbe7c92 | 38 | // srand ( randomSeed() ); |
ftagius | 29:0ea07cc7124b | 39 | // generate a random wait time |
ftagius | 32:a2472bbe7c92 | 40 | // int mywait = ((rand()%2 +1)); |
ftagius | 32:a2472bbe7c92 | 41 | int mywait = 3; |
ftagius | 29:0ea07cc7124b | 42 | int i=0; |
ftagius | 29:0ea07cc7124b | 43 | int rcvd=0; |
ftagius | 29:0ea07cc7124b | 44 | int lost=0; |
ftagius | 32:a2472bbe7c92 | 45 | |
ftagius | 32:a2472bbe7c92 | 46 | // check_gps(); |
ftagius | 29:0ea07cc7124b | 47 | for (i=0;i<max_pkts;i++) |
ftagius | 29:0ea07cc7124b | 48 | { |
ftagius | 29:0ea07cc7124b | 49 | |
ftagius | 29:0ea07cc7124b | 50 | if (pkt_data[i]==1) |
ftagius | 29:0ea07cc7124b | 51 | { |
ftagius | 29:0ea07cc7124b | 52 | //printf("rcvd, pkt count=%d\r\n",i); |
ftagius | 29:0ea07cc7124b | 53 | rcvd++; |
ftagius | 29:0ea07cc7124b | 54 | } |
ftagius | 29:0ea07cc7124b | 55 | else if (pkt_data[i] == 2) |
ftagius | 29:0ea07cc7124b | 56 | { |
ftagius | 29:0ea07cc7124b | 57 | //printf("lose, pkt count=%d\r\n",i); |
ftagius | 29:0ea07cc7124b | 58 | lost++; |
ftagius | 29:0ea07cc7124b | 59 | } |
ftagius | 29:0ea07cc7124b | 60 | |
ftagius | 29:0ea07cc7124b | 61 | } |
ftagius | 29:0ea07cc7124b | 62 | if ((rcvd + lost) > 0) |
ftagius | 29:0ea07cc7124b | 63 | { |
ftagius | 29:0ea07cc7124b | 64 | per = 100 - (rcvd*100 / (rcvd + lost)); |
ftagius | 29:0ea07cc7124b | 65 | //printf("rcvd = %d, lost = %d per = %d\r\n",rcvd, lost, per); |
ftagius | 29:0ea07cc7124b | 66 | } |
ftagius | 29:0ea07cc7124b | 67 | else |
ftagius | 29:0ea07cc7124b | 68 | per = 0; |
ftagius | 29:0ea07cc7124b | 69 | |
ftagius | 29:0ea07cc7124b | 70 | |
ftagius | 29:0ea07cc7124b | 71 | switch( State ) |
ftagius | 29:0ea07cc7124b | 72 | { |
ftagius | 29:0ea07cc7124b | 73 | |
ftagius | 29:0ea07cc7124b | 74 | case RX_TIMEOUT: |
ftagius | 29:0ea07cc7124b | 75 | |
ftagius | 29:0ea07cc7124b | 76 | printf("rx timeout in gps/hello loop. "); |
ftagius | 29:0ea07cc7124b | 77 | printf("per=%d\r\n", per); |
ftagius | 32:a2472bbe7c92 | 78 | r_latitude = 0; |
ftagius | 32:a2472bbe7c92 | 79 | r_longitude = 0; |
ftagius | 29:0ea07cc7124b | 80 | // printf("rcv - rssi = %d snr = %d msg=%s\r\n", RssiValue, SnrValue, BufferRx); |
ftagius | 29:0ea07cc7124b | 81 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 82 | cLCD.printf("RI:NA SR:NA "); |
ftagius | 29:0ea07cc7124b | 83 | case RX: |
ftagius | 29:0ea07cc7124b | 84 | |
ftagius | 29:0ea07cc7124b | 85 | if( BufferSize > 0 ) |
ftagius | 29:0ea07cc7124b | 86 | { |
ftagius | 29:0ea07cc7124b | 87 | if( strncmp( ( const char* )BufferRx, ( const char* )HelloMsg, 5 ) == 0 ) |
ftagius | 29:0ea07cc7124b | 88 | { |
ftagius | 32:a2472bbe7c92 | 89 | // radfta led = !led; |
ftagius | 29:0ea07cc7124b | 90 | printf("rcv - rssi = %d snr = %d msg=%s ", RssiValue, SnrValue, BufferRx ); |
ftagius | 29:0ea07cc7124b | 91 | printf("per=%d\r\n", per); |
ftagius | 29:0ea07cc7124b | 92 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 93 | cLCD.printf("RI:%d SR:%d ", RssiValue, SnrValue); |
ftagius | 29:0ea07cc7124b | 94 | } |
ftagius | 29:0ea07cc7124b | 95 | else |
ftagius | 29:0ea07cc7124b | 96 | { |
ftagius | 32:a2472bbe7c92 | 97 | |
ftagius | 32:a2472bbe7c92 | 98 | if(sscanf((char *)BufferRx, "GP,%f,%f", &r_latitude, &r_longitude ) >= 1) |
ftagius | 29:0ea07cc7124b | 99 | { |
ftagius | 32:a2472bbe7c92 | 100 | // radfta led = !led; |
ftagius | 32:a2472bbe7c92 | 101 | 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 | 102 | printf("per=%d\r\n", per); |
ftagius | 29:0ea07cc7124b | 103 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 104 | cLCD.printf("RI:%d SR:%d ", RssiValue,SnrValue); |
ftagius | 29:0ea07cc7124b | 105 | //printf("Msg received - Lat: %0.4f Lon: %0.4f\r\n", latitude, longitude); |
ftagius | 29:0ea07cc7124b | 106 | } |
ftagius | 29:0ea07cc7124b | 107 | else |
ftagius | 29:0ea07cc7124b | 108 | printf("strange received message: %s\r\n", BufferRx); |
ftagius | 32:a2472bbe7c92 | 109 | |
ftagius | 31:2c813f321db7 | 110 | } |
ftagius | 29:0ea07cc7124b | 111 | } |
ftagius | 29:0ea07cc7124b | 112 | case RX_ERROR: |
ftagius | 32:a2472bbe7c92 | 113 | |
ftagius | 29:0ea07cc7124b | 114 | if (gpsEnabled) |
ftagius | 32:a2472bbe7c92 | 115 | sprintf((char *)BufferTx, "GP,%4.5f,%4.5f", gpsd.lat_deg, gpsd.lon_deg); |
ftagius | 29:0ea07cc7124b | 116 | else |
ftagius | 29:0ea07cc7124b | 117 | strcpy( ( char* )BufferTx, ( char* )HelloMsg ); |
ftagius | 32:a2472bbe7c92 | 118 | #if 0 |
ftagius | 32:a2472bbe7c92 | 119 | radfta |
ftagius | 29:0ea07cc7124b | 120 | BufferSize=strlen((char *)BufferTx); |
ftagius | 29:0ea07cc7124b | 121 | //printf("Sending new buffer\r\n"); |
ftagius | 29:0ea07cc7124b | 122 | Radio.Send( BufferTx, BufferSize ); |
ftagius | 32:a2472bbe7c92 | 123 | #endif |
ftagius | 29:0ea07cc7124b | 124 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 125 | |
ftagius | 29:0ea07cc7124b | 126 | break; |
ftagius | 29:0ea07cc7124b | 127 | case TX: |
ftagius | 29:0ea07cc7124b | 128 | //printf("tx state\r\n"); |
ftagius | 29:0ea07cc7124b | 129 | //wait_ms (30); |
ftagius | 29:0ea07cc7124b | 130 | |
ftagius | 29:0ea07cc7124b | 131 | Radio.Rx( RX_TIMEOUT_VALUE / mywait ); |
ftagius | 29:0ea07cc7124b | 132 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 133 | break; |
ftagius | 29:0ea07cc7124b | 134 | case TX_TIMEOUT: |
ftagius | 29:0ea07cc7124b | 135 | debug("tx timeout\r\n"); |
ftagius | 29:0ea07cc7124b | 136 | Radio.Tx( TX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 137 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 138 | break; |
ftagius | 29:0ea07cc7124b | 139 | case LOWPOWER: |
ftagius | 32:a2472bbe7c92 | 140 | //printf("in wait\r\n"); |
ftagius | 32:a2472bbe7c92 | 141 | // wait_ms (1); |
ftagius | 29:0ea07cc7124b | 142 | break; |
ftagius | 29:0ea07cc7124b | 143 | default: |
ftagius | 29:0ea07cc7124b | 144 | debug("state is set to low power\r\n"); |
ftagius | 29:0ea07cc7124b | 145 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 146 | break; |
ftagius | 29:0ea07cc7124b | 147 | } |
ftagius | 29:0ea07cc7124b | 148 | } |