Bootcamp application example using LoRaWAN-lib MAC layer implementation. Provides an application example controlling a 3 color LED and a light sensor.

Dependencies:   mbed Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72 by Semtech

For a detailed description of the LoRaWAN operations, please visit the MBED dedicated page at https://developer.mbed.org/teams/Semtech/code/LoRaWAN-demo-72/

Committer:
mluis
Date:
Mon Apr 24 13:31:49 2017 +0000
Revision:
9:0083afd69815
Parent:
7:ceb4063e6863
WARNING: Radio API timings changed from micro-seconds to milliseconds; ; Synchronized with https://github.com/Lora-net/LoRaMac-node git revision e506c246652fa44c3f24cecb89d0707b49ece739; Updated all libraries to the latest versions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:45496a70a8a5 1 /*
mluis 0:45496a70a8a5 2 / _____) _ | |
mluis 0:45496a70a8a5 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:45496a70a8a5 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:45496a70a8a5 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:45496a70a8a5 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:45496a70a8a5 7 (C)2015 Semtech
mluis 0:45496a70a8a5 8
mluis 0:45496a70a8a5 9 Description: LoRaMac classA device implementation
mluis 0:45496a70a8a5 10
mluis 0:45496a70a8a5 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:45496a70a8a5 12
mluis 0:45496a70a8a5 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:45496a70a8a5 14 */
mluis 0:45496a70a8a5 15 #include "mbed.h"
mluis 0:45496a70a8a5 16 #include "board.h"
mluis 0:45496a70a8a5 17 #include "radio.h"
mluis 0:45496a70a8a5 18
mluis 0:45496a70a8a5 19 #include "LoRaMac.h"
mluis 9:0083afd69815 20 #include "Commissioning.h"
mluis 0:45496a70a8a5 21 #include "SerialDisplay.h"
GregCr 2:48d8d4806d48 22 #include "DigitDisplay.h"
GregCr 2:48d8d4806d48 23 #include "ChainableLED.h"
mluis 0:45496a70a8a5 24
mluis 0:45496a70a8a5 25 /*!
mluis 9:0083afd69815 26 * Defines the application data transmission duty cycle. 5s, value in [ms].
mluis 0:45496a70a8a5 27 */
mluis 9:0083afd69815 28 #define APP_TX_DUTYCYCLE 5000
mluis 0:45496a70a8a5 29
mluis 0:45496a70a8a5 30 /*!
mluis 0:45496a70a8a5 31 * Defines a random delay for application data transmission duty cycle. 1s,
mluis 9:0083afd69815 32 * value in [ms].
mluis 0:45496a70a8a5 33 */
mluis 9:0083afd69815 34 #define APP_TX_DUTYCYCLE_RND 1000
mluis 0:45496a70a8a5 35
mluis 0:45496a70a8a5 36 /*!
mluis 5:fa113b25f612 37 * Default datarate
mluis 0:45496a70a8a5 38 */
GregCr 2:48d8d4806d48 39 #define LORAWAN_DEFAULT_DATARATE DR_5
mluis 0:45496a70a8a5 40
mluis 0:45496a70a8a5 41 /*!
mluis 0:45496a70a8a5 42 * LoRaWAN confirmed messages
mluis 0:45496a70a8a5 43 */
GregCr 2:48d8d4806d48 44 #define LORAWAN_CONFIRMED_MSG_ON false
mluis 0:45496a70a8a5 45
mluis 0:45496a70a8a5 46 /*!
mluis 0:45496a70a8a5 47 * LoRaWAN Adaptive Data Rate
mluis 0:45496a70a8a5 48 *
mluis 0:45496a70a8a5 49 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:45496a70a8a5 50 */
mluis 0:45496a70a8a5 51 #define LORAWAN_ADR_ON 1
mluis 0:45496a70a8a5 52
mluis 0:45496a70a8a5 53 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 54
mluis 0:45496a70a8a5 55 #include "LoRaMacTest.h"
mluis 0:45496a70a8a5 56
mluis 0:45496a70a8a5 57 /*!
mluis 0:45496a70a8a5 58 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:45496a70a8a5 59 *
mluis 0:45496a70a8a5 60 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:45496a70a8a5 61 */
GregCr 2:48d8d4806d48 62 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:45496a70a8a5 63
mluis 5:fa113b25f612 64 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 5:fa113b25f612 65
mluis 9:0083afd69815 66 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:fa113b25f612 67
mluis 5:fa113b25f612 68 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 69 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 70 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 71 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 72 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 73 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 5:fa113b25f612 74 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 5:fa113b25f612 75
mluis 5:fa113b25f612 76 #endif
mluis 5:fa113b25f612 77
mluis 0:45496a70a8a5 78 #endif
mluis 0:45496a70a8a5 79
mluis 0:45496a70a8a5 80 /*!
mluis 0:45496a70a8a5 81 * LoRaWAN application port
mluis 0:45496a70a8a5 82 */
GregCr 3:d4142832d5af 83 #define LORAWAN_APP_PORT 10
mluis 0:45496a70a8a5 84
mluis 0:45496a70a8a5 85 /*!
mluis 0:45496a70a8a5 86 * User application data buffer size
mluis 0:45496a70a8a5 87 */
mluis 0:45496a70a8a5 88 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 0:45496a70a8a5 89 #define LORAWAN_APP_DATA_SIZE 6
mluis 0:45496a70a8a5 90
mluis 0:45496a70a8a5 91 #else
GregCr 3:d4142832d5af 92 #define LORAWAN_APP_DATA_SIZE 5
mluis 0:45496a70a8a5 93
mluis 0:45496a70a8a5 94 #endif
mluis 0:45496a70a8a5 95
mluis 0:45496a70a8a5 96 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:45496a70a8a5 97 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:45496a70a8a5 98 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:45496a70a8a5 99
mluis 7:ceb4063e6863 100 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:45496a70a8a5 101
mluis 0:45496a70a8a5 102 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:45496a70a8a5 103 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:45496a70a8a5 104
mluis 0:45496a70a8a5 105 /*!
mluis 0:45496a70a8a5 106 * Device address
mluis 0:45496a70a8a5 107 */
mluis 0:45496a70a8a5 108 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:45496a70a8a5 109
mluis 0:45496a70a8a5 110 #endif
mluis 0:45496a70a8a5 111
mluis 0:45496a70a8a5 112 /*!
mluis 0:45496a70a8a5 113 * Application port
mluis 0:45496a70a8a5 114 */
mluis 0:45496a70a8a5 115 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 116
mluis 0:45496a70a8a5 117 /*!
mluis 0:45496a70a8a5 118 * User application data size
mluis 0:45496a70a8a5 119 */
mluis 0:45496a70a8a5 120 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 121
mluis 0:45496a70a8a5 122 /*!
mluis 0:45496a70a8a5 123 * User application data buffer size
mluis 0:45496a70a8a5 124 */
mluis 0:45496a70a8a5 125 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 0:45496a70a8a5 126
mluis 0:45496a70a8a5 127 /*!
mluis 0:45496a70a8a5 128 * User application data
mluis 0:45496a70a8a5 129 */
mluis 0:45496a70a8a5 130 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 0:45496a70a8a5 131
mluis 0:45496a70a8a5 132 /*!
mluis 0:45496a70a8a5 133 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 0:45496a70a8a5 134 */
mluis 0:45496a70a8a5 135 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 136
mluis 0:45496a70a8a5 137 /*!
mluis 0:45496a70a8a5 138 * Defines the application data transmission duty cycle
mluis 0:45496a70a8a5 139 */
mluis 0:45496a70a8a5 140 static uint32_t TxDutyCycleTime;
mluis 0:45496a70a8a5 141
mluis 0:45496a70a8a5 142 /*!
mluis 0:45496a70a8a5 143 * Timer to handle the application data transmission duty cycle
mluis 0:45496a70a8a5 144 */
mluis 0:45496a70a8a5 145 static TimerEvent_t TxNextPacketTimer;
mluis 0:45496a70a8a5 146
mluis 0:45496a70a8a5 147 /*!
mluis 0:45496a70a8a5 148 * Specifies the state of the application LED
mluis 0:45496a70a8a5 149 */
mluis 0:45496a70a8a5 150 static bool AppLedStateOn = false;
mluis 0:45496a70a8a5 151 volatile bool Led3StateChanged = false;
mluis 0:45496a70a8a5 152 /*!
mluis 0:45496a70a8a5 153 * Timer to handle the state of LED1
mluis 0:45496a70a8a5 154 */
mluis 0:45496a70a8a5 155 static TimerEvent_t Led1Timer;
mluis 0:45496a70a8a5 156 volatile bool Led1State = false;
mluis 0:45496a70a8a5 157 volatile bool Led1StateChanged = false;
mluis 0:45496a70a8a5 158 /*!
mluis 0:45496a70a8a5 159 * Timer to handle the state of LED2
mluis 0:45496a70a8a5 160 */
mluis 0:45496a70a8a5 161 static TimerEvent_t Led2Timer;
mluis 0:45496a70a8a5 162 volatile bool Led2State = false;
mluis 0:45496a70a8a5 163 volatile bool Led2StateChanged = false;
mluis 0:45496a70a8a5 164
mluis 0:45496a70a8a5 165 /*!
mluis 0:45496a70a8a5 166 * Indicates if a new packet can be sent
mluis 0:45496a70a8a5 167 */
mluis 0:45496a70a8a5 168 static bool NextTx = true;
mluis 0:45496a70a8a5 169
GregCr 3:d4142832d5af 170 /*!
GregCr 3:d4142832d5af 171 * Hold the value returned from the Light Sensor
GregCr 3:d4142832d5af 172 */
GregCr 3:d4142832d5af 173 static float LightValue = 0.0;
GregCr 2:48d8d4806d48 174
GregCr 3:d4142832d5af 175 /*!
GregCr 3:d4142832d5af 176 * Control the 3-color LED
GregCr 3:d4142832d5af 177 * 0: automatic (LED goes brigther as the light decrease,
GregCr 3:d4142832d5af 178 * 1: manual (The LED is controlled by the user)
GregCr 3:d4142832d5af 179 */
mluis 5:fa113b25f612 180 static uint8_t LightMode = 0; // 0: automatic, 1: manual
GregCr 3:d4142832d5af 181
GregCr 3:d4142832d5af 182 /*!
GregCr 3:d4142832d5af 183 * Ticker to create a PWM for the buzzer
GregCr 3:d4142832d5af 184 */
mluis 5:fa113b25f612 185 Ticker BuzzerTimer;
GregCr 2:48d8d4806d48 186
GregCr 2:48d8d4806d48 187
GregCr 3:d4142832d5af 188 /*!
GregCr 3:d4142832d5af 189 * Constructor for Buzzer
GregCr 3:d4142832d5af 190 */
mluis 5:fa113b25f612 191 DigitalOut Buzzer( A3 );
GregCr 3:d4142832d5af 192
GregCr 3:d4142832d5af 193 /*!
GregCr 3:d4142832d5af 194 * Constructor for the 3-color LED
GregCr 3:d4142832d5af 195 */
GregCr 3:d4142832d5af 196 #define NUM_LED 3
mluis 5:fa113b25f612 197 ChainableLED ColorLed( D6, D7, NUM_LED );
GregCr 2:48d8d4806d48 198
GregCr 3:d4142832d5af 199 /*!
GregCr 3:d4142832d5af 200 * Constructor for Light Sensor
GregCr 3:d4142832d5af 201 */
GregCr 3:d4142832d5af 202 AnalogIn LightSens( A1 );
GregCr 3:d4142832d5af 203
GregCr 3:d4142832d5af 204 /*!
GregCr 3:d4142832d5af 205 * Constructor for 4 Digit 7 semgent display
GregCr 3:d4142832d5af 206 */
mluis 5:fa113b25f612 207 DigitDisplay Display( D8, D9 );
GregCr 3:d4142832d5af 208
mluis 0:45496a70a8a5 209 /*!
mluis 0:45496a70a8a5 210 * Device states
mluis 0:45496a70a8a5 211 */
mluis 9:0083afd69815 212 static enum eDeviceState
mluis 0:45496a70a8a5 213 {
mluis 0:45496a70a8a5 214 DEVICE_STATE_INIT,
mluis 0:45496a70a8a5 215 DEVICE_STATE_JOIN,
mluis 0:45496a70a8a5 216 DEVICE_STATE_SEND,
mluis 0:45496a70a8a5 217 DEVICE_STATE_CYCLE,
mluis 0:45496a70a8a5 218 DEVICE_STATE_SLEEP
mluis 0:45496a70a8a5 219 }DeviceState;
mluis 0:45496a70a8a5 220
mluis 0:45496a70a8a5 221 /*!
mluis 0:45496a70a8a5 222 * LoRaWAN compliance tests support data
mluis 0:45496a70a8a5 223 */
mluis 0:45496a70a8a5 224 struct ComplianceTest_s
mluis 0:45496a70a8a5 225 {
mluis 0:45496a70a8a5 226 bool Running;
mluis 0:45496a70a8a5 227 uint8_t State;
mluis 0:45496a70a8a5 228 bool IsTxConfirmed;
mluis 0:45496a70a8a5 229 uint8_t AppPort;
mluis 0:45496a70a8a5 230 uint8_t AppDataSize;
mluis 0:45496a70a8a5 231 uint8_t *AppDataBuffer;
mluis 0:45496a70a8a5 232 uint16_t DownLinkCounter;
mluis 0:45496a70a8a5 233 bool LinkCheck;
mluis 0:45496a70a8a5 234 uint8_t DemodMargin;
mluis 0:45496a70a8a5 235 uint8_t NbGateways;
mluis 0:45496a70a8a5 236 }ComplianceTest;
mluis 0:45496a70a8a5 237
mluis 0:45496a70a8a5 238 /*
mluis 0:45496a70a8a5 239 * SerialDisplay managment variables
mluis 0:45496a70a8a5 240 */
mluis 0:45496a70a8a5 241
mluis 0:45496a70a8a5 242 /*!
mluis 0:45496a70a8a5 243 * Indicates if the MAC layer network join status has changed.
mluis 0:45496a70a8a5 244 */
mluis 0:45496a70a8a5 245 static bool IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 246
mluis 0:45496a70a8a5 247 /*!
mluis 0:45496a70a8a5 248 * Strucure containing the Uplink status
mluis 0:45496a70a8a5 249 */
mluis 0:45496a70a8a5 250 struct sLoRaMacUplinkStatus
mluis 0:45496a70a8a5 251 {
mluis 0:45496a70a8a5 252 uint8_t Acked;
mluis 0:45496a70a8a5 253 int8_t Datarate;
mluis 0:45496a70a8a5 254 uint16_t UplinkCounter;
mluis 0:45496a70a8a5 255 uint8_t Port;
mluis 0:45496a70a8a5 256 uint8_t *Buffer;
mluis 0:45496a70a8a5 257 uint8_t BufferSize;
mluis 0:45496a70a8a5 258 }LoRaMacUplinkStatus;
mluis 0:45496a70a8a5 259 volatile bool UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 260
mluis 0:45496a70a8a5 261 /*!
mluis 0:45496a70a8a5 262 * Strucure containing the Downlink status
mluis 0:45496a70a8a5 263 */
mluis 0:45496a70a8a5 264 struct sLoRaMacDownlinkStatus
mluis 0:45496a70a8a5 265 {
mluis 0:45496a70a8a5 266 int16_t Rssi;
mluis 0:45496a70a8a5 267 int8_t Snr;
mluis 0:45496a70a8a5 268 uint16_t DownlinkCounter;
mluis 0:45496a70a8a5 269 bool RxData;
mluis 0:45496a70a8a5 270 uint8_t Port;
mluis 0:45496a70a8a5 271 uint8_t *Buffer;
mluis 0:45496a70a8a5 272 uint8_t BufferSize;
mluis 0:45496a70a8a5 273 }LoRaMacDownlinkStatus;
mluis 0:45496a70a8a5 274 volatile bool DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 275
mluis 0:45496a70a8a5 276 void SerialDisplayRefresh( void )
mluis 0:45496a70a8a5 277 {
mluis 0:45496a70a8a5 278 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 279
mluis 0:45496a70a8a5 280 SerialDisplayInit( );
mluis 0:45496a70a8a5 281 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 282
mluis 0:45496a70a8a5 283 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:45496a70a8a5 284 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:45496a70a8a5 285 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:45496a70a8a5 286 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:45496a70a8a5 287 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:ceb4063e6863 288 #endif
mluis 0:45496a70a8a5 289 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:45496a70a8a5 290 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:45496a70a8a5 291 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:45496a70a8a5 292
mluis 0:45496a70a8a5 293 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 294 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 295 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 296
mluis 0:45496a70a8a5 297 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 298 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 299 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 300 #else
mluis 0:45496a70a8a5 301 SerialDisplayUpdateDutyCycle( false );
mluis 0:45496a70a8a5 302 #endif
mluis 0:45496a70a8a5 303 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 304
mluis 0:45496a70a8a5 305 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 306 }
mluis 0:45496a70a8a5 307
mluis 0:45496a70a8a5 308 void SerialRxProcess( void )
mluis 0:45496a70a8a5 309 {
mluis 0:45496a70a8a5 310 if( SerialDisplayReadable( ) == true )
mluis 0:45496a70a8a5 311 {
mluis 0:45496a70a8a5 312 switch( SerialDisplayGetChar( ) )
mluis 0:45496a70a8a5 313 {
mluis 0:45496a70a8a5 314 case 'R':
mluis 0:45496a70a8a5 315 case 'r':
mluis 0:45496a70a8a5 316 // Refresh Serial screen
mluis 0:45496a70a8a5 317 SerialDisplayRefresh( );
mluis 0:45496a70a8a5 318 break;
mluis 0:45496a70a8a5 319 default:
mluis 0:45496a70a8a5 320 break;
mluis 0:45496a70a8a5 321 }
mluis 0:45496a70a8a5 322 }
mluis 0:45496a70a8a5 323 }
mluis 0:45496a70a8a5 324
mluis 0:45496a70a8a5 325 /*!
mluis 0:45496a70a8a5 326 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 327 */
mluis 0:45496a70a8a5 328 static void PrepareTxFrame( uint8_t port )
mluis 0:45496a70a8a5 329 {
mluis 0:45496a70a8a5 330 switch( port )
mluis 0:45496a70a8a5 331 {
GregCr 2:48d8d4806d48 332 case 10:
GregCr 2:48d8d4806d48 333 {
mluis 5:fa113b25f612 334 uint32_t tempValue = ( uint32_t )( LightValue * 1000000.0 );
GregCr 3:d4142832d5af 335 AppData[0] = LightMode;
GregCr 3:d4142832d5af 336 AppData[1] = ( ( tempValue & 0xFF000000 ) >> 24 ) & 0xFF;
GregCr 3:d4142832d5af 337 AppData[2] = ( ( tempValue & 0x00FF0000 ) >> 16 ) & 0xFF;
GregCr 3:d4142832d5af 338 AppData[3] = ( ( tempValue & 0x0000FF00 ) >> 8 ) & 0xFF;
mluis 5:fa113b25f612 339 AppData[4] = ( tempValue & 0x000000FF );
GregCr 2:48d8d4806d48 340 }
GregCr 2:48d8d4806d48 341 break;
mluis 0:45496a70a8a5 342 case 15:
mluis 0:45496a70a8a5 343 {
mluis 0:45496a70a8a5 344 AppData[0] = AppLedStateOn;
mluis 0:45496a70a8a5 345 if( IsTxConfirmed == true )
mluis 0:45496a70a8a5 346 {
mluis 0:45496a70a8a5 347 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 0:45496a70a8a5 348 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 0:45496a70a8a5 349 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 0:45496a70a8a5 350 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 0:45496a70a8a5 351 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 352 }
mluis 0:45496a70a8a5 353 }
mluis 0:45496a70a8a5 354 break;
mluis 0:45496a70a8a5 355 case 224:
mluis 0:45496a70a8a5 356 if( ComplianceTest.LinkCheck == true )
mluis 0:45496a70a8a5 357 {
mluis 0:45496a70a8a5 358 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 359 AppDataSize = 3;
mluis 0:45496a70a8a5 360 AppData[0] = 5;
mluis 0:45496a70a8a5 361 AppData[1] = ComplianceTest.DemodMargin;
mluis 0:45496a70a8a5 362 AppData[2] = ComplianceTest.NbGateways;
mluis 0:45496a70a8a5 363 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 364 }
mluis 0:45496a70a8a5 365 else
mluis 0:45496a70a8a5 366 {
mluis 0:45496a70a8a5 367 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 368 {
mluis 0:45496a70a8a5 369 case 4:
mluis 0:45496a70a8a5 370 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 371 break;
mluis 0:45496a70a8a5 372 case 1:
mluis 0:45496a70a8a5 373 AppDataSize = 2;
mluis 0:45496a70a8a5 374 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 0:45496a70a8a5 375 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 0:45496a70a8a5 376 break;
mluis 0:45496a70a8a5 377 }
mluis 0:45496a70a8a5 378 }
mluis 0:45496a70a8a5 379 break;
mluis 0:45496a70a8a5 380 default:
mluis 0:45496a70a8a5 381 break;
mluis 0:45496a70a8a5 382 }
mluis 0:45496a70a8a5 383 }
mluis 0:45496a70a8a5 384
mluis 0:45496a70a8a5 385 /*!
mluis 0:45496a70a8a5 386 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 387 *
mluis 0:45496a70a8a5 388 * \retval [0: frame could be send, 1: error]
mluis 0:45496a70a8a5 389 */
mluis 0:45496a70a8a5 390 static bool SendFrame( void )
mluis 0:45496a70a8a5 391 {
mluis 0:45496a70a8a5 392 McpsReq_t mcpsReq;
mluis 0:45496a70a8a5 393 LoRaMacTxInfo_t txInfo;
mluis 9:0083afd69815 394
mluis 0:45496a70a8a5 395 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 396 {
mluis 0:45496a70a8a5 397 // Send empty frame in order to flush MAC commands
mluis 0:45496a70a8a5 398 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 399 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 0:45496a70a8a5 400 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 0:45496a70a8a5 401 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 5:fa113b25f612 402
mluis 0:45496a70a8a5 403 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 404 LoRaMacUplinkStatus.Port = 0;
mluis 0:45496a70a8a5 405 LoRaMacUplinkStatus.Buffer = NULL;
mluis 0:45496a70a8a5 406 LoRaMacUplinkStatus.BufferSize = 0;
mluis 0:45496a70a8a5 407 SerialDisplayUpdateFrameType( false );
mluis 0:45496a70a8a5 408 }
mluis 0:45496a70a8a5 409 else
mluis 0:45496a70a8a5 410 {
mluis 0:45496a70a8a5 411 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 412 LoRaMacUplinkStatus.Port = AppPort;
mluis 0:45496a70a8a5 413 LoRaMacUplinkStatus.Buffer = AppData;
mluis 0:45496a70a8a5 414 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 0:45496a70a8a5 415 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 0:45496a70a8a5 416
mluis 0:45496a70a8a5 417 if( IsTxConfirmed == false )
mluis 0:45496a70a8a5 418 {
mluis 0:45496a70a8a5 419 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 420 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 0:45496a70a8a5 421 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 422 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 0:45496a70a8a5 423 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 424 }
mluis 0:45496a70a8a5 425 else
mluis 0:45496a70a8a5 426 {
mluis 0:45496a70a8a5 427 mcpsReq.Type = MCPS_CONFIRMED;
mluis 0:45496a70a8a5 428 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 0:45496a70a8a5 429 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 430 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 4:de084e23d884 431 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 0:45496a70a8a5 432 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 433 }
mluis 0:45496a70a8a5 434 }
mluis 0:45496a70a8a5 435
mluis 0:45496a70a8a5 436 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 437 {
mluis 0:45496a70a8a5 438 return false;
mluis 0:45496a70a8a5 439 }
mluis 0:45496a70a8a5 440 return true;
mluis 0:45496a70a8a5 441 }
mluis 0:45496a70a8a5 442
mluis 0:45496a70a8a5 443 /*!
mluis 0:45496a70a8a5 444 * \brief Function executed on TxNextPacket Timeout event
mluis 0:45496a70a8a5 445 */
mluis 0:45496a70a8a5 446 static void OnTxNextPacketTimerEvent( void )
mluis 0:45496a70a8a5 447 {
mluis 0:45496a70a8a5 448 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 449 LoRaMacStatus_t status;
mluis 0:45496a70a8a5 450
mluis 0:45496a70a8a5 451 TimerStop( &TxNextPacketTimer );
mluis 0:45496a70a8a5 452
mluis 0:45496a70a8a5 453 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 454 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 455
mluis 0:45496a70a8a5 456 if( status == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 457 {
mluis 0:45496a70a8a5 458 if( mibReq.Param.IsNetworkJoined == true )
mluis 0:45496a70a8a5 459 {
mluis 0:45496a70a8a5 460 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 461 NextTx = true;
mluis 0:45496a70a8a5 462 }
mluis 0:45496a70a8a5 463 else
mluis 0:45496a70a8a5 464 {
mluis 0:45496a70a8a5 465 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 466 }
mluis 0:45496a70a8a5 467 }
mluis 0:45496a70a8a5 468 }
mluis 0:45496a70a8a5 469
mluis 0:45496a70a8a5 470 /*!
mluis 0:45496a70a8a5 471 * \brief Function executed on Led 1 Timeout event
mluis 0:45496a70a8a5 472 */
mluis 0:45496a70a8a5 473 static void OnLed1TimerEvent( void )
mluis 0:45496a70a8a5 474 {
mluis 0:45496a70a8a5 475 TimerStop( &Led1Timer );
mluis 0:45496a70a8a5 476 // Switch LED 1 OFF
mluis 0:45496a70a8a5 477 Led1State = false;
mluis 0:45496a70a8a5 478 Led1StateChanged = true;
mluis 0:45496a70a8a5 479 }
mluis 0:45496a70a8a5 480
mluis 0:45496a70a8a5 481 /*!
mluis 0:45496a70a8a5 482 * \brief Function executed on Led 2 Timeout event
mluis 0:45496a70a8a5 483 */
mluis 0:45496a70a8a5 484 static void OnLed2TimerEvent( void )
mluis 0:45496a70a8a5 485 {
mluis 0:45496a70a8a5 486 TimerStop( &Led2Timer );
mluis 0:45496a70a8a5 487 // Switch LED 2 OFF
mluis 0:45496a70a8a5 488 Led2State = false;
mluis 0:45496a70a8a5 489 Led2StateChanged = true;
mluis 0:45496a70a8a5 490 }
mluis 0:45496a70a8a5 491
mluis 0:45496a70a8a5 492 /*!
GregCr 3:d4142832d5af 493 * \brief Function executed on Buzzer Timeout event
GregCr 3:d4142832d5af 494 */
mluis 5:fa113b25f612 495 static void OnBuzzerTimerEvent( void )
GregCr 3:d4142832d5af 496 {
mluis 5:fa113b25f612 497 Buzzer = 0;
mluis 5:fa113b25f612 498 BuzzerTimer.detach( );
GregCr 3:d4142832d5af 499 }
GregCr 3:d4142832d5af 500
GregCr 3:d4142832d5af 501 /*!
mluis 0:45496a70a8a5 502 * \brief MCPS-Confirm event function
mluis 0:45496a70a8a5 503 *
mluis 5:fa113b25f612 504 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 505 * containing confirm attributes.
mluis 0:45496a70a8a5 506 */
mluis 5:fa113b25f612 507 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:45496a70a8a5 508 {
mluis 5:fa113b25f612 509 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 510 {
mluis 5:fa113b25f612 511 switch( mcpsConfirm->McpsRequest )
mluis 0:45496a70a8a5 512 {
mluis 0:45496a70a8a5 513 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 514 {
mluis 0:45496a70a8a5 515 // Check Datarate
mluis 0:45496a70a8a5 516 // Check TxPower
mluis 0:45496a70a8a5 517 break;
mluis 0:45496a70a8a5 518 }
mluis 0:45496a70a8a5 519 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 520 {
mluis 0:45496a70a8a5 521 // Check Datarate
mluis 0:45496a70a8a5 522 // Check TxPower
mluis 0:45496a70a8a5 523 // Check AckReceived
mluis 5:fa113b25f612 524 // Check NbTrials
mluis 5:fa113b25f612 525 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 0:45496a70a8a5 526 break;
mluis 0:45496a70a8a5 527 }
mluis 0:45496a70a8a5 528 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 529 {
mluis 0:45496a70a8a5 530 break;
mluis 0:45496a70a8a5 531 }
mluis 0:45496a70a8a5 532 default:
mluis 0:45496a70a8a5 533 break;
mluis 0:45496a70a8a5 534 }
mluis 5:fa113b25f612 535 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 5:fa113b25f612 536 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 5:fa113b25f612 537
mluis 7:ceb4063e6863 538 // Switch LED 1 ON
mluis 7:ceb4063e6863 539 Led1State = true;
mluis 7:ceb4063e6863 540 Led1StateChanged = true;
mluis 7:ceb4063e6863 541 TimerStart( &Led1Timer );
mluis 7:ceb4063e6863 542
mluis 0:45496a70a8a5 543 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 544 }
mluis 0:45496a70a8a5 545 NextTx = true;
mluis 0:45496a70a8a5 546 }
mluis 0:45496a70a8a5 547
mluis 0:45496a70a8a5 548 /*!
mluis 0:45496a70a8a5 549 * \brief MCPS-Indication event function
mluis 0:45496a70a8a5 550 *
mluis 5:fa113b25f612 551 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 0:45496a70a8a5 552 * containing indication attributes.
mluis 0:45496a70a8a5 553 */
mluis 5:fa113b25f612 554 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 0:45496a70a8a5 555 {
mluis 5:fa113b25f612 556 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 557 {
mluis 0:45496a70a8a5 558 return;
mluis 0:45496a70a8a5 559 }
mluis 0:45496a70a8a5 560
mluis 5:fa113b25f612 561 switch( mcpsIndication->McpsIndication )
mluis 0:45496a70a8a5 562 {
mluis 0:45496a70a8a5 563 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 564 {
mluis 0:45496a70a8a5 565 break;
mluis 0:45496a70a8a5 566 }
mluis 0:45496a70a8a5 567 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 568 {
mluis 0:45496a70a8a5 569 break;
mluis 0:45496a70a8a5 570 }
mluis 0:45496a70a8a5 571 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 572 {
mluis 0:45496a70a8a5 573 break;
mluis 0:45496a70a8a5 574 }
mluis 0:45496a70a8a5 575 case MCPS_MULTICAST:
mluis 0:45496a70a8a5 576 {
mluis 0:45496a70a8a5 577 break;
mluis 0:45496a70a8a5 578 }
mluis 0:45496a70a8a5 579 default:
mluis 0:45496a70a8a5 580 break;
mluis 0:45496a70a8a5 581 }
mluis 0:45496a70a8a5 582
mluis 0:45496a70a8a5 583 // Check Multicast
mluis 0:45496a70a8a5 584 // Check Port
mluis 0:45496a70a8a5 585 // Check Datarate
mluis 0:45496a70a8a5 586 // Check FramePending
mluis 0:45496a70a8a5 587 // Check Buffer
mluis 0:45496a70a8a5 588 // Check BufferSize
mluis 0:45496a70a8a5 589 // Check Rssi
mluis 0:45496a70a8a5 590 // Check Snr
mluis 0:45496a70a8a5 591 // Check RxSlot
mluis 5:fa113b25f612 592 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 5:fa113b25f612 593 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 0:45496a70a8a5 594 {
mluis 0:45496a70a8a5 595 // Invert and divide by 4
mluis 5:fa113b25f612 596 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 0:45496a70a8a5 597 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 598 }
mluis 0:45496a70a8a5 599 else
mluis 0:45496a70a8a5 600 {
mluis 0:45496a70a8a5 601 // Divide by 4
mluis 5:fa113b25f612 602 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 0:45496a70a8a5 603 }
mluis 0:45496a70a8a5 604 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 5:fa113b25f612 605 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 5:fa113b25f612 606 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 5:fa113b25f612 607 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 5:fa113b25f612 608 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 609
mluis 0:45496a70a8a5 610 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 611 {
mluis 0:45496a70a8a5 612 ComplianceTest.DownLinkCounter++;
mluis 0:45496a70a8a5 613 }
mluis 0:45496a70a8a5 614
mluis 5:fa113b25f612 615 if( mcpsIndication->RxData == true )
mluis 0:45496a70a8a5 616 {
mluis 5:fa113b25f612 617 switch( mcpsIndication->Port )
mluis 0:45496a70a8a5 618 {
mluis 0:45496a70a8a5 619 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:45496a70a8a5 620 case 2:
mluis 5:fa113b25f612 621 if( mcpsIndication->BufferSize == 1 )
mluis 0:45496a70a8a5 622 {
mluis 5:fa113b25f612 623 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 0:45496a70a8a5 624 Led3StateChanged = true;
mluis 0:45496a70a8a5 625 }
mluis 0:45496a70a8a5 626 break;
GregCr 2:48d8d4806d48 627 case 10:
mluis 5:fa113b25f612 628 Display.write( 0, mcpsIndication->Buffer[0] );
mluis 5:fa113b25f612 629 Display.write( 1, mcpsIndication->Buffer[1] );
mluis 5:fa113b25f612 630 Display.write( 2, mcpsIndication->Buffer[2] );
mluis 5:fa113b25f612 631 Display.write( 3, mcpsIndication->Buffer[3] );
GregCr 2:48d8d4806d48 632 break;
GregCr 2:48d8d4806d48 633 case 20:
mluis 5:fa113b25f612 634 LightMode = mcpsIndication->Buffer[0];
GregCr 2:48d8d4806d48 635 if( LightMode )
GregCr 2:48d8d4806d48 636 {
mluis 5:fa113b25f612 637 ColorLed.setColorRGB( 0, mcpsIndication->Buffer[1], mcpsIndication->Buffer[2], mcpsIndication->Buffer[3] );
GregCr 2:48d8d4806d48 638 }
GregCr 2:48d8d4806d48 639 break;
GregCr 2:48d8d4806d48 640 case 30:
mluis 5:fa113b25f612 641 BuzzerTimer.attach_us( &OnBuzzerTimerEvent, 200000 );
mluis 5:fa113b25f612 642 Buzzer = 1;
GregCr 2:48d8d4806d48 643 break;
mluis 0:45496a70a8a5 644 case 224:
mluis 0:45496a70a8a5 645 if( ComplianceTest.Running == false )
mluis 0:45496a70a8a5 646 {
mluis 0:45496a70a8a5 647 // Check compliance test enable command (i)
mluis 5:fa113b25f612 648 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 5:fa113b25f612 649 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 5:fa113b25f612 650 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 5:fa113b25f612 651 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 5:fa113b25f612 652 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 0:45496a70a8a5 653 {
mluis 0:45496a70a8a5 654 IsTxConfirmed = false;
mluis 0:45496a70a8a5 655 AppPort = 224;
mluis 0:45496a70a8a5 656 AppDataSize = 2;
mluis 0:45496a70a8a5 657 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 658 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 659 ComplianceTest.DemodMargin = 0;
mluis 0:45496a70a8a5 660 ComplianceTest.NbGateways = 0;
mluis 0:45496a70a8a5 661 ComplianceTest.Running = true;
mluis 0:45496a70a8a5 662 ComplianceTest.State = 1;
mluis 9:0083afd69815 663
mluis 0:45496a70a8a5 664 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 665 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 666 mibReq.Param.AdrEnable = true;
mluis 0:45496a70a8a5 667 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 668
mluis 0:45496a70a8a5 669 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 670 LoRaMacTestSetDutyCycleOn( false );
mluis 0:45496a70a8a5 671 #endif
mluis 0:45496a70a8a5 672 }
mluis 0:45496a70a8a5 673 }
mluis 0:45496a70a8a5 674 else
mluis 0:45496a70a8a5 675 {
mluis 5:fa113b25f612 676 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 0:45496a70a8a5 677 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 678 {
mluis 0:45496a70a8a5 679 case 0: // Check compliance test disable command (ii)
mluis 0:45496a70a8a5 680 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 681 AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 682 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 683 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 684 ComplianceTest.Running = false;
mluis 9:0083afd69815 685
mluis 0:45496a70a8a5 686 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 687 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 688 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 689 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 690 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 691 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 692 #endif
mluis 0:45496a70a8a5 693 break;
mluis 0:45496a70a8a5 694 case 1: // (iii, iv)
mluis 0:45496a70a8a5 695 AppDataSize = 2;
mluis 0:45496a70a8a5 696 break;
mluis 0:45496a70a8a5 697 case 2: // Enable confirmed messages (v)
mluis 0:45496a70a8a5 698 IsTxConfirmed = true;
mluis 0:45496a70a8a5 699 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 700 break;
mluis 0:45496a70a8a5 701 case 3: // Disable confirmed messages (vi)
mluis 0:45496a70a8a5 702 IsTxConfirmed = false;
mluis 0:45496a70a8a5 703 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 704 break;
mluis 0:45496a70a8a5 705 case 4: // (vii)
mluis 5:fa113b25f612 706 AppDataSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 707
mluis 0:45496a70a8a5 708 AppData[0] = 4;
mluis 0:45496a70a8a5 709 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 0:45496a70a8a5 710 {
mluis 5:fa113b25f612 711 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 0:45496a70a8a5 712 }
mluis 0:45496a70a8a5 713 break;
mluis 0:45496a70a8a5 714 case 5: // (viii)
mluis 0:45496a70a8a5 715 {
mluis 0:45496a70a8a5 716 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 717 mlmeReq.Type = MLME_LINK_CHECK;
mluis 0:45496a70a8a5 718 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 719 }
mluis 0:45496a70a8a5 720 break;
mluis 7:ceb4063e6863 721 case 6: // (ix)
mluis 7:ceb4063e6863 722 {
mluis 7:ceb4063e6863 723 MlmeReq_t mlmeReq;
mluis 7:ceb4063e6863 724
mluis 9:0083afd69815 725 // Disable TestMode and revert back to normal operation
mluis 9:0083afd69815 726 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 9:0083afd69815 727 AppPort = LORAWAN_APP_PORT;
mluis 9:0083afd69815 728 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 9:0083afd69815 729 ComplianceTest.DownLinkCounter = 0;
mluis 9:0083afd69815 730 ComplianceTest.Running = false;
mluis 9:0083afd69815 731
mluis 9:0083afd69815 732 MibRequestConfirm_t mibReq;
mluis 9:0083afd69815 733 mibReq.Type = MIB_ADR;
mluis 9:0083afd69815 734 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 9:0083afd69815 735 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:0083afd69815 736 #if defined( USE_BAND_868 )
mluis 9:0083afd69815 737 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 9:0083afd69815 738 #endif
mluis 9:0083afd69815 739
mluis 7:ceb4063e6863 740 mlmeReq.Type = MLME_JOIN;
mluis 7:ceb4063e6863 741
mluis 7:ceb4063e6863 742 mlmeReq.Req.Join.DevEui = DevEui;
mluis 7:ceb4063e6863 743 mlmeReq.Req.Join.AppEui = AppEui;
mluis 7:ceb4063e6863 744 mlmeReq.Req.Join.AppKey = AppKey;
mluis 9:0083afd69815 745 mlmeReq.Req.Join.NbTrials = 3;
mluis 7:ceb4063e6863 746
mluis 7:ceb4063e6863 747 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:ceb4063e6863 748 DeviceState = DEVICE_STATE_SLEEP;
mluis 7:ceb4063e6863 749 }
mluis 7:ceb4063e6863 750 break;
mluis 9:0083afd69815 751 case 7: // (x)
mluis 9:0083afd69815 752 {
mluis 9:0083afd69815 753 if( mcpsIndication->BufferSize == 3 )
mluis 9:0083afd69815 754 {
mluis 9:0083afd69815 755 MlmeReq_t mlmeReq;
mluis 9:0083afd69815 756 mlmeReq.Type = MLME_TXCW;
mluis 9:0083afd69815 757 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:0083afd69815 758 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:0083afd69815 759 }
mluis 9:0083afd69815 760 else if( mcpsIndication->BufferSize == 7 )
mluis 9:0083afd69815 761 {
mluis 9:0083afd69815 762 MlmeReq_t mlmeReq;
mluis 9:0083afd69815 763 mlmeReq.Type = MLME_TXCW_1;
mluis 9:0083afd69815 764 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:0083afd69815 765 mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
mluis 9:0083afd69815 766 mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
mluis 9:0083afd69815 767 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:0083afd69815 768 }
mluis 9:0083afd69815 769 ComplianceTest.State = 1;
mluis 9:0083afd69815 770 }
mluis 9:0083afd69815 771 break;
mluis 0:45496a70a8a5 772 default:
mluis 0:45496a70a8a5 773 break;
mluis 0:45496a70a8a5 774 }
mluis 0:45496a70a8a5 775 }
mluis 0:45496a70a8a5 776 break;
mluis 0:45496a70a8a5 777 default:
mluis 0:45496a70a8a5 778 break;
mluis 0:45496a70a8a5 779 }
mluis 0:45496a70a8a5 780 }
mluis 0:45496a70a8a5 781
mluis 0:45496a70a8a5 782 // Switch LED 2 ON for each received downlink
mluis 0:45496a70a8a5 783 Led2State = true;
mluis 0:45496a70a8a5 784 Led2StateChanged = true;
mluis 0:45496a70a8a5 785 TimerStart( &Led2Timer );
mluis 0:45496a70a8a5 786 DownlinkStatusUpdated = true;
mluis 0:45496a70a8a5 787 }
mluis 0:45496a70a8a5 788
mluis 0:45496a70a8a5 789 /*!
mluis 0:45496a70a8a5 790 * \brief MLME-Confirm event function
mluis 0:45496a70a8a5 791 *
mluis 5:fa113b25f612 792 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 793 * containing confirm attributes.
mluis 0:45496a70a8a5 794 */
mluis 5:fa113b25f612 795 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 0:45496a70a8a5 796 {
mluis 9:0083afd69815 797 switch( mlmeConfirm->MlmeRequest )
mluis 0:45496a70a8a5 798 {
mluis 9:0083afd69815 799 case MLME_JOIN:
mluis 0:45496a70a8a5 800 {
mluis 9:0083afd69815 801 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 802 {
mluis 0:45496a70a8a5 803 // Status is OK, node has joined the network
mluis 0:45496a70a8a5 804 IsNetworkJoinedStatusUpdate = true;
mluis 7:ceb4063e6863 805 DeviceState = DEVICE_STATE_SEND;
mluis 9:0083afd69815 806 }
mluis 9:0083afd69815 807 else
mluis 9:0083afd69815 808 {
mluis 9:0083afd69815 809 // Join was not successful. Try to join again
mluis 9:0083afd69815 810 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 811 }
mluis 9:0083afd69815 812 break;
mluis 9:0083afd69815 813 }
mluis 9:0083afd69815 814 case MLME_LINK_CHECK:
mluis 9:0083afd69815 815 {
mluis 9:0083afd69815 816 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 817 {
mluis 0:45496a70a8a5 818 // Check DemodMargin
mluis 0:45496a70a8a5 819 // Check NbGateways
mluis 0:45496a70a8a5 820 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 821 {
mluis 0:45496a70a8a5 822 ComplianceTest.LinkCheck = true;
mluis 5:fa113b25f612 823 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 5:fa113b25f612 824 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 0:45496a70a8a5 825 }
mluis 0:45496a70a8a5 826 }
mluis 9:0083afd69815 827 break;
mluis 0:45496a70a8a5 828 }
mluis 9:0083afd69815 829 default:
mluis 9:0083afd69815 830 break;
mluis 0:45496a70a8a5 831 }
mluis 0:45496a70a8a5 832 NextTx = true;
mluis 0:45496a70a8a5 833 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 834 }
mluis 0:45496a70a8a5 835
mluis 0:45496a70a8a5 836 /**
mluis 0:45496a70a8a5 837 * Main application entry point.
mluis 0:45496a70a8a5 838 */
mluis 0:45496a70a8a5 839 int main( void )
mluis 0:45496a70a8a5 840 {
mluis 0:45496a70a8a5 841 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 0:45496a70a8a5 842 LoRaMacCallback_t LoRaMacCallbacks;
mluis 0:45496a70a8a5 843 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 844
mluis 0:45496a70a8a5 845 BoardInit( );
mluis 0:45496a70a8a5 846 SerialDisplayInit( );
mluis 0:45496a70a8a5 847
mluis 5:fa113b25f612 848 LightMode = 0; // 0: manual, 1: automatic
mluis 5:fa113b25f612 849 Buzzer = 0; // 0: OFF, 1: ON
mluis 5:fa113b25f612 850
mluis 7:ceb4063e6863 851 SerialDisplayUpdateEui( 5, DevEui );
mluis 7:ceb4063e6863 852 SerialDisplayUpdateEui( 6, AppEui );
mluis 7:ceb4063e6863 853 SerialDisplayUpdateKey( 7, AppKey );
mluis 7:ceb4063e6863 854
mluis 7:ceb4063e6863 855 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 7:ceb4063e6863 856 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 7:ceb4063e6863 857 SerialDisplayUpdateDevAddr( DevAddr );
mluis 7:ceb4063e6863 858 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 7:ceb4063e6863 859 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:ceb4063e6863 860 #endif
mluis 7:ceb4063e6863 861
mluis 0:45496a70a8a5 862 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 863
mluis 0:45496a70a8a5 864 while( 1 )
mluis 0:45496a70a8a5 865 {
mluis 0:45496a70a8a5 866 SerialRxProcess( );
mluis 0:45496a70a8a5 867 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:45496a70a8a5 868 {
mluis 0:45496a70a8a5 869 IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 870 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 871 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 872 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 873 }
mluis 0:45496a70a8a5 874 if( Led1StateChanged == true )
mluis 0:45496a70a8a5 875 {
mluis 0:45496a70a8a5 876 Led1StateChanged = false;
mluis 0:45496a70a8a5 877 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:45496a70a8a5 878 }
mluis 0:45496a70a8a5 879 if( Led2StateChanged == true )
mluis 0:45496a70a8a5 880 {
mluis 0:45496a70a8a5 881 Led2StateChanged = false;
mluis 0:45496a70a8a5 882 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 883 }
mluis 0:45496a70a8a5 884 if( Led3StateChanged == true )
mluis 0:45496a70a8a5 885 {
mluis 0:45496a70a8a5 886 Led3StateChanged = false;
mluis 0:45496a70a8a5 887 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 888 }
mluis 0:45496a70a8a5 889 if( UplinkStatusUpdated == true )
mluis 0:45496a70a8a5 890 {
mluis 0:45496a70a8a5 891 UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 892 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 0:45496a70a8a5 893 }
mluis 0:45496a70a8a5 894 if( DownlinkStatusUpdated == true )
mluis 0:45496a70a8a5 895 {
mluis 0:45496a70a8a5 896 DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 897 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 898 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:45496a70a8a5 899 }
mluis 0:45496a70a8a5 900
mluis 0:45496a70a8a5 901 switch( DeviceState )
mluis 0:45496a70a8a5 902 {
mluis 0:45496a70a8a5 903 case DEVICE_STATE_INIT:
mluis 0:45496a70a8a5 904 {
mluis 0:45496a70a8a5 905 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 0:45496a70a8a5 906 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 0:45496a70a8a5 907 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 0:45496a70a8a5 908 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 0:45496a70a8a5 909 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 0:45496a70a8a5 910
mluis 0:45496a70a8a5 911 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 0:45496a70a8a5 912
mluis 0:45496a70a8a5 913 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 9:0083afd69815 914 TimerSetValue( &Led1Timer, 25 );
mluis 0:45496a70a8a5 915
mluis 0:45496a70a8a5 916 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 9:0083afd69815 917 TimerSetValue( &Led2Timer, 25 );
mluis 0:45496a70a8a5 918
mluis 0:45496a70a8a5 919 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 920 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 921 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 922
mluis 0:45496a70a8a5 923 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 924 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 925 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 926
mluis 0:45496a70a8a5 927 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 928 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 929 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 5:fa113b25f612 930
mluis 9:0083afd69815 931 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:fa113b25f612 932 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 5:fa113b25f612 933 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 5:fa113b25f612 934 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 5:fa113b25f612 935 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 5:fa113b25f612 936 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
mluis 5:fa113b25f612 937 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
mluis 5:fa113b25f612 938 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 7:ceb4063e6863 939
mluis 9:0083afd69815 940 mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
mluis 9:0083afd69815 941 mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 9:0083afd69815 942 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:0083afd69815 943
mluis 7:ceb4063e6863 944 mibReq.Type = MIB_RX2_CHANNEL;
mluis 7:ceb4063e6863 945 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 7:ceb4063e6863 946 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 5:fa113b25f612 947 #endif
mluis 5:fa113b25f612 948
mluis 0:45496a70a8a5 949 #endif
mluis 0:45496a70a8a5 950 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 951 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 952 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 953
mluis 0:45496a70a8a5 954 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 0:45496a70a8a5 955
mluis 0:45496a70a8a5 956 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 957 break;
mluis 0:45496a70a8a5 958 }
mluis 0:45496a70a8a5 959 case DEVICE_STATE_JOIN:
mluis 0:45496a70a8a5 960 {
mluis 0:45496a70a8a5 961 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:45496a70a8a5 962 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 963
mluis 0:45496a70a8a5 964 mlmeReq.Type = MLME_JOIN;
mluis 0:45496a70a8a5 965
mluis 0:45496a70a8a5 966 mlmeReq.Req.Join.DevEui = DevEui;
mluis 0:45496a70a8a5 967 mlmeReq.Req.Join.AppEui = AppEui;
mluis 0:45496a70a8a5 968 mlmeReq.Req.Join.AppKey = AppKey;
mluis 0:45496a70a8a5 969
mluis 0:45496a70a8a5 970 if( NextTx == true )
mluis 0:45496a70a8a5 971 {
mluis 0:45496a70a8a5 972 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 973 }
mluis 7:ceb4063e6863 974 DeviceState = DEVICE_STATE_SLEEP;
mluis 0:45496a70a8a5 975 #else
mluis 0:45496a70a8a5 976 mibReq.Type = MIB_NET_ID;
mluis 0:45496a70a8a5 977 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 0:45496a70a8a5 978 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 979
mluis 0:45496a70a8a5 980 mibReq.Type = MIB_DEV_ADDR;
mluis 0:45496a70a8a5 981 mibReq.Param.DevAddr = DevAddr;
mluis 0:45496a70a8a5 982 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 983
mluis 0:45496a70a8a5 984 mibReq.Type = MIB_NWK_SKEY;
mluis 0:45496a70a8a5 985 mibReq.Param.NwkSKey = NwkSKey;
mluis 0:45496a70a8a5 986 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 987
mluis 0:45496a70a8a5 988 mibReq.Type = MIB_APP_SKEY;
mluis 0:45496a70a8a5 989 mibReq.Param.AppSKey = AppSKey;
mluis 0:45496a70a8a5 990 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 991
mluis 0:45496a70a8a5 992 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 993 mibReq.Param.IsNetworkJoined = true;
mluis 0:45496a70a8a5 994 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 995
mluis 0:45496a70a8a5 996 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 997 #endif
mluis 0:45496a70a8a5 998 IsNetworkJoinedStatusUpdate = true;
mluis 0:45496a70a8a5 999 break;
mluis 0:45496a70a8a5 1000 }
mluis 0:45496a70a8a5 1001 case DEVICE_STATE_SEND:
mluis 0:45496a70a8a5 1002 {
mluis 0:45496a70a8a5 1003 if( NextTx == true )
mluis 0:45496a70a8a5 1004 {
mluis 0:45496a70a8a5 1005 SerialDisplayUpdateUplinkAcked( false );
mluis 0:45496a70a8a5 1006 SerialDisplayUpdateDonwlinkRxData( false );
mluis 0:45496a70a8a5 1007 PrepareTxFrame( AppPort );
mluis 0:45496a70a8a5 1008
mluis 0:45496a70a8a5 1009 NextTx = SendFrame( );
mluis 0:45496a70a8a5 1010 }
mluis 0:45496a70a8a5 1011 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 1012 {
mluis 5:fa113b25f612 1013 // Schedule next packet transmission
mluis 9:0083afd69815 1014 TxDutyCycleTime = 5000; // 5000 ms
mluis 0:45496a70a8a5 1015 }
mluis 0:45496a70a8a5 1016 else
mluis 0:45496a70a8a5 1017 {
mluis 0:45496a70a8a5 1018 // Schedule next packet transmission
mluis 0:45496a70a8a5 1019 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 0:45496a70a8a5 1020 }
mluis 0:45496a70a8a5 1021 DeviceState = DEVICE_STATE_CYCLE;
mluis 0:45496a70a8a5 1022 break;
mluis 0:45496a70a8a5 1023 }
mluis 0:45496a70a8a5 1024 case DEVICE_STATE_CYCLE:
mluis 0:45496a70a8a5 1025 {
mluis 5:fa113b25f612 1026 DeviceState = DEVICE_STATE_SLEEP;
mluis 5:fa113b25f612 1027
mluis 0:45496a70a8a5 1028 // Schedule next packet transmission
mluis 0:45496a70a8a5 1029 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 0:45496a70a8a5 1030 TimerStart( &TxNextPacketTimer );
mluis 0:45496a70a8a5 1031 break;
mluis 0:45496a70a8a5 1032 }
mluis 0:45496a70a8a5 1033 case DEVICE_STATE_SLEEP:
mluis 0:45496a70a8a5 1034 {
mluis 0:45496a70a8a5 1035 // Wake up through events
mluis 0:45496a70a8a5 1036 break;
mluis 0:45496a70a8a5 1037 }
mluis 0:45496a70a8a5 1038 default:
mluis 0:45496a70a8a5 1039 {
mluis 0:45496a70a8a5 1040 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 1041 break;
mluis 0:45496a70a8a5 1042 }
GregCr 2:48d8d4806d48 1043
GregCr 2:48d8d4806d48 1044 }
mluis 5:fa113b25f612 1045
GregCr 2:48d8d4806d48 1046 // Read light sensor
mluis 5:fa113b25f612 1047 LightValue = ( 1 - ( LightSens.read( ) * 1.65 ) );
mluis 5:fa113b25f612 1048
GregCr 2:48d8d4806d48 1049 // Set automatic RGB from light sensor
GregCr 2:48d8d4806d48 1050 if( LightMode == 0 )
GregCr 2:48d8d4806d48 1051 {
mluis 5:fa113b25f612 1052 ColorLed.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
mluis 0:45496a70a8a5 1053 }
mluis 0:45496a70a8a5 1054 }
mluis 0:45496a70a8a5 1055 }