CaryCoders
/
demo_SX1276_standalone
standalone sx1276 demo program
Fork of SX1276_GPS by
main.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 |
---|---|---|---|
wim | 0:88d87b1c1f8b | 1 | #include "mbed.h" |
vtraveller | 25:24654d08a99a | 2 | #include "lcdadafruit.h" |
ftagius | 29:0ea07cc7124b | 3 | #include "sx1276-hal.h" |
ftagius | 29:0ea07cc7124b | 4 | #include "GPS.h" |
ftagius | 29:0ea07cc7124b | 5 | #include "main.h" |
ftagius | 29:0ea07cc7124b | 6 | #include "debug.h" |
ftagius | 29:0ea07cc7124b | 7 | #include "serial_api.h" |
ftagius | 32:a2472bbe7c92 | 8 | #include "datetime.h" |
ftagius | 32:a2472bbe7c92 | 9 | #include "time.h" |
ftagius | 32:a2472bbe7c92 | 10 | #include <string> |
wim | 0:88d87b1c1f8b | 11 | |
ftagius | 29:0ea07cc7124b | 12 | |
ftagius | 29:0ea07cc7124b | 13 | #define USE_MODEM_LORA 1 |
ftagius | 29:0ea07cc7124b | 14 | //#define RF_FREQUENCY 868000000 // Hz |
ftagius | 29:0ea07cc7124b | 15 | //#define RF_FREQUENCY 880030000 |
ftagius | 31:2c813f321db7 | 16 | #define RF_FREQUENCY 915000000.0 // Hz |
ftagius | 31:2c813f321db7 | 17 | //#define RF_FREQUENCY 413000000.0 // Hz |
ftagius | 32:a2472bbe7c92 | 18 | |
ftagius | 29:0ea07cc7124b | 19 | |
ftagius | 29:0ea07cc7124b | 20 | //#define BUFFER_SIZE 64 // Define the payload size here |
ftagius | 29:0ea07cc7124b | 21 | |
ftagius | 29:0ea07cc7124b | 22 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
ftagius | 32:a2472bbe7c92 | 23 | //DigitalOut led(LED2); |
ftagius | 32:a2472bbe7c92 | 24 | DigitalOut led(LED_RED); |
ftagius | 29:0ea07cc7124b | 25 | #else |
ftagius | 29:0ea07cc7124b | 26 | DigitalOut led(LED1); |
ftagius | 29:0ea07cc7124b | 27 | #endif |
ftagius | 29:0ea07cc7124b | 28 | Serial pc(USBTX, USBRX); |
ftagius | 29:0ea07cc7124b | 29 | |
ftagius | 29:0ea07cc7124b | 30 | float Frequency = RF_FREQUENCY; |
ftagius | 32:a2472bbe7c92 | 31 | float distance = -1; |
ftagius | 32:a2472bbe7c92 | 32 | float r_latitude = 0; |
ftagius | 32:a2472bbe7c92 | 33 | float r_longitude = 0; |
ftagius | 29:0ea07cc7124b | 34 | int TxPower = TX_OUTPUT_POWER; |
ftagius | 29:0ea07cc7124b | 35 | int Bandwidth = LORA_BANDWIDTH; |
ftagius | 29:0ea07cc7124b | 36 | int SpreadingFactor = LORA_SPREADING_FACTOR; |
ftagius | 29:0ea07cc7124b | 37 | int CodingRate = LORA_CODINGRATE; |
ftagius | 32:a2472bbe7c92 | 38 | struct tm Clock; |
ftagius | 29:0ea07cc7124b | 39 | |
ftagius | 29:0ea07cc7124b | 40 | /* |
ftagius | 29:0ea07cc7124b | 41 | * Global variables declarations |
ftagius | 29:0ea07cc7124b | 42 | */ |
ftagius | 29:0ea07cc7124b | 43 | typedef RadioState States_t; |
ftagius | 29:0ea07cc7124b | 44 | volatile States_t State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 45 | |
ftagius | 29:0ea07cc7124b | 46 | SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, NULL ); |
ftagius | 29:0ea07cc7124b | 47 | // for hand wired I2C cI2C(PTC9, PTC8); |
ftagius | 29:0ea07cc7124b | 48 | I2C cI2C(PTC9, PTC8); |
ftagius | 29:0ea07cc7124b | 49 | // for stacked shield I2C cI2C(PTC2, PTC1); |
ftagius | 29:0ea07cc7124b | 50 | //I2C cI2C(PTC2, PTC1); |
ftagius | 29:0ea07cc7124b | 51 | LCDadafruit cLCD(cI2C); |
ftagius | 29:0ea07cc7124b | 52 | GPS gpsd(PTE0,PTE1); |
ftagius | 29:0ea07cc7124b | 53 | |
ftagius | 29:0ea07cc7124b | 54 | // for other board GPS gpsd(PTE20, PTE21); |
ftagius | 29:0ea07cc7124b | 55 | |
ftagius | 29:0ea07cc7124b | 56 | uint8_t PingMsg[256]; |
ftagius | 29:0ea07cc7124b | 57 | uint8_t PongMsg[256]; |
ftagius | 29:0ea07cc7124b | 58 | uint8_t HelloMsg[256]; |
ftagius | 29:0ea07cc7124b | 59 | char pcbuf[PCBUF_SIZE]; |
ftagius | 29:0ea07cc7124b | 60 | uint16_t BufferSize = BUFFER_SIZE; |
ftagius | 29:0ea07cc7124b | 61 | uint8_t BufferTx[BUFFER_SIZE]; |
ftagius | 29:0ea07cc7124b | 62 | uint8_t BufferRx[BUFFER_SIZE]; |
ftagius | 29:0ea07cc7124b | 63 | int16_t RssiValue = 0.0; |
ftagius | 29:0ea07cc7124b | 64 | int8_t SnrValue = 0.0; |
ftagius | 29:0ea07cc7124b | 65 | int txLen = 0; |
ftagius | 29:0ea07cc7124b | 66 | int pkt_count = 0; |
ftagius | 29:0ea07cc7124b | 67 | int max_pkts = 20; |
ftagius | 29:0ea07cc7124b | 68 | int pkt_data[20]; |
ftagius | 29:0ea07cc7124b | 69 | int per = 0; |
ftagius | 32:a2472bbe7c92 | 70 | int tx_rate_us = 100000; // 1 second |
ftagius | 29:0ea07cc7124b | 71 | app_e app = APP_NONE; |
ftagius | 29:0ea07cc7124b | 72 | bool isMaster = true; |
ftagius | 29:0ea07cc7124b | 73 | bool AlwaysMaster = false; |
ftagius | 29:0ea07cc7124b | 74 | bool AlwaysSlave = false; |
ftagius | 29:0ea07cc7124b | 75 | bool ackRcvd = true; |
ftagius | 29:0ea07cc7124b | 76 | bool rxTimeout = false; |
ftagius | 29:0ea07cc7124b | 77 | bool gpsEnabled = true; |
ftagius | 32:a2472bbe7c92 | 78 | InterruptIn PPS(PTA1); //pps IRQ |
ftagius | 32:a2472bbe7c92 | 79 | Ticker usec_tick; |
ftagius | 32:a2472bbe7c92 | 80 | |
ftagius | 32:a2472bbe7c92 | 81 | void tx_pkt() { |
ftagius | 32:a2472bbe7c92 | 82 | led = !led; |
ftagius | 32:a2472bbe7c92 | 83 | BufferSize=strlen((char *)BufferTx); |
ftagius | 32:a2472bbe7c92 | 84 | // printf("Buffersize = %d\r\n",BufferSize); |
ftagius | 32:a2472bbe7c92 | 85 | Radio.Send( BufferTx, BufferSize ); |
ftagius | 32:a2472bbe7c92 | 86 | //Radio.Rx( RX_TIMEOUT_VALUE / 1 ); |
ftagius | 32:a2472bbe7c92 | 87 | } |
ftagius | 29:0ea07cc7124b | 88 | |
ftagius | 32:a2472bbe7c92 | 89 | void ppsSync() |
ftagius | 29:0ea07cc7124b | 90 | { |
ftagius | 32:a2472bbe7c92 | 91 | static int pps_count=0; |
ftagius | 32:a2472bbe7c92 | 92 | |
ftagius | 32:a2472bbe7c92 | 93 | ++pps_count; |
ftagius | 32:a2472bbe7c92 | 94 | if (pps_count == 1) |
ftagius | 29:0ea07cc7124b | 95 | { |
ftagius | 32:a2472bbe7c92 | 96 | // detach the timer |
ftagius | 32:a2472bbe7c92 | 97 | usec_tick.detach(); |
ftagius | 32:a2472bbe7c92 | 98 | printf("call attach\r\n"); |
ftagius | 32:a2472bbe7c92 | 99 | //wait_ms(500); |
ftagius | 32:a2472bbe7c92 | 100 | usec_tick.attach_us(&tx_pkt, tx_rate_us); |
ftagius | 29:0ea07cc7124b | 101 | } |
ftagius | 29:0ea07cc7124b | 102 | else |
ftagius | 29:0ea07cc7124b | 103 | { |
ftagius | 32:a2472bbe7c92 | 104 | if (pps_count > 10) |
ftagius | 32:a2472bbe7c92 | 105 | { |
ftagius | 32:a2472bbe7c92 | 106 | pps_count = 1; |
ftagius | 32:a2472bbe7c92 | 107 | } |
ftagius | 29:0ea07cc7124b | 108 | } |
ftagius | 29:0ea07cc7124b | 109 | |
ftagius | 32:a2472bbe7c92 | 110 | //printf("got a pps\r\n"); |
ftagius | 32:a2472bbe7c92 | 111 | //ShowDateTime(); |
ftagius | 32:a2472bbe7c92 | 112 | |
ftagius | 29:0ea07cc7124b | 113 | } |
vtraveller | 3:ed09f95739df | 114 | int main() |
vtraveller | 21:c44cfd3259c0 | 115 | { |
ftagius | 29:0ea07cc7124b | 116 | |
ftagius | 29:0ea07cc7124b | 117 | int i; |
ftagius | 32:a2472bbe7c92 | 118 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
ftagius | 32:a2472bbe7c92 | 119 | const int refresh_Time = 1000; //refresh time in ms |
ftagius | 32:a2472bbe7c92 | 120 | bool gpsFix=false; |
ftagius | 32:a2472bbe7c92 | 121 | gps_setup(); |
ftagius | 29:0ea07cc7124b | 122 | pc.baud(9600); |
vtraveller | 21:c44cfd3259c0 | 123 | cI2C.frequency(400000); // I2C can handle two different frequencies - switch to high speed if asked |
ftagius | 29:0ea07cc7124b | 124 | cLCD.clear(); |
ftagius | 29:0ea07cc7124b | 125 | cLCD.home(); |
ftagius | 29:0ea07cc7124b | 126 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 127 | cLCD.printf(" HOMER "); |
ftagius | 29:0ea07cc7124b | 128 | cLCD.setCursor(0,1); |
ftagius | 29:0ea07cc7124b | 129 | cLCD.printf(" DOH! "); |
ftagius | 29:0ea07cc7124b | 130 | |
ftagius | 29:0ea07cc7124b | 131 | rxTimeout = false; |
ftagius | 29:0ea07cc7124b | 132 | // PTD1 (the SCK pin for the one SPI interface onboard the KL25z) is actually an output to the Blue LED. |
ftagius | 29:0ea07cc7124b | 133 | // the lora shield drives SCK, which turns on the blue led. use PTC5 instead of the default PTD1 for SCK |
ftagius | 29:0ea07cc7124b | 134 | |
ftagius | 29:0ea07cc7124b | 135 | if (RADIO_INSTALLED) |
ftagius | 29:0ea07cc7124b | 136 | debug( " SX1276 Test Applications \r\n\n" ); |
ftagius | 29:0ea07cc7124b | 137 | led = 1; |
ftagius | 29:0ea07cc7124b | 138 | |
ftagius | 29:0ea07cc7124b | 139 | // init pkt_data array to 0 |
ftagius | 29:0ea07cc7124b | 140 | for (i=0;i<pkt_count;i++) |
ftagius | 29:0ea07cc7124b | 141 | pkt_data[pkt_count]=0; |
ftagius | 29:0ea07cc7124b | 142 | |
ftagius | 29:0ea07cc7124b | 143 | // verify the connection with the board |
ftagius | 29:0ea07cc7124b | 144 | if (RADIO_INSTALLED) |
ftagius | 29:0ea07cc7124b | 145 | { |
ftagius | 29:0ea07cc7124b | 146 | |
ftagius | 29:0ea07cc7124b | 147 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
ftagius | 29:0ea07cc7124b | 148 | { |
ftagius | 29:0ea07cc7124b | 149 | debug( "Radio could not be detected!\r\n", NULL ); |
ftagius | 29:0ea07cc7124b | 150 | wait( 1 ); |
ftagius | 29:0ea07cc7124b | 151 | } |
ftagius | 29:0ea07cc7124b | 152 | |
ftagius | 29:0ea07cc7124b | 153 | |
ftagius | 29:0ea07cc7124b | 154 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "> Board Type: SX1276MB1LAS < \r\n" ); |
ftagius | 29:0ea07cc7124b | 155 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "> Board Type: SX1276MB1MAS < \r\n" ); |
ftagius | 29:0ea07cc7124b | 156 | Radio.SetChannel( Frequency ); |
ftagius | 29:0ea07cc7124b | 157 | debug_if( !LORA_FHSS_ENABLED, "> LORA Mode < \r\n"); |
ftagius | 29:0ea07cc7124b | 158 | configRxTx(); |
ftagius | 29:0ea07cc7124b | 159 | |
ftagius | 29:0ea07cc7124b | 160 | //print_status(); |
ftagius | 29:0ea07cc7124b | 161 | debug_if( DEBUG_MESSAGE, "Starting Application loop\r\n" ); |
ftagius | 29:0ea07cc7124b | 162 | wait(1); |
ftagius | 29:0ea07cc7124b | 163 | |
ftagius | 29:0ea07cc7124b | 164 | // led = 0; |
ftagius | 29:0ea07cc7124b | 165 | |
ftagius | 29:0ea07cc7124b | 166 | strcat((char *)PingMsg,"PING"); |
ftagius | 29:0ea07cc7124b | 167 | strcat((char *)PongMsg,"PONG"); |
ftagius | 29:0ea07cc7124b | 168 | strcat((char *)HelloMsg,"HELLO_7"); |
ftagius | 29:0ea07cc7124b | 169 | |
ftagius | 29:0ea07cc7124b | 170 | Radio.Rx( RX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 171 | Radio.Tx( TX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 172 | |
ftagius | 29:0ea07cc7124b | 173 | app = APP_HELLO; |
ftagius | 29:0ea07cc7124b | 174 | } |
ftagius | 29:0ea07cc7124b | 175 | else |
ftagius | 29:0ea07cc7124b | 176 | { |
ftagius | 29:0ea07cc7124b | 177 | app = APP_GPS; |
ftagius | 29:0ea07cc7124b | 178 | pc.printf("Starting GPS App\r\n"); |
ftagius | 29:0ea07cc7124b | 179 | } |
ftagius | 29:0ea07cc7124b | 180 | |
ftagius | 32:a2472bbe7c92 | 181 | refresh_Timer.start(); //starts the clock on the timer |
ftagius | 32:a2472bbe7c92 | 182 | 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 |
ftagius | 32:a2472bbe7c92 | 183 | PPS.rise(&ppsSync); // call ppsSync when the PPS irq is detected |
ftagius | 32:a2472bbe7c92 | 184 | |
ftagius | 29:0ea07cc7124b | 185 | while( 1 ) |
ftagius | 29:0ea07cc7124b | 186 | { |
ftagius | 32:a2472bbe7c92 | 187 | check_gps(); |
ftagius | 32:a2472bbe7c92 | 188 | if (refresh_Timer.read_ms() >= refresh_Time) |
ftagius | 32:a2472bbe7c92 | 189 | { |
ftagius | 32:a2472bbe7c92 | 190 | // printf("reset timer popped\r\n"); |
ftagius | 32:a2472bbe7c92 | 191 | if (gpsd.fix) { |
ftagius | 32:a2472bbe7c92 | 192 | // got a gps fix |
ftagius | 32:a2472bbe7c92 | 193 | if (gpsFix == false) |
ftagius | 32:a2472bbe7c92 | 194 | { |
ftagius | 32:a2472bbe7c92 | 195 | // first time fix obtained |
ftagius | 32:a2472bbe7c92 | 196 | gpsFix = true; |
ftagius | 32:a2472bbe7c92 | 197 | // set the rtc with the latest gps time |
ftagius | 32:a2472bbe7c92 | 198 | |
ftagius | 32:a2472bbe7c92 | 199 | 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); |
ftagius | 32:a2472bbe7c92 | 200 | // update rtc with the latest gps time stamp |
ftagius | 32:a2472bbe7c92 | 201 | SetDateTime(gpsd.year+2000, |
ftagius | 32:a2472bbe7c92 | 202 | gpsd.month, |
ftagius | 32:a2472bbe7c92 | 203 | gpsd.day, |
ftagius | 32:a2472bbe7c92 | 204 | gpsd.hour, |
ftagius | 32:a2472bbe7c92 | 205 | gpsd.minute, |
ftagius | 32:a2472bbe7c92 | 206 | gpsd.seconds); |
ftagius | 32:a2472bbe7c92 | 207 | } |
ftagius | 32:a2472bbe7c92 | 208 | #if 0 |
ftagius | 32:a2472bbe7c92 | 209 | else |
ftagius | 32:a2472bbe7c92 | 210 | { |
ftagius | 32:a2472bbe7c92 | 211 | printf("radfta - "); |
ftagius | 32:a2472bbe7c92 | 212 | ShowDateTime(); |
ftagius | 32:a2472bbe7c92 | 213 | // 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); |
ftagius | 32:a2472bbe7c92 | 214 | } |
ftagius | 32:a2472bbe7c92 | 215 | #endif |
ftagius | 32:a2472bbe7c92 | 216 | |
ftagius | 32:a2472bbe7c92 | 217 | } |
ftagius | 32:a2472bbe7c92 | 218 | else |
ftagius | 32:a2472bbe7c92 | 219 | { |
ftagius | 32:a2472bbe7c92 | 220 | gpsFix = false; |
ftagius | 32:a2472bbe7c92 | 221 | cLCD.setCursor(0,1); |
ftagius | 32:a2472bbe7c92 | 222 | cLCD.printf("No GPS. per:%d ",per); |
ftagius | 32:a2472bbe7c92 | 223 | //pc.printf("Waiting for GPS FIX\r\n"); |
ftagius | 32:a2472bbe7c92 | 224 | |
ftagius | 32:a2472bbe7c92 | 225 | } |
ftagius | 32:a2472bbe7c92 | 226 | |
ftagius | 32:a2472bbe7c92 | 227 | refresh_Timer.reset(); |
ftagius | 32:a2472bbe7c92 | 228 | } |
ftagius | 32:a2472bbe7c92 | 229 | |
ftagius | 29:0ea07cc7124b | 230 | switch (app) { |
ftagius | 29:0ea07cc7124b | 231 | case APP_PING: |
ftagius | 29:0ea07cc7124b | 232 | start_ping_pong(); |
ftagius | 29:0ea07cc7124b | 233 | break; |
ftagius | 29:0ea07cc7124b | 234 | case APP_CHAT: |
ftagius | 29:0ea07cc7124b | 235 | console_chat(); |
ftagius | 29:0ea07cc7124b | 236 | break; |
ftagius | 29:0ea07cc7124b | 237 | case APP_GPS: |
ftagius | 29:0ea07cc7124b | 238 | check_gps(); |
ftagius | 29:0ea07cc7124b | 239 | break; |
ftagius | 29:0ea07cc7124b | 240 | case APP_HELLO: |
ftagius | 29:0ea07cc7124b | 241 | start_hello(); |
ftagius | 29:0ea07cc7124b | 242 | break; |
ftagius | 29:0ea07cc7124b | 243 | case APP_CONSOLE: |
ftagius | 29:0ea07cc7124b | 244 | // printf("-chat-> x"); |
ftagius | 29:0ea07cc7124b | 245 | fflush(stdout); |
ftagius | 29:0ea07cc7124b | 246 | console(); |
ftagius | 29:0ea07cc7124b | 247 | break; |
ftagius | 29:0ea07cc7124b | 248 | default: |
ftagius | 29:0ea07cc7124b | 249 | printf("unknown app %d\r\n", app); |
ftagius | 29:0ea07cc7124b | 250 | break; |
ftagius | 29:0ea07cc7124b | 251 | } // ...switch (app) |
ftagius | 29:0ea07cc7124b | 252 | |
ftagius | 29:0ea07cc7124b | 253 | } |
ftagius | 29:0ea07cc7124b | 254 | |
wim | 0:88d87b1c1f8b | 255 | } |