CaryCoders
/
demo_SX1276_standalone
standalone sx1276 demo program
Fork of SX1276_GPS by
utilities.cpp@33:319cbac3b6eb, 2015-09-03 (annotated)
- Committer:
- ftagius
- Date:
- Thu Sep 03 14:33:55 2015 +0000
- Revision:
- 33:319cbac3b6eb
- Parent:
- 32:a2472bbe7c92
sx1276 demo program requiring only the freedom board and a semtech shield
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 | |
ftagius | 33:319cbac3b6eb | 7 | #ifndef STANDALONE |
ftagius | 32:a2472bbe7c92 | 8 | void gps_setup(void) |
ftagius | 32:a2472bbe7c92 | 9 | { |
ftagius | 32:a2472bbe7c92 | 10 | printf("setting up gps\r\n"); |
ftagius | 32:a2472bbe7c92 | 11 | gpsd.setBaud(9600); |
ftagius | 32:a2472bbe7c92 | 12 | gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
ftagius | 32:a2472bbe7c92 | 13 | gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
ftagius | 32:a2472bbe7c92 | 14 | gpsd.sendCommand(PGCMD_NOANTENNA); |
ftagius | 32:a2472bbe7c92 | 15 | gpsd.day=01; |
ftagius | 32:a2472bbe7c92 | 16 | gpsd.month=01; |
ftagius | 32:a2472bbe7c92 | 17 | gpsd.year=15; |
ftagius | 32:a2472bbe7c92 | 18 | gpsd.hour=1; |
ftagius | 32:a2472bbe7c92 | 19 | gpsd.minute=1; |
ftagius | 32:a2472bbe7c92 | 20 | gpsd.seconds=1; |
ftagius | 32:a2472bbe7c92 | 21 | } |
ftagius | 33:319cbac3b6eb | 22 | #endif |
ftagius | 33:319cbac3b6eb | 23 | int get_kbd_str2(char* buf, int size) |
ftagius | 29:0ea07cc7124b | 24 | { |
ftagius | 29:0ea07cc7124b | 25 | char c; |
ftagius | 29:0ea07cc7124b | 26 | int i; |
ftagius | 29:0ea07cc7124b | 27 | static int prev_len; |
ftagius | 29:0ea07cc7124b | 28 | |
ftagius | 29:0ea07cc7124b | 29 | for (i = 0;;) { |
ftagius | 29:0ea07cc7124b | 30 | if (pc.readable()) { |
ftagius | 29:0ea07cc7124b | 31 | c = pc.getc(); |
ftagius | 29:0ea07cc7124b | 32 | if (c == 8 && i > 0) { |
ftagius | 29:0ea07cc7124b | 33 | pc.putc(8); |
ftagius | 29:0ea07cc7124b | 34 | pc.putc(' '); |
ftagius | 29:0ea07cc7124b | 35 | pc.putc(8); |
ftagius | 29:0ea07cc7124b | 36 | i--; |
ftagius | 29:0ea07cc7124b | 37 | } else if (c == '\r') { |
ftagius | 29:0ea07cc7124b | 38 | if (i == 0) { |
ftagius | 29:0ea07cc7124b | 39 | if (app == APP_CHAT) |
ftagius | 29:0ea07cc7124b | 40 | return 0; |
ftagius | 29:0ea07cc7124b | 41 | else |
ftagius | 29:0ea07cc7124b | 42 | return prev_len; // repeat previous |
ftagius | 29:0ea07cc7124b | 43 | } else { |
ftagius | 29:0ea07cc7124b | 44 | buf[i] = 0; // null terminate |
ftagius | 29:0ea07cc7124b | 45 | prev_len = i; |
ftagius | 29:0ea07cc7124b | 46 | return i; |
ftagius | 29:0ea07cc7124b | 47 | } |
ftagius | 29:0ea07cc7124b | 48 | } else if (c == 3) { |
ftagius | 29:0ea07cc7124b | 49 | // ctrl-C abort |
ftagius | 29:0ea07cc7124b | 50 | return -1; |
ftagius | 29:0ea07cc7124b | 51 | } else if (i < size) { |
ftagius | 29:0ea07cc7124b | 52 | buf[i++] = c; |
ftagius | 29:0ea07cc7124b | 53 | pc.putc(c); |
ftagius | 29:0ea07cc7124b | 54 | } |
ftagius | 33:319cbac3b6eb | 55 | } else { |
ftagius | 33:319cbac3b6eb | 56 | if (app == APP_PING) |
ftagius | 33:319cbac3b6eb | 57 | ping_pong(); |
ftagius | 33:319cbac3b6eb | 58 | if (app == APP_HELLO) |
ftagius | 33:319cbac3b6eb | 59 | hello(); |
ftagius | 33:319cbac3b6eb | 60 | #ifndef STANDALONE |
ftagius | 33:319cbac3b6eb | 61 | if (app == APP_GPS) |
ftagius | 33:319cbac3b6eb | 62 | check_gps(); |
ftagius | 33:319cbac3b6eb | 63 | #endif |
ftagius | 33:319cbac3b6eb | 64 | #if 1 |
ftagius | 33:319cbac3b6eb | 65 | if (app == APP_CHAT) |
ftagius | 33:319cbac3b6eb | 66 | { |
ftagius | 33:319cbac3b6eb | 67 | // printf("utility call to console chat\r\n"); |
ftagius | 33:319cbac3b6eb | 68 | check_rx_chat(); |
ftagius | 33:319cbac3b6eb | 69 | } |
ftagius | 33:319cbac3b6eb | 70 | #endif |
ftagius | 33:319cbac3b6eb | 71 | } |
ftagius | 33:319cbac3b6eb | 72 | } // ...for() |
ftagius | 33:319cbac3b6eb | 73 | } |
ftagius | 33:319cbac3b6eb | 74 | |
ftagius | 33:319cbac3b6eb | 75 | int get_kbd_str(char* buf, int size) |
ftagius | 33:319cbac3b6eb | 76 | { |
ftagius | 33:319cbac3b6eb | 77 | char c; |
ftagius | 33:319cbac3b6eb | 78 | int i; |
ftagius | 33:319cbac3b6eb | 79 | static int prev_len; |
ftagius | 33:319cbac3b6eb | 80 | |
ftagius | 33:319cbac3b6eb | 81 | for (i = 0;;) { |
ftagius | 33:319cbac3b6eb | 82 | if (pc.readable()) { |
ftagius | 33:319cbac3b6eb | 83 | c = pc.getc(); |
ftagius | 33:319cbac3b6eb | 84 | if (c == 8 && i > 0) { |
ftagius | 33:319cbac3b6eb | 85 | pc.putc(8); |
ftagius | 33:319cbac3b6eb | 86 | pc.putc(' '); |
ftagius | 33:319cbac3b6eb | 87 | pc.putc(8); |
ftagius | 33:319cbac3b6eb | 88 | i--; |
ftagius | 33:319cbac3b6eb | 89 | } else if (c == '\r') { |
ftagius | 33:319cbac3b6eb | 90 | if (i == 0) { |
ftagius | 33:319cbac3b6eb | 91 | if (app == APP_CHAT) |
ftagius | 33:319cbac3b6eb | 92 | return 0; |
ftagius | 33:319cbac3b6eb | 93 | else |
ftagius | 33:319cbac3b6eb | 94 | return prev_len; // repeat previous |
ftagius | 33:319cbac3b6eb | 95 | } else { |
ftagius | 33:319cbac3b6eb | 96 | buf[i] = 0; // null terminate |
ftagius | 33:319cbac3b6eb | 97 | prev_len = i; |
ftagius | 33:319cbac3b6eb | 98 | return i; |
ftagius | 33:319cbac3b6eb | 99 | } |
ftagius | 33:319cbac3b6eb | 100 | } else if (c == 3) { |
ftagius | 33:319cbac3b6eb | 101 | { |
ftagius | 33:319cbac3b6eb | 102 | // ctrl-C abort |
ftagius | 33:319cbac3b6eb | 103 | //printf("ctrl c\r\n"); |
ftagius | 33:319cbac3b6eb | 104 | app = APP_CONSOLE; |
ftagius | 33:319cbac3b6eb | 105 | return -1; |
ftagius | 33:319cbac3b6eb | 106 | } |
ftagius | 33:319cbac3b6eb | 107 | } else if (i < size) { |
ftagius | 33:319cbac3b6eb | 108 | buf[i++] = c; |
ftagius | 33:319cbac3b6eb | 109 | pc.putc(c); |
ftagius | 33:319cbac3b6eb | 110 | } |
ftagius | 32:a2472bbe7c92 | 111 | } |
ftagius | 32:a2472bbe7c92 | 112 | |
ftagius | 32:a2472bbe7c92 | 113 | else { |
ftagius | 32:a2472bbe7c92 | 114 | #if 0 |
ftagius | 33:319cbac3b6eb | 115 | return 1; |
ftagius | 32:a2472bbe7c92 | 116 | #else |
ftagius | 29:0ea07cc7124b | 117 | if (app == APP_PING) |
ftagius | 29:0ea07cc7124b | 118 | ping_pong(); |
ftagius | 29:0ea07cc7124b | 119 | if (app == APP_HELLO) |
ftagius | 32:a2472bbe7c92 | 120 | { |
ftagius | 33:319cbac3b6eb | 121 | #ifndef STANDALONE |
ftagius | 33:319cbac3b6eb | 122 | check_gps(); |
ftagius | 33:319cbac3b6eb | 123 | #endif |
ftagius | 33:319cbac3b6eb | 124 | hello(); |
ftagius | 33:319cbac3b6eb | 125 | |
ftagius | 32:a2472bbe7c92 | 126 | } |
ftagius | 33:319cbac3b6eb | 127 | #ifndef STANDALONE |
ftagius | 29:0ea07cc7124b | 128 | if (app == APP_GPS) |
ftagius | 29:0ea07cc7124b | 129 | check_gps(); |
ftagius | 33:319cbac3b6eb | 130 | #endif |
ftagius | 29:0ea07cc7124b | 131 | #if 1 |
ftagius | 29:0ea07cc7124b | 132 | if (app == APP_CHAT) |
ftagius | 29:0ea07cc7124b | 133 | { |
ftagius | 29:0ea07cc7124b | 134 | // printf("utility call to console chat\r\n"); |
ftagius | 29:0ea07cc7124b | 135 | check_rx_chat(); |
ftagius | 29:0ea07cc7124b | 136 | } |
ftagius | 29:0ea07cc7124b | 137 | #endif |
ftagius | 32:a2472bbe7c92 | 138 | #endif |
ftagius | 29:0ea07cc7124b | 139 | } |
ftagius | 32:a2472bbe7c92 | 140 | |
ftagius | 29:0ea07cc7124b | 141 | } // ...for() |
ftagius | 29:0ea07cc7124b | 142 | } |
ftagius | 32:a2472bbe7c92 | 143 | void OnTxTimeout( void ) |
ftagius | 32:a2472bbe7c92 | 144 | { |
ftagius | 32:a2472bbe7c92 | 145 | Radio.Sleep( ); |
ftagius | 32:a2472bbe7c92 | 146 | State = TX_TIMEOUT; |
ftagius | 32:a2472bbe7c92 | 147 | //debug_if( DEBUG_MESSAGE, "> OnTxTimeout\r\n" ); |
ftagius | 32:a2472bbe7c92 | 148 | } |
ftagius | 32:a2472bbe7c92 | 149 | |
ftagius | 32:a2472bbe7c92 | 150 | void OnRxTimeout( void ) |
ftagius | 32:a2472bbe7c92 | 151 | { |
ftagius | 32:a2472bbe7c92 | 152 | Radio.Sleep( ); |
ftagius | 32:a2472bbe7c92 | 153 | int i; |
ftagius | 32:a2472bbe7c92 | 154 | if (pkt_count >= max_pkts) |
ftagius | 32:a2472bbe7c92 | 155 | { |
ftagius | 32:a2472bbe7c92 | 156 | pkt_count=0; |
ftagius | 32:a2472bbe7c92 | 157 | } |
ftagius | 32:a2472bbe7c92 | 158 | |
ftagius | 32:a2472bbe7c92 | 159 | // mark the rolling avg that a pkt has not been received |
ftagius | 32:a2472bbe7c92 | 160 | pkt_data[pkt_count]=2; |
ftagius | 32:a2472bbe7c92 | 161 | |
ftagius | 32:a2472bbe7c92 | 162 | for (i=0; i< BUFFER_SIZE; i++) |
ftagius | 32:a2472bbe7c92 | 163 | BufferRx[ i ] = '\0'; // clear the buffer |
ftagius | 32:a2472bbe7c92 | 164 | BufferSize = 0; |
ftagius | 32:a2472bbe7c92 | 165 | State = RX_TIMEOUT; |
ftagius | 32:a2472bbe7c92 | 166 | rxTimeout = false; |
ftagius | 32:a2472bbe7c92 | 167 | // debug_if( DEBUG_MESSAGE, "> OnRxTimeout\r\n" ); |
ftagius | 32:a2472bbe7c92 | 168 | } |
ftagius | 32:a2472bbe7c92 | 169 | |
ftagius | 32:a2472bbe7c92 | 170 | void OnRxError( void ) |
ftagius | 32:a2472bbe7c92 | 171 | { |
ftagius | 32:a2472bbe7c92 | 172 | Radio.Sleep( ); |
ftagius | 32:a2472bbe7c92 | 173 | if (pkt_count >= max_pkts) |
ftagius | 32:a2472bbe7c92 | 174 | { |
ftagius | 32:a2472bbe7c92 | 175 | pkt_count=0; |
ftagius | 32:a2472bbe7c92 | 176 | } |
ftagius | 32:a2472bbe7c92 | 177 | |
ftagius | 32:a2472bbe7c92 | 178 | // mark the rolling avg that a pkt has not been received |
ftagius | 32:a2472bbe7c92 | 179 | pkt_data[pkt_count]=2; |
ftagius | 32:a2472bbe7c92 | 180 | State = RX_ERROR; |
ftagius | 32:a2472bbe7c92 | 181 | debug_if( DEBUG_MESSAGE, "> OnRxError\r\n" ); |
ftagius | 32:a2472bbe7c92 | 182 | } |
ftagius | 32:a2472bbe7c92 | 183 | |
ftagius | 32:a2472bbe7c92 | 184 | |
ftagius | 32:a2472bbe7c92 | 185 | char* itoa(int val, int base){ |
ftagius | 32:a2472bbe7c92 | 186 | |
ftagius | 32:a2472bbe7c92 | 187 | static char buf[32] = {0}; |
ftagius | 32:a2472bbe7c92 | 188 | |
ftagius | 32:a2472bbe7c92 | 189 | int i = 30; |
ftagius | 32:a2472bbe7c92 | 190 | |
ftagius | 32:a2472bbe7c92 | 191 | for(; val && i ; --i, val /= base) |
ftagius | 32:a2472bbe7c92 | 192 | |
ftagius | 32:a2472bbe7c92 | 193 | buf[i] = "0123456789abcdef"[val % base]; |
ftagius | 32:a2472bbe7c92 | 194 | |
ftagius | 32:a2472bbe7c92 | 195 | return &buf[i+1]; |
ftagius | 32:a2472bbe7c92 | 196 | |
ftagius | 32:a2472bbe7c92 | 197 | } |
ftagius | 32:a2472bbe7c92 | 198 | |
ftagius | 32:a2472bbe7c92 | 199 | unsigned int randomSeed(){ |
ftagius | 32:a2472bbe7c92 | 200 | AnalogIn randIn(A0); // analog input for random number seed |
ftagius | 32:a2472bbe7c92 | 201 | unsigned int rnum = 0; // int = 4 bytes - 32 bits |
ftagius | 32:a2472bbe7c92 | 202 | // build 32 bit random number from the 2 lower bits from 16 analog reads |
ftagius | 32:a2472bbe7c92 | 203 | for (int i = 0; i<4;i++){ |
ftagius | 32:a2472bbe7c92 | 204 | rnum = rnum | ((randIn.read_u16() & 15) << i*4); |
ftagius | 32:a2472bbe7c92 | 205 | wait_ms(5); |
ftagius | 32:a2472bbe7c92 | 206 | } |
ftagius | 32:a2472bbe7c92 | 207 | return rnum; |
ftagius | 32:a2472bbe7c92 | 208 | } |
ftagius | 32:a2472bbe7c92 | 209 | |
ftagius | 32:a2472bbe7c92 | 210 | void configRxTx() { |
ftagius | 32:a2472bbe7c92 | 211 | |
ftagius | 32:a2472bbe7c92 | 212 | |
ftagius | 32:a2472bbe7c92 | 213 | Radio.SetTxConfig( MODEM_LORA, TxPower, 0, Bandwidth, |
ftagius | 32:a2472bbe7c92 | 214 | SpreadingFactor, CodingRate, |
ftagius | 32:a2472bbe7c92 | 215 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
ftagius | 32:a2472bbe7c92 | 216 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
ftagius | 32:a2472bbe7c92 | 217 | LORA_IQ_INVERSION_ON, 2000000 ); |
ftagius | 32:a2472bbe7c92 | 218 | |
ftagius | 32:a2472bbe7c92 | 219 | Radio.SetRxConfig( MODEM_LORA, Bandwidth, SpreadingFactor, |
ftagius | 32:a2472bbe7c92 | 220 | CodingRate, 0, LORA_PREAMBLE_LENGTH, |
ftagius | 32:a2472bbe7c92 | 221 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
ftagius | 32:a2472bbe7c92 | 222 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
ftagius | 32:a2472bbe7c92 | 223 | LORA_IQ_INVERSION_ON, true ); |
ftagius | 32:a2472bbe7c92 | 224 | } |
ftagius | 32:a2472bbe7c92 | 225 | |
ftagius | 32:a2472bbe7c92 | 226 | void print_bandwidth() |
ftagius | 32:a2472bbe7c92 | 227 | { |
ftagius | 32:a2472bbe7c92 | 228 | printf("bandwidth: "); |
ftagius | 32:a2472bbe7c92 | 229 | switch (((Radio.Read(REG_LR_MODEMCONFIG1) & 0xf0) >> 4)) { |
ftagius | 32:a2472bbe7c92 | 230 | case 0: printf("7.8KHz "); break; |
ftagius | 32:a2472bbe7c92 | 231 | case 1: printf("10.4KHz "); break; |
ftagius | 32:a2472bbe7c92 | 232 | case 2: printf("15.6KHz "); break; |
ftagius | 32:a2472bbe7c92 | 233 | case 3: printf("20.8KHz "); break; |
ftagius | 32:a2472bbe7c92 | 234 | case 4: printf("31.25KHz "); break; |
ftagius | 32:a2472bbe7c92 | 235 | case 5: printf("41.7KHz "); break; |
ftagius | 32:a2472bbe7c92 | 236 | case 6: printf("62.5KHz "); break; |
ftagius | 32:a2472bbe7c92 | 237 | case 7: printf("125KHz "); break; |
ftagius | 32:a2472bbe7c92 | 238 | case 8: printf("250KHz "); break; |
ftagius | 32:a2472bbe7c92 | 239 | case 9: printf("500KHz "); break; |
ftagius | 32:a2472bbe7c92 | 240 | default: printf("%x ", Radio.Read(REG_LR_MODEMCONFIG1)); break; |
ftagius | 32:a2472bbe7c92 | 241 | } |
ftagius | 32:a2472bbe7c92 | 242 | //printf("\r\n"); |
ftagius | 32:a2472bbe7c92 | 243 | } |
ftagius | 32:a2472bbe7c92 | 244 | |
ftagius | 32:a2472bbe7c92 | 245 | void print_cr() |
ftagius | 32:a2472bbe7c92 | 246 | { |
ftagius | 32:a2472bbe7c92 | 247 | int cr = (Radio.Read(REG_LR_MODEMCONFIG1) & 0x0f)>>1; |
ftagius | 32:a2472bbe7c92 | 248 | printf("coding rate: "); |
ftagius | 32:a2472bbe7c92 | 249 | switch (cr) |
ftagius | 32:a2472bbe7c92 | 250 | { |
ftagius | 32:a2472bbe7c92 | 251 | case 1: |
ftagius | 32:a2472bbe7c92 | 252 | printf("4/5"); |
ftagius | 32:a2472bbe7c92 | 253 | break; |
ftagius | 32:a2472bbe7c92 | 254 | case 2: |
ftagius | 32:a2472bbe7c92 | 255 | printf("4/6"); |
ftagius | 32:a2472bbe7c92 | 256 | break; |
ftagius | 32:a2472bbe7c92 | 257 | case 3: |
ftagius | 32:a2472bbe7c92 | 258 | printf("4/7"); |
ftagius | 32:a2472bbe7c92 | 259 | break; |
ftagius | 32:a2472bbe7c92 | 260 | case 4: |
ftagius | 32:a2472bbe7c92 | 261 | printf("4/8"); |
ftagius | 32:a2472bbe7c92 | 262 | break; |
ftagius | 32:a2472bbe7c92 | 263 | default: |
ftagius | 32:a2472bbe7c92 | 264 | printf("unknown"); |
ftagius | 32:a2472bbe7c92 | 265 | break; |
ftagius | 32:a2472bbe7c92 | 266 | |
ftagius | 32:a2472bbe7c92 | 267 | } |
ftagius | 32:a2472bbe7c92 | 268 | printf(" "); |
ftagius | 32:a2472bbe7c92 | 269 | } |
ftagius | 32:a2472bbe7c92 | 270 | |
ftagius | 32:a2472bbe7c92 | 271 | void print_power() |
ftagius | 32:a2472bbe7c92 | 272 | { |
ftagius | 32:a2472bbe7c92 | 273 | uint8_t paConfig = 0; |
ftagius | 32:a2472bbe7c92 | 274 | uint8_t paDac = 0; |
ftagius | 32:a2472bbe7c92 | 275 | |
ftagius | 32:a2472bbe7c92 | 276 | paConfig = Radio.Read( REG_PACONFIG ); |
ftagius | 32:a2472bbe7c92 | 277 | paDac = Radio.Read( REG_PADAC ); |
ftagius | 32:a2472bbe7c92 | 278 | |
ftagius | 32:a2472bbe7c92 | 279 | paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK ) | Radio.GetPaSelect( Radio.GetChannel()*1000000); |
ftagius | 32:a2472bbe7c92 | 280 | paConfig = ( paConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70; |
ftagius | 32:a2472bbe7c92 | 281 | printf("channel = %f getpa = %x, paConfig =%x \r\n",Radio.GetChannel(), Radio.GetPaSelect((uint32_t)Radio.GetChannel()*1000000), paConfig ); |
ftagius | 32:a2472bbe7c92 | 282 | if( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST ) |
ftagius | 32:a2472bbe7c92 | 283 | { |
ftagius | 32:a2472bbe7c92 | 284 | if( ( paDac & RF_PADAC_20DBM_ON ) == RF_PADAC_20DBM_ON ) |
ftagius | 32:a2472bbe7c92 | 285 | { |
ftagius | 32:a2472bbe7c92 | 286 | printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f ) + 5 ); |
ftagius | 32:a2472bbe7c92 | 287 | } |
ftagius | 32:a2472bbe7c92 | 288 | else |
ftagius | 32:a2472bbe7c92 | 289 | printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) &0x0f) + 2 ); |
ftagius | 32:a2472bbe7c92 | 290 | } |
ftagius | 32:a2472bbe7c92 | 291 | else |
ftagius | 32:a2472bbe7c92 | 292 | { |
ftagius | 32:a2472bbe7c92 | 293 | printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f ) - 1 ); |
ftagius | 32:a2472bbe7c92 | 294 | } |
ftagius | 32:a2472bbe7c92 | 295 | |
ftagius | 32:a2472bbe7c92 | 296 | } |
ftagius | 32:a2472bbe7c92 | 297 | |
ftagius | 32:a2472bbe7c92 | 298 | void print_errata() |
ftagius | 32:a2472bbe7c92 | 299 | { |
ftagius | 32:a2472bbe7c92 | 300 | |
ftagius | 32:a2472bbe7c92 | 301 | int seqconfig1 = (Radio.Read(REG_SEQCONFIG1)); |
ftagius | 32:a2472bbe7c92 | 302 | int timer2 = (Radio.Read(REG_TIMER2COEF)); |
ftagius | 32:a2472bbe7c92 | 303 | printf("seqconfig1 = 0x%02x\r\n", seqconfig1); |
ftagius | 32:a2472bbe7c92 | 304 | printf("timer2coef = 0x%02x\r\n", timer2); |
ftagius | 32:a2472bbe7c92 | 305 | |
ftagius | 32:a2472bbe7c92 | 306 | } |
ftagius | 32:a2472bbe7c92 | 307 | void print_status() |
ftagius | 32:a2472bbe7c92 | 308 | { |
ftagius | 32:a2472bbe7c92 | 309 | |
ftagius | 32:a2472bbe7c92 | 310 | printf("Radio version: 0x%02x Channel: %.1f MHz ", \ |
ftagius | 32:a2472bbe7c92 | 311 | Radio.Read(REG_VERSION),\ |
ftagius | 32:a2472bbe7c92 | 312 | Radio.GetChannel()\ |
ftagius | 32:a2472bbe7c92 | 313 | ); |
ftagius | 32:a2472bbe7c92 | 314 | print_power(); |
ftagius | 32:a2472bbe7c92 | 315 | print_bandwidth(); printf("\r\n"); |
ftagius | 32:a2472bbe7c92 | 316 | printf("Spreading Factor: %d ",SpreadingFactor); |
ftagius | 32:a2472bbe7c92 | 317 | print_cr(); |
ftagius | 32:a2472bbe7c92 | 318 | if (isMaster) |
ftagius | 32:a2472bbe7c92 | 319 | printf("Mode: master "); |
ftagius | 32:a2472bbe7c92 | 320 | else |
ftagius | 32:a2472bbe7c92 | 321 | printf("Mode: slave "); |
ftagius | 32:a2472bbe7c92 | 322 | |
ftagius | 32:a2472bbe7c92 | 323 | |
ftagius | 32:a2472bbe7c92 | 324 | printf("\r\n"); |
ftagius | 32:a2472bbe7c92 | 325 | print_errata(); |
ftagius | 32:a2472bbe7c92 | 326 | printf("\r\n"); |
ftagius | 32:a2472bbe7c92 | 327 | |
ftagius | 32:a2472bbe7c92 | 328 | } |
ftagius | 32:a2472bbe7c92 | 329 | |
ftagius | 32:a2472bbe7c92 | 330 | |
ftagius | 32:a2472bbe7c92 | 331 | void OnTxDone( void ) |
ftagius | 32:a2472bbe7c92 | 332 | { |
ftagius | 32:a2472bbe7c92 | 333 | Radio.Sleep( ); |
ftagius | 32:a2472bbe7c92 | 334 | State = TX; |
ftagius | 32:a2472bbe7c92 | 335 | pkt_count++; |
ftagius | 32:a2472bbe7c92 | 336 | |
ftagius | 32:a2472bbe7c92 | 337 | |
ftagius | 32:a2472bbe7c92 | 338 | //debug_if( DEBUG_MESSAGE, "> OnTxDone\r\n" ); |
ftagius | 32:a2472bbe7c92 | 339 | } |
ftagius | 32:a2472bbe7c92 | 340 | |
ftagius | 32:a2472bbe7c92 | 341 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
ftagius | 32:a2472bbe7c92 | 342 | { |
ftagius | 32:a2472bbe7c92 | 343 | Radio.Sleep( ); |
ftagius | 32:a2472bbe7c92 | 344 | if (pkt_count >= max_pkts) |
ftagius | 32:a2472bbe7c92 | 345 | { |
ftagius | 32:a2472bbe7c92 | 346 | pkt_count=0; |
ftagius | 32:a2472bbe7c92 | 347 | } |
ftagius | 32:a2472bbe7c92 | 348 | |
ftagius | 32:a2472bbe7c92 | 349 | // mark the rolling avg that a pkt has been received |
ftagius | 32:a2472bbe7c92 | 350 | pkt_data[pkt_count]=1; |
ftagius | 32:a2472bbe7c92 | 351 | //printf("rx done, pkt_count = %d\r\n", pkt_count); |
ftagius | 32:a2472bbe7c92 | 352 | |
ftagius | 32:a2472bbe7c92 | 353 | BufferSize = size; |
ftagius | 32:a2472bbe7c92 | 354 | int i; |
ftagius | 32:a2472bbe7c92 | 355 | for (i=0; i< BUFFER_SIZE; i++) |
ftagius | 32:a2472bbe7c92 | 356 | BufferRx[ i ] = '\0'; // clear the buffer |
ftagius | 32:a2472bbe7c92 | 357 | |
ftagius | 32:a2472bbe7c92 | 358 | memcpy( BufferRx, payload, BufferSize ); |
ftagius | 32:a2472bbe7c92 | 359 | if (rssi > 100) |
ftagius | 32:a2472bbe7c92 | 360 | { |
ftagius | 32:a2472bbe7c92 | 361 | rssi *= -1; |
ftagius | 32:a2472bbe7c92 | 362 | } |
ftagius | 32:a2472bbe7c92 | 363 | RssiValue = rssi; |
ftagius | 32:a2472bbe7c92 | 364 | SnrValue = snr; |
ftagius | 32:a2472bbe7c92 | 365 | State = RX; |
ftagius | 32:a2472bbe7c92 | 366 | rxTimeout = false; |
ftagius | 32:a2472bbe7c92 | 367 | if (app == APP_PING) |
ftagius | 32:a2472bbe7c92 | 368 | debug_if( DEBUG_MESSAGE, "> OnRxDone size=%d rssi=%d snr=%d \r\n", size, rssi, snr ); |
ftagius | 32:a2472bbe7c92 | 369 | |
ftagius | 32:a2472bbe7c92 | 370 | } |
ftagius | 32:a2472bbe7c92 | 371 | |
ftagius | 33:319cbac3b6eb | 372 | void OnCadDone( bool activity ) |
ftagius | 33:319cbac3b6eb | 373 | { |
ftagius | 33:319cbac3b6eb | 374 | //Radio.Sleep(); |
ftagius | 33:319cbac3b6eb | 375 | State = CAD; |
ftagius | 33:319cbac3b6eb | 376 | if (activity) |
ftagius | 33:319cbac3b6eb | 377 | { |
ftagius | 33:319cbac3b6eb | 378 | debug_if( DEBUG_MESSAGE, "> OnCadDone\n\r" ); |
ftagius | 33:319cbac3b6eb | 379 | printf("on cad done\r\n"); |
ftagius | 33:319cbac3b6eb | 380 | Radio.StartCad(); |
ftagius | 33:319cbac3b6eb | 381 | } |
ftagius | 33:319cbac3b6eb | 382 | else |
ftagius | 33:319cbac3b6eb | 383 | { |
ftagius | 33:319cbac3b6eb | 384 | // printf("false\r\n"); |
ftagius | 33:319cbac3b6eb | 385 | Radio.StartCad(); |
ftagius | 33:319cbac3b6eb | 386 | } |
ftagius | 33:319cbac3b6eb | 387 | } |
ftagius | 33:319cbac3b6eb | 388 | |
ftagius | 33:319cbac3b6eb | 389 | #ifndef STANDALONE |
ftagius | 32:a2472bbe7c92 | 390 | void find_distance() |
ftagius | 32:a2472bbe7c92 | 391 | { |
ftagius | 32:a2472bbe7c92 | 392 | //float dist = 0; |
ftagius | 32:a2472bbe7c92 | 393 | float lat2 = 0; |
ftagius | 32:a2472bbe7c92 | 394 | float lon2 = 0; |
ftagius | 32:a2472bbe7c92 | 395 | float lat1 = 0; |
ftagius | 32:a2472bbe7c92 | 396 | float lon1 = 0; |
ftagius | 32:a2472bbe7c92 | 397 | float dLat = 0; |
ftagius | 32:a2472bbe7c92 | 398 | float dLon = 0; |
ftagius | 32:a2472bbe7c92 | 399 | float c = 0; |
ftagius | 32:a2472bbe7c92 | 400 | float a = 0; |
ftagius | 32:a2472bbe7c92 | 401 | float d = 0; |
ftagius | 32:a2472bbe7c92 | 402 | |
ftagius | 32:a2472bbe7c92 | 403 | distance = 0; |
ftagius | 33:319cbac3b6eb | 404 | |
ftagius | 32:a2472bbe7c92 | 405 | if (gpsd.lat_deg == 0.0) |
ftagius | 32:a2472bbe7c92 | 406 | { |
ftagius | 32:a2472bbe7c92 | 407 | distance = -1; |
ftagius | 32:a2472bbe7c92 | 408 | return; |
ftagius | 32:a2472bbe7c92 | 409 | } |
ftagius | 32:a2472bbe7c92 | 410 | if (gpsd.lon_deg == 0.0) |
ftagius | 32:a2472bbe7c92 | 411 | { |
ftagius | 32:a2472bbe7c92 | 412 | distance = -1; |
ftagius | 32:a2472bbe7c92 | 413 | return; |
ftagius | 32:a2472bbe7c92 | 414 | } |
ftagius | 32:a2472bbe7c92 | 415 | |
ftagius | 33:319cbac3b6eb | 416 | lat1 = r_latitude_last; |
ftagius | 33:319cbac3b6eb | 417 | lon1 = r_longitude_last; |
ftagius | 32:a2472bbe7c92 | 418 | |
ftagius | 32:a2472bbe7c92 | 419 | lat2 = gpsd.lat_deg; |
ftagius | 32:a2472bbe7c92 | 420 | lon2 = gpsd.lon_deg; |
ftagius | 32:a2472bbe7c92 | 421 | //Calculate Distance |
ftagius | 32:a2472bbe7c92 | 422 | dLat = (lat2-lat1)*(PI/180); |
ftagius | 32:a2472bbe7c92 | 423 | dLon = (lon2-lon1)*(PI/180); |
ftagius | 32:a2472bbe7c92 | 424 | a = sin(dLat/2) * sin(dLat/2) + |
ftagius | 32:a2472bbe7c92 | 425 | sin(dLon/2) * sin(dLon/2) * cos(lat1*(PI/180)) * cos(lat2*(PI/180)); |
ftagius | 32:a2472bbe7c92 | 426 | c = 2 * atan2(sqrt(a), sqrt(1-a)); |
ftagius | 32:a2472bbe7c92 | 427 | d = 6371000 * c; |
ftagius | 32:a2472bbe7c92 | 428 | |
ftagius | 32:a2472bbe7c92 | 429 | //GPS is only precise to 5 meters, so throw out bad data |
ftagius | 32:a2472bbe7c92 | 430 | if ( d <= 5) { |
ftagius | 32:a2472bbe7c92 | 431 | d = 0; |
ftagius | 32:a2472bbe7c92 | 432 | } |
ftagius | 32:a2472bbe7c92 | 433 | distance = distance + d; |
ftagius | 33:319cbac3b6eb | 434 | #if 0 |
ftagius | 33:319cbac3b6eb | 435 | if (distance > 10000) |
ftagius | 33:319cbac3b6eb | 436 | { |
ftagius | 33:319cbac3b6eb | 437 | printf("distance: %f\r\n", distance); |
ftagius | 33:319cbac3b6eb | 438 | printf("lat1=%f lon1=%f lat2=%f lon2=%f\r\n",lat1,lon1,lat2,lon2); |
ftagius | 33:319cbac3b6eb | 439 | } |
ftagius | 33:319cbac3b6eb | 440 | #endif |
ftagius | 32:a2472bbe7c92 | 441 | |
ftagius | 32:a2472bbe7c92 | 442 | } |
ftagius | 32:a2472bbe7c92 | 443 | |
ftagius | 32:a2472bbe7c92 | 444 | int check_gps(void) |
ftagius | 32:a2472bbe7c92 | 445 | { |
ftagius | 32:a2472bbe7c92 | 446 | |
ftagius | 32:a2472bbe7c92 | 447 | char c; |
ftagius | 32:a2472bbe7c92 | 448 | int debug=0; |
ftagius | 32:a2472bbe7c92 | 449 | c = gpsd.read(); //queries the GPS |
ftagius | 32:a2472bbe7c92 | 450 | if (debug) |
ftagius | 32:a2472bbe7c92 | 451 | { |
ftagius | 32:a2472bbe7c92 | 452 | if (c) { |
ftagius | 32:a2472bbe7c92 | 453 | printf("%c", c); //this line will echo the GPS data if not paused |
ftagius | 32:a2472bbe7c92 | 454 | return 1; |
ftagius | 32:a2472bbe7c92 | 455 | |
ftagius | 32:a2472bbe7c92 | 456 | } |
ftagius | 32:a2472bbe7c92 | 457 | } |
ftagius | 32:a2472bbe7c92 | 458 | |
ftagius | 32:a2472bbe7c92 | 459 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
ftagius | 32:a2472bbe7c92 | 460 | if ( gpsd.newNMEAreceived() ) { |
ftagius | 33:319cbac3b6eb | 461 | //printf("nmea received \r\n"); |
ftagius | 33:319cbac3b6eb | 462 | if (r_latitude_last != 0) |
ftagius | 32:a2472bbe7c92 | 463 | { |
ftagius | 32:a2472bbe7c92 | 464 | find_distance(); |
ftagius | 32:a2472bbe7c92 | 465 | //printf("distance is %f\r\n",distance); |
ftagius | 32:a2472bbe7c92 | 466 | if (distance < 50000 ) |
ftagius | 32:a2472bbe7c92 | 467 | { |
ftagius | 32:a2472bbe7c92 | 468 | if (distance >= 0) |
ftagius | 32:a2472bbe7c92 | 469 | { |
ftagius | 32:a2472bbe7c92 | 470 | cLCD.setCursor(0,1); |
ftagius | 32:a2472bbe7c92 | 471 | cLCD.printf("d:%0.1fm per:%d ", distance,per); |
ftagius | 32:a2472bbe7c92 | 472 | } |
ftagius | 33:319cbac3b6eb | 473 | |
ftagius | 32:a2472bbe7c92 | 474 | } |
ftagius | 33:319cbac3b6eb | 475 | //printf("distance = %f, %f\r\n",distance, r_latitude_last); |
ftagius | 32:a2472bbe7c92 | 476 | } |
ftagius | 33:319cbac3b6eb | 477 | else |
ftagius | 33:319cbac3b6eb | 478 | { |
ftagius | 33:319cbac3b6eb | 479 | cLCD.setCursor(0,1); |
ftagius | 33:319cbac3b6eb | 480 | if (gpsd.lat_deg != 0) |
ftagius | 33:319cbac3b6eb | 481 | cLCD.printf("%0.4f %0.4f", gpsd.lat_deg, gpsd.lon_deg); |
ftagius | 33:319cbac3b6eb | 482 | else |
ftagius | 33:319cbac3b6eb | 483 | cLCD.printf("No GPS. per:%d ",per); |
ftagius | 33:319cbac3b6eb | 484 | } |
ftagius | 33:319cbac3b6eb | 485 | |
ftagius | 33:319cbac3b6eb | 486 | |
ftagius | 32:a2472bbe7c92 | 487 | |
ftagius | 33:319cbac3b6eb | 488 | if ( !gpsd.parse(gpsd.lastNMEA()) ) |
ftagius | 32:a2472bbe7c92 | 489 | { |
ftagius | 32:a2472bbe7c92 | 490 | // radfta led = !led; |
ftagius | 32:a2472bbe7c92 | 491 | #if 0 |
ftagius | 32:a2472bbe7c92 | 492 | if (gpsd.lat_deg != 0) |
ftagius | 32:a2472bbe7c92 | 493 | { |
ftagius | 32:a2472bbe7c92 | 494 | cLCD.setCursor(0,1); |
ftagius | 32:a2472bbe7c92 | 495 | cLCD.printf("d:Nx per:%d ", per); |
ftagius | 32:a2472bbe7c92 | 496 | cLCD.printf("%0.4f %0.4f", gpsd.lat_deg, gpsd.lon_deg); |
ftagius | 32:a2472bbe7c92 | 497 | } |
ftagius | 32:a2472bbe7c92 | 498 | #endif |
ftagius | 32:a2472bbe7c92 | 499 | |
ftagius | 32:a2472bbe7c92 | 500 | |
ftagius | 32:a2472bbe7c92 | 501 | |
ftagius | 32:a2472bbe7c92 | 502 | return 0; |
ftagius | 32:a2472bbe7c92 | 503 | } |
ftagius | 32:a2472bbe7c92 | 504 | else |
ftagius | 32:a2472bbe7c92 | 505 | { |
ftagius | 32:a2472bbe7c92 | 506 | // printf("GPS %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds); |
ftagius | 32:a2472bbe7c92 | 507 | return 1; |
ftagius | 32:a2472bbe7c92 | 508 | } |
ftagius | 32:a2472bbe7c92 | 509 | |
ftagius | 32:a2472bbe7c92 | 510 | } |
ftagius | 32:a2472bbe7c92 | 511 | else |
ftagius | 32:a2472bbe7c92 | 512 | { |
ftagius | 32:a2472bbe7c92 | 513 | //printf("GPS %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds); |
ftagius | 32:a2472bbe7c92 | 514 | return 0; |
ftagius | 32:a2472bbe7c92 | 515 | } |
ftagius | 32:a2472bbe7c92 | 516 | |
ftagius | 32:a2472bbe7c92 | 517 | } |
ftagius | 33:319cbac3b6eb | 518 | #endif |