light sensor

Dependencies:   X_NUCLEO_IKS01A1 LoRaWAN-lib SX1276Lib mbed

Fork of LoRaWAN-SX1276-Application-Demo by Uttam Bhat

Committer:
terence304
Date:
Fri Sep 08 20:42:14 2017 +0000
Revision:
4:a8706ec8a6d0
Parent:
3:4bca7f8f731a
Child:
5:f04a6f68e159
add light sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ubhat 0:42863a11464a 1 /*
ubhat 0:42863a11464a 2 / _____) _ | |
ubhat 0:42863a11464a 3 ( (____ _____ ____ _| |_ _____ ____| |__
ubhat 0:42863a11464a 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
ubhat 0:42863a11464a 5 _____) ) ____| | | || |_| ____( (___| | | |
ubhat 0:42863a11464a 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
ubhat 0:42863a11464a 7 (C)2015 Semtech
ubhat 0:42863a11464a 8
ubhat 0:42863a11464a 9 Description: Define events during Join, Tx & Rx
ubhat 0:42863a11464a 10 Prepare TX packet by appending with appropriate application data
ubhat 0:42863a11464a 11
ubhat 0:42863a11464a 12 License: Revised BSD License, see LICENSE.TXT file include in the project
ubhat 0:42863a11464a 13
ubhat 0:42863a11464a 14 Maintainer: Uttam Bhat
ubhat 0:42863a11464a 15 */
ubhat 0:42863a11464a 16
ubhat 0:42863a11464a 17 #include "LoRaEventProc.h"
ubhat 0:42863a11464a 18
ubhat 0:42863a11464a 19 /*!
ubhat 0:42863a11464a 20 * Defines the application data transmission duty cycle
ubhat 0:42863a11464a 21 */
ubhat 0:42863a11464a 22 uint32_t TxDutyCycleTime = APP_TX_DUTYCYCLE;
ubhat 0:42863a11464a 23
ubhat 0:42863a11464a 24 bool AppLed = 0;
ubhat 0:42863a11464a 25
ubhat 3:4bca7f8f731a 26 uint8_t AppCase = 0;
ubhat 3:4bca7f8f731a 27
ubhat 0:42863a11464a 28 /*!
ubhat 0:42863a11464a 29 * \brief Prepares the payload of the frame based on application port
ubhat 0:42863a11464a 30 */
ubhat 0:42863a11464a 31 void PrepareLoRaFrame( uint8_t port )
ubhat 0:42863a11464a 32 {
ubhat 0:42863a11464a 33
ubhat 0:42863a11464a 34 switch( port )
ubhat 0:42863a11464a 35 {
ubhat 0:42863a11464a 36 case 5:
ubhat 0:42863a11464a 37 {
ubhat 0:42863a11464a 38 uint8_t tmp;
ubhat 0:42863a11464a 39 uint8_t tmpLength;
ubhat 0:42863a11464a 40 uint8_t ptrIndex = 0;
ubhat 0:42863a11464a 41
ubhat 0:42863a11464a 42 // Point the pointer to position index of Tx Buffer
ubhat 0:42863a11464a 43 LoRaApp.ApplicationPtrPos( ptrIndex );
ubhat 0:42863a11464a 44
ubhat 0:42863a11464a 45 tmp = ( AppLed != 0 ) ? 0x0F : 0x00;
ubhat 0:42863a11464a 46 tmpLength = 1;
ubhat 0:42863a11464a 47
ubhat 0:42863a11464a 48 LoRaApp.ApplicationAppendData( &tmp, tmpLength ); // Populate lower nibble of 0th Byte with LED state
ubhat 0:42863a11464a 49
ubhat 0:42863a11464a 50 /*!
ubhat 2:78df92a365c2 51 * Generate Ramp data bytes
ubhat 2:78df92a365c2 52 * Appends incremental values of 1 Byte each to TX buffer until Full
ubhat 2:78df92a365c2 53 */
ubhat 2:78df92a365c2 54 LoRaApp.ApplicationCall( AppRamp );
ubhat 2:78df92a365c2 55
ubhat 2:78df92a365c2 56 break;
ubhat 2:78df92a365c2 57 }
ubhat 2:78df92a365c2 58
ubhat 2:78df92a365c2 59 case 8:
ubhat 2:78df92a365c2 60 {
ubhat 2:78df92a365c2 61 uint8_t ptrIndex = 0;
ubhat 2:78df92a365c2 62
ubhat 2:78df92a365c2 63 //Point the pointer to position index of Tx Buffer
ubhat 2:78df92a365c2 64 LoRaApp.ApplicationPtrPos( ptrIndex );
ubhat 2:78df92a365c2 65
ubhat 2:78df92a365c2 66 /*!
ubhat 2:78df92a365c2 67 * Read Pressure
ubhat 2:78df92a365c2 68 * Appends 2 Bytes to TX buffer
ubhat 2:78df92a365c2 69 */
ubhat 2:78df92a365c2 70 LoRaApp.ApplicationCall( AppPressr );
ubhat 2:78df92a365c2 71
ubhat 2:78df92a365c2 72 /*!
ubhat 0:42863a11464a 73 * Read Temperature
ubhat 0:42863a11464a 74 * Appends 1 Byte to TX buffer
ubhat 0:42863a11464a 75 */
ubhat 0:42863a11464a 76 LoRaApp.ApplicationCall( AppTemp );
ubhat 2:78df92a365c2 77
ubhat 0:42863a11464a 78 /*!
ubhat 2:78df92a365c2 79 * Read Humidity
ubhat 0:42863a11464a 80 * Appends 1 Byte to TX buffer
ubhat 0:42863a11464a 81 */
ubhat 2:78df92a365c2 82 LoRaApp.ApplicationCall( AppHumid );
ubhat 0:42863a11464a 83
ubhat 0:42863a11464a 84 /*!
ubhat 0:42863a11464a 85 * Read Accelerometer
ubhat 0:42863a11464a 86 * Appends 2 Bytes to TX buffer
ubhat 0:42863a11464a 87 * Value Orientation
ubhat 0:42863a11464a 88 * 0x99 0x00 horizontal + faceup
ubhat 0:42863a11464a 89 * 0x66 0x00 horizontal + facedown
ubhat 0:42863a11464a 90 * 0x00 0x11 vertical
ubhat 0:42863a11464a 91 */
ubhat 0:42863a11464a 92 LoRaApp.ApplicationCall( AppAccl ); // Generate Accelerometer data bytes
ubhat 0:42863a11464a 93
ubhat 0:42863a11464a 94
ubhat 0:42863a11464a 95 break;
ubhat 0:42863a11464a 96 }
ubhat 3:4bca7f8f731a 97
ubhat 3:4bca7f8f731a 98 // IKAS sensor using Cayenne Payload Format
ubhat 3:4bca7f8f731a 99 case 9:
ubhat 3:4bca7f8f731a 100 {
ubhat 3:4bca7f8f731a 101 #ifdef USE_CAYENNE_LPP
ubhat 3:4bca7f8f731a 102 uint8_t ptrIndex = 0;
ubhat 3:4bca7f8f731a 103
ubhat 3:4bca7f8f731a 104 uint16_t AppSize = 0;
ubhat 3:4bca7f8f731a 105
ubhat 3:4bca7f8f731a 106 uint8_t tmp[2] = {0};
ubhat 3:4bca7f8f731a 107
ubhat 3:4bca7f8f731a 108 //Point the pointer to position index of Tx Buffer
ubhat 3:4bca7f8f731a 109 LoRaApp.ApplicationPtrPos( ptrIndex );
ubhat 3:4bca7f8f731a 110
ubhat 3:4bca7f8f731a 111 AppCase = AppCase > 3 ? 0 : AppCase;
ubhat 3:4bca7f8f731a 112
ubhat 3:4bca7f8f731a 113 while( 1 )
ubhat 3:4bca7f8f731a 114 {
ubhat 3:4bca7f8f731a 115 switch( AppCase )
ubhat 3:4bca7f8f731a 116 {
ubhat 3:4bca7f8f731a 117 case 0:
ubhat 3:4bca7f8f731a 118 {
ubhat 3:4bca7f8f731a 119 tmp[0] = 0;
ubhat 3:4bca7f8f731a 120 tmp[1] = 115; // Data Type PRESSURE: 115
ubhat 3:4bca7f8f731a 121 LoRaApp.ApplicationAppendData( tmp, 2 );
ubhat 3:4bca7f8f731a 122
ubhat 3:4bca7f8f731a 123 /*!
ubhat 3:4bca7f8f731a 124 * Read Pressure
ubhat 3:4bca7f8f731a 125 * Appends 2 Bytes to TX buffer
ubhat 3:4bca7f8f731a 126 */
ubhat 3:4bca7f8f731a 127 LoRaApp.ApplicationCall( AppPressr );
ubhat 3:4bca7f8f731a 128
ubhat 3:4bca7f8f731a 129 AppSize += maxLPPsize[AppCase];
ubhat 3:4bca7f8f731a 130
ubhat 3:4bca7f8f731a 131 break;
ubhat 3:4bca7f8f731a 132 }
ubhat 3:4bca7f8f731a 133
ubhat 3:4bca7f8f731a 134 case 1:
ubhat 3:4bca7f8f731a 135 {
ubhat 3:4bca7f8f731a 136 tmp[0] = 1;
ubhat 3:4bca7f8f731a 137 tmp[1] = 103; // Data Type TEMP: 103
ubhat 3:4bca7f8f731a 138 LoRaApp.ApplicationAppendData( tmp, 2 );
ubhat 3:4bca7f8f731a 139
ubhat 3:4bca7f8f731a 140 /*!
ubhat 3:4bca7f8f731a 141 * Read Temperature
ubhat 3:4bca7f8f731a 142 * Appends 1 Byte to TX buffer
ubhat 3:4bca7f8f731a 143 */
ubhat 3:4bca7f8f731a 144 LoRaApp.ApplicationCall( AppTemp );
ubhat 3:4bca7f8f731a 145
ubhat 3:4bca7f8f731a 146 AppSize += maxLPPsize[AppCase];
ubhat 3:4bca7f8f731a 147
ubhat 3:4bca7f8f731a 148 break;
ubhat 3:4bca7f8f731a 149 }
ubhat 3:4bca7f8f731a 150
ubhat 3:4bca7f8f731a 151 case 2:
ubhat 3:4bca7f8f731a 152 {
ubhat 3:4bca7f8f731a 153 tmp[0] = 2;
ubhat 3:4bca7f8f731a 154 tmp[1] = 104; // Data Type HUMIDITY: 103
ubhat 3:4bca7f8f731a 155 LoRaApp.ApplicationAppendData( tmp, 2 );
ubhat 3:4bca7f8f731a 156
ubhat 3:4bca7f8f731a 157 /*!
ubhat 3:4bca7f8f731a 158 * Read Humidity
ubhat 3:4bca7f8f731a 159 * Appends 1 Byte to TX buffer
ubhat 3:4bca7f8f731a 160 */
ubhat 3:4bca7f8f731a 161 LoRaApp.ApplicationCall( AppHumid );
ubhat 3:4bca7f8f731a 162
ubhat 3:4bca7f8f731a 163 AppSize += maxLPPsize[AppCase];
ubhat 3:4bca7f8f731a 164
ubhat 3:4bca7f8f731a 165 break;
ubhat 3:4bca7f8f731a 166 }
ubhat 3:4bca7f8f731a 167
ubhat 3:4bca7f8f731a 168 case 3:
ubhat 3:4bca7f8f731a 169 {
ubhat 3:4bca7f8f731a 170 tmp[0] = 3;
ubhat 3:4bca7f8f731a 171 tmp[1] = 113; // Data Type Accl: 113
ubhat 3:4bca7f8f731a 172 LoRaApp.ApplicationAppendData( tmp, 2 );
ubhat 3:4bca7f8f731a 173 /*!
ubhat 3:4bca7f8f731a 174 * Read Accelerometer
ubhat 3:4bca7f8f731a 175 */
ubhat 3:4bca7f8f731a 176 LoRaApp.ApplicationCall( AppAccl ); // Generate Accelerometer data bytes
ubhat 3:4bca7f8f731a 177
ubhat 3:4bca7f8f731a 178 AppSize += maxLPPsize[AppCase];
ubhat 3:4bca7f8f731a 179
ubhat 3:4bca7f8f731a 180 break;
ubhat 3:4bca7f8f731a 181 }
ubhat 3:4bca7f8f731a 182 }
ubhat 3:4bca7f8f731a 183
ubhat 3:4bca7f8f731a 184 AppCase++;
ubhat 3:4bca7f8f731a 185
ubhat 3:4bca7f8f731a 186 if( AppSize + maxLPPsize[AppCase] > LORAWAN_APP_DATA_SIZE )
ubhat 3:4bca7f8f731a 187 {
ubhat 3:4bca7f8f731a 188 break;
ubhat 3:4bca7f8f731a 189 }
ubhat 3:4bca7f8f731a 190 }
ubhat 3:4bca7f8f731a 191
ubhat 3:4bca7f8f731a 192 AppDataSize = AppSize;
ubhat 3:4bca7f8f731a 193 #endif
ubhat 3:4bca7f8f731a 194 break;
ubhat 3:4bca7f8f731a 195 }
ubhat 0:42863a11464a 196
ubhat 0:42863a11464a 197 // Push-Button Demo
ubhat 0:42863a11464a 198 case 11:
ubhat 0:42863a11464a 199 {
ubhat 0:42863a11464a 200 uint8_t ptrIndex = 0;
ubhat 0:42863a11464a 201
ubhat 0:42863a11464a 202 //Point the pointer to position index of Tx Buffer
ubhat 0:42863a11464a 203 LoRaApp.ApplicationPtrPos( ptrIndex );
ubhat 0:42863a11464a 204
ubhat 0:42863a11464a 205 LoRaApp.ApplicationCall( AppPushButton ); // Transmit uplink counter
ubhat 0:42863a11464a 206
ubhat 0:42863a11464a 207 break;
ubhat 0:42863a11464a 208 }
terence304 4:a8706ec8a6d0 209
terence304 4:a8706ec8a6d0 210 case 13:
terence304 4:a8706ec8a6d0 211 {
terence304 4:a8706ec8a6d0 212 uint8_t ptrIndex = 0;
terence304 4:a8706ec8a6d0 213 uint8_t data_header[2] = {0};
terence304 4:a8706ec8a6d0 214
terence304 4:a8706ec8a6d0 215 //Point the pointer to position index of Tx Buffer
terence304 4:a8706ec8a6d0 216 LoRaApp.ApplicationPtrPos( ptrIndex );
terence304 4:a8706ec8a6d0 217
terence304 4:a8706ec8a6d0 218 data_header[0] = 0;
terence304 4:a8706ec8a6d0 219 data_header[1] = 0;
terence304 4:a8706ec8a6d0 220
terence304 4:a8706ec8a6d0 221 LoRaApp.ApplicationAppendData( data_header, 2 );
terence304 4:a8706ec8a6d0 222 LoRaApp.ApplicationCall( AppExtLight ); // Transmit uplink counter
terence304 4:a8706ec8a6d0 223 break;
terence304 4:a8706ec8a6d0 224 }
ubhat 2:78df92a365c2 225
ubhat 0:42863a11464a 226 default:
ubhat 0:42863a11464a 227 break;
ubhat 0:42863a11464a 228 }
ubhat 0:42863a11464a 229 }
ubhat 0:42863a11464a 230
ubhat 0:42863a11464a 231
ubhat 0:42863a11464a 232 /*!
ubhat 0:42863a11464a 233 * \brief Sets Interrupt for next payload transmission
ubhat 0:42863a11464a 234 */
ubhat 0:42863a11464a 235 void InitNextTxInterrupt( uint8_t port )
ubhat 0:42863a11464a 236 {
ubhat 0:42863a11464a 237 switch( port )
ubhat 0:42863a11464a 238 {
ubhat 0:42863a11464a 239 /* GPS Application Demo
ubhat 0:42863a11464a 240 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 241 */
ubhat 0:42863a11464a 242 case 5:
ubhat 0:42863a11464a 243 {
ubhat 0:42863a11464a 244 }
ubhat 0:42863a11464a 245
ubhat 0:42863a11464a 246 /* Senet + M2X demo
ubhat 0:42863a11464a 247 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 248 */
ubhat 0:42863a11464a 249 case 6:
ubhat 0:42863a11464a 250 {
ubhat 0:42863a11464a 251
ubhat 0:42863a11464a 252 }
ubhat 0:42863a11464a 253
ubhat 0:42863a11464a 254 /* Senet GPS Demo
ubhat 0:42863a11464a 255 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 256 */
ubhat 0:42863a11464a 257 case 7:
ubhat 0:42863a11464a 258 {
ubhat 0:42863a11464a 259 // Schedule next packet transmission
ubhat 0:42863a11464a 260 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
ubhat 0:42863a11464a 261 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
ubhat 0:42863a11464a 262 TimerStart( &TxNextPacketTimer );
ubhat 0:42863a11464a 263 break;
ubhat 0:42863a11464a 264 }
ubhat 0:42863a11464a 265
ubhat 0:42863a11464a 266 /* Push Button Demo
ubhat 0:42863a11464a 267 Send Packet Immedietly if PC0 = GND
ubhat 0:42863a11464a 268 */
ubhat 0:42863a11464a 269 case 11:
ubhat 1:80c1daf19aa4 270 {
ubhat 0:42863a11464a 271 volatile bool PushButtonStatus;
ubhat 0:42863a11464a 272
ubhat 1:80c1daf19aa4 273 PushButtonStatus = UsrButton;
ubhat 0:42863a11464a 274
ubhat 0:42863a11464a 275 if(PushButtonStatus == 0)
ubhat 0:42863a11464a 276 {
ubhat 0:42863a11464a 277 // Send Pkt immedietly if PC = GND
ubhat 0:42863a11464a 278 DeviceState = DEVICE_STATE_SEND;
ubhat 0:42863a11464a 279 NextTx = true;
ubhat 0:42863a11464a 280 }
ubhat 0:42863a11464a 281 else
ubhat 0:42863a11464a 282 {
ubhat 0:42863a11464a 283 // Keep polling
ubhat 0:42863a11464a 284 IsTxIntUpdate = true;
ubhat 0:42863a11464a 285 }
ubhat 1:80c1daf19aa4 286
ubhat 0:42863a11464a 287 break;
ubhat 0:42863a11464a 288 }
ubhat 0:42863a11464a 289
ubhat 0:42863a11464a 290 /* Orientation Demo
ubhat 0:42863a11464a 291 Send Packet Immedietly if Mote is Vertical
ubhat 0:42863a11464a 292 */
ubhat 0:42863a11464a 293 case 12:
ubhat 0:42863a11464a 294 {
ubhat 0:42863a11464a 295 CheckOrientation( );
ubhat 0:42863a11464a 296
ubhat 0:42863a11464a 297 if(VerticalStatus == true)
ubhat 0:42863a11464a 298 {
ubhat 0:42863a11464a 299 // Send Pkt immedietly if PC = GND
ubhat 0:42863a11464a 300 DeviceState = DEVICE_STATE_SEND;
ubhat 0:42863a11464a 301 NextTx = true;
ubhat 0:42863a11464a 302 }
ubhat 0:42863a11464a 303 else
ubhat 0:42863a11464a 304 {
ubhat 0:42863a11464a 305 // Keep polling
ubhat 0:42863a11464a 306 IsTxIntUpdate = true;
ubhat 0:42863a11464a 307 }
ubhat 0:42863a11464a 308 break;
ubhat 0:42863a11464a 309 }
ubhat 0:42863a11464a 310
ubhat 0:42863a11464a 311 /* Compliance Test
ubhat 0:42863a11464a 312 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 313 */
ubhat 0:42863a11464a 314 case 224:
ubhat 0:42863a11464a 315 {
ubhat 0:42863a11464a 316 // Schedule next packet transmission
ubhat 0:42863a11464a 317 TimerSetValue( &TxNextPacketTimer, COMPLIANCE_TX_DUTYCYCLE );
ubhat 0:42863a11464a 318 TimerStart( &TxNextPacketTimer );
ubhat 0:42863a11464a 319 break;
ubhat 0:42863a11464a 320 }
ubhat 0:42863a11464a 321
ubhat 0:42863a11464a 322 default:
ubhat 0:42863a11464a 323 {
ubhat 0:42863a11464a 324 // Schedule next packet transmission
ubhat 0:42863a11464a 325 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
ubhat 0:42863a11464a 326 TimerStart( &TxNextPacketTimer );
ubhat 0:42863a11464a 327 break;
ubhat 0:42863a11464a 328 }
ubhat 0:42863a11464a 329 }
ubhat 0:42863a11464a 330
ubhat 0:42863a11464a 331 }
ubhat 0:42863a11464a 332
ubhat 0:42863a11464a 333 /*!
ubhat 0:42863a11464a 334 * \brief What to do during JOIN process ? blink/toggle LED etc.
ubhat 0:42863a11464a 335 */
ubhat 0:42863a11464a 336 void JoinEvent( void )
ubhat 0:42863a11464a 337 {
ubhat 0:42863a11464a 338 // CtrlLED is defined in LoRaBoardAppIf.h
ubhat 0:42863a11464a 339 // param 1: LED color (Red, Yellow or Green)
ubhat 0:42863a11464a 340 // param 2: LED_ON or LED_OFF
ubhat 0:42863a11464a 341 //CtrlLED( Red, LED_ON );
ubhat 0:42863a11464a 342 }
ubhat 0:42863a11464a 343
ubhat 0:42863a11464a 344
ubhat 0:42863a11464a 345 /*!
ubhat 0:42863a11464a 346 * \brief What to do during TX ? blink/toggle LED etc.
ubhat 0:42863a11464a 347 */
ubhat 0:42863a11464a 348 void TxEvent( void )
ubhat 0:42863a11464a 349 {
ubhat 0:42863a11464a 350 int blinkTime = 25000;
ubhat 0:42863a11464a 351
ubhat 0:42863a11464a 352 // Blink Red LED for 25msec
ubhat 0:42863a11464a 353 //BlinkLED( Red, blinkTime );
ubhat 0:42863a11464a 354 }
ubhat 0:42863a11464a 355
ubhat 0:42863a11464a 356 void RxEvent()
ubhat 0:42863a11464a 357 {
ubhat 0:42863a11464a 358 // Toggle yellow LED
ubhat 0:42863a11464a 359 //ToggleLED( Yellow );
ubhat 0:42863a11464a 360
ubhat 0:42863a11464a 361 // If Rx Data is 0x01 turn on Green LED else if 0x0 Turn Green LED off
ubhat 0:42863a11464a 362 if( LoRaMacDownlinkStatus.BufferSize == 1 )
ubhat 0:42863a11464a 363 {
ubhat 0:42863a11464a 364 if( LoRaMacDownlinkStatus.Buffer[0] == 0x01 )
ubhat 0:42863a11464a 365 {
ubhat 0:42863a11464a 366 AppLed = 1;
ubhat 0:42863a11464a 367 }
ubhat 0:42863a11464a 368 else
ubhat 0:42863a11464a 369 {
ubhat 0:42863a11464a 370 if( LoRaMacDownlinkStatus.Buffer[0] == 0x00 )
ubhat 0:42863a11464a 371 {
ubhat 0:42863a11464a 372 AppLed = 0;
ubhat 0:42863a11464a 373 }
ubhat 0:42863a11464a 374 }
ubhat 0:42863a11464a 375 }
ubhat 0:42863a11464a 376
ubhat 0:42863a11464a 377 if( AppLed != 0 )
ubhat 0:42863a11464a 378 {
ubhat 0:42863a11464a 379 // Turn USR_LED ON
ubhat 2:78df92a365c2 380 //UsrLED = 3.3;
ubhat 0:42863a11464a 381 }
ubhat 0:42863a11464a 382 else
ubhat 0:42863a11464a 383 {
ubhat 0:42863a11464a 384 // Turn USR_LED OFF
ubhat 2:78df92a365c2 385 //UsrLED = 0;
ubhat 0:42863a11464a 386 }
ubhat 0:42863a11464a 387 }
ubhat 0:42863a11464a 388