Long Range / Mbed 2 deprecated SX1272_LoRaWAN_App_LR

Dependencies:   X_NUCLEO_IKS01A2 driver_mbed_TH02 mbed LoRaWAN-lib-v1_0_1 SX1272Lib

Fork of Training-Aug2018-SX1272-X-NUCLEO-IKS01A2 by Uttam Bhat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaEventProc.cpp Source File

LoRaEventProc.cpp

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2015 Semtech
00008 
00009 Description: Define events during Join, Tx & Rx
00010             Prepare TX packet by appending with appropriate application data
00011 
00012 License: Revised BSD License, see LICENSE.TXT file include in the project
00013 
00014 Maintainer: Uttam Bhat
00015 */
00016 
00017 #include "LoRaEventProc.h"
00018 
00019 /*!
00020  * Defines the application data transmission duty cycle
00021  */
00022 uint32_t TxDutyCycleTime = APP_TX_DUTYCYCLE;
00023 
00024 bool AppLed = 0;
00025 
00026 uint8_t AppCase = 0;
00027 
00028 /*!
00029  * \brief   Prepares the payload of the frame based on application port
00030  */
00031 void PrepareLoRaFrame( uint8_t port )
00032 {
00033 
00034     switch( port ) {
00035         case 5: {
00036             uint8_t tmp;
00037             uint8_t tmpLength;
00038             uint8_t ptrIndex = 0;
00039 
00040             // Point the pointer to position index of Tx Buffer
00041             LoRaApp.ApplicationPtrPos( ptrIndex );
00042 
00043             tmp = ( AppLed != 0 ) ? 0x0F : 0x00;
00044             tmpLength = 1;
00045 
00046             LoRaApp.ApplicationAppendData( &tmp, tmpLength ); // Populate lower nibble of 0th Byte with LED state
00047 
00048             /*!
00049             *  Generate Ramp data bytes
00050             *  Appends incremental values of 1 Byte each to TX buffer until Full
00051             */
00052             LoRaApp.ApplicationCall( AppRamp );
00053 
00054             break;
00055         }
00056 
00057         case 8: {
00058             uint8_t ptrIndex = 0;
00059 
00060             //Point the pointer to position index of Tx Buffer
00061             LoRaApp.ApplicationPtrPos( ptrIndex );
00062 
00063             /*!
00064             *  Read Pressure
00065             *  Appends 2 Bytes to TX buffer
00066             */
00067             LoRaApp.ApplicationCall( AppPressr );
00068 
00069             /*!
00070             *  Read Temperature
00071             *  Appends 1 Byte to TX buffer
00072             */
00073             LoRaApp.ApplicationCall( AppTemp );
00074 
00075             /*!
00076             *  Read Humidity
00077             *  Appends 1 Byte to TX buffer
00078             */
00079             LoRaApp.ApplicationCall( AppHumid );
00080 
00081             /*!
00082             *  Read Accelerometer
00083             *  Appends 2 Bytes to TX buffer
00084             *   Value       Orientation
00085             *   0x99 0x00   horizontal + faceup
00086             *   0x66 0x00   horizontal + facedown
00087             *   0x00 0x11   vertical
00088             */
00089             LoRaApp.ApplicationCall( AppAccl ); // Generate Accelerometer data bytes
00090 
00091 
00092             break;
00093         }
00094 
00095         // IKAS sensor using Cayenne Payload Format
00096         case 9: {
00097 #ifdef USE_CAYENNE_LPP
00098             uint8_t ptrIndex = 0;
00099 
00100             uint16_t AppSize = 0;
00101 
00102             uint8_t tmp[2] = {0};
00103 
00104             //Point the pointer to position index of Tx Buffer
00105             LoRaApp.ApplicationPtrPos( ptrIndex );
00106 
00107             AppCase = AppCase > 4 ? 0 : AppCase;
00108 
00109             while( 1 ) {
00110                 switch( AppCase ) {
00111                     case 0: {
00112                         tmp[0] = 0;
00113                         tmp[1] = 115; // Data Type PRESSURE: 115
00114                         LoRaApp.ApplicationAppendData( tmp, 2 );
00115 
00116                         /*!
00117                         *  Read Pressure
00118                         *  Appends 2 Bytes to TX buffer
00119                         */
00120                         LoRaApp.ApplicationCall( AppPressr );
00121 
00122                         AppSize += maxLPPsize[AppCase];
00123 
00124                         break;
00125                     }
00126 
00127                     case 1: {
00128                         tmp[0] = 1;
00129                         tmp[1] = 103; // Data Type TEMP: 103
00130                         LoRaApp.ApplicationAppendData( tmp, 2 );
00131 
00132                         /*!
00133                         *  Read Temperature
00134                         *  Appends 1 Byte to TX buffer
00135                         */
00136                         LoRaApp.ApplicationCall( AppTemp );
00137 
00138                         AppSize += maxLPPsize[AppCase];
00139 
00140                         break;
00141                     }
00142 
00143                     case 2: {
00144                         tmp[0] = 2;
00145                         tmp[1] = 104; // Data Type HUMIDITY: 103
00146                         LoRaApp.ApplicationAppendData( tmp, 2 );
00147 
00148                         /*!
00149                         *  Read Humidity
00150                         *  Appends 1 Byte to TX buffer
00151                         */
00152                         LoRaApp.ApplicationCall( AppHumid );
00153 
00154                         AppSize += maxLPPsize[AppCase];
00155 
00156                         break;
00157                     }
00158 
00159                     case 3: {
00160                         tmp[0] = 3;
00161                         tmp[1] = 113; // Data Type Accl: 113
00162                         LoRaApp.ApplicationAppendData( tmp, 2 );
00163                         /*!
00164                         *  Read Accelerometer
00165                         */
00166                         LoRaApp.ApplicationCall( AppAccl ); // Generate Accelerometer data bytes
00167 
00168                         AppSize += maxLPPsize[AppCase];
00169 
00170                         break;
00171                     }
00172                     
00173                     case 4: {
00174                         tmp[0] = 4;
00175                         tmp[1] = 134; // Data Type Accl: 113
00176                         LoRaApp.ApplicationAppendData( tmp, 2 );
00177                         /*!
00178                         *  Read Accelerometer
00179                         */
00180                         LoRaApp.ApplicationCall( AppGyro ); // Generate Accelerometer data bytes
00181 
00182                         AppSize += maxLPPsize[AppCase];
00183 
00184                         break;
00185                     }
00186                 }
00187 
00188                 AppCase++;
00189 
00190                 if( AppSize + maxLPPsize[AppCase] > LORAWAN_APP_DATA_SIZE ) {
00191                     break;
00192                 }
00193             }
00194 
00195             AppDataSize = AppSize;
00196 #endif
00197             break;
00198         }
00199 
00200         // Push-Button Demo
00201         case 11: {
00202             uint8_t ptrIndex = 0;
00203             
00204             uint8_t tmp[2] = {0};
00205 
00206             //Point the pointer to position index of Tx Buffer
00207             LoRaApp.ApplicationPtrPos( ptrIndex );
00208             
00209             tmp[0] = 0;
00210             tmp[1] = 0; 
00211             LoRaApp.ApplicationAppendData( tmp, 2 );
00212 
00213             LoRaApp.ApplicationCall( AppPushButton ); // Transmit uplink counter
00214 
00215             break;
00216         }
00217 
00218         // GROVE sensor using Cayenne Payload Format
00219         case 12: {
00220 #ifdef USE_CAYENNE_LPP
00221             uint8_t ptrIndex = 0;
00222 
00223             uint8_t tmp[2] = {0};
00224 
00225             //Point the pointer to position index of Tx Buffer
00226             LoRaApp.ApplicationPtrPos( ptrIndex );
00227 
00228             tmp[0] = 1;
00229             tmp[1] = 103; // Data Type TEMP: 103
00230             LoRaApp.ApplicationAppendData( tmp, 2 );
00231             
00232             printf("Temp Call\r\n");
00233 
00234             /*!
00235             *  Read Temperature
00236             *  Appends 1 Byte to TX buffer
00237             */
00238             LoRaApp.ApplicationCall( AppTemp );
00239 
00240             tmp[0] = 2;
00241             tmp[1] = 104; // Data Type HUMIDITY: 103
00242             LoRaApp.ApplicationAppendData( tmp, 2 );
00243             
00244             printf("Humidity Call\r\n");
00245 
00246             /*!
00247             *  Read Humidity
00248             *  Appends 1 Byte to TX buffer
00249             */
00250             LoRaApp.ApplicationCall( AppHumid );
00251 
00252 #endif
00253             break;
00254         }
00255 
00256         default:
00257             break;
00258     }
00259 }
00260 
00261 
00262 /*!
00263  * \brief   Sets Interrupt for next payload transmission
00264  */
00265 void InitNextTxInterrupt( uint8_t port )
00266 {
00267     switch( port ) {
00268             /* GPS Application Demo
00269                 Set Timer interrupt for next uplink
00270             */
00271         case 5: {
00272         }
00273 
00274         /* Senet + M2X demo
00275         Set Timer interrupt for next uplink
00276         */
00277         case 6: {
00278 
00279         }
00280 
00281         /* Senet GPS Demo
00282            Set Timer interrupt for next uplink
00283         */
00284         case 7: {
00285             // Schedule next packet transmission
00286             TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
00287             TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
00288             TimerStart( &TxNextPacketTimer );
00289             break;
00290         }
00291 
00292         /* Push Button Demo
00293            Send Packet Immedietly if PC0 = GND
00294         */
00295         case 11: {
00296             volatile bool PushButtonStatus;
00297 
00298             PushButtonStatus = UsrButton;
00299 
00300             if(PushButtonStatus == 0) {
00301                 // Send Pkt immedietly if PC = GND
00302                 DeviceState = DEVICE_STATE_SEND;
00303                 NextTx = true;
00304             } else {
00305                 // Keep polling
00306                 IsTxIntUpdate = true;
00307             }
00308 
00309             break;
00310         }
00311 
00312         /* Orientation Demo
00313            Send Packet Immedietly if Mote is Vertical
00314         */
00315         case 12: {
00316             // Schedule next packet transmission
00317             TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
00318             TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
00319             TimerStart( &TxNextPacketTimer );
00320             break;
00321         }
00322 
00323         /* Compliance Test
00324            Set Timer interrupt for next uplink
00325         */
00326         case 224: {
00327             // Schedule next packet transmission
00328             TimerSetValue( &TxNextPacketTimer, COMPLIANCE_TX_DUTYCYCLE );
00329             TimerStart( &TxNextPacketTimer );
00330             break;
00331         }
00332 
00333         default: {
00334             // Schedule next packet transmission
00335             TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
00336             TimerStart( &TxNextPacketTimer );
00337             break;
00338         }
00339     }
00340 
00341 }
00342 
00343 /*!
00344  * \brief   What to do during JOIN process ? blink/toggle LED etc.
00345  */
00346 void JoinEvent( void )
00347 {
00348     // CtrlLED is defined in LoRaBoardAppIf.h
00349     // param 1: LED color (Red, Yellow or Green)
00350     // param 2: LED_ON or LED_OFF
00351     //CtrlLED( Red, LED_ON );
00352 }
00353 
00354 
00355 /*!
00356  * \brief   What to do during TX ? blink/toggle LED etc.
00357  */
00358 void TxEvent( void )
00359 {
00360 
00361 }
00362 
00363 void RxEvent()
00364 {
00365     // Toggle yellow LED
00366     //ToggleLED( Yellow );
00367 
00368     // If Rx Data is 0x01 turn on Green LED else if 0x0 Turn Green LED off
00369     if( LoRaMacDownlinkStatus.BufferSize == 1 ) {
00370         if( LoRaMacDownlinkStatus.Buffer[0] == 0x01 ) {
00371             AppLed = 1;
00372         } else {
00373             if( LoRaMacDownlinkStatus.Buffer[0] == 0x00 ) {
00374                 AppLed = 0;
00375             }
00376         }
00377     }
00378 
00379     if( AppLed != 0 ) {
00380         // Turn  USR_LED ON
00381         //UsrLED = 3.3;
00382     } else {
00383         // Turn  USR_LED OFF
00384         //UsrLED = 0;
00385     }
00386 }
00387