light sensor

Dependencies:   X_NUCLEO_IKS01A1 LoRaWAN-lib SX1276Lib mbed

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

Committer:
ubhat
Date:
Sat Aug 27 04:03:02 2016 +0000
Revision:
2:78df92a365c2
Parent:
1:80c1daf19aa4
Child:
3:4bca7f8f731a
X_Nucleo_IKS01A1 Sensor application (Port 8)

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 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 2:78df92a365c2 49 * Generate Ramp data bytes
ubhat 2:78df92a365c2 50 * Appends incremental values of 1 Byte each to TX buffer until Full
ubhat 2:78df92a365c2 51 */
ubhat 2:78df92a365c2 52 LoRaApp.ApplicationCall( AppRamp );
ubhat 2:78df92a365c2 53
ubhat 2:78df92a365c2 54 break;
ubhat 2:78df92a365c2 55 }
ubhat 2:78df92a365c2 56
ubhat 2:78df92a365c2 57 case 8:
ubhat 2:78df92a365c2 58 {
ubhat 2:78df92a365c2 59 uint8_t ptrIndex = 0;
ubhat 2:78df92a365c2 60
ubhat 2:78df92a365c2 61 //Point the pointer to position index of Tx Buffer
ubhat 2:78df92a365c2 62 LoRaApp.ApplicationPtrPos( ptrIndex );
ubhat 2:78df92a365c2 63
ubhat 2:78df92a365c2 64 /*!
ubhat 2:78df92a365c2 65 * Read Pressure
ubhat 2:78df92a365c2 66 * Appends 2 Bytes to TX buffer
ubhat 2:78df92a365c2 67 */
ubhat 2:78df92a365c2 68 LoRaApp.ApplicationCall( AppPressr );
ubhat 2:78df92a365c2 69
ubhat 2:78df92a365c2 70 /*!
ubhat 0:42863a11464a 71 * Read Temperature
ubhat 0:42863a11464a 72 * Appends 1 Byte to TX buffer
ubhat 0:42863a11464a 73 */
ubhat 0:42863a11464a 74 LoRaApp.ApplicationCall( AppTemp );
ubhat 2:78df92a365c2 75
ubhat 0:42863a11464a 76 /*!
ubhat 2:78df92a365c2 77 * Read Humidity
ubhat 0:42863a11464a 78 * Appends 1 Byte to TX buffer
ubhat 0:42863a11464a 79 */
ubhat 2:78df92a365c2 80 LoRaApp.ApplicationCall( AppHumid );
ubhat 0:42863a11464a 81
ubhat 0:42863a11464a 82 /*!
ubhat 0:42863a11464a 83 * Read Accelerometer
ubhat 0:42863a11464a 84 * Appends 2 Bytes to TX buffer
ubhat 0:42863a11464a 85 * Value Orientation
ubhat 0:42863a11464a 86 * 0x99 0x00 horizontal + faceup
ubhat 0:42863a11464a 87 * 0x66 0x00 horizontal + facedown
ubhat 0:42863a11464a 88 * 0x00 0x11 vertical
ubhat 0:42863a11464a 89 */
ubhat 0:42863a11464a 90 LoRaApp.ApplicationCall( AppAccl ); // Generate Accelerometer data bytes
ubhat 0:42863a11464a 91
ubhat 0:42863a11464a 92
ubhat 0:42863a11464a 93 break;
ubhat 0:42863a11464a 94 }
ubhat 0:42863a11464a 95
ubhat 0:42863a11464a 96 // Push-Button Demo
ubhat 0:42863a11464a 97 case 11:
ubhat 0:42863a11464a 98 {
ubhat 0:42863a11464a 99 uint8_t ptrIndex = 0;
ubhat 0:42863a11464a 100
ubhat 0:42863a11464a 101 //Point the pointer to position index of Tx Buffer
ubhat 0:42863a11464a 102 LoRaApp.ApplicationPtrPos( ptrIndex );
ubhat 0:42863a11464a 103
ubhat 0:42863a11464a 104 LoRaApp.ApplicationCall( AppPushButton ); // Transmit uplink counter
ubhat 0:42863a11464a 105
ubhat 0:42863a11464a 106 break;
ubhat 0:42863a11464a 107 }
ubhat 2:78df92a365c2 108
ubhat 0:42863a11464a 109 default:
ubhat 0:42863a11464a 110 break;
ubhat 0:42863a11464a 111 }
ubhat 0:42863a11464a 112 }
ubhat 0:42863a11464a 113
ubhat 0:42863a11464a 114
ubhat 0:42863a11464a 115 /*!
ubhat 0:42863a11464a 116 * \brief Sets Interrupt for next payload transmission
ubhat 0:42863a11464a 117 */
ubhat 0:42863a11464a 118 void InitNextTxInterrupt( uint8_t port )
ubhat 0:42863a11464a 119 {
ubhat 0:42863a11464a 120 switch( port )
ubhat 0:42863a11464a 121 {
ubhat 0:42863a11464a 122 /* GPS Application Demo
ubhat 0:42863a11464a 123 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 124 */
ubhat 0:42863a11464a 125 case 5:
ubhat 0:42863a11464a 126 {
ubhat 0:42863a11464a 127 }
ubhat 0:42863a11464a 128
ubhat 0:42863a11464a 129 /* Senet + M2X demo
ubhat 0:42863a11464a 130 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 131 */
ubhat 0:42863a11464a 132 case 6:
ubhat 0:42863a11464a 133 {
ubhat 0:42863a11464a 134
ubhat 0:42863a11464a 135 }
ubhat 0:42863a11464a 136
ubhat 0:42863a11464a 137 /* Senet GPS Demo
ubhat 0:42863a11464a 138 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 139 */
ubhat 0:42863a11464a 140 case 7:
ubhat 0:42863a11464a 141 {
ubhat 0:42863a11464a 142 // Schedule next packet transmission
ubhat 0:42863a11464a 143 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
ubhat 0:42863a11464a 144 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
ubhat 0:42863a11464a 145 TimerStart( &TxNextPacketTimer );
ubhat 0:42863a11464a 146 break;
ubhat 0:42863a11464a 147 }
ubhat 0:42863a11464a 148
ubhat 0:42863a11464a 149 /* Push Button Demo
ubhat 0:42863a11464a 150 Send Packet Immedietly if PC0 = GND
ubhat 0:42863a11464a 151 */
ubhat 0:42863a11464a 152 case 11:
ubhat 1:80c1daf19aa4 153 {
ubhat 0:42863a11464a 154 volatile bool PushButtonStatus;
ubhat 0:42863a11464a 155
ubhat 1:80c1daf19aa4 156 PushButtonStatus = UsrButton;
ubhat 0:42863a11464a 157
ubhat 0:42863a11464a 158 if(PushButtonStatus == 0)
ubhat 0:42863a11464a 159 {
ubhat 0:42863a11464a 160 // Send Pkt immedietly if PC = GND
ubhat 0:42863a11464a 161 DeviceState = DEVICE_STATE_SEND;
ubhat 0:42863a11464a 162 NextTx = true;
ubhat 0:42863a11464a 163 }
ubhat 0:42863a11464a 164 else
ubhat 0:42863a11464a 165 {
ubhat 0:42863a11464a 166 // Keep polling
ubhat 0:42863a11464a 167 IsTxIntUpdate = true;
ubhat 0:42863a11464a 168 }
ubhat 1:80c1daf19aa4 169
ubhat 0:42863a11464a 170 break;
ubhat 0:42863a11464a 171 }
ubhat 0:42863a11464a 172
ubhat 0:42863a11464a 173 /* Orientation Demo
ubhat 0:42863a11464a 174 Send Packet Immedietly if Mote is Vertical
ubhat 0:42863a11464a 175 */
ubhat 0:42863a11464a 176 case 12:
ubhat 0:42863a11464a 177 {
ubhat 0:42863a11464a 178 CheckOrientation( );
ubhat 0:42863a11464a 179
ubhat 0:42863a11464a 180 if(VerticalStatus == true)
ubhat 0:42863a11464a 181 {
ubhat 0:42863a11464a 182 // Send Pkt immedietly if PC = GND
ubhat 0:42863a11464a 183 DeviceState = DEVICE_STATE_SEND;
ubhat 0:42863a11464a 184 NextTx = true;
ubhat 0:42863a11464a 185 }
ubhat 0:42863a11464a 186 else
ubhat 0:42863a11464a 187 {
ubhat 0:42863a11464a 188 // Keep polling
ubhat 0:42863a11464a 189 IsTxIntUpdate = true;
ubhat 0:42863a11464a 190 }
ubhat 0:42863a11464a 191 break;
ubhat 0:42863a11464a 192 }
ubhat 0:42863a11464a 193
ubhat 0:42863a11464a 194 /* Compliance Test
ubhat 0:42863a11464a 195 Set Timer interrupt for next uplink
ubhat 0:42863a11464a 196 */
ubhat 0:42863a11464a 197 case 224:
ubhat 0:42863a11464a 198 {
ubhat 0:42863a11464a 199 // Schedule next packet transmission
ubhat 0:42863a11464a 200 TimerSetValue( &TxNextPacketTimer, COMPLIANCE_TX_DUTYCYCLE );
ubhat 0:42863a11464a 201 TimerStart( &TxNextPacketTimer );
ubhat 0:42863a11464a 202 break;
ubhat 0:42863a11464a 203 }
ubhat 0:42863a11464a 204
ubhat 0:42863a11464a 205 default:
ubhat 0:42863a11464a 206 {
ubhat 0:42863a11464a 207 // Schedule next packet transmission
ubhat 0:42863a11464a 208 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
ubhat 0:42863a11464a 209 TimerStart( &TxNextPacketTimer );
ubhat 0:42863a11464a 210 break;
ubhat 0:42863a11464a 211 }
ubhat 0:42863a11464a 212 }
ubhat 0:42863a11464a 213
ubhat 0:42863a11464a 214 }
ubhat 0:42863a11464a 215
ubhat 0:42863a11464a 216 /*!
ubhat 0:42863a11464a 217 * \brief What to do during JOIN process ? blink/toggle LED etc.
ubhat 0:42863a11464a 218 */
ubhat 0:42863a11464a 219 void JoinEvent( void )
ubhat 0:42863a11464a 220 {
ubhat 0:42863a11464a 221 // CtrlLED is defined in LoRaBoardAppIf.h
ubhat 0:42863a11464a 222 // param 1: LED color (Red, Yellow or Green)
ubhat 0:42863a11464a 223 // param 2: LED_ON or LED_OFF
ubhat 0:42863a11464a 224 //CtrlLED( Red, LED_ON );
ubhat 0:42863a11464a 225 }
ubhat 0:42863a11464a 226
ubhat 0:42863a11464a 227
ubhat 0:42863a11464a 228 /*!
ubhat 0:42863a11464a 229 * \brief What to do during TX ? blink/toggle LED etc.
ubhat 0:42863a11464a 230 */
ubhat 0:42863a11464a 231 void TxEvent( void )
ubhat 0:42863a11464a 232 {
ubhat 0:42863a11464a 233 int blinkTime = 25000;
ubhat 0:42863a11464a 234
ubhat 0:42863a11464a 235 // Blink Red LED for 25msec
ubhat 0:42863a11464a 236 //BlinkLED( Red, blinkTime );
ubhat 0:42863a11464a 237 }
ubhat 0:42863a11464a 238
ubhat 0:42863a11464a 239 void RxEvent()
ubhat 0:42863a11464a 240 {
ubhat 0:42863a11464a 241 // Toggle yellow LED
ubhat 0:42863a11464a 242 //ToggleLED( Yellow );
ubhat 0:42863a11464a 243
ubhat 0:42863a11464a 244 // If Rx Data is 0x01 turn on Green LED else if 0x0 Turn Green LED off
ubhat 0:42863a11464a 245 if( LoRaMacDownlinkStatus.BufferSize == 1 )
ubhat 0:42863a11464a 246 {
ubhat 0:42863a11464a 247 if( LoRaMacDownlinkStatus.Buffer[0] == 0x01 )
ubhat 0:42863a11464a 248 {
ubhat 0:42863a11464a 249 AppLed = 1;
ubhat 0:42863a11464a 250 }
ubhat 0:42863a11464a 251 else
ubhat 0:42863a11464a 252 {
ubhat 0:42863a11464a 253 if( LoRaMacDownlinkStatus.Buffer[0] == 0x00 )
ubhat 0:42863a11464a 254 {
ubhat 0:42863a11464a 255 AppLed = 0;
ubhat 0:42863a11464a 256 }
ubhat 0:42863a11464a 257 }
ubhat 0:42863a11464a 258 }
ubhat 0:42863a11464a 259
ubhat 0:42863a11464a 260 if( AppLed != 0 )
ubhat 0:42863a11464a 261 {
ubhat 0:42863a11464a 262 // Turn USR_LED ON
ubhat 2:78df92a365c2 263 //UsrLED = 3.3;
ubhat 0:42863a11464a 264 }
ubhat 0:42863a11464a 265 else
ubhat 0:42863a11464a 266 {
ubhat 0:42863a11464a 267 // Turn USR_LED OFF
ubhat 2:78df92a365c2 268 //UsrLED = 0;
ubhat 0:42863a11464a 269 }
ubhat 0:42863a11464a 270 }
ubhat 0:42863a11464a 271