light sensor
Dependencies: X_NUCLEO_IKS01A1 LoRaWAN-lib SX1276Lib mbed
Fork of LoRaWAN-SX1276-Application-Demo by
app/LoRaEventProc.cpp@1:80c1daf19aa4, 2016-08-26 (annotated)
- Committer:
- ubhat
- Date:
- Fri Aug 26 20:51:07 2016 +0000
- Revision:
- 1:80c1daf19aa4
- Parent:
- 0:42863a11464a
- Child:
- 2:78df92a365c2
Push Button Example (PORT 11)
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:42863a11464a | 26 | /*! |
ubhat | 0:42863a11464a | 27 | * \brief Prepares the payload of the frame based on application port |
ubhat | 0:42863a11464a | 28 | */ |
ubhat | 0:42863a11464a | 29 | void PrepareLoRaFrame( uint8_t port ) |
ubhat | 0:42863a11464a | 30 | { |
ubhat | 0:42863a11464a | 31 | |
ubhat | 0:42863a11464a | 32 | switch( port ) |
ubhat | 0:42863a11464a | 33 | { |
ubhat | 0:42863a11464a | 34 | case 5: |
ubhat | 0:42863a11464a | 35 | { |
ubhat | 0:42863a11464a | 36 | uint8_t tmp; |
ubhat | 0:42863a11464a | 37 | uint8_t tmpLength; |
ubhat | 0:42863a11464a | 38 | uint8_t ptrIndex = 0; |
ubhat | 0:42863a11464a | 39 | |
ubhat | 0:42863a11464a | 40 | // Point the pointer to position index of Tx Buffer |
ubhat | 0:42863a11464a | 41 | LoRaApp.ApplicationPtrPos( ptrIndex ); |
ubhat | 0:42863a11464a | 42 | |
ubhat | 0:42863a11464a | 43 | tmp = ( AppLed != 0 ) ? 0x0F : 0x00; |
ubhat | 0:42863a11464a | 44 | tmpLength = 1; |
ubhat | 0:42863a11464a | 45 | |
ubhat | 0:42863a11464a | 46 | LoRaApp.ApplicationAppendData( &tmp, tmpLength ); // Populate lower nibble of 0th Byte with LED state |
ubhat | 0:42863a11464a | 47 | |
ubhat | 0:42863a11464a | 48 | /*! |
ubhat | 0:42863a11464a | 49 | * Read Temperature |
ubhat | 0:42863a11464a | 50 | * Appends 1 Byte to TX buffer |
ubhat | 0:42863a11464a | 51 | */ |
ubhat | 0:42863a11464a | 52 | LoRaApp.ApplicationCall( AppTemp ); |
ubhat | 0:42863a11464a | 53 | |
ubhat | 0:42863a11464a | 54 | /*! |
ubhat | 0:42863a11464a | 55 | * Read Battery |
ubhat | 0:42863a11464a | 56 | * Appends 1 Byte to TX buffer |
ubhat | 0:42863a11464a | 57 | */ |
ubhat | 0:42863a11464a | 58 | LoRaApp.ApplicationCall( AppBat ); |
ubhat | 0:42863a11464a | 59 | |
ubhat | 0:42863a11464a | 60 | /*! |
ubhat | 0:42863a11464a | 61 | * Read GPS coordinates |
ubhat | 0:42863a11464a | 62 | * Appends 8 Bytes (3 bytes longitude, 3 bytes latitude, 2 bytes altitude) to TX buffer |
ubhat | 0:42863a11464a | 63 | */ |
ubhat | 0:42863a11464a | 64 | LoRaApp.ApplicationCall( AppGps ); |
ubhat | 0:42863a11464a | 65 | |
ubhat | 0:42863a11464a | 66 | /*! |
ubhat | 0:42863a11464a | 67 | * Read Accelerometer |
ubhat | 0:42863a11464a | 68 | * Appends 2 Bytes to TX buffer |
ubhat | 0:42863a11464a | 69 | * Value Orientation |
ubhat | 0:42863a11464a | 70 | * 0x99 0x00 horizontal + faceup |
ubhat | 0:42863a11464a | 71 | * 0x66 0x00 horizontal + facedown |
ubhat | 0:42863a11464a | 72 | * 0x00 0x11 vertical |
ubhat | 0:42863a11464a | 73 | */ |
ubhat | 0:42863a11464a | 74 | LoRaApp.ApplicationCall( AppAccl ); // Generate Accelerometer data bytes |
ubhat | 0:42863a11464a | 75 | |
ubhat | 0:42863a11464a | 76 | /*! |
ubhat | 0:42863a11464a | 77 | * Generate Ramp data bytes |
ubhat | 0:42863a11464a | 78 | * Appends incremental values of 1 Byte each to TX buffer until Full |
ubhat | 0:42863a11464a | 79 | */ |
ubhat | 0:42863a11464a | 80 | LoRaApp.ApplicationCall( AppRamp ); |
ubhat | 0:42863a11464a | 81 | |
ubhat | 0:42863a11464a | 82 | break; |
ubhat | 0:42863a11464a | 83 | } |
ubhat | 0:42863a11464a | 84 | |
ubhat | 0:42863a11464a | 85 | // Senet M2X ORIENTATION Demo |
ubhat | 0:42863a11464a | 86 | // Set LORAWAN_APP_DATA_SIZE to 2 |
ubhat | 0:42863a11464a | 87 | case 6: |
ubhat | 0:42863a11464a | 88 | { |
ubhat | 0:42863a11464a | 89 | uint8_t ptrIndex = 1; |
ubhat | 0:42863a11464a | 90 | |
ubhat | 0:42863a11464a | 91 | //Point the pointer to position index of Tx Buffer |
ubhat | 0:42863a11464a | 92 | LoRaApp.ApplicationPtrPos( ptrIndex ); |
ubhat | 0:42863a11464a | 93 | |
ubhat | 0:42863a11464a | 94 | LoRaApp.ApplicationCall( AppAcclSenet ); // Generate Accelerometer data bytes |
ubhat | 0:42863a11464a | 95 | |
ubhat | 0:42863a11464a | 96 | |
ubhat | 0:42863a11464a | 97 | |
ubhat | 0:42863a11464a | 98 | break; |
ubhat | 0:42863a11464a | 99 | } |
ubhat | 0:42863a11464a | 100 | |
ubhat | 0:42863a11464a | 101 | /* Senet GPS Demo |
ubhat | 0:42863a11464a | 102 | Data Format (in Hex): |
ubhat | 0:42863a11464a | 103 | [01, 02, Lattitude (3 bytes), Longitude (3 Bytes), Elevation (2 bytes), Tx Power (1 byte)] |
ubhat | 0:42863a11464a | 104 | */ |
ubhat | 0:42863a11464a | 105 | case 7: |
ubhat | 0:42863a11464a | 106 | { |
ubhat | 0:42863a11464a | 107 | uint8_t ptrIndex = 0; |
ubhat | 0:42863a11464a | 108 | uint8_t tmp[] = {0x01, 0x02}; |
ubhat | 0:42863a11464a | 109 | |
ubhat | 0:42863a11464a | 110 | //Point the pointer to position index of Tx Buffer |
ubhat | 0:42863a11464a | 111 | LoRaApp.ApplicationPtrPos( ptrIndex ); |
ubhat | 0:42863a11464a | 112 | |
ubhat | 0:42863a11464a | 113 | LoRaApp.ApplicationAppendData( tmp, 2 ); |
ubhat | 0:42863a11464a | 114 | |
ubhat | 0:42863a11464a | 115 | LoRaApp.ApplicationCall( AppGps ); // Generate Accelerometer data bytes |
ubhat | 0:42863a11464a | 116 | |
ubhat | 0:42863a11464a | 117 | uint8_t pow = 30 - 2*(( uint8_t ) LoRaMacUplinkStatus.TxPower); |
ubhat | 0:42863a11464a | 118 | LoRaApp.ApplicationAppendData( &pow, 1 ); |
ubhat | 0:42863a11464a | 119 | |
ubhat | 0:42863a11464a | 120 | break; |
ubhat | 0:42863a11464a | 121 | } |
ubhat | 0:42863a11464a | 122 | |
ubhat | 0:42863a11464a | 123 | // Push-Button Demo |
ubhat | 0:42863a11464a | 124 | case 11: |
ubhat | 0:42863a11464a | 125 | { |
ubhat | 0:42863a11464a | 126 | uint8_t ptrIndex = 0; |
ubhat | 0:42863a11464a | 127 | |
ubhat | 0:42863a11464a | 128 | //Point the pointer to position index of Tx Buffer |
ubhat | 0:42863a11464a | 129 | LoRaApp.ApplicationPtrPos( ptrIndex ); |
ubhat | 0:42863a11464a | 130 | |
ubhat | 0:42863a11464a | 131 | LoRaApp.ApplicationCall( AppPushButton ); // Transmit uplink counter |
ubhat | 0:42863a11464a | 132 | |
ubhat | 0:42863a11464a | 133 | break; |
ubhat | 0:42863a11464a | 134 | } |
ubhat | 0:42863a11464a | 135 | |
ubhat | 0:42863a11464a | 136 | // Transmit on Vertical Orientation Demo |
ubhat | 0:42863a11464a | 137 | case 12: |
ubhat | 0:42863a11464a | 138 | { |
ubhat | 0:42863a11464a | 139 | uint8_t ptrIndex = 0; |
ubhat | 0:42863a11464a | 140 | |
ubhat | 0:42863a11464a | 141 | //Point the pointer to position index of Tx Buffer |
ubhat | 0:42863a11464a | 142 | LoRaApp.ApplicationPtrPos( ptrIndex ); |
ubhat | 0:42863a11464a | 143 | |
ubhat | 0:42863a11464a | 144 | LoRaApp.ApplicationCall( AppPushButton ); // Transmit uplink counter |
ubhat | 0:42863a11464a | 145 | |
ubhat | 0:42863a11464a | 146 | break; |
ubhat | 0:42863a11464a | 147 | } |
ubhat | 0:42863a11464a | 148 | |
ubhat | 0:42863a11464a | 149 | default: |
ubhat | 0:42863a11464a | 150 | break; |
ubhat | 0:42863a11464a | 151 | } |
ubhat | 0:42863a11464a | 152 | } |
ubhat | 0:42863a11464a | 153 | |
ubhat | 0:42863a11464a | 154 | |
ubhat | 0:42863a11464a | 155 | /*! |
ubhat | 0:42863a11464a | 156 | * \brief Sets Interrupt for next payload transmission |
ubhat | 0:42863a11464a | 157 | */ |
ubhat | 0:42863a11464a | 158 | void InitNextTxInterrupt( uint8_t port ) |
ubhat | 0:42863a11464a | 159 | { |
ubhat | 0:42863a11464a | 160 | switch( port ) |
ubhat | 0:42863a11464a | 161 | { |
ubhat | 0:42863a11464a | 162 | /* GPS Application Demo |
ubhat | 0:42863a11464a | 163 | Set Timer interrupt for next uplink |
ubhat | 0:42863a11464a | 164 | */ |
ubhat | 0:42863a11464a | 165 | case 5: |
ubhat | 0:42863a11464a | 166 | { |
ubhat | 0:42863a11464a | 167 | } |
ubhat | 0:42863a11464a | 168 | |
ubhat | 0:42863a11464a | 169 | /* Senet + M2X demo |
ubhat | 0:42863a11464a | 170 | Set Timer interrupt for next uplink |
ubhat | 0:42863a11464a | 171 | */ |
ubhat | 0:42863a11464a | 172 | case 6: |
ubhat | 0:42863a11464a | 173 | { |
ubhat | 0:42863a11464a | 174 | |
ubhat | 0:42863a11464a | 175 | } |
ubhat | 0:42863a11464a | 176 | |
ubhat | 0:42863a11464a | 177 | /* Senet GPS Demo |
ubhat | 0:42863a11464a | 178 | Set Timer interrupt for next uplink |
ubhat | 0:42863a11464a | 179 | */ |
ubhat | 0:42863a11464a | 180 | case 7: |
ubhat | 0:42863a11464a | 181 | { |
ubhat | 0:42863a11464a | 182 | // Schedule next packet transmission |
ubhat | 0:42863a11464a | 183 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
ubhat | 0:42863a11464a | 184 | TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); |
ubhat | 0:42863a11464a | 185 | TimerStart( &TxNextPacketTimer ); |
ubhat | 0:42863a11464a | 186 | break; |
ubhat | 0:42863a11464a | 187 | } |
ubhat | 0:42863a11464a | 188 | |
ubhat | 0:42863a11464a | 189 | /* Push Button Demo |
ubhat | 0:42863a11464a | 190 | Send Packet Immedietly if PC0 = GND |
ubhat | 0:42863a11464a | 191 | */ |
ubhat | 0:42863a11464a | 192 | case 11: |
ubhat | 1:80c1daf19aa4 | 193 | { |
ubhat | 0:42863a11464a | 194 | volatile bool PushButtonStatus; |
ubhat | 0:42863a11464a | 195 | |
ubhat | 1:80c1daf19aa4 | 196 | PushButtonStatus = UsrButton; |
ubhat | 0:42863a11464a | 197 | |
ubhat | 0:42863a11464a | 198 | if(PushButtonStatus == 0) |
ubhat | 0:42863a11464a | 199 | { |
ubhat | 0:42863a11464a | 200 | // Send Pkt immedietly if PC = GND |
ubhat | 0:42863a11464a | 201 | DeviceState = DEVICE_STATE_SEND; |
ubhat | 0:42863a11464a | 202 | NextTx = true; |
ubhat | 0:42863a11464a | 203 | } |
ubhat | 0:42863a11464a | 204 | else |
ubhat | 0:42863a11464a | 205 | { |
ubhat | 0:42863a11464a | 206 | // Keep polling |
ubhat | 0:42863a11464a | 207 | IsTxIntUpdate = true; |
ubhat | 0:42863a11464a | 208 | } |
ubhat | 1:80c1daf19aa4 | 209 | |
ubhat | 0:42863a11464a | 210 | break; |
ubhat | 0:42863a11464a | 211 | } |
ubhat | 0:42863a11464a | 212 | |
ubhat | 0:42863a11464a | 213 | /* Orientation Demo |
ubhat | 0:42863a11464a | 214 | Send Packet Immedietly if Mote is Vertical |
ubhat | 0:42863a11464a | 215 | */ |
ubhat | 0:42863a11464a | 216 | case 12: |
ubhat | 0:42863a11464a | 217 | { |
ubhat | 0:42863a11464a | 218 | CheckOrientation( ); |
ubhat | 0:42863a11464a | 219 | |
ubhat | 0:42863a11464a | 220 | if(VerticalStatus == true) |
ubhat | 0:42863a11464a | 221 | { |
ubhat | 0:42863a11464a | 222 | // Send Pkt immedietly if PC = GND |
ubhat | 0:42863a11464a | 223 | DeviceState = DEVICE_STATE_SEND; |
ubhat | 0:42863a11464a | 224 | NextTx = true; |
ubhat | 0:42863a11464a | 225 | } |
ubhat | 0:42863a11464a | 226 | else |
ubhat | 0:42863a11464a | 227 | { |
ubhat | 0:42863a11464a | 228 | // Keep polling |
ubhat | 0:42863a11464a | 229 | IsTxIntUpdate = true; |
ubhat | 0:42863a11464a | 230 | } |
ubhat | 0:42863a11464a | 231 | break; |
ubhat | 0:42863a11464a | 232 | } |
ubhat | 0:42863a11464a | 233 | |
ubhat | 0:42863a11464a | 234 | /* Compliance Test |
ubhat | 0:42863a11464a | 235 | Set Timer interrupt for next uplink |
ubhat | 0:42863a11464a | 236 | */ |
ubhat | 0:42863a11464a | 237 | case 224: |
ubhat | 0:42863a11464a | 238 | { |
ubhat | 0:42863a11464a | 239 | // Schedule next packet transmission |
ubhat | 0:42863a11464a | 240 | TimerSetValue( &TxNextPacketTimer, COMPLIANCE_TX_DUTYCYCLE ); |
ubhat | 0:42863a11464a | 241 | TimerStart( &TxNextPacketTimer ); |
ubhat | 0:42863a11464a | 242 | break; |
ubhat | 0:42863a11464a | 243 | } |
ubhat | 0:42863a11464a | 244 | |
ubhat | 0:42863a11464a | 245 | default: |
ubhat | 0:42863a11464a | 246 | { |
ubhat | 0:42863a11464a | 247 | // Schedule next packet transmission |
ubhat | 0:42863a11464a | 248 | TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); |
ubhat | 0:42863a11464a | 249 | TimerStart( &TxNextPacketTimer ); |
ubhat | 0:42863a11464a | 250 | break; |
ubhat | 0:42863a11464a | 251 | } |
ubhat | 0:42863a11464a | 252 | } |
ubhat | 0:42863a11464a | 253 | |
ubhat | 0:42863a11464a | 254 | } |
ubhat | 0:42863a11464a | 255 | |
ubhat | 0:42863a11464a | 256 | /*! |
ubhat | 0:42863a11464a | 257 | * \brief What to do during JOIN process ? blink/toggle LED etc. |
ubhat | 0:42863a11464a | 258 | */ |
ubhat | 0:42863a11464a | 259 | void JoinEvent( void ) |
ubhat | 0:42863a11464a | 260 | { |
ubhat | 0:42863a11464a | 261 | // CtrlLED is defined in LoRaBoardAppIf.h |
ubhat | 0:42863a11464a | 262 | // param 1: LED color (Red, Yellow or Green) |
ubhat | 0:42863a11464a | 263 | // param 2: LED_ON or LED_OFF |
ubhat | 0:42863a11464a | 264 | //CtrlLED( Red, LED_ON ); |
ubhat | 0:42863a11464a | 265 | } |
ubhat | 0:42863a11464a | 266 | |
ubhat | 0:42863a11464a | 267 | |
ubhat | 0:42863a11464a | 268 | /*! |
ubhat | 0:42863a11464a | 269 | * \brief What to do during TX ? blink/toggle LED etc. |
ubhat | 0:42863a11464a | 270 | */ |
ubhat | 0:42863a11464a | 271 | void TxEvent( void ) |
ubhat | 0:42863a11464a | 272 | { |
ubhat | 0:42863a11464a | 273 | int blinkTime = 25000; |
ubhat | 0:42863a11464a | 274 | |
ubhat | 0:42863a11464a | 275 | // Blink Red LED for 25msec |
ubhat | 0:42863a11464a | 276 | //BlinkLED( Red, blinkTime ); |
ubhat | 0:42863a11464a | 277 | } |
ubhat | 0:42863a11464a | 278 | |
ubhat | 0:42863a11464a | 279 | void RxEvent() |
ubhat | 0:42863a11464a | 280 | { |
ubhat | 0:42863a11464a | 281 | // Toggle yellow LED |
ubhat | 0:42863a11464a | 282 | //ToggleLED( Yellow ); |
ubhat | 0:42863a11464a | 283 | |
ubhat | 0:42863a11464a | 284 | // If Rx Data is 0x01 turn on Green LED else if 0x0 Turn Green LED off |
ubhat | 0:42863a11464a | 285 | if( LoRaMacDownlinkStatus.BufferSize == 1 ) |
ubhat | 0:42863a11464a | 286 | { |
ubhat | 0:42863a11464a | 287 | if( LoRaMacDownlinkStatus.Buffer[0] == 0x01 ) |
ubhat | 0:42863a11464a | 288 | { |
ubhat | 0:42863a11464a | 289 | AppLed = 1; |
ubhat | 0:42863a11464a | 290 | } |
ubhat | 0:42863a11464a | 291 | else |
ubhat | 0:42863a11464a | 292 | { |
ubhat | 0:42863a11464a | 293 | if( LoRaMacDownlinkStatus.Buffer[0] == 0x00 ) |
ubhat | 0:42863a11464a | 294 | { |
ubhat | 0:42863a11464a | 295 | AppLed = 0; |
ubhat | 0:42863a11464a | 296 | } |
ubhat | 0:42863a11464a | 297 | } |
ubhat | 0:42863a11464a | 298 | } |
ubhat | 0:42863a11464a | 299 | |
ubhat | 0:42863a11464a | 300 | if( AppLed != 0 ) |
ubhat | 0:42863a11464a | 301 | { |
ubhat | 0:42863a11464a | 302 | // Turn USR_LED ON |
ubhat | 0:42863a11464a | 303 | //CtrlLED( Usr, LED_ON ); |
ubhat | 0:42863a11464a | 304 | } |
ubhat | 0:42863a11464a | 305 | else |
ubhat | 0:42863a11464a | 306 | { |
ubhat | 0:42863a11464a | 307 | // Turn USR_LED OFF |
ubhat | 0:42863a11464a | 308 | //CtrlLED( Usr, LED_OFF ); |
ubhat | 0:42863a11464a | 309 | } |
ubhat | 0:42863a11464a | 310 | } |
ubhat | 0:42863a11464a | 311 |