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 LoRaApp.cpp Source File

LoRaApp.cpp

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2015 Semtech
00008 
00009 Description: User-defined applications such as GPS, Temp, Accelerometer, LED indications etc.
00010             Event based actions such as LED blink on Tx, LED toggle on downlink etc
00011 
00012 License: Revised BSD License, see LICENSE.TXT file include in the project
00013 
00014 Maintainer: Uttam Bhat
00015 */
00016 
00017 #include "LoRaApp.h"
00018 
00019 float sensor_data;
00020 
00021 int32_t Accl_Value[3] = {0};
00022 
00023 
00024 #ifdef USE_CAYENNE_LPP
00025 /*
00026 .... Pressure
00027 .... Temperature
00028 .... Humidity
00029 .... Accelerometer
00030 */
00031 uint8_t maxLPPsize[5] = {4, 4, 3, 8, 8};
00032 #endif
00033 
00034 bool VerticalStatus = false;
00035 
00036 
00037 Application::Application( uint8_t * memptr )
00038 {
00039     BuffAddr = memptr;
00040     memset( BuffAddr, 0, LORAWAN_APP_DATA_MAX_SIZE );
00041     BuffPtr = 0;    
00042 }
00043 
00044 Application::~Application( )
00045 {
00046 }
00047 
00048 void Application::ApplicationAppendData( uint8_t *pData, uint8_t len )
00049 {
00050     memcpy( BuffAddr + BuffPtr, pData, len );
00051     BuffPtr += len;
00052 }
00053 
00054 void Application::ApplicationPtrPos( uint8_t ptrPos )
00055 {
00056     BuffPtr = ptrPos;
00057 }
00058 
00059 void Application::ApplicationCall( eAppType App )
00060 {
00061     switch( App )
00062     {        
00063         // Appends 1 Byte to TX buffer
00064         case AppTemp:
00065         {   
00066                         
00067             hum_temp->get_temperature(&sensor_data);
00068                      
00069             printf("Temp = %f, %d\r\n", sensor_data, (int8_t) sensor_data);
00070                                
00071             if( ( BuffPtr + 1 ) <= LORAWAN_APP_DATA_SIZE )
00072             {
00073 #ifdef USE_CAYENNE_LPP
00074                 int16_t tmp_data = (int16_t) (sensor_data*10 + 0.5);
00075                 BuffAddr[BuffPtr++] = (int8_t) ( ( tmp_data >> 8 ) & 0xFF );
00076                 BuffAddr[BuffPtr++] = (int8_t) tmp_data & 0xFF;
00077 #else                
00078                 BuffAddr[BuffPtr++] = (int8_t) sensor_data;
00079 #endif                
00080             }
00081                         
00082             break;
00083         }
00084         
00085         // Appends 2 Bytes to TX buffer
00086         case AppPressr:
00087         {                            
00088             press_temp->get_pressure(&sensor_data);
00089                                     
00090             printf("Pressure = %f, %d\r\n", sensor_data, (uint16_t) sensor_data);
00091                                
00092             if( ( BuffPtr + 2 ) <= LORAWAN_APP_DATA_SIZE )
00093             {
00094 #ifdef USE_CAYENNE_LPP
00095                 int16_t tmp;
00096                 
00097                 tmp =  (int16_t) ( sensor_data * 10 );
00098                 BuffAddr[BuffPtr++] = ( tmp >> 8 ) & 0xFF;
00099                 BuffAddr[BuffPtr++] = ( tmp ) & 0xFF;
00100 #else                
00101                 BuffAddr[BuffPtr++] = ( (int16_t) sensor_data >> 8 ) & 0xFF;
00102                 BuffAddr[BuffPtr++] = ( (int16_t) sensor_data ) & 0xFF;
00103 #endif                
00104             }
00105                  
00106             break;
00107         }
00108         
00109         // Appends 2 Bytes to TX buffer
00110         case AppHumid:
00111         {   
00112                                  
00113             hum_temp->get_humidity(&sensor_data);           
00114                                   
00115             printf("Humidity = %f, %d\r\n", sensor_data, (uint8_t) sensor_data);
00116                                
00117             if( ( BuffPtr + 1 ) <= LORAWAN_APP_DATA_SIZE )
00118             {
00119 #ifdef USE_CAYENNE_LPP
00120                 BuffAddr[BuffPtr++] = (uint8_t) ( sensor_data * 2 );
00121 #else                
00122                 BuffAddr[BuffPtr++] = (int8_t) sensor_data;
00123 #endif                
00124             }
00125                         
00126             break;
00127         }
00128         
00129         // Appends 2 Bytes to TX buffer
00130         case AppGyro:
00131         {                  
00132             acc_gyro->get_g_axes(Accl_Value);         
00133                                   
00134             printf("Gyro X/Y/Z = %d/%d/%d\r\n", Accl_Value[0], Accl_Value[1], Accl_Value[2]);
00135                                
00136             if( ( BuffPtr + 6 ) <= LORAWAN_APP_DATA_SIZE )
00137             {
00138                 Accl_Value[0] /= 10;
00139                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[0] >> 8 ) & 0xFF;
00140                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[0] ) & 0xFF; 
00141                 Accl_Value[1] /= 10;
00142                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[1] >> 8 ) & 0xFF;
00143                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[1] ) & 0xFF; 
00144                 Accl_Value[2] /= 10;
00145                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[2] >> 8 ) & 0xFF;
00146                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[2] ) & 0xFF;                                         
00147             }
00148                         
00149             break;
00150         }
00151 
00152         // Appends 1 Byte to TX buffer
00153         case AppBat:
00154         {  
00155             if( ( BuffPtr + 1 ) <= LORAWAN_APP_DATA_SIZE )
00156             {
00157                 BuffAddr[BuffPtr++] = BoardGetBatteryLevel( );              // Per LoRaWAN spec; 0 = Charging; 1...254 = level, 255 = N/A
00158             }
00159             break;
00160         }
00161 
00162         // Appends incremental values of 1 Byte each to TX buffer until Full
00163         case AppRamp:
00164         {
00165             int32_t i, j;
00166 
00167             // Populate Tx Buffer with increasing byte values starting from 0x00, 0x01, 0x02 ... 
00168             for( i = BuffPtr, j = 0; i < LORAWAN_APP_DATA_SIZE; i++ )
00169             {
00170                 BuffAddr[i] = j++;
00171             }
00172             BuffPtr = LORAWAN_APP_DATA_SIZE;
00173             break;
00174         }
00175 
00176         // Appends 2 Bytes to TX buffer
00177         case AppAccl:
00178         {  
00179                         
00180             accelerometer->get_x_axes(Accl_Value);
00181                                     
00182             printf("X/Y/Z = %d/%d/%d\r\n", Accl_Value[0], Accl_Value[1], Accl_Value[2]);
00183                                
00184             if( ( BuffPtr + 6 ) <= LORAWAN_APP_DATA_SIZE )
00185             {
00186                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[0] >> 8 ) & 0xFF;
00187                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[0] ) & 0xFF; 
00188                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[1] >> 8 ) & 0xFF;
00189                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[1] ) & 0xFF; 
00190                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[2] >> 8 ) & 0xFF;
00191                 BuffAddr[BuffPtr++] = ( (int16_t) Accl_Value[2] ) & 0xFF; 
00192             }
00193                  
00194             break;
00195         }       
00196 
00197         case AppPushButton:
00198         {   
00199             static uint8_t PushButtonCnt = 0;
00200 
00201             PushButtonCnt = LoRaMacUplinkStatus.UplinkCounter;
00202             PushButtonCnt = (PushButtonCnt + 1) & 0xFF;
00203                 
00204             //memcpy( &BuffAddr[BuffPtr], p, sizeof(uint16_t) );
00205             BuffAddr[BuffPtr] = PushButtonCnt;
00206             
00207             break;
00208         }
00209 
00210         default:
00211         {            
00212             break;
00213         }
00214     }
00215 }
00216 
00217 /*
00218 static void OnRedLedTimerEvent( void )
00219 {
00220     TimerStop( &RedLedTimer.LedTimer );
00221 
00222     if( RedLed == LED_OFF )
00223     {
00224         RedLed = LED_ON;
00225     }
00226     else
00227     {
00228         RedLed = LED_OFF;
00229     }
00230 }
00231 
00232 static void OnYellowLedTimerEvent( void )
00233 {
00234     TimerStop( &YellowLedTimer.LedTimer );
00235 
00236     if( YellowLed == LED_OFF )
00237     {
00238         YellowLed = LED_ON;
00239     }
00240     else
00241     {
00242         YellowLed = LED_OFF;
00243     }
00244 }
00245 
00246 static void OnGreenLedTimerEvent( void )
00247 {
00248     TimerStop( &GreenLedTimer.LedTimer );
00249 
00250     if( GreenLed == LED_OFF )
00251     {
00252         GreenLed = LED_ON;
00253     }
00254     else
00255     {
00256         GreenLed = LED_OFF;
00257     }
00258 }
00259 
00260 TimerLed::TimerLed( eLedType led )
00261 {
00262     switch( led )
00263     {
00264         case Red:
00265         {
00266             TimerInit( &LedTimer, OnRedLedTimerEvent );
00267             break;
00268         }
00269 
00270         case Yellow:
00271         {
00272             TimerInit( &LedTimer, OnYellowLedTimerEvent );
00273             break;
00274         }
00275 
00276         case Green:
00277         {
00278             TimerInit( &LedTimer, OnGreenLedTimerEvent );
00279             break;
00280         }
00281     }
00282     
00283 }
00284         
00285 TimerLed::~TimerLed( )
00286 {
00287 }
00288 
00289 void BlinkLED( eLedType led, uint32_t time )
00290 {
00291     switch( led )
00292     {
00293         case Red:
00294         {
00295             TimerSetValue( &RedLedTimer.LedTimer, time );
00296             TimerStart( &RedLedTimer.LedTimer );
00297             RedLed = LED_ON;
00298             break;
00299         }
00300 
00301         case Yellow:
00302         {
00303             TimerSetValue( &YellowLedTimer.LedTimer, time );
00304             TimerStart( &YellowLedTimer.LedTimer );
00305             YellowLed = LED_ON;
00306             break;
00307         }
00308 
00309         case Green:
00310         {
00311             TimerSetValue( &GreenLedTimer.LedTimer, time );
00312             TimerStart( &GreenLedTimer.LedTimer );
00313             GreenLed = LED_ON;
00314             break;
00315         }
00316     }
00317 }
00318 
00319 void ToggleLED( eLedType led )
00320 {
00321     switch( led )
00322     {
00323         case Red:
00324         {
00325             if( RedLed == LED_OFF )
00326             {
00327                 RedLed = LED_ON;
00328             }
00329             else
00330             {
00331                 RedLed = LED_OFF;
00332             }
00333             break;
00334         }
00335 
00336         case Yellow:
00337         {
00338             if( YellowLed == LED_OFF )
00339             {
00340                 YellowLed = LED_ON;
00341             }
00342             else
00343             {
00344                 YellowLed = LED_OFF;
00345             }
00346             break;
00347         }
00348 
00349         case Green:
00350         {
00351             if( GreenLed == LED_OFF )
00352             {
00353                 GreenLed = LED_ON;
00354             }
00355             else
00356             {
00357                 GreenLed = LED_OFF;
00358             }
00359             break;
00360         }
00361     }
00362 }   
00363 
00364 void CtrlLED( eLedType led, uint8_t state )
00365 {
00366     switch( led )
00367     {
00368         case Red:
00369         {
00370             RedLed = state;
00371             break;
00372         }
00373 
00374         case Yellow:
00375         {
00376             YellowLed = state;
00377             break;
00378         }
00379 
00380         case Green:
00381         {
00382             GreenLed = state;
00383             break;
00384         }
00385 
00386         case Usr:
00387         {
00388             if( state )
00389             {
00390                 UsrLed = LED_ON;
00391             }
00392             else
00393             {
00394                 UsrLed = LED_OFF;
00395             }
00396             break;
00397         }
00398     }
00399 }
00400 */
00401 void CheckOrientation( void )
00402 {
00403     /*
00404     uint8_t statusReg; 
00405     
00406     // Read the PS_STATUS register
00407     statusReg = Mma8451q.read_single( MMA8451_PL_STATUS );
00408 
00409      // If Orientation of the Mote changed then populate Upper Nibble of 0th Byte of Tx Buffer                       
00410     if( ( statusReg & 0x40 ) != 0 )
00411     {           
00412         CtrlLED( Green, LED_OFF );
00413         VerticalStatus = false; // horizontal
00414     }
00415     else
00416     {        
00417         CtrlLED( Green, LED_ON );
00418         VerticalStatus = true; // vertical
00419     } 
00420     */      
00421 }