Nagaraj Krishnamurthy / LoRaWAN-NAMote72-Application-Demo_IoTium

Dependencies:   LoRaWAN-lib SX1272Lib lib_gps lib_mma8451q lib_mpl3115a2 mbed

Fork of LoRaWAN-NAMote72-Application-Demo_Multitech by Nagaraj Krishnamurthy

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 /*!
00027  * \brief   Prepares the payload of the frame based on application port
00028  */
00029 void PrepareLoRaFrame( uint8_t port )
00030 {
00031 
00032     printf( "###### ===== PrepareLoRaFrame %d ==== ######\r\n", port );
00033 
00034     switch( port )
00035     {    
00036     case 5:
00037         {   
00038             uint8_t tmp;
00039             uint8_t tmpLength;
00040             uint8_t ptrIndex = 0;
00041             
00042             // Point the pointer to position index of Tx Buffer
00043             LoRaApp.ApplicationPtrPos( ptrIndex );
00044 
00045             tmp = ( AppLed != 0 ) ? 0x0F : 0x00;
00046             tmpLength = 1;
00047             
00048             LoRaApp.ApplicationAppendData( &tmp, tmpLength ); // Populate lower nibble of 0th Byte with LED state            
00049 
00050             /*!
00051             *  Read Temperature
00052             *  Appends 1 Byte to TX buffer
00053             */
00054             LoRaApp.ApplicationCall( AppTemp );
00055 
00056             /*!
00057             *  Read Battery
00058             *  Appends 1 Byte to TX buffer
00059             */
00060             LoRaApp.ApplicationCall( AppBat );
00061 
00062             /*!
00063             *  Read GPS coordinates
00064             *  Appends 8 Bytes (3 bytes longitude, 3 bytes latitude, 2 bytes altitude) to TX buffer
00065             */
00066             //LoRaApp.ApplicationCall( AppGps );            
00067 
00068             /*!
00069             *  Read Accelerometer
00070             *  Appends 2 Bytes to TX buffer
00071             *   Value       Orientation
00072             *   0x99 0x00   horizontal + faceup
00073             *   0x66 0x00   horizontal + facedown
00074             *   0x00 0x11   vertical
00075             */
00076             LoRaApp.ApplicationCall( AppAccl ); // Generate Accelerometer data bytes    
00077 
00078             /*!
00079             *  Generate Ramp data bytes
00080             *  Appends incremental values of 1 Byte each to TX buffer until Full
00081             */
00082             LoRaApp.ApplicationCall( AppRamp );
00083             
00084 
00085             break;
00086         }   
00087 
00088     // Senet M2X ORIENTATION Demo 
00089     // Set LORAWAN_APP_DATA_SIZE to 2
00090     case 6: 
00091         { 
00092             uint8_t ptrIndex = 1;
00093             
00094             //Point the pointer to position index of Tx Buffer
00095             LoRaApp.ApplicationPtrPos( ptrIndex );
00096             
00097             LoRaApp.ApplicationCall( AppAcclSenet ); // Generate Accelerometer data bytes    
00098             
00099             
00100 
00101             break;
00102         }
00103         
00104     /* Senet GPS Demo 
00105        Data Format (in Hex):
00106             [01, 02, Lattitude (3 bytes), Longitude (3 Bytes), Elevation (2 bytes), Tx Power (1 byte)]
00107     */  
00108     case 7: 
00109         { 
00110             uint8_t ptrIndex = 0;
00111             uint8_t tmp[] = {0x01, 0x02};
00112             
00113             //Point the pointer to position index of Tx Buffer
00114             LoRaApp.ApplicationPtrPos( ptrIndex );
00115             
00116             LoRaApp.ApplicationAppendData( tmp, 2 );  
00117             
00118             LoRaApp.ApplicationCall( AppGps ); // Generate Accelerometer data bytes  
00119             
00120             uint8_t pow = 30 - 2*(( uint8_t ) LoRaMacUplinkStatus.TxPower);
00121             LoRaApp.ApplicationAppendData( &pow, 1 );  
00122 
00123             break;
00124         }
00125 
00126     // Push-Button Demo     
00127     case 11: 
00128         { 
00129             uint8_t ptrIndex = 0;
00130             
00131             //Point the pointer to position index of Tx Buffer
00132             LoRaApp.ApplicationPtrPos( ptrIndex );
00133             
00134             LoRaApp.ApplicationCall( AppPushButton ); // Transmit uplink counter   
00135             
00136             break;
00137         }
00138 
00139     // Transmit on Vertical Orientation Demo     
00140     case 12: 
00141         { 
00142             uint8_t ptrIndex = 0;
00143             
00144             //Point the pointer to position index of Tx Buffer
00145             LoRaApp.ApplicationPtrPos( ptrIndex );
00146             
00147             LoRaApp.ApplicationCall( AppPushButton ); // Transmit uplink counter      
00148             
00149             break;
00150         }
00151 
00152     
00153     /* Senet myDevices Sensor Demo 
00154     Data Format (in Hex):
00155     [Pressure (2 Bytes), Temperature (1 Byte), X-Axis (2 bytes), Y-Axis (2 bytes), Z-Axis (2 bytes), ]
00156         */  
00157     case 13: 
00158         { 
00159             uint8_t ptrIndex = 0;
00160             uint8_t tmp = 0;
00161             
00162             //Point the pointer to position index of Tx Buffer
00163             LoRaApp.ApplicationPtrPos( ptrIndex );
00164             
00165             LoRaApp.ApplicationCall( AppPrsr);
00166 
00167             LoRaApp.ApplicationCall( AppTemp );
00168 
00169             LoRaApp.ApplicationCall( AppBat );
00170             
00171             LoRaApp.ApplicationCall( AppAcclSensor ); // Generate Accelerometer data bytes  
00172 
00173             if( AppLed == 1 )
00174             {
00175                 tmp = 0x01;
00176             }
00177             else
00178             {
00179                 tmp = 0x00;
00180             }
00181             
00182             LoRaApp.ApplicationAppendData( &tmp, 1 );                        
00183 
00184             break;
00185         }
00186         
00187     default:
00188         break;
00189     }
00190 }
00191 
00192 
00193 /*!
00194  * \brief   Sets Interrupt for next payload transmission
00195  */
00196 void InitNextTxInterrupt( uint8_t port )
00197 {   
00198     switch( port )
00199     {  
00200     /* GPS Application Demo
00201         Set Timer interrupt for next uplink
00202     */
00203     case 5:
00204         { 
00205         }
00206 
00207     /* Senet + M2X demo
00208     Set Timer interrupt for next uplink
00209     */
00210     case 6: 
00211         { 
00212             
00213         }
00214         
00215     /* Senet GPS Demo 
00216        Set Timer interrupt for next uplink
00217     */  
00218     case 7: 
00219         { 
00220             // Schedule next packet transmission
00221             TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
00222             TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
00223             TimerStart( &TxNextPacketTimer );
00224             break;
00225         }
00226 
00227     /* Push Button Demo 
00228        Send Packet Immedietly if PC0 = GND
00229     */  
00230     case 11: 
00231         { 
00232             volatile bool PushButtonStatus;
00233 
00234             PushButtonStatus = PC0;
00235             
00236             if(PushButtonStatus == 0)
00237             {
00238                 // Send Pkt immedietly if PC = GND
00239                 DeviceState = DEVICE_STATE_SEND;
00240                 NextTx = true;
00241             }
00242             else
00243             {
00244                 // Keep polling
00245                 IsTxIntUpdate = true;
00246             }
00247             break;
00248         }   
00249 
00250     /* Orientation Demo 
00251        Send Packet Immedietly if Mote is Vertical
00252     */  
00253     case 12: 
00254         {             
00255             CheckOrientation( );
00256             
00257             if(VerticalStatus == true)
00258             {
00259                 // Send Pkt immedietly if PC = GND
00260                 DeviceState = DEVICE_STATE_SEND;
00261                 NextTx = true;
00262             }
00263             else
00264             {
00265                 // Keep polling
00266                 IsTxIntUpdate = true;
00267             }
00268             break;
00269         }   
00270 
00271     /* Compliance Test 
00272        Set Timer interrupt for next uplink
00273     */
00274     case 224:
00275         {               
00276             // Schedule next packet transmission
00277             TimerSetValue( &TxNextPacketTimer, COMPLIANCE_TX_DUTYCYCLE );
00278             TimerStart( &TxNextPacketTimer );
00279             break;
00280         }
00281 
00282     default:
00283         {
00284             // Schedule next packet transmission
00285             TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
00286             TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
00287             TimerStart( &TxNextPacketTimer );
00288             break;
00289         }
00290     }
00291 
00292 }
00293 
00294 /*!
00295  * \brief   What to do during JOIN process ? blink/toggle LED etc.
00296  */
00297 void JoinEvent( void )
00298 {
00299     // CtrlLED is defined in LoRaBoardAppIf.h
00300     // param 1: LED color (Red, Yellow or Green)
00301     // param 2: LED_ON or LED_OFF
00302     CtrlLED( Red, LED_ON ); 
00303 }
00304 
00305 
00306 /*!
00307  * \brief   What to do during TX ? blink/toggle LED etc.
00308  */
00309 void TxEvent( void )
00310 {
00311     int blinkTime = 25000;
00312 
00313     // Blink Red LED for 25msec
00314     BlinkLED( Red, blinkTime );
00315 }
00316 
00317 void RxEvent()
00318 {
00319     // Toggle yellow LED
00320     ToggleLED( Yellow );  
00321 
00322     // If Rx Data is 0x01 turn on Green LED else if 0x0 Turn Green LED off
00323     if( LoRaMacDownlinkStatus.BufferSize == 1 )
00324     {       
00325         if( LoRaMacDownlinkStatus.Buffer[0] == 0x01 )
00326         {
00327             AppLed = 1;
00328         }
00329         else
00330         {
00331             if( LoRaMacDownlinkStatus.Buffer[0] == 0x00 )
00332             {
00333                 AppLed = 0;
00334             }
00335         }
00336     }
00337 
00338     if( AppLed != 0 )
00339     {
00340         // Turn  USR_LED ON
00341         CtrlLED( Usr, LED_ON );
00342     }
00343     else
00344     {
00345         // Turn  USR_LED OFF
00346         CtrlLED( Usr, LED_OFF );
00347     }
00348 }
00349