Semtech / LoRaWAN-NAMote72-Application-Demo

Dependencies:   LoRaWAN-lib mbed lib_mpl3115a2 lib_mma8451q lib_gps SX1272Lib

Dependents:   LoRaWAN-NAMote72-BVS-confirmed-tester-0-7v1_copy

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