standalone sx1276 demo program

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

Committer:
ftagius
Date:
Tue Jun 16 11:53:20 2015 +0000
Revision:
29:0ea07cc7124b
Parent:
28:fbcd3bac0cd7
Child:
31:2c813f321db7
working version of the sx1276 radio with integrated gps and lcd;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:88d87b1c1f8b 1 #include "mbed.h"
vtraveller 25:24654d08a99a 2 #include "lcdadafruit.h"
vtraveller 26:0cfd95d8f270 3 #include "keyreaderadafruit.h"
vtraveller 25:24654d08a99a 4 #include "keyreadernull.h"
vtraveller 10:3fcab08717fc 5 #include "RTclock.h"
vtraveller 10:3fcab08717fc 6 #include "DateModule.h"
vtraveller 10:3fcab08717fc 7 #include "TempModule.h"
vtraveller 10:3fcab08717fc 8 #include "TimeModule.h"
vtraveller 10:3fcab08717fc 9 #include "TitleModule.h"
vtraveller 17:731a47339cb8 10 #include "SyncModule.h"
ftagius 29:0ea07cc7124b 11 #include "GPSModule.h"
ftagius 29:0ea07cc7124b 12 #include "LatitudeModule.h"
ftagius 29:0ea07cc7124b 13 #include "RadioModule.h"
ftagius 29:0ea07cc7124b 14 #include "MenuManager.h"
ftagius 29:0ea07cc7124b 15 #include "sx1276-hal.h"
ftagius 29:0ea07cc7124b 16 #include "GPS.h"
ftagius 29:0ea07cc7124b 17 #include "main.h"
ftagius 29:0ea07cc7124b 18 #include "debug.h"
ftagius 29:0ea07cc7124b 19 #include "vt100.h"
ftagius 29:0ea07cc7124b 20 #include "serial_api.h"
wim 0:88d87b1c1f8b 21
ftagius 29:0ea07cc7124b 22
ftagius 29:0ea07cc7124b 23 /* Set this flag to '1' to display debug messages on the console */
ftagius 29:0ea07cc7124b 24 #define DEBUG_MESSAGE 1
ftagius 29:0ea07cc7124b 25 #define USE_MODEM_LORA 1
ftagius 29:0ea07cc7124b 26
ftagius 29:0ea07cc7124b 27 //#define RF_FREQUENCY 868000000 // Hz
ftagius 29:0ea07cc7124b 28 //#define RF_FREQUENCY 880030000
ftagius 29:0ea07cc7124b 29 //#define RF_FREQUENCY 915000000.0 // Hz
ftagius 29:0ea07cc7124b 30 #define RF_FREQUENCY 413000000.0 // Hz
ftagius 29:0ea07cc7124b 31 #define TX_OUTPUT_POWER 20 // 14 dBm
ftagius 29:0ea07cc7124b 32 #define LORA_BANDWIDTH 2 // [0: 125 kHz,
ftagius 29:0ea07cc7124b 33 // 1: 250 kHz,
ftagius 29:0ea07cc7124b 34 // 2: 500 kHz,
ftagius 29:0ea07cc7124b 35 // 3: Reserved]
ftagius 29:0ea07cc7124b 36 // #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
ftagius 29:0ea07cc7124b 37 #define LORA_SPREADING_FACTOR 12 // [SF7..SF12]
ftagius 29:0ea07cc7124b 38 // #define LORA_CODINGRATE 1 // [1: 4/5,
ftagius 29:0ea07cc7124b 39 #define LORA_CODINGRATE 2 // [1: 4/5,
ftagius 29:0ea07cc7124b 40 // 2: 4/6,
ftagius 29:0ea07cc7124b 41 // 3: 4/7,
ftagius 29:0ea07cc7124b 42 // 4: 4/8]
ftagius 29:0ea07cc7124b 43 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
ftagius 29:0ea07cc7124b 44 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
ftagius 29:0ea07cc7124b 45 #define LORA_FIX_LENGTH_PAYLOAD_ON false
ftagius 29:0ea07cc7124b 46 #define LORA_FHSS_ENABLED false
ftagius 29:0ea07cc7124b 47 #define LORA_NB_SYMB_HOP 4
ftagius 29:0ea07cc7124b 48 #define LORA_IQ_INVERSION_ON false
ftagius 29:0ea07cc7124b 49 #define LORA_CRC_ENABLED true
ftagius 29:0ea07cc7124b 50
ftagius 29:0ea07cc7124b 51 //#define BUFFER_SIZE 64 // Define the payload size here
ftagius 29:0ea07cc7124b 52
ftagius 29:0ea07cc7124b 53 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
ftagius 29:0ea07cc7124b 54 DigitalOut led(LED2);
ftagius 29:0ea07cc7124b 55 //DigitalOut led(LED_RED);
ftagius 29:0ea07cc7124b 56 #else
ftagius 29:0ea07cc7124b 57 DigitalOut led(LED1);
ftagius 29:0ea07cc7124b 58 #endif
ftagius 29:0ea07cc7124b 59 Serial pc(USBTX, USBRX);
ftagius 29:0ea07cc7124b 60
ftagius 29:0ea07cc7124b 61 float Frequency = RF_FREQUENCY;
ftagius 29:0ea07cc7124b 62 int TxPower = TX_OUTPUT_POWER;
ftagius 29:0ea07cc7124b 63 int Bandwidth = LORA_BANDWIDTH;
ftagius 29:0ea07cc7124b 64 int SpreadingFactor = LORA_SPREADING_FACTOR;
ftagius 29:0ea07cc7124b 65 int CodingRate = LORA_CODINGRATE;
ftagius 29:0ea07cc7124b 66
ftagius 29:0ea07cc7124b 67 /*
ftagius 29:0ea07cc7124b 68 * Global variables declarations
ftagius 29:0ea07cc7124b 69 */
ftagius 29:0ea07cc7124b 70 typedef RadioState States_t;
ftagius 29:0ea07cc7124b 71 volatile States_t State = LOWPOWER;
ftagius 29:0ea07cc7124b 72 vt100 ctrl;
ftagius 29:0ea07cc7124b 73
ftagius 29:0ea07cc7124b 74 SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, NULL );
ftagius 29:0ea07cc7124b 75 // for hand wired I2C cI2C(PTC9, PTC8);
ftagius 29:0ea07cc7124b 76 I2C cI2C(PTC9, PTC8);
ftagius 29:0ea07cc7124b 77 // for stacked shield I2C cI2C(PTC2, PTC1);
ftagius 29:0ea07cc7124b 78 //I2C cI2C(PTC2, PTC1);
ftagius 29:0ea07cc7124b 79 LCDadafruit cLCD(cI2C);
ftagius 29:0ea07cc7124b 80 GPS gpsd(PTE0,PTE1);
ftagius 29:0ea07cc7124b 81
ftagius 29:0ea07cc7124b 82 // for other board GPS gpsd(PTE20, PTE21);
ftagius 29:0ea07cc7124b 83
ftagius 29:0ea07cc7124b 84 uint8_t PingMsg[256];
ftagius 29:0ea07cc7124b 85 uint8_t PongMsg[256];
ftagius 29:0ea07cc7124b 86 uint8_t HelloMsg[256];
ftagius 29:0ea07cc7124b 87 char pcbuf[PCBUF_SIZE];
ftagius 29:0ea07cc7124b 88 uint16_t BufferSize = BUFFER_SIZE;
ftagius 29:0ea07cc7124b 89 uint8_t BufferTx[BUFFER_SIZE];
ftagius 29:0ea07cc7124b 90 uint8_t BufferRx[BUFFER_SIZE];
ftagius 29:0ea07cc7124b 91 int16_t RssiValue = 0.0;
ftagius 29:0ea07cc7124b 92 int8_t SnrValue = 0.0;
ftagius 29:0ea07cc7124b 93 int txLen = 0;
ftagius 29:0ea07cc7124b 94 int pkt_count = 0;
ftagius 29:0ea07cc7124b 95 int max_pkts = 20;
ftagius 29:0ea07cc7124b 96 int pkt_data[20];
ftagius 29:0ea07cc7124b 97 int per = 0;
ftagius 29:0ea07cc7124b 98 app_e app = APP_NONE;
ftagius 29:0ea07cc7124b 99 bool isMaster = true;
ftagius 29:0ea07cc7124b 100 bool AlwaysMaster = false;
ftagius 29:0ea07cc7124b 101 bool AlwaysSlave = false;
ftagius 29:0ea07cc7124b 102 bool ackRcvd = true;
ftagius 29:0ea07cc7124b 103 bool rxTimeout = false;
ftagius 29:0ea07cc7124b 104 bool gpsEnabled = true;
ftagius 29:0ea07cc7124b 105
ftagius 29:0ea07cc7124b 106 char* itoa(int val, int base){
ftagius 29:0ea07cc7124b 107
ftagius 29:0ea07cc7124b 108 static char buf[32] = {0};
ftagius 29:0ea07cc7124b 109
ftagius 29:0ea07cc7124b 110 int i = 30;
ftagius 29:0ea07cc7124b 111
ftagius 29:0ea07cc7124b 112 for(; val && i ; --i, val /= base)
ftagius 29:0ea07cc7124b 113
ftagius 29:0ea07cc7124b 114 buf[i] = "0123456789abcdef"[val % base];
ftagius 29:0ea07cc7124b 115
ftagius 29:0ea07cc7124b 116 return &buf[i+1];
ftagius 29:0ea07cc7124b 117
ftagius 29:0ea07cc7124b 118 }
ftagius 29:0ea07cc7124b 119
ftagius 29:0ea07cc7124b 120 unsigned int randomSeed(){
ftagius 29:0ea07cc7124b 121 AnalogIn randIn(A0); // analog input for random number seed
ftagius 29:0ea07cc7124b 122 unsigned int rnum = 0; // int = 4 bytes - 32 bits
ftagius 29:0ea07cc7124b 123 // build 32 bit random number from the 2 lower bits from 16 analog reads
ftagius 29:0ea07cc7124b 124 for (int i = 0; i<4;i++){
ftagius 29:0ea07cc7124b 125 rnum = rnum | ((randIn.read_u16() & 15) << i*4);
ftagius 29:0ea07cc7124b 126 wait_ms(5);
ftagius 29:0ea07cc7124b 127 }
ftagius 29:0ea07cc7124b 128 return rnum;
ftagius 29:0ea07cc7124b 129 }
ftagius 29:0ea07cc7124b 130
ftagius 29:0ea07cc7124b 131 void configRxTx() {
ftagius 29:0ea07cc7124b 132
ftagius 29:0ea07cc7124b 133
ftagius 29:0ea07cc7124b 134 Radio.SetTxConfig( MODEM_LORA, TxPower, 0, Bandwidth,
ftagius 29:0ea07cc7124b 135 SpreadingFactor, CodingRate,
ftagius 29:0ea07cc7124b 136 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
ftagius 29:0ea07cc7124b 137 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
ftagius 29:0ea07cc7124b 138 LORA_IQ_INVERSION_ON, 2000000 );
ftagius 29:0ea07cc7124b 139
ftagius 29:0ea07cc7124b 140 Radio.SetRxConfig( MODEM_LORA, Bandwidth, SpreadingFactor,
ftagius 29:0ea07cc7124b 141 CodingRate, 0, LORA_PREAMBLE_LENGTH,
ftagius 29:0ea07cc7124b 142 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
ftagius 29:0ea07cc7124b 143 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
ftagius 29:0ea07cc7124b 144 LORA_IQ_INVERSION_ON, true );
ftagius 29:0ea07cc7124b 145 }
ftagius 29:0ea07cc7124b 146
ftagius 29:0ea07cc7124b 147 void print_bandwidth()
ftagius 29:0ea07cc7124b 148 {
ftagius 29:0ea07cc7124b 149 printf("bandwidth: ");
ftagius 29:0ea07cc7124b 150 switch (((Radio.Read(REG_LR_MODEMCONFIG1) & 0xf0) >> 4)) {
ftagius 29:0ea07cc7124b 151 case 0: printf("7.8KHz "); break;
ftagius 29:0ea07cc7124b 152 case 1: printf("10.4KHz "); break;
ftagius 29:0ea07cc7124b 153 case 2: printf("15.6KHz "); break;
ftagius 29:0ea07cc7124b 154 case 3: printf("20.8KHz "); break;
ftagius 29:0ea07cc7124b 155 case 4: printf("31.25KHz "); break;
ftagius 29:0ea07cc7124b 156 case 5: printf("41.7KHz "); break;
ftagius 29:0ea07cc7124b 157 case 6: printf("62.5KHz "); break;
ftagius 29:0ea07cc7124b 158 case 7: printf("125KHz "); break;
ftagius 29:0ea07cc7124b 159 case 8: printf("250KHz "); break;
ftagius 29:0ea07cc7124b 160 case 9: printf("500KHz "); break;
ftagius 29:0ea07cc7124b 161 default: printf("%x ", Radio.Read(REG_LR_MODEMCONFIG1)); break;
ftagius 29:0ea07cc7124b 162 }
ftagius 29:0ea07cc7124b 163 //printf("\r\n");
ftagius 29:0ea07cc7124b 164 }
ftagius 29:0ea07cc7124b 165
ftagius 29:0ea07cc7124b 166 void print_cr()
ftagius 29:0ea07cc7124b 167 {
ftagius 29:0ea07cc7124b 168 int cr = (Radio.Read(REG_LR_MODEMCONFIG1) & 0x0f)>>1;
ftagius 29:0ea07cc7124b 169 printf("coding rate: ");
ftagius 29:0ea07cc7124b 170 switch (cr)
ftagius 29:0ea07cc7124b 171 {
ftagius 29:0ea07cc7124b 172 case 1:
ftagius 29:0ea07cc7124b 173 printf("4/5");
ftagius 29:0ea07cc7124b 174 break;
ftagius 29:0ea07cc7124b 175 case 2:
ftagius 29:0ea07cc7124b 176 printf("4/6");
ftagius 29:0ea07cc7124b 177 break;
ftagius 29:0ea07cc7124b 178 case 3:
ftagius 29:0ea07cc7124b 179 printf("4/7");
ftagius 29:0ea07cc7124b 180 break;
ftagius 29:0ea07cc7124b 181 case 4:
ftagius 29:0ea07cc7124b 182 printf("4/8");
ftagius 29:0ea07cc7124b 183 break;
ftagius 29:0ea07cc7124b 184 default:
ftagius 29:0ea07cc7124b 185 printf("unknown");
ftagius 29:0ea07cc7124b 186 break;
ftagius 29:0ea07cc7124b 187
ftagius 29:0ea07cc7124b 188 }
ftagius 29:0ea07cc7124b 189 printf(" ");
ftagius 29:0ea07cc7124b 190 }
ftagius 29:0ea07cc7124b 191
ftagius 29:0ea07cc7124b 192 void print_power()
ftagius 29:0ea07cc7124b 193 {
ftagius 29:0ea07cc7124b 194 uint8_t paConfig = 0;
ftagius 29:0ea07cc7124b 195 uint8_t paDac = 0;
ftagius 29:0ea07cc7124b 196
ftagius 29:0ea07cc7124b 197 paConfig = Radio.Read( REG_PACONFIG );
ftagius 29:0ea07cc7124b 198 paDac = Radio.Read( REG_PADAC );
ftagius 29:0ea07cc7124b 199
ftagius 29:0ea07cc7124b 200 paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK ) | Radio.GetPaSelect( Radio.GetChannel()*1000000);
ftagius 29:0ea07cc7124b 201 paConfig = ( paConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
ftagius 29:0ea07cc7124b 202 printf("channel = %f getpa = %x, paConfig =%x \r\n",Radio.GetChannel(), Radio.GetPaSelect((uint32_t)Radio.GetChannel()*1000000), paConfig );
ftagius 29:0ea07cc7124b 203 if( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
ftagius 29:0ea07cc7124b 204 {
ftagius 29:0ea07cc7124b 205 if( ( paDac & RF_PADAC_20DBM_ON ) == RF_PADAC_20DBM_ON )
ftagius 29:0ea07cc7124b 206 {
ftagius 29:0ea07cc7124b 207 printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f ) + 5 );
ftagius 29:0ea07cc7124b 208 }
ftagius 29:0ea07cc7124b 209 else
ftagius 29:0ea07cc7124b 210 printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) &0x0f) + 2 );
ftagius 29:0ea07cc7124b 211 }
ftagius 29:0ea07cc7124b 212 else
ftagius 29:0ea07cc7124b 213 {
ftagius 29:0ea07cc7124b 214 printf("Power: %d dBm ", ( uint8_t )( ( uint16_t )( paConfig) & 0x0f ) - 1 );
ftagius 29:0ea07cc7124b 215 }
ftagius 29:0ea07cc7124b 216
ftagius 29:0ea07cc7124b 217 }
ftagius 29:0ea07cc7124b 218
ftagius 29:0ea07cc7124b 219 void print_errata()
ftagius 29:0ea07cc7124b 220 {
ftagius 29:0ea07cc7124b 221
ftagius 29:0ea07cc7124b 222 int seqconfig1 = (Radio.Read(REG_SEQCONFIG1));
ftagius 29:0ea07cc7124b 223 int timer2 = (Radio.Read(REG_TIMER2COEF));
ftagius 29:0ea07cc7124b 224 printf("seqconfig1 = 0x%02x\r\n", seqconfig1);
ftagius 29:0ea07cc7124b 225 printf("timer2coef = 0x%02x\r\n", timer2);
ftagius 29:0ea07cc7124b 226
ftagius 29:0ea07cc7124b 227 }
ftagius 29:0ea07cc7124b 228 void print_status()
ftagius 29:0ea07cc7124b 229 {
ftagius 29:0ea07cc7124b 230
ftagius 29:0ea07cc7124b 231 printf("Radio version: 0x%02x Channel: %.1f MHz ", \
ftagius 29:0ea07cc7124b 232 Radio.Read(REG_VERSION),\
ftagius 29:0ea07cc7124b 233 Radio.GetChannel()\
ftagius 29:0ea07cc7124b 234 );
ftagius 29:0ea07cc7124b 235 print_power();
ftagius 29:0ea07cc7124b 236 print_bandwidth(); printf("\r\n");
ftagius 29:0ea07cc7124b 237 printf("Spreading Factor: %d ",SpreadingFactor);
ftagius 29:0ea07cc7124b 238 print_cr();
ftagius 29:0ea07cc7124b 239 if (isMaster)
ftagius 29:0ea07cc7124b 240 printf("Mode: master ");
ftagius 29:0ea07cc7124b 241 else
ftagius 29:0ea07cc7124b 242 printf("Mode: slave ");
ftagius 29:0ea07cc7124b 243
ftagius 29:0ea07cc7124b 244
ftagius 29:0ea07cc7124b 245 printf("\r\n");
ftagius 29:0ea07cc7124b 246 print_errata();
ftagius 29:0ea07cc7124b 247 printf("\r\n");
ftagius 29:0ea07cc7124b 248
ftagius 29:0ea07cc7124b 249 }
ftagius 29:0ea07cc7124b 250
ftagius 29:0ea07cc7124b 251
ftagius 29:0ea07cc7124b 252 void OnTxDone( void )
ftagius 29:0ea07cc7124b 253 {
ftagius 29:0ea07cc7124b 254 Radio.Sleep( );
ftagius 29:0ea07cc7124b 255 State = TX;
ftagius 29:0ea07cc7124b 256 pkt_count++;
ftagius 29:0ea07cc7124b 257
ftagius 29:0ea07cc7124b 258
ftagius 29:0ea07cc7124b 259 //debug_if( DEBUG_MESSAGE, "> OnTxDone\r\n" );
ftagius 29:0ea07cc7124b 260 }
ftagius 29:0ea07cc7124b 261
ftagius 29:0ea07cc7124b 262 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
ftagius 29:0ea07cc7124b 263 {
ftagius 29:0ea07cc7124b 264 Radio.Sleep( );
ftagius 29:0ea07cc7124b 265 if (pkt_count >= max_pkts)
ftagius 29:0ea07cc7124b 266 {
ftagius 29:0ea07cc7124b 267 pkt_count=0;
ftagius 29:0ea07cc7124b 268 }
ftagius 29:0ea07cc7124b 269
ftagius 29:0ea07cc7124b 270 // mark the rolling avg that a pkt has been received
ftagius 29:0ea07cc7124b 271 pkt_data[pkt_count]=1;
ftagius 29:0ea07cc7124b 272 //printf("rx done, pkt_count = %d\r\n", pkt_count);
ftagius 29:0ea07cc7124b 273
ftagius 29:0ea07cc7124b 274 BufferSize = size;
ftagius 29:0ea07cc7124b 275 int i;
ftagius 29:0ea07cc7124b 276 for (i=0; i< BUFFER_SIZE; i++)
ftagius 29:0ea07cc7124b 277 BufferRx[ i ] = '\0'; // clear the buffer
ftagius 29:0ea07cc7124b 278
ftagius 29:0ea07cc7124b 279 memcpy( BufferRx, payload, BufferSize );
ftagius 29:0ea07cc7124b 280 if (rssi > 100)
ftagius 29:0ea07cc7124b 281 {
ftagius 29:0ea07cc7124b 282 rssi *= -1;
ftagius 29:0ea07cc7124b 283 }
ftagius 29:0ea07cc7124b 284 RssiValue = rssi;
ftagius 29:0ea07cc7124b 285 SnrValue = snr;
ftagius 29:0ea07cc7124b 286 State = RX;
ftagius 29:0ea07cc7124b 287 rxTimeout = false;
ftagius 29:0ea07cc7124b 288 if (app == APP_PING)
ftagius 29:0ea07cc7124b 289 debug_if( DEBUG_MESSAGE, "> OnRxDone size=%d rssi=%d snr=%d \r\n", size, rssi, snr );
ftagius 29:0ea07cc7124b 290
ftagius 29:0ea07cc7124b 291 }
ftagius 29:0ea07cc7124b 292
ftagius 29:0ea07cc7124b 293 void OnTxTimeout( void )
ftagius 29:0ea07cc7124b 294 {
ftagius 29:0ea07cc7124b 295 Radio.Sleep( );
ftagius 29:0ea07cc7124b 296 State = TX_TIMEOUT;
ftagius 29:0ea07cc7124b 297 //debug_if( DEBUG_MESSAGE, "> OnTxTimeout\r\n" );
ftagius 29:0ea07cc7124b 298 }
ftagius 29:0ea07cc7124b 299
ftagius 29:0ea07cc7124b 300 void OnRxTimeout( void )
ftagius 29:0ea07cc7124b 301 {
ftagius 29:0ea07cc7124b 302 Radio.Sleep( );
ftagius 29:0ea07cc7124b 303 int i;
ftagius 29:0ea07cc7124b 304 if (pkt_count >= max_pkts)
ftagius 29:0ea07cc7124b 305 {
ftagius 29:0ea07cc7124b 306 pkt_count=0;
ftagius 29:0ea07cc7124b 307 }
ftagius 29:0ea07cc7124b 308
ftagius 29:0ea07cc7124b 309 // mark the rolling avg that a pkt has not been received
ftagius 29:0ea07cc7124b 310 pkt_data[pkt_count]=2;
ftagius 29:0ea07cc7124b 311
ftagius 29:0ea07cc7124b 312 for (i=0; i< BUFFER_SIZE; i++)
ftagius 29:0ea07cc7124b 313 BufferRx[ i ] = '\0'; // clear the buffer
ftagius 29:0ea07cc7124b 314 BufferSize = 0;
ftagius 29:0ea07cc7124b 315 State = RX_TIMEOUT;
ftagius 29:0ea07cc7124b 316 rxTimeout = false;
ftagius 29:0ea07cc7124b 317 // debug_if( DEBUG_MESSAGE, "> OnRxTimeout\r\n" );
ftagius 29:0ea07cc7124b 318 }
ftagius 29:0ea07cc7124b 319
ftagius 29:0ea07cc7124b 320 void OnRxError( void )
ftagius 29:0ea07cc7124b 321 {
ftagius 29:0ea07cc7124b 322 Radio.Sleep( );
ftagius 29:0ea07cc7124b 323 if (pkt_count >= max_pkts)
ftagius 29:0ea07cc7124b 324 {
ftagius 29:0ea07cc7124b 325 pkt_count=0;
ftagius 29:0ea07cc7124b 326 }
ftagius 29:0ea07cc7124b 327
ftagius 29:0ea07cc7124b 328 // mark the rolling avg that a pkt has not been received
ftagius 29:0ea07cc7124b 329 pkt_data[pkt_count]=2;
ftagius 29:0ea07cc7124b 330 State = RX_ERROR;
ftagius 29:0ea07cc7124b 331 debug_if( DEBUG_MESSAGE, "> OnRxError\r\n" );
ftagius 29:0ea07cc7124b 332 }
ftagius 29:0ea07cc7124b 333
vtraveller 10:3fcab08717fc 334
vtraveller 3:ed09f95739df 335 int main()
vtraveller 21:c44cfd3259c0 336 {
ftagius 29:0ea07cc7124b 337
ftagius 29:0ea07cc7124b 338 int i;
ftagius 29:0ea07cc7124b 339 gpsd.setBaud57600();
ftagius 29:0ea07cc7124b 340 pc.baud(9600);
ftagius 29:0ea07cc7124b 341 //pc.baud(57600);
vtraveller 21:c44cfd3259c0 342 cI2C.frequency(400000); // I2C can handle two different frequencies - switch to high speed if asked
ftagius 29:0ea07cc7124b 343 cLCD.clear();
ftagius 29:0ea07cc7124b 344 cLCD.home();
ftagius 29:0ea07cc7124b 345 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 346 cLCD.printf(" HOMER ");
ftagius 29:0ea07cc7124b 347 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 348 cLCD.printf(" DOH! ");
ftagius 29:0ea07cc7124b 349
ftagius 29:0ea07cc7124b 350 rxTimeout = false;
ftagius 29:0ea07cc7124b 351 // PTD1 (the SCK pin for the one SPI interface onboard the KL25z) is actually an output to the Blue LED.
ftagius 29:0ea07cc7124b 352 // the lora shield drives SCK, which turns on the blue led. use PTC5 instead of the default PTD1 for SCK
ftagius 29:0ea07cc7124b 353
ftagius 29:0ea07cc7124b 354 if (RADIO_INSTALLED)
ftagius 29:0ea07cc7124b 355 debug( " SX1276 Test Applications \r\n\n" );
ftagius 29:0ea07cc7124b 356 led = 1;
ftagius 29:0ea07cc7124b 357
ftagius 29:0ea07cc7124b 358 // init pkt_data array to 0
ftagius 29:0ea07cc7124b 359 for (i=0;i<pkt_count;i++)
ftagius 29:0ea07cc7124b 360 pkt_data[pkt_count]=0;
ftagius 29:0ea07cc7124b 361
ftagius 29:0ea07cc7124b 362 // verify the connection with the board
ftagius 29:0ea07cc7124b 363 if (RADIO_INSTALLED)
ftagius 29:0ea07cc7124b 364 {
ftagius 29:0ea07cc7124b 365
ftagius 29:0ea07cc7124b 366 while( Radio.Read( REG_VERSION ) == 0x00 )
ftagius 29:0ea07cc7124b 367 {
ftagius 29:0ea07cc7124b 368 debug( "Radio could not be detected!\r\n", NULL );
ftagius 29:0ea07cc7124b 369 wait( 1 );
ftagius 29:0ea07cc7124b 370 }
ftagius 29:0ea07cc7124b 371
ftagius 29:0ea07cc7124b 372
ftagius 29:0ea07cc7124b 373 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "> Board Type: SX1276MB1LAS < \r\n" );
ftagius 29:0ea07cc7124b 374 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "> Board Type: SX1276MB1MAS < \r\n" );
ftagius 29:0ea07cc7124b 375 Radio.SetChannel( Frequency );
ftagius 29:0ea07cc7124b 376 debug_if( !LORA_FHSS_ENABLED, "> LORA Mode < \r\n");
ftagius 29:0ea07cc7124b 377 configRxTx();
ftagius 29:0ea07cc7124b 378
ftagius 29:0ea07cc7124b 379 //print_status();
ftagius 29:0ea07cc7124b 380 debug_if( DEBUG_MESSAGE, "Starting Application loop\r\n" );
ftagius 29:0ea07cc7124b 381 wait(1);
ftagius 29:0ea07cc7124b 382
ftagius 29:0ea07cc7124b 383 // led = 0;
ftagius 29:0ea07cc7124b 384
ftagius 29:0ea07cc7124b 385 strcat((char *)PingMsg,"PING");
ftagius 29:0ea07cc7124b 386 strcat((char *)PongMsg,"PONG");
ftagius 29:0ea07cc7124b 387 strcat((char *)HelloMsg,"HELLO_7");
ftagius 29:0ea07cc7124b 388
ftagius 29:0ea07cc7124b 389 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 390 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 391
ftagius 29:0ea07cc7124b 392 app = APP_HELLO;
ftagius 29:0ea07cc7124b 393 }
ftagius 29:0ea07cc7124b 394 else
ftagius 29:0ea07cc7124b 395 {
ftagius 29:0ea07cc7124b 396 app = APP_GPS;
ftagius 29:0ea07cc7124b 397 pc.printf("Starting GPS App\r\n");
ftagius 29:0ea07cc7124b 398 }
ftagius 29:0ea07cc7124b 399
ftagius 29:0ea07cc7124b 400 while( 0 )
ftagius 29:0ea07cc7124b 401 {
ftagius 29:0ea07cc7124b 402 static int count =1;
ftagius 29:0ea07cc7124b 403 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 404 wait(1);
ftagius 29:0ea07cc7124b 405 cLCD.printf("%d ",count);
ftagius 29:0ea07cc7124b 406 count++;
vtraveller 25:24654d08a99a 407
ftagius 29:0ea07cc7124b 408 }
ftagius 29:0ea07cc7124b 409 while( 1 )
ftagius 29:0ea07cc7124b 410 {
ftagius 29:0ea07cc7124b 411 // wait_ms( 50 );
ftagius 29:0ea07cc7124b 412 switch (app) {
ftagius 29:0ea07cc7124b 413 case APP_PING:
ftagius 29:0ea07cc7124b 414 start_ping_pong();
ftagius 29:0ea07cc7124b 415 break;
ftagius 29:0ea07cc7124b 416 case APP_CHAT:
ftagius 29:0ea07cc7124b 417 console_chat();
ftagius 29:0ea07cc7124b 418 break;
ftagius 29:0ea07cc7124b 419 case APP_GPS:
ftagius 29:0ea07cc7124b 420 check_gps();
ftagius 29:0ea07cc7124b 421 break;
ftagius 29:0ea07cc7124b 422 case APP_HELLO:
ftagius 29:0ea07cc7124b 423 start_hello();
ftagius 29:0ea07cc7124b 424 wait(0.11);
ftagius 29:0ea07cc7124b 425 break;
ftagius 29:0ea07cc7124b 426 case APP_CONSOLE:
ftagius 29:0ea07cc7124b 427 // printf("-chat-> x");
ftagius 29:0ea07cc7124b 428 fflush(stdout);
ftagius 29:0ea07cc7124b 429 console();
ftagius 29:0ea07cc7124b 430 break;
ftagius 29:0ea07cc7124b 431 default:
ftagius 29:0ea07cc7124b 432 printf("unknown app %d\r\n", app);
ftagius 29:0ea07cc7124b 433 break;
ftagius 29:0ea07cc7124b 434 } // ...switch (app)
ftagius 29:0ea07cc7124b 435
ftagius 29:0ea07cc7124b 436 }
ftagius 29:0ea07cc7124b 437
ftagius 29:0ea07cc7124b 438
vtraveller 26:0cfd95d8f270 439 //KeyReaderNull cKeys(cI2C);
ftagius 29:0ea07cc7124b 440 #if 0
vtraveller 26:0cfd95d8f270 441 KeyReaderAdafruit cKeys(cI2C);
vtraveller 20:93c70a1869ee 442
vtraveller 23:94d2f0d62247 443 RTclock cClock(cI2C, 0x68 << 1, RTclock::eDS3231);
vtraveller 13:9641bc42db92 444
vtraveller 13:9641bc42db92 445 // Spin up RTC
vtraveller 19:72c02acb601d 446 cClock.mapTime();
ftagius 29:0ea07cc7124b 447
vtraveller 10:3fcab08717fc 448 // Set up display modules
vtraveller 10:3fcab08717fc 449 Module * aModules[] =
vtraveller 10:3fcab08717fc 450 {
ftagius 29:0ea07cc7124b 451 //new TitleModule(cLCD,cClock),
ftagius 29:0ea07cc7124b 452 // new TempModule(cLCD,cI2C,0x18 << 1),
ftagius 29:0ea07cc7124b 453 // new TimeModule(cLCD,cClock),
ftagius 29:0ea07cc7124b 454 // new DateModule(cLCD,cClock),
ftagius 29:0ea07cc7124b 455 new RadioModule(cLCD,cClock),
ftagius 29:0ea07cc7124b 456 new LatitudeModule(cLCD,cClock),
ftagius 29:0ea07cc7124b 457 new GPSModule(cLCD,cClock),
ftagius 29:0ea07cc7124b 458
vtraveller 10:3fcab08717fc 459 };
ftagius 29:0ea07cc7124b 460
vtraveller 10:3fcab08717fc 461
vtraveller 13:9641bc42db92 462 // Set up the menu manager
vtraveller 25:24654d08a99a 463 // MenuManager cMenuManager(aModules,_countof(aModules),cLCD,cKeys,16,2);
vtraveller 25:24654d08a99a 464 MenuManager cMenuManager(aModules,_countof(aModules),cLCD,cKeys);
vtraveller 13:9641bc42db92 465
ftagius 29:0ea07cc7124b 466
ftagius 29:0ea07cc7124b 467 // Start menu manager loop
ftagius 29:0ea07cc7124b 468
ftagius 29:0ea07cc7124b 469
ftagius 29:0ea07cc7124b 470
ftagius 29:0ea07cc7124b 471
ftagius 29:0ea07cc7124b 472 cMenuManager.loop();
ftagius 29:0ea07cc7124b 473 #endif
ftagius 29:0ea07cc7124b 474
wim 0:88d87b1c1f8b 475 }