Amir Chaudhary
/
Hexmodal-firmware_v3-1_hex_w_3d_14
Implemented LED Indicator Patterns
app/main.cpp@30:c186bf174348, 2019-09-04 (annotated)
- Committer:
- amirchaudhary
- Date:
- Wed Sep 04 15:42:07 2019 +0000
- Revision:
- 30:c186bf174348
- Parent:
- 29:ca6caa47ef38
Anish code for RTC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:92bca02df485 | 1 | /* |
mluis | 0:92bca02df485 | 2 | / _____) _ | | |
mluis | 0:92bca02df485 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
mluis | 0:92bca02df485 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
mluis | 0:92bca02df485 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
mluis | 0:92bca02df485 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
mluis | 0:92bca02df485 | 7 | (C)2015 Semtech |
mluis | 0:92bca02df485 | 8 | |
mluis | 0:92bca02df485 | 9 | Description: LoRaMac classA device implementation |
mluis | 0:92bca02df485 | 10 | |
mluis | 0:92bca02df485 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
mluis | 0:92bca02df485 | 12 | |
mluis | 0:92bca02df485 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
mluis | 0:92bca02df485 | 14 | */ |
mluis | 0:92bca02df485 | 15 | #include "mbed.h" |
mluis | 0:92bca02df485 | 16 | #include "board.h" |
mluis | 0:92bca02df485 | 17 | #include "radio.h" |
mluis | 0:92bca02df485 | 18 | |
mluis | 0:92bca02df485 | 19 | #include "LoRaMac.h" |
mluis | 9:ee9dcbb9708d | 20 | #include "Commissioning.h" |
mluis | 0:92bca02df485 | 21 | #include "SerialDisplay.h" |
mluis | 0:92bca02df485 | 22 | |
mluis | 0:92bca02df485 | 23 | /*! |
mluis | 9:ee9dcbb9708d | 24 | * Defines the application data transmission duty cycle. 5s, value in [ms]. |
mluis | 0:92bca02df485 | 25 | */ |
mluis | 9:ee9dcbb9708d | 26 | #define APP_TX_DUTYCYCLE 5000 |
mluis | 1:352f608c3337 | 27 | |
mluis | 1:352f608c3337 | 28 | /*! |
mluis | 1:352f608c3337 | 29 | * Defines a random delay for application data transmission duty cycle. 1s, |
mluis | 9:ee9dcbb9708d | 30 | * value in [ms]. |
mluis | 1:352f608c3337 | 31 | */ |
mluis | 9:ee9dcbb9708d | 32 | #define APP_TX_DUTYCYCLE_RND 1000 |
mluis | 0:92bca02df485 | 33 | |
mluis | 0:92bca02df485 | 34 | /*! |
mluis | 5:1e9f6a365854 | 35 | * Default datarate |
mluis | 3:9c6f7f082151 | 36 | */ |
mluis | 3:9c6f7f082151 | 37 | #define LORAWAN_DEFAULT_DATARATE DR_0 |
mluis | 3:9c6f7f082151 | 38 | |
mluis | 3:9c6f7f082151 | 39 | /*! |
mluis | 0:92bca02df485 | 40 | * LoRaWAN confirmed messages |
mluis | 0:92bca02df485 | 41 | */ |
mluis | 0:92bca02df485 | 42 | #define LORAWAN_CONFIRMED_MSG_ON true |
mluis | 0:92bca02df485 | 43 | |
mluis | 0:92bca02df485 | 44 | /*! |
mluis | 3:9c6f7f082151 | 45 | * LoRaWAN Adaptive Data Rate |
mluis | 0:92bca02df485 | 46 | * |
mluis | 0:92bca02df485 | 47 | * \remark Please note that when ADR is enabled the end-device should be static |
mluis | 0:92bca02df485 | 48 | */ |
mluis | 0:92bca02df485 | 49 | #define LORAWAN_ADR_ON 1 |
mluis | 0:92bca02df485 | 50 | |
mluis | 1:352f608c3337 | 51 | #if defined( USE_BAND_868 ) |
mluis | 1:352f608c3337 | 52 | |
mluis | 3:9c6f7f082151 | 53 | #include "LoRaMacTest.h" |
mluis | 3:9c6f7f082151 | 54 | |
mluis | 0:92bca02df485 | 55 | /*! |
mluis | 0:92bca02df485 | 56 | * LoRaWAN ETSI duty cycle control enable/disable |
mluis | 0:92bca02df485 | 57 | * |
mluis | 0:92bca02df485 | 58 | * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes |
mluis | 0:92bca02df485 | 59 | */ |
mluis | 9:ee9dcbb9708d | 60 | #define LORAWAN_DUTYCYCLE_ON false |
mluis | 0:92bca02df485 | 61 | |
mluis | 5:1e9f6a365854 | 62 | #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1 |
mluis | 5:1e9f6a365854 | 63 | |
mluis | 9:ee9dcbb9708d | 64 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 5:1e9f6a365854 | 65 | |
mluis | 5:1e9f6a365854 | 66 | #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 67 | #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 68 | #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 69 | #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 70 | #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 71 | #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } |
mluis | 5:1e9f6a365854 | 72 | #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } |
mluis | 5:1e9f6a365854 | 73 | |
mluis | 5:1e9f6a365854 | 74 | #endif |
mluis | 5:1e9f6a365854 | 75 | |
mluis | 1:352f608c3337 | 76 | #endif |
mluis | 1:352f608c3337 | 77 | |
mluis | 0:92bca02df485 | 78 | /*! |
mluis | 0:92bca02df485 | 79 | * LoRaWAN application port |
mluis | 0:92bca02df485 | 80 | */ |
mluis | 0:92bca02df485 | 81 | #define LORAWAN_APP_PORT 15 |
mluis | 0:92bca02df485 | 82 | |
mluis | 0:92bca02df485 | 83 | /*! |
mluis | 0:92bca02df485 | 84 | * User application data buffer size |
mluis | 0:92bca02df485 | 85 | */ |
uss1994 | 10:9a4efdd07a77 | 86 | #define LORAWAN_APP_DATA_SIZE 41 |
mluis | 0:92bca02df485 | 87 | |
mluis | 0:92bca02df485 | 88 | static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; |
mluis | 0:92bca02df485 | 89 | static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; |
mluis | 0:92bca02df485 | 90 | static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; |
mluis | 0:92bca02df485 | 91 | |
mluis | 7:3173f0508a98 | 92 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:92bca02df485 | 93 | |
mluis | 0:92bca02df485 | 94 | static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; |
mluis | 0:92bca02df485 | 95 | static uint8_t AppSKey[] = LORAWAN_APPSKEY; |
mluis | 0:92bca02df485 | 96 | |
mluis | 3:9c6f7f082151 | 97 | /*! |
mluis | 3:9c6f7f082151 | 98 | * Device address |
mluis | 3:9c6f7f082151 | 99 | */ |
mluis | 3:9c6f7f082151 | 100 | static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS; |
mluis | 0:92bca02df485 | 101 | |
mluis | 3:9c6f7f082151 | 102 | #endif |
mluis | 0:92bca02df485 | 103 | |
mluis | 0:92bca02df485 | 104 | /*! |
mluis | 1:352f608c3337 | 105 | * Application port |
mluis | 1:352f608c3337 | 106 | */ |
mluis | 1:352f608c3337 | 107 | static uint8_t AppPort = LORAWAN_APP_PORT; |
mluis | 1:352f608c3337 | 108 | |
mluis | 1:352f608c3337 | 109 | /*! |
mluis | 1:352f608c3337 | 110 | * User application data size |
mluis | 1:352f608c3337 | 111 | */ |
mluis | 1:352f608c3337 | 112 | static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 1:352f608c3337 | 113 | |
mluis | 1:352f608c3337 | 114 | /*! |
mluis | 1:352f608c3337 | 115 | * User application data buffer size |
mluis | 1:352f608c3337 | 116 | */ |
mluis | 1:352f608c3337 | 117 | #define LORAWAN_APP_DATA_MAX_SIZE 64 |
mluis | 1:352f608c3337 | 118 | |
mluis | 1:352f608c3337 | 119 | /*! |
mluis | 0:92bca02df485 | 120 | * User application data |
mluis | 0:92bca02df485 | 121 | */ |
mluis | 1:352f608c3337 | 122 | static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; |
mluis | 1:352f608c3337 | 123 | |
mluis | 1:352f608c3337 | 124 | /*! |
mluis | 1:352f608c3337 | 125 | * Indicates if the node is sending confirmed or unconfirmed messages |
mluis | 1:352f608c3337 | 126 | */ |
mluis | 1:352f608c3337 | 127 | static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 0:92bca02df485 | 128 | |
mluis | 0:92bca02df485 | 129 | /*! |
mluis | 0:92bca02df485 | 130 | * Defines the application data transmission duty cycle |
mluis | 0:92bca02df485 | 131 | */ |
mluis | 0:92bca02df485 | 132 | static uint32_t TxDutyCycleTime; |
mluis | 0:92bca02df485 | 133 | |
mluis | 1:352f608c3337 | 134 | /*! |
mluis | 1:352f608c3337 | 135 | * Timer to handle the application data transmission duty cycle |
mluis | 1:352f608c3337 | 136 | */ |
mluis | 1:352f608c3337 | 137 | static TimerEvent_t TxNextPacketTimer; |
mluis | 0:92bca02df485 | 138 | |
mluis | 3:9c6f7f082151 | 139 | /*! |
uss1994 | 10:9a4efdd07a77 | 140 | * SOME APPLICATION PARAMETERS |
uss1994 | 10:9a4efdd07a77 | 141 | */ |
uss1994 | 18:0a4de80c02a6 | 142 | |
uss1994 | 10:9a4efdd07a77 | 143 | DigitalOut myled(D7); |
uss1994 | 10:9a4efdd07a77 | 144 | |
uss1994 | 10:9a4efdd07a77 | 145 | DigitalOut relayPin(D6); |
uss1994 | 25:2902c751ee57 | 146 | AnalogIn BAT_PIN(A2); |
uss1994 | 25:2902c751ee57 | 147 | AnalogIn LIGHT_1_PIN(A1); |
uss1994 | 10:9a4efdd07a77 | 148 | AnalogIn LIGHT_2_PIN(A5); |
uss1994 | 19:6f88d1cabaca | 149 | AnalogIn RH_PIN(PC_2); |
uss1994 | 10:9a4efdd07a77 | 150 | AnalogIn VCE_PIN(PB_1); |
uss1994 | 10:9a4efdd07a77 | 151 | |
amirchaudhary | 27:f024c8c51c6e | 152 | /* |
amirchaudhary | 27:f024c8c51c6e | 153 | D7 == Green |
amirchaudhary | 27:f024c8c51c6e | 154 | D8 == Blue |
amirchaudhary | 27:f024c8c51c6e | 155 | */ |
amirchaudhary | 27:f024c8c51c6e | 156 | |
amirchaudhary | 27:f024c8c51c6e | 157 | DigitalOut myGreenLed(D7); // ON = Lora Connected |
amirchaudhary | 27:f024c8c51c6e | 158 | DigitalOut myBlueLed(D8); // Blink twice = Sends data or increments counter value Pelase proceed |
amirchaudhary | 27:f024c8c51c6e | 159 | |
amirchaudhary | 14:9ba8f6975308 | 160 | InterruptIn button1(USER_BUTTON); |
uss1994 | 11:e433cbbfd436 | 161 | volatile bool button1_pressed = false; // Used in the main loop |
uss1994 | 11:e433cbbfd436 | 162 | volatile bool button1_enabled = true; // Used for debouncing |
uss1994 | 11:e433cbbfd436 | 163 | Timeout button1_timeout; // Used for debouncing |
uss1994 | 11:e433cbbfd436 | 164 | int press_count; |
uss1994 | 11:e433cbbfd436 | 165 | Timeout buttonOptions_timeout; // Used for determining number of presses |
uss1994 | 11:e433cbbfd436 | 166 | |
uss1994 | 10:9a4efdd07a77 | 167 | unsigned int time_window = 5; // Default 3600 seconds in an hour |
uss1994 | 26:2cdfd2d309a6 | 168 | unsigned long long_interval = 31557600; // Default 31557600 seconds in a year |
uss1994 | 26:2cdfd2d309a6 | 169 | unsigned int hb_interval = 43200; // Default 86400 seconds in a day |
uss1994 | 26:2cdfd2d309a6 | 170 | unsigned long test_interval = 2592000; // Default 2592000 seconds in a month |
uss1994 | 10:9a4efdd07a77 | 171 | |
uss1994 | 10:9a4efdd07a77 | 172 | time_t next_stest; |
uss1994 | 10:9a4efdd07a77 | 173 | time_t next_ltest; |
uss1994 | 10:9a4efdd07a77 | 174 | |
uss1994 | 10:9a4efdd07a77 | 175 | uint8_t hb_data[5]; |
uss1994 | 16:e1d692816864 | 176 | uint8_t tdata[10]; |
uss1994 | 10:9a4efdd07a77 | 177 | |
uss1994 | 10:9a4efdd07a77 | 178 | uint8_t *data; |
uss1994 | 10:9a4efdd07a77 | 179 | uint8_t data_len; |
uss1994 | 10:9a4efdd07a77 | 180 | |
uss1994 | 10:9a4efdd07a77 | 181 | bool running_test; |
uss1994 | 10:9a4efdd07a77 | 182 | bool joining; |
uss1994 | 16:e1d692816864 | 183 | // bool received_downlink; |
uss1994 | 10:9a4efdd07a77 | 184 | |
uss1994 | 10:9a4efdd07a77 | 185 | time_t current_time; |
uss1994 | 10:9a4efdd07a77 | 186 | |
uss1994 | 10:9a4efdd07a77 | 187 | unsigned long last_hb; |
uss1994 | 10:9a4efdd07a77 | 188 | unsigned long test_start; |
uss1994 | 10:9a4efdd07a77 | 189 | |
uss1994 | 16:e1d692816864 | 190 | uint8_t measurements[5]; |
uss1994 | 10:9a4efdd07a77 | 191 | |
uss1994 | 10:9a4efdd07a77 | 192 | unsigned long test_duration; |
uss1994 | 10:9a4efdd07a77 | 193 | |
uss1994 | 10:9a4efdd07a77 | 194 | /*! |
mluis | 3:9c6f7f082151 | 195 | * Specifies the state of the application LED |
mluis | 3:9c6f7f082151 | 196 | */ |
mluis | 3:9c6f7f082151 | 197 | static bool AppLedStateOn = false; |
mluis | 3:9c6f7f082151 | 198 | volatile bool Led3StateChanged = false; |
mluis | 0:92bca02df485 | 199 | /*! |
mluis | 3:9c6f7f082151 | 200 | * Timer to handle the state of LED1 |
mluis | 0:92bca02df485 | 201 | */ |
mluis | 3:9c6f7f082151 | 202 | static TimerEvent_t Led1Timer; |
mluis | 3:9c6f7f082151 | 203 | volatile bool Led1State = false; |
mluis | 3:9c6f7f082151 | 204 | volatile bool Led1StateChanged = false; |
mluis | 3:9c6f7f082151 | 205 | /*! |
mluis | 3:9c6f7f082151 | 206 | * Timer to handle the state of LED2 |
mluis | 3:9c6f7f082151 | 207 | */ |
mluis | 3:9c6f7f082151 | 208 | static TimerEvent_t Led2Timer; |
mluis | 3:9c6f7f082151 | 209 | volatile bool Led2State = false; |
mluis | 3:9c6f7f082151 | 210 | volatile bool Led2StateChanged = false; |
mluis | 0:92bca02df485 | 211 | |
amirchaudhary | 27:f024c8c51c6e | 212 | enum GreenLedState_e{ |
amirchaudhary | 27:f024c8c51c6e | 213 | GREEN_LED_STATE_INIT=0, |
amirchaudhary | 27:f024c8c51c6e | 214 | GREEN_LED_STATE_JOINING, |
amirchaudhary | 27:f024c8c51c6e | 215 | GREEN_LED_STATE_JOINED |
amirchaudhary | 27:f024c8c51c6e | 216 | }GreenLedState; |
amirchaudhary | 27:f024c8c51c6e | 217 | volatile uint32_t GreenLedTickCounter=0; |
amirchaudhary | 27:f024c8c51c6e | 218 | |
amirchaudhary | 27:f024c8c51c6e | 219 | volatile bool BatteryLowFlag = true; |
amirchaudhary | 27:f024c8c51c6e | 220 | |
amirchaudhary | 27:f024c8c51c6e | 221 | volatile uint32_t GreenLedJoiningCounter=0; |
amirchaudhary | 27:f024c8c51c6e | 222 | |
amirchaudhary | 27:f024c8c51c6e | 223 | static TimerEvent_t GreenLedTimer; |
amirchaudhary | 27:f024c8c51c6e | 224 | static void OnGreenLedTimerEvent( void ) |
amirchaudhary | 27:f024c8c51c6e | 225 | { |
amirchaudhary | 27:f024c8c51c6e | 226 | MibRequestConfirm_t mibReq; |
amirchaudhary | 27:f024c8c51c6e | 227 | LoRaMacStatus_t status; |
amirchaudhary | 27:f024c8c51c6e | 228 | |
amirchaudhary | 27:f024c8c51c6e | 229 | if(BatteryLowFlag == true) |
amirchaudhary | 27:f024c8c51c6e | 230 | { |
amirchaudhary | 27:f024c8c51c6e | 231 | myGreenLed = 0; // OFF |
amirchaudhary | 27:f024c8c51c6e | 232 | } |
amirchaudhary | 27:f024c8c51c6e | 233 | else |
amirchaudhary | 27:f024c8c51c6e | 234 | { |
amirchaudhary | 27:f024c8c51c6e | 235 | // battery ok |
amirchaudhary | 27:f024c8c51c6e | 236 | GreenLedTickCounter++; |
amirchaudhary | 27:f024c8c51c6e | 237 | switch(GreenLedState) |
amirchaudhary | 27:f024c8c51c6e | 238 | { |
amirchaudhary | 27:f024c8c51c6e | 239 | case GREEN_LED_STATE_INIT: |
amirchaudhary | 27:f024c8c51c6e | 240 | { |
amirchaudhary | 27:f024c8c51c6e | 241 | GreenLedState = GREEN_LED_STATE_JOINING; |
amirchaudhary | 27:f024c8c51c6e | 242 | } |
amirchaudhary | 27:f024c8c51c6e | 243 | break; |
amirchaudhary | 27:f024c8c51c6e | 244 | |
amirchaudhary | 27:f024c8c51c6e | 245 | case GREEN_LED_STATE_JOINING: |
amirchaudhary | 27:f024c8c51c6e | 246 | { |
amirchaudhary | 27:f024c8c51c6e | 247 | GreenLedJoiningCounter++; |
amirchaudhary | 27:f024c8c51c6e | 248 | if((GreenLedJoiningCounter % 2) == 0) |
amirchaudhary | 27:f024c8c51c6e | 249 | { |
amirchaudhary | 27:f024c8c51c6e | 250 | GreenLedJoiningCounter = 0; |
amirchaudhary | 27:f024c8c51c6e | 251 | myGreenLed = !myGreenLed; // toggle |
amirchaudhary | 27:f024c8c51c6e | 252 | |
amirchaudhary | 27:f024c8c51c6e | 253 | // check if we have joined the network |
amirchaudhary | 27:f024c8c51c6e | 254 | mibReq.Type = MIB_NETWORK_JOINED; |
amirchaudhary | 27:f024c8c51c6e | 255 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
amirchaudhary | 27:f024c8c51c6e | 256 | if( status == LORAMAC_STATUS_OK ) |
amirchaudhary | 27:f024c8c51c6e | 257 | { |
amirchaudhary | 27:f024c8c51c6e | 258 | if( mibReq.Param.IsNetworkJoined == true ) |
amirchaudhary | 27:f024c8c51c6e | 259 | { |
amirchaudhary | 27:f024c8c51c6e | 260 | GreenLedState = GREEN_LED_STATE_JOINED; |
amirchaudhary | 27:f024c8c51c6e | 261 | } |
amirchaudhary | 27:f024c8c51c6e | 262 | } |
amirchaudhary | 27:f024c8c51c6e | 263 | } |
amirchaudhary | 27:f024c8c51c6e | 264 | } |
amirchaudhary | 27:f024c8c51c6e | 265 | break; |
amirchaudhary | 27:f024c8c51c6e | 266 | |
amirchaudhary | 27:f024c8c51c6e | 267 | case GREEN_LED_STATE_JOINED: |
amirchaudhary | 27:f024c8c51c6e | 268 | { |
amirchaudhary | 27:f024c8c51c6e | 269 | myGreenLed = 1; |
amirchaudhary | 27:f024c8c51c6e | 270 | } |
amirchaudhary | 27:f024c8c51c6e | 271 | break; |
amirchaudhary | 27:f024c8c51c6e | 272 | } |
amirchaudhary | 27:f024c8c51c6e | 273 | } |
amirchaudhary | 27:f024c8c51c6e | 274 | } |
amirchaudhary | 27:f024c8c51c6e | 275 | |
amirchaudhary | 27:f024c8c51c6e | 276 | static TimerEvent_t BatteryCheckTimer; |
amirchaudhary | 27:f024c8c51c6e | 277 | volatile bool BatteryCheckFlag=false; |
amirchaudhary | 27:f024c8c51c6e | 278 | static void OnBatteryCheckTimerEvent( void ) |
amirchaudhary | 27:f024c8c51c6e | 279 | { |
amirchaudhary | 27:f024c8c51c6e | 280 | BatteryCheckFlag = true; |
amirchaudhary | 27:f024c8c51c6e | 281 | } |
amirchaudhary | 27:f024c8c51c6e | 282 | |
mluis | 0:92bca02df485 | 283 | /*! |
mluis | 0:92bca02df485 | 284 | * Indicates if a new packet can be sent |
mluis | 0:92bca02df485 | 285 | */ |
mluis | 3:9c6f7f082151 | 286 | static bool NextTx = true; |
mluis | 0:92bca02df485 | 287 | |
mluis | 3:9c6f7f082151 | 288 | /*! |
mluis | 3:9c6f7f082151 | 289 | * Device states |
mluis | 3:9c6f7f082151 | 290 | */ |
amirchaudhary | 27:f024c8c51c6e | 291 | |
amirchaudhary | 27:f024c8c51c6e | 292 | |
amirchaudhary | 27:f024c8c51c6e | 293 | // Application Globals |
amirchaudhary | 27:f024c8c51c6e | 294 | |
amirchaudhary | 27:f024c8c51c6e | 295 | |
amirchaudhary | 27:f024c8c51c6e | 296 | |
amirchaudhary | 27:f024c8c51c6e | 297 | |
amirchaudhary | 27:f024c8c51c6e | 298 | |
mluis | 9:ee9dcbb9708d | 299 | static enum eDeviceState |
mluis | 3:9c6f7f082151 | 300 | { |
mluis | 3:9c6f7f082151 | 301 | DEVICE_STATE_INIT, |
mluis | 3:9c6f7f082151 | 302 | DEVICE_STATE_JOIN, |
mluis | 3:9c6f7f082151 | 303 | DEVICE_STATE_SEND, |
mluis | 3:9c6f7f082151 | 304 | DEVICE_STATE_CYCLE, |
mluis | 3:9c6f7f082151 | 305 | DEVICE_STATE_SLEEP |
mluis | 3:9c6f7f082151 | 306 | }DeviceState; |
mluis | 0:92bca02df485 | 307 | |
uss1994 | 10:9a4efdd07a77 | 308 | static enum eMessageType |
uss1994 | 10:9a4efdd07a77 | 309 | { |
uss1994 | 10:9a4efdd07a77 | 310 | MESSAGE_TYPE_HB, |
uss1994 | 10:9a4efdd07a77 | 311 | MESSAGE_TYPE_SHORT_TEST, |
uss1994 | 10:9a4efdd07a77 | 312 | MESSAGE_TYPE_LONG_TEST |
uss1994 | 10:9a4efdd07a77 | 313 | }MessageType; |
uss1994 | 10:9a4efdd07a77 | 314 | |
mluis | 3:9c6f7f082151 | 315 | /*! |
mluis | 3:9c6f7f082151 | 316 | * LoRaWAN compliance tests support data |
mluis | 3:9c6f7f082151 | 317 | */ |
mluis | 3:9c6f7f082151 | 318 | struct ComplianceTest_s |
mluis | 3:9c6f7f082151 | 319 | { |
mluis | 3:9c6f7f082151 | 320 | bool Running; |
mluis | 3:9c6f7f082151 | 321 | uint8_t State; |
mluis | 3:9c6f7f082151 | 322 | bool IsTxConfirmed; |
mluis | 3:9c6f7f082151 | 323 | uint8_t AppPort; |
mluis | 3:9c6f7f082151 | 324 | uint8_t AppDataSize; |
mluis | 3:9c6f7f082151 | 325 | uint8_t *AppDataBuffer; |
mluis | 3:9c6f7f082151 | 326 | uint16_t DownLinkCounter; |
mluis | 3:9c6f7f082151 | 327 | bool LinkCheck; |
mluis | 3:9c6f7f082151 | 328 | uint8_t DemodMargin; |
mluis | 3:9c6f7f082151 | 329 | uint8_t NbGateways; |
mluis | 3:9c6f7f082151 | 330 | }ComplianceTest; |
mluis | 0:92bca02df485 | 331 | |
mluis | 3:9c6f7f082151 | 332 | /* |
mluis | 3:9c6f7f082151 | 333 | * SerialDisplay managment variables |
mluis | 3:9c6f7f082151 | 334 | */ |
mluis | 1:352f608c3337 | 335 | |
mluis | 3:9c6f7f082151 | 336 | /*! |
mluis | 3:9c6f7f082151 | 337 | * Indicates if the MAC layer network join status has changed. |
mluis | 3:9c6f7f082151 | 338 | */ |
mluis | 3:9c6f7f082151 | 339 | static bool IsNetworkJoinedStatusUpdate = false; |
mluis | 3:9c6f7f082151 | 340 | |
mluis | 3:9c6f7f082151 | 341 | /*! |
mluis | 3:9c6f7f082151 | 342 | * Strucure containing the Uplink status |
mluis | 3:9c6f7f082151 | 343 | */ |
mluis | 0:92bca02df485 | 344 | struct sLoRaMacUplinkStatus |
mluis | 0:92bca02df485 | 345 | { |
mluis | 0:92bca02df485 | 346 | uint8_t Acked; |
mluis | 0:92bca02df485 | 347 | int8_t Datarate; |
mluis | 0:92bca02df485 | 348 | uint16_t UplinkCounter; |
mluis | 0:92bca02df485 | 349 | uint8_t Port; |
mluis | 0:92bca02df485 | 350 | uint8_t *Buffer; |
mluis | 0:92bca02df485 | 351 | uint8_t BufferSize; |
mluis | 0:92bca02df485 | 352 | }LoRaMacUplinkStatus; |
mluis | 3:9c6f7f082151 | 353 | volatile bool UplinkStatusUpdated = false; |
mluis | 0:92bca02df485 | 354 | |
mluis | 3:9c6f7f082151 | 355 | /*! |
mluis | 3:9c6f7f082151 | 356 | * Strucure containing the Downlink status |
mluis | 3:9c6f7f082151 | 357 | */ |
mluis | 0:92bca02df485 | 358 | struct sLoRaMacDownlinkStatus |
mluis | 0:92bca02df485 | 359 | { |
mluis | 0:92bca02df485 | 360 | int16_t Rssi; |
mluis | 0:92bca02df485 | 361 | int8_t Snr; |
mluis | 0:92bca02df485 | 362 | uint16_t DownlinkCounter; |
mluis | 0:92bca02df485 | 363 | bool RxData; |
mluis | 0:92bca02df485 | 364 | uint8_t Port; |
mluis | 0:92bca02df485 | 365 | uint8_t *Buffer; |
mluis | 0:92bca02df485 | 366 | uint8_t BufferSize; |
mluis | 0:92bca02df485 | 367 | }LoRaMacDownlinkStatus; |
mluis | 3:9c6f7f082151 | 368 | volatile bool DownlinkStatusUpdated = false; |
mluis | 0:92bca02df485 | 369 | |
mluis | 0:92bca02df485 | 370 | void SerialDisplayRefresh( void ) |
mluis | 0:92bca02df485 | 371 | { |
mluis | 3:9c6f7f082151 | 372 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 373 | |
mluis | 0:92bca02df485 | 374 | SerialDisplayInit( ); |
mluis | 0:92bca02df485 | 375 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 0:92bca02df485 | 376 | |
mluis | 0:92bca02df485 | 377 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:92bca02df485 | 378 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 3:9c6f7f082151 | 379 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 0:92bca02df485 | 380 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 0:92bca02df485 | 381 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 7:3173f0508a98 | 382 | #endif |
mluis | 0:92bca02df485 | 383 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 0:92bca02df485 | 384 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 0:92bca02df485 | 385 | SerialDisplayUpdateKey( 7, AppKey ); |
mluis | 3:9c6f7f082151 | 386 | |
mluis | 3:9c6f7f082151 | 387 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 388 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 389 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:92bca02df485 | 390 | |
mluis | 0:92bca02df485 | 391 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 1:352f608c3337 | 392 | #if defined( USE_BAND_868 ) |
mluis | 0:92bca02df485 | 393 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 1:352f608c3337 | 394 | #else |
mluis | 1:352f608c3337 | 395 | SerialDisplayUpdateDutyCycle( false ); |
mluis | 1:352f608c3337 | 396 | #endif |
mluis | 0:92bca02df485 | 397 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
mluis | 0:92bca02df485 | 398 | |
mluis | 0:92bca02df485 | 399 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:92bca02df485 | 400 | } |
mluis | 0:92bca02df485 | 401 | |
mluis | 0:92bca02df485 | 402 | void SerialRxProcess( void ) |
mluis | 0:92bca02df485 | 403 | { |
mluis | 0:92bca02df485 | 404 | if( SerialDisplayReadable( ) == true ) |
mluis | 0:92bca02df485 | 405 | { |
mluis | 0:92bca02df485 | 406 | switch( SerialDisplayGetChar( ) ) |
mluis | 0:92bca02df485 | 407 | { |
mluis | 0:92bca02df485 | 408 | case 'R': |
mluis | 0:92bca02df485 | 409 | case 'r': |
mluis | 0:92bca02df485 | 410 | // Refresh Serial screen |
mluis | 0:92bca02df485 | 411 | SerialDisplayRefresh( ); |
mluis | 0:92bca02df485 | 412 | break; |
mluis | 0:92bca02df485 | 413 | default: |
mluis | 0:92bca02df485 | 414 | break; |
mluis | 0:92bca02df485 | 415 | } |
mluis | 0:92bca02df485 | 416 | } |
mluis | 0:92bca02df485 | 417 | } |
mluis | 0:92bca02df485 | 418 | |
uss1994 | 11:e433cbbfd436 | 419 | // Enables button when bouncing is over |
uss1994 | 11:e433cbbfd436 | 420 | void button1_enabled_cb(void) |
uss1994 | 11:e433cbbfd436 | 421 | { |
uss1994 | 11:e433cbbfd436 | 422 | button1_enabled = true; |
uss1994 | 11:e433cbbfd436 | 423 | } |
uss1994 | 11:e433cbbfd436 | 424 | |
uss1994 | 11:e433cbbfd436 | 425 | // ISR handling button pressed event |
uss1994 | 11:e433cbbfd436 | 426 | void button1_onpressed_cb(void) |
uss1994 | 11:e433cbbfd436 | 427 | { |
uss1994 | 11:e433cbbfd436 | 428 | if (button1_enabled) { // Disabled while the button is bouncing |
uss1994 | 11:e433cbbfd436 | 429 | button1_enabled = false; |
uss1994 | 11:e433cbbfd436 | 430 | button1_pressed = true; // To be read by the main loop |
uss1994 | 24:416a3fec29ea | 431 | button1_timeout.attach(callback(button1_enabled_cb), 0.2); // Debounce time 200 ms |
uss1994 | 11:e433cbbfd436 | 432 | } |
uss1994 | 11:e433cbbfd436 | 433 | } |
uss1994 | 11:e433cbbfd436 | 434 | |
uss1994 | 10:9a4efdd07a77 | 435 | // **************************** COMMUNICATION PACKET DEFINITION METHODS ******************************** // |
uss1994 | 10:9a4efdd07a77 | 436 | |
uss1994 | 10:9a4efdd07a77 | 437 | void heartbeat_message(uint8_t *hb_data, uint8_t *current_date) { |
uss1994 | 10:9a4efdd07a77 | 438 | hb_data[0] = 0xD2; |
uss1994 | 10:9a4efdd07a77 | 439 | memcpy(hb_data+1, current_date, 4); |
uss1994 | 10:9a4efdd07a77 | 440 | } |
uss1994 | 10:9a4efdd07a77 | 441 | |
uss1994 | 16:e1d692816864 | 442 | void short_test_result(uint8_t *tdata, uint8_t *test_start_date, uint8_t *measurements) { |
uss1994 | 16:e1d692816864 | 443 | tdata[0] = 0xD7; |
uss1994 | 16:e1d692816864 | 444 | memcpy(tdata+1, test_start_date, 4); |
uss1994 | 16:e1d692816864 | 445 | memcpy(tdata+5, measurements, 5); |
uss1994 | 10:9a4efdd07a77 | 446 | } |
uss1994 | 10:9a4efdd07a77 | 447 | |
uss1994 | 16:e1d692816864 | 448 | void long_test_result(uint8_t *tdata, uint8_t *test_start_date, uint8_t *measurements) { |
uss1994 | 16:e1d692816864 | 449 | tdata[0] = 0xD8; |
uss1994 | 16:e1d692816864 | 450 | memcpy(tdata+1, test_start_date, 4); |
uss1994 | 16:e1d692816864 | 451 | memcpy(tdata+5, measurements, 5); |
uss1994 | 10:9a4efdd07a77 | 452 | } |
uss1994 | 10:9a4efdd07a77 | 453 | |
mluis | 0:92bca02df485 | 454 | /*! |
mluis | 3:9c6f7f082151 | 455 | * \brief Prepares the payload of the frame |
mluis | 0:92bca02df485 | 456 | */ |
mluis | 0:92bca02df485 | 457 | static void PrepareTxFrame( uint8_t port ) |
mluis | 0:92bca02df485 | 458 | { |
mluis | 1:352f608c3337 | 459 | switch( port ) |
mluis | 1:352f608c3337 | 460 | { |
mluis | 1:352f608c3337 | 461 | case 15: |
uss1994 | 10:9a4efdd07a77 | 462 | { |
uss1994 | 10:9a4efdd07a77 | 463 | switch ( MessageType ) |
mluis | 1:352f608c3337 | 464 | { |
uss1994 | 10:9a4efdd07a77 | 465 | case MESSAGE_TYPE_HB: |
uss1994 | 10:9a4efdd07a77 | 466 | { |
uss1994 | 10:9a4efdd07a77 | 467 | AppDataSize = 5; |
uss1994 | 10:9a4efdd07a77 | 468 | heartbeat_message(AppData, (uint8_t *)¤t_time); |
uss1994 | 10:9a4efdd07a77 | 469 | break; |
uss1994 | 10:9a4efdd07a77 | 470 | } |
uss1994 | 10:9a4efdd07a77 | 471 | case MESSAGE_TYPE_SHORT_TEST: |
uss1994 | 10:9a4efdd07a77 | 472 | { |
uss1994 | 16:e1d692816864 | 473 | AppDataSize = 10; |
uss1994 | 16:e1d692816864 | 474 | short_test_result(AppData, (uint8_t *)¤t_time, measurements); |
uss1994 | 10:9a4efdd07a77 | 475 | break; |
uss1994 | 10:9a4efdd07a77 | 476 | } |
uss1994 | 10:9a4efdd07a77 | 477 | case MESSAGE_TYPE_LONG_TEST: |
uss1994 | 10:9a4efdd07a77 | 478 | { |
uss1994 | 16:e1d692816864 | 479 | AppDataSize = 10; |
uss1994 | 16:e1d692816864 | 480 | long_test_result(AppData, (uint8_t *)¤t_time, measurements); |
uss1994 | 10:9a4efdd07a77 | 481 | break; |
uss1994 | 10:9a4efdd07a77 | 482 | } |
uss1994 | 10:9a4efdd07a77 | 483 | default: |
uss1994 | 10:9a4efdd07a77 | 484 | { |
uss1994 | 10:9a4efdd07a77 | 485 | AppDataSize = 5; |
uss1994 | 10:9a4efdd07a77 | 486 | heartbeat_message(AppData, (uint8_t *)¤t_time); |
uss1994 | 10:9a4efdd07a77 | 487 | break; |
uss1994 | 10:9a4efdd07a77 | 488 | } |
mluis | 1:352f608c3337 | 489 | } |
mluis | 1:352f608c3337 | 490 | } |
mluis | 1:352f608c3337 | 491 | break; |
mluis | 1:352f608c3337 | 492 | case 224: |
mluis | 3:9c6f7f082151 | 493 | if( ComplianceTest.LinkCheck == true ) |
mluis | 1:352f608c3337 | 494 | { |
mluis | 3:9c6f7f082151 | 495 | ComplianceTest.LinkCheck = false; |
mluis | 1:352f608c3337 | 496 | AppDataSize = 3; |
mluis | 1:352f608c3337 | 497 | AppData[0] = 5; |
mluis | 3:9c6f7f082151 | 498 | AppData[1] = ComplianceTest.DemodMargin; |
mluis | 3:9c6f7f082151 | 499 | AppData[2] = ComplianceTest.NbGateways; |
mluis | 3:9c6f7f082151 | 500 | ComplianceTest.State = 1; |
mluis | 1:352f608c3337 | 501 | } |
mluis | 1:352f608c3337 | 502 | else |
mluis | 1:352f608c3337 | 503 | { |
mluis | 3:9c6f7f082151 | 504 | switch( ComplianceTest.State ) |
mluis | 1:352f608c3337 | 505 | { |
mluis | 1:352f608c3337 | 506 | case 4: |
mluis | 3:9c6f7f082151 | 507 | ComplianceTest.State = 1; |
mluis | 1:352f608c3337 | 508 | break; |
mluis | 1:352f608c3337 | 509 | case 1: |
mluis | 1:352f608c3337 | 510 | AppDataSize = 2; |
mluis | 3:9c6f7f082151 | 511 | AppData[0] = ComplianceTest.DownLinkCounter >> 8; |
mluis | 3:9c6f7f082151 | 512 | AppData[1] = ComplianceTest.DownLinkCounter; |
mluis | 1:352f608c3337 | 513 | break; |
mluis | 1:352f608c3337 | 514 | } |
mluis | 1:352f608c3337 | 515 | } |
mluis | 1:352f608c3337 | 516 | break; |
mluis | 3:9c6f7f082151 | 517 | default: |
mluis | 3:9c6f7f082151 | 518 | break; |
mluis | 1:352f608c3337 | 519 | } |
mluis | 0:92bca02df485 | 520 | } |
mluis | 0:92bca02df485 | 521 | |
mluis | 3:9c6f7f082151 | 522 | /*! |
mluis | 3:9c6f7f082151 | 523 | * \brief Prepares the payload of the frame |
mluis | 3:9c6f7f082151 | 524 | * |
mluis | 3:9c6f7f082151 | 525 | * \retval [0: frame could be send, 1: error] |
mluis | 3:9c6f7f082151 | 526 | */ |
mluis | 0:92bca02df485 | 527 | static bool SendFrame( void ) |
mluis | 0:92bca02df485 | 528 | { |
mluis | 3:9c6f7f082151 | 529 | McpsReq_t mcpsReq; |
mluis | 3:9c6f7f082151 | 530 | LoRaMacTxInfo_t txInfo; |
mluis | 9:ee9dcbb9708d | 531 | |
mluis | 3:9c6f7f082151 | 532 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
mluis | 1:352f608c3337 | 533 | { |
mluis | 3:9c6f7f082151 | 534 | // Send empty frame in order to flush MAC commands |
mluis | 3:9c6f7f082151 | 535 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 3:9c6f7f082151 | 536 | mcpsReq.Req.Unconfirmed.fBuffer = NULL; |
mluis | 3:9c6f7f082151 | 537 | mcpsReq.Req.Unconfirmed.fBufferSize = 0; |
mluis | 3:9c6f7f082151 | 538 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 5:1e9f6a365854 | 539 | |
mluis | 3:9c6f7f082151 | 540 | LoRaMacUplinkStatus.Acked = false; |
uss1994 | 18:0a4de80c02a6 | 541 | LoRaMacUplinkStatus.Port = 0; |
mluis | 3:9c6f7f082151 | 542 | LoRaMacUplinkStatus.Buffer = NULL; |
mluis | 3:9c6f7f082151 | 543 | LoRaMacUplinkStatus.BufferSize = 0; |
mluis | 3:9c6f7f082151 | 544 | SerialDisplayUpdateFrameType( false ); |
uss1994 | 19:6f88d1cabaca | 545 | // SerialDisplayPrintDebugStatus( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) ); |
uss1994 | 15:f4805eb13d73 | 546 | |
mluis | 1:352f608c3337 | 547 | } |
mluis | 1:352f608c3337 | 548 | else |
mluis | 1:352f608c3337 | 549 | { |
mluis | 3:9c6f7f082151 | 550 | LoRaMacUplinkStatus.Acked = false; |
mluis | 3:9c6f7f082151 | 551 | LoRaMacUplinkStatus.Port = AppPort; |
mluis | 3:9c6f7f082151 | 552 | LoRaMacUplinkStatus.Buffer = AppData; |
mluis | 3:9c6f7f082151 | 553 | LoRaMacUplinkStatus.BufferSize = AppDataSize; |
mluis | 3:9c6f7f082151 | 554 | SerialDisplayUpdateFrameType( IsTxConfirmed ); |
mluis | 3:9c6f7f082151 | 555 | |
mluis | 3:9c6f7f082151 | 556 | if( IsTxConfirmed == false ) |
mluis | 3:9c6f7f082151 | 557 | { |
mluis | 3:9c6f7f082151 | 558 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 3:9c6f7f082151 | 559 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
mluis | 3:9c6f7f082151 | 560 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
mluis | 3:9c6f7f082151 | 561 | mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; |
mluis | 3:9c6f7f082151 | 562 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 3:9c6f7f082151 | 563 | } |
mluis | 3:9c6f7f082151 | 564 | else |
mluis | 3:9c6f7f082151 | 565 | { |
mluis | 3:9c6f7f082151 | 566 | mcpsReq.Type = MCPS_CONFIRMED; |
mluis | 3:9c6f7f082151 | 567 | mcpsReq.Req.Confirmed.fPort = AppPort; |
mluis | 3:9c6f7f082151 | 568 | mcpsReq.Req.Confirmed.fBuffer = AppData; |
mluis | 3:9c6f7f082151 | 569 | mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; |
mluis | 4:00cf2370c99d | 570 | mcpsReq.Req.Confirmed.NbTrials = 8; |
mluis | 3:9c6f7f082151 | 571 | mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 3:9c6f7f082151 | 572 | } |
mluis | 1:352f608c3337 | 573 | } |
mluis | 1:352f608c3337 | 574 | |
mluis | 3:9c6f7f082151 | 575 | if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) |
mluis | 0:92bca02df485 | 576 | { |
uss1994 | 18:0a4de80c02a6 | 577 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
uss1994 | 18:0a4de80c02a6 | 578 | { |
uss1994 | 18:0a4de80c02a6 | 579 | return true; |
uss1994 | 18:0a4de80c02a6 | 580 | } |
mluis | 0:92bca02df485 | 581 | return false; |
mluis | 0:92bca02df485 | 582 | } |
mluis | 3:9c6f7f082151 | 583 | return true; |
mluis | 0:92bca02df485 | 584 | } |
mluis | 0:92bca02df485 | 585 | |
mluis | 0:92bca02df485 | 586 | /*! |
mluis | 0:92bca02df485 | 587 | * \brief Function executed on TxNextPacket Timeout event |
mluis | 0:92bca02df485 | 588 | */ |
mluis | 0:92bca02df485 | 589 | static void OnTxNextPacketTimerEvent( void ) |
mluis | 0:92bca02df485 | 590 | { |
mluis | 3:9c6f7f082151 | 591 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 592 | LoRaMacStatus_t status; |
mluis | 3:9c6f7f082151 | 593 | |
mluis | 0:92bca02df485 | 594 | TimerStop( &TxNextPacketTimer ); |
mluis | 3:9c6f7f082151 | 595 | |
mluis | 3:9c6f7f082151 | 596 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 597 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 598 | |
mluis | 3:9c6f7f082151 | 599 | if( status == LORAMAC_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 600 | { |
mluis | 3:9c6f7f082151 | 601 | if( mibReq.Param.IsNetworkJoined == true ) |
mluis | 3:9c6f7f082151 | 602 | { |
mluis | 3:9c6f7f082151 | 603 | DeviceState = DEVICE_STATE_SEND; |
mluis | 3:9c6f7f082151 | 604 | NextTx = true; |
mluis | 3:9c6f7f082151 | 605 | } |
mluis | 3:9c6f7f082151 | 606 | else |
mluis | 3:9c6f7f082151 | 607 | { |
mluis | 3:9c6f7f082151 | 608 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 3:9c6f7f082151 | 609 | } |
mluis | 3:9c6f7f082151 | 610 | } |
mluis | 0:92bca02df485 | 611 | } |
mluis | 0:92bca02df485 | 612 | |
mluis | 0:92bca02df485 | 613 | /*! |
mluis | 0:92bca02df485 | 614 | * \brief Function executed on Led 1 Timeout event |
mluis | 0:92bca02df485 | 615 | */ |
mluis | 0:92bca02df485 | 616 | static void OnLed1TimerEvent( void ) |
mluis | 0:92bca02df485 | 617 | { |
mluis | 0:92bca02df485 | 618 | TimerStop( &Led1Timer ); |
mluis | 3:9c6f7f082151 | 619 | // Switch LED 1 OFF |
mluis | 1:352f608c3337 | 620 | Led1State = false; |
mluis | 0:92bca02df485 | 621 | Led1StateChanged = true; |
mluis | 0:92bca02df485 | 622 | } |
mluis | 0:92bca02df485 | 623 | |
mluis | 0:92bca02df485 | 624 | /*! |
mluis | 0:92bca02df485 | 625 | * \brief Function executed on Led 2 Timeout event |
mluis | 0:92bca02df485 | 626 | */ |
mluis | 0:92bca02df485 | 627 | static void OnLed2TimerEvent( void ) |
mluis | 0:92bca02df485 | 628 | { |
mluis | 0:92bca02df485 | 629 | TimerStop( &Led2Timer ); |
mluis | 3:9c6f7f082151 | 630 | // Switch LED 2 OFF |
mluis | 1:352f608c3337 | 631 | Led2State = false; |
mluis | 0:92bca02df485 | 632 | Led2StateChanged = true; |
mluis | 0:92bca02df485 | 633 | } |
mluis | 0:92bca02df485 | 634 | |
mluis | 0:92bca02df485 | 635 | /*! |
mluis | 3:9c6f7f082151 | 636 | * \brief MCPS-Confirm event function |
mluis | 3:9c6f7f082151 | 637 | * |
mluis | 5:1e9f6a365854 | 638 | * \param [IN] mcpsConfirm - Pointer to the confirm structure, |
mluis | 3:9c6f7f082151 | 639 | * containing confirm attributes. |
mluis | 0:92bca02df485 | 640 | */ |
mluis | 5:1e9f6a365854 | 641 | static void McpsConfirm( McpsConfirm_t *mcpsConfirm ) |
mluis | 0:92bca02df485 | 642 | { |
mluis | 5:1e9f6a365854 | 643 | if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 0:92bca02df485 | 644 | { |
mluis | 5:1e9f6a365854 | 645 | switch( mcpsConfirm->McpsRequest ) |
mluis | 3:9c6f7f082151 | 646 | { |
mluis | 3:9c6f7f082151 | 647 | case MCPS_UNCONFIRMED: |
mluis | 3:9c6f7f082151 | 648 | { |
mluis | 3:9c6f7f082151 | 649 | // Check Datarate |
mluis | 3:9c6f7f082151 | 650 | // Check TxPower |
mluis | 3:9c6f7f082151 | 651 | break; |
mluis | 3:9c6f7f082151 | 652 | } |
mluis | 3:9c6f7f082151 | 653 | case MCPS_CONFIRMED: |
mluis | 3:9c6f7f082151 | 654 | { |
mluis | 3:9c6f7f082151 | 655 | // Check Datarate |
mluis | 3:9c6f7f082151 | 656 | // Check TxPower |
mluis | 3:9c6f7f082151 | 657 | // Check AckReceived |
mluis | 5:1e9f6a365854 | 658 | // Check NbTrials |
mluis | 5:1e9f6a365854 | 659 | LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived; |
mluis | 3:9c6f7f082151 | 660 | break; |
mluis | 3:9c6f7f082151 | 661 | } |
mluis | 3:9c6f7f082151 | 662 | case MCPS_PROPRIETARY: |
mluis | 3:9c6f7f082151 | 663 | { |
mluis | 3:9c6f7f082151 | 664 | break; |
mluis | 3:9c6f7f082151 | 665 | } |
mluis | 3:9c6f7f082151 | 666 | default: |
mluis | 3:9c6f7f082151 | 667 | break; |
mluis | 3:9c6f7f082151 | 668 | } |
mluis | 5:1e9f6a365854 | 669 | LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate; |
mluis | 5:1e9f6a365854 | 670 | LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter; |
mluis | 5:1e9f6a365854 | 671 | |
mluis | 7:3173f0508a98 | 672 | // Switch LED 1 ON |
mluis | 7:3173f0508a98 | 673 | Led1State = true; |
mluis | 7:3173f0508a98 | 674 | Led1StateChanged = true; |
mluis | 7:3173f0508a98 | 675 | TimerStart( &Led1Timer ); |
mluis | 7:3173f0508a98 | 676 | |
mluis | 3:9c6f7f082151 | 677 | UplinkStatusUpdated = true; |
mluis | 3:9c6f7f082151 | 678 | } |
mluis | 3:9c6f7f082151 | 679 | NextTx = true; |
mluis | 3:9c6f7f082151 | 680 | } |
mluis | 3:9c6f7f082151 | 681 | |
mluis | 3:9c6f7f082151 | 682 | /*! |
mluis | 3:9c6f7f082151 | 683 | * \brief MCPS-Indication event function |
mluis | 3:9c6f7f082151 | 684 | * |
mluis | 5:1e9f6a365854 | 685 | * \param [IN] mcpsIndication - Pointer to the indication structure, |
mluis | 3:9c6f7f082151 | 686 | * containing indication attributes. |
mluis | 3:9c6f7f082151 | 687 | */ |
mluis | 5:1e9f6a365854 | 688 | static void McpsIndication( McpsIndication_t *mcpsIndication ) |
mluis | 3:9c6f7f082151 | 689 | { |
mluis | 5:1e9f6a365854 | 690 | if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 691 | { |
mluis | 3:9c6f7f082151 | 692 | return; |
mluis | 3:9c6f7f082151 | 693 | } |
mluis | 3:9c6f7f082151 | 694 | |
mluis | 5:1e9f6a365854 | 695 | switch( mcpsIndication->McpsIndication ) |
mluis | 3:9c6f7f082151 | 696 | { |
mluis | 3:9c6f7f082151 | 697 | case MCPS_UNCONFIRMED: |
mluis | 3:9c6f7f082151 | 698 | { |
mluis | 3:9c6f7f082151 | 699 | break; |
mluis | 3:9c6f7f082151 | 700 | } |
mluis | 3:9c6f7f082151 | 701 | case MCPS_CONFIRMED: |
mluis | 3:9c6f7f082151 | 702 | { |
mluis | 3:9c6f7f082151 | 703 | break; |
mluis | 3:9c6f7f082151 | 704 | } |
mluis | 3:9c6f7f082151 | 705 | case MCPS_PROPRIETARY: |
mluis | 3:9c6f7f082151 | 706 | { |
mluis | 3:9c6f7f082151 | 707 | break; |
mluis | 3:9c6f7f082151 | 708 | } |
mluis | 3:9c6f7f082151 | 709 | case MCPS_MULTICAST: |
mluis | 3:9c6f7f082151 | 710 | { |
mluis | 3:9c6f7f082151 | 711 | break; |
mluis | 3:9c6f7f082151 | 712 | } |
mluis | 3:9c6f7f082151 | 713 | default: |
mluis | 3:9c6f7f082151 | 714 | break; |
mluis | 3:9c6f7f082151 | 715 | } |
mluis | 3:9c6f7f082151 | 716 | |
mluis | 3:9c6f7f082151 | 717 | // Check Multicast |
mluis | 3:9c6f7f082151 | 718 | // Check Port |
mluis | 3:9c6f7f082151 | 719 | // Check Datarate |
mluis | 3:9c6f7f082151 | 720 | // Check FramePending |
mluis | 3:9c6f7f082151 | 721 | // Check Buffer |
mluis | 3:9c6f7f082151 | 722 | // Check BufferSize |
mluis | 3:9c6f7f082151 | 723 | // Check Rssi |
mluis | 3:9c6f7f082151 | 724 | // Check Snr |
mluis | 3:9c6f7f082151 | 725 | // Check RxSlot |
mluis | 5:1e9f6a365854 | 726 | LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi; |
mluis | 5:1e9f6a365854 | 727 | if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1 |
mluis | 3:9c6f7f082151 | 728 | { |
mluis | 3:9c6f7f082151 | 729 | // Invert and divide by 4 |
mluis | 5:1e9f6a365854 | 730 | LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2; |
mluis | 3:9c6f7f082151 | 731 | LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr; |
mluis | 0:92bca02df485 | 732 | } |
mluis | 0:92bca02df485 | 733 | else |
mluis | 0:92bca02df485 | 734 | { |
mluis | 3:9c6f7f082151 | 735 | // Divide by 4 |
mluis | 5:1e9f6a365854 | 736 | LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2; |
mluis | 3:9c6f7f082151 | 737 | } |
mluis | 3:9c6f7f082151 | 738 | LoRaMacDownlinkStatus.DownlinkCounter++; |
mluis | 5:1e9f6a365854 | 739 | LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData; |
mluis | 5:1e9f6a365854 | 740 | LoRaMacDownlinkStatus.Port = mcpsIndication->Port; |
mluis | 5:1e9f6a365854 | 741 | LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer; |
mluis | 5:1e9f6a365854 | 742 | LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize; |
mluis | 3:9c6f7f082151 | 743 | |
mluis | 3:9c6f7f082151 | 744 | if( ComplianceTest.Running == true ) |
mluis | 3:9c6f7f082151 | 745 | { |
mluis | 3:9c6f7f082151 | 746 | ComplianceTest.DownLinkCounter++; |
mluis | 3:9c6f7f082151 | 747 | } |
mluis | 0:92bca02df485 | 748 | |
mluis | 5:1e9f6a365854 | 749 | if( mcpsIndication->RxData == true ) |
mluis | 3:9c6f7f082151 | 750 | { |
mluis | 5:1e9f6a365854 | 751 | switch( mcpsIndication->Port ) |
mluis | 0:92bca02df485 | 752 | { |
mluis | 3:9c6f7f082151 | 753 | case 1: // The application LED can be controlled on port 1 or 2 |
mluis | 3:9c6f7f082151 | 754 | case 2: |
mluis | 5:1e9f6a365854 | 755 | if( mcpsIndication->BufferSize == 1 ) |
mluis | 1:352f608c3337 | 756 | { |
mluis | 5:1e9f6a365854 | 757 | AppLedStateOn = mcpsIndication->Buffer[0] & 0x01; |
mluis | 3:9c6f7f082151 | 758 | Led3StateChanged = true; |
mluis | 3:9c6f7f082151 | 759 | } |
mluis | 3:9c6f7f082151 | 760 | break; |
mluis | 3:9c6f7f082151 | 761 | case 224: |
mluis | 3:9c6f7f082151 | 762 | if( ComplianceTest.Running == false ) |
mluis | 3:9c6f7f082151 | 763 | { |
mluis | 3:9c6f7f082151 | 764 | // Check compliance test enable command (i) |
mluis | 5:1e9f6a365854 | 765 | if( ( mcpsIndication->BufferSize == 4 ) && |
mluis | 5:1e9f6a365854 | 766 | ( mcpsIndication->Buffer[0] == 0x01 ) && |
mluis | 5:1e9f6a365854 | 767 | ( mcpsIndication->Buffer[1] == 0x01 ) && |
mluis | 5:1e9f6a365854 | 768 | ( mcpsIndication->Buffer[2] == 0x01 ) && |
mluis | 5:1e9f6a365854 | 769 | ( mcpsIndication->Buffer[3] == 0x01 ) ) |
mluis | 1:352f608c3337 | 770 | { |
mluis | 3:9c6f7f082151 | 771 | IsTxConfirmed = false; |
mluis | 3:9c6f7f082151 | 772 | AppPort = 224; |
mluis | 3:9c6f7f082151 | 773 | AppDataSize = 2; |
mluis | 3:9c6f7f082151 | 774 | ComplianceTest.DownLinkCounter = 0; |
mluis | 3:9c6f7f082151 | 775 | ComplianceTest.LinkCheck = false; |
mluis | 3:9c6f7f082151 | 776 | ComplianceTest.DemodMargin = 0; |
mluis | 3:9c6f7f082151 | 777 | ComplianceTest.NbGateways = 0; |
mluis | 3:9c6f7f082151 | 778 | ComplianceTest.Running = true; |
mluis | 3:9c6f7f082151 | 779 | ComplianceTest.State = 1; |
mluis | 9:ee9dcbb9708d | 780 | |
mluis | 3:9c6f7f082151 | 781 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 782 | mibReq.Type = MIB_ADR; |
mluis | 3:9c6f7f082151 | 783 | mibReq.Param.AdrEnable = true; |
mluis | 3:9c6f7f082151 | 784 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 785 | |
mluis | 3:9c6f7f082151 | 786 | #if defined( USE_BAND_868 ) |
mluis | 3:9c6f7f082151 | 787 | LoRaMacTestSetDutyCycleOn( false ); |
mluis | 3:9c6f7f082151 | 788 | #endif |
mluis | 1:352f608c3337 | 789 | } |
mluis | 1:352f608c3337 | 790 | } |
mluis | 0:92bca02df485 | 791 | else |
mluis | 0:92bca02df485 | 792 | { |
mluis | 5:1e9f6a365854 | 793 | ComplianceTest.State = mcpsIndication->Buffer[0]; |
mluis | 3:9c6f7f082151 | 794 | switch( ComplianceTest.State ) |
mluis | 3:9c6f7f082151 | 795 | { |
mluis | 3:9c6f7f082151 | 796 | case 0: // Check compliance test disable command (ii) |
mluis | 3:9c6f7f082151 | 797 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 3:9c6f7f082151 | 798 | AppPort = LORAWAN_APP_PORT; |
mluis | 3:9c6f7f082151 | 799 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 3:9c6f7f082151 | 800 | ComplianceTest.DownLinkCounter = 0; |
mluis | 3:9c6f7f082151 | 801 | ComplianceTest.Running = false; |
mluis | 9:ee9dcbb9708d | 802 | |
mluis | 3:9c6f7f082151 | 803 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 804 | mibReq.Type = MIB_ADR; |
mluis | 3:9c6f7f082151 | 805 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 3:9c6f7f082151 | 806 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 807 | #if defined( USE_BAND_868 ) |
mluis | 3:9c6f7f082151 | 808 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 3:9c6f7f082151 | 809 | #endif |
mluis | 3:9c6f7f082151 | 810 | break; |
mluis | 3:9c6f7f082151 | 811 | case 1: // (iii, iv) |
mluis | 3:9c6f7f082151 | 812 | AppDataSize = 2; |
mluis | 3:9c6f7f082151 | 813 | break; |
mluis | 3:9c6f7f082151 | 814 | case 2: // Enable confirmed messages (v) |
mluis | 3:9c6f7f082151 | 815 | IsTxConfirmed = true; |
mluis | 3:9c6f7f082151 | 816 | ComplianceTest.State = 1; |
mluis | 3:9c6f7f082151 | 817 | break; |
mluis | 3:9c6f7f082151 | 818 | case 3: // Disable confirmed messages (vi) |
mluis | 3:9c6f7f082151 | 819 | IsTxConfirmed = false; |
mluis | 3:9c6f7f082151 | 820 | ComplianceTest.State = 1; |
mluis | 3:9c6f7f082151 | 821 | break; |
mluis | 3:9c6f7f082151 | 822 | case 4: // (vii) |
mluis | 5:1e9f6a365854 | 823 | AppDataSize = mcpsIndication->BufferSize; |
mluis | 3:9c6f7f082151 | 824 | |
mluis | 3:9c6f7f082151 | 825 | AppData[0] = 4; |
mluis | 3:9c6f7f082151 | 826 | for( uint8_t i = 1; i < AppDataSize; i++ ) |
mluis | 3:9c6f7f082151 | 827 | { |
mluis | 5:1e9f6a365854 | 828 | AppData[i] = mcpsIndication->Buffer[i] + 1; |
mluis | 3:9c6f7f082151 | 829 | } |
mluis | 3:9c6f7f082151 | 830 | break; |
mluis | 3:9c6f7f082151 | 831 | case 5: // (viii) |
mluis | 3:9c6f7f082151 | 832 | { |
mluis | 3:9c6f7f082151 | 833 | MlmeReq_t mlmeReq; |
mluis | 3:9c6f7f082151 | 834 | mlmeReq.Type = MLME_LINK_CHECK; |
mluis | 3:9c6f7f082151 | 835 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 3:9c6f7f082151 | 836 | } |
mluis | 3:9c6f7f082151 | 837 | break; |
mluis | 7:3173f0508a98 | 838 | case 6: // (ix) |
mluis | 7:3173f0508a98 | 839 | { |
mluis | 7:3173f0508a98 | 840 | MlmeReq_t mlmeReq; |
mluis | 7:3173f0508a98 | 841 | |
mluis | 9:ee9dcbb9708d | 842 | // Disable TestMode and revert back to normal operation |
mluis | 9:ee9dcbb9708d | 843 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 9:ee9dcbb9708d | 844 | AppPort = LORAWAN_APP_PORT; |
mluis | 9:ee9dcbb9708d | 845 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 9:ee9dcbb9708d | 846 | ComplianceTest.DownLinkCounter = 0; |
mluis | 9:ee9dcbb9708d | 847 | ComplianceTest.Running = false; |
mluis | 9:ee9dcbb9708d | 848 | |
mluis | 9:ee9dcbb9708d | 849 | MibRequestConfirm_t mibReq; |
mluis | 9:ee9dcbb9708d | 850 | mibReq.Type = MIB_ADR; |
mluis | 9:ee9dcbb9708d | 851 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 9:ee9dcbb9708d | 852 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 9:ee9dcbb9708d | 853 | #if defined( USE_BAND_868 ) |
mluis | 9:ee9dcbb9708d | 854 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 9:ee9dcbb9708d | 855 | #endif |
mluis | 9:ee9dcbb9708d | 856 | |
mluis | 7:3173f0508a98 | 857 | mlmeReq.Type = MLME_JOIN; |
mluis | 7:3173f0508a98 | 858 | |
mluis | 7:3173f0508a98 | 859 | mlmeReq.Req.Join.DevEui = DevEui; |
mluis | 7:3173f0508a98 | 860 | mlmeReq.Req.Join.AppEui = AppEui; |
mluis | 7:3173f0508a98 | 861 | mlmeReq.Req.Join.AppKey = AppKey; |
mluis | 9:ee9dcbb9708d | 862 | mlmeReq.Req.Join.NbTrials = 3; |
mluis | 7:3173f0508a98 | 863 | |
mluis | 7:3173f0508a98 | 864 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 7:3173f0508a98 | 865 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 7:3173f0508a98 | 866 | } |
mluis | 7:3173f0508a98 | 867 | break; |
mluis | 9:ee9dcbb9708d | 868 | case 7: // (x) |
mluis | 9:ee9dcbb9708d | 869 | { |
mluis | 9:ee9dcbb9708d | 870 | if( mcpsIndication->BufferSize == 3 ) |
mluis | 9:ee9dcbb9708d | 871 | { |
mluis | 9:ee9dcbb9708d | 872 | MlmeReq_t mlmeReq; |
mluis | 9:ee9dcbb9708d | 873 | mlmeReq.Type = MLME_TXCW; |
mluis | 9:ee9dcbb9708d | 874 | mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); |
mluis | 9:ee9dcbb9708d | 875 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 9:ee9dcbb9708d | 876 | } |
mluis | 9:ee9dcbb9708d | 877 | else if( mcpsIndication->BufferSize == 7 ) |
mluis | 9:ee9dcbb9708d | 878 | { |
mluis | 9:ee9dcbb9708d | 879 | MlmeReq_t mlmeReq; |
mluis | 9:ee9dcbb9708d | 880 | mlmeReq.Type = MLME_TXCW_1; |
mluis | 9:ee9dcbb9708d | 881 | mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); |
mluis | 9:ee9dcbb9708d | 882 | mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100; |
mluis | 9:ee9dcbb9708d | 883 | mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6]; |
mluis | 9:ee9dcbb9708d | 884 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 9:ee9dcbb9708d | 885 | } |
mluis | 9:ee9dcbb9708d | 886 | ComplianceTest.State = 1; |
mluis | 9:ee9dcbb9708d | 887 | } |
mluis | 9:ee9dcbb9708d | 888 | break; |
mluis | 3:9c6f7f082151 | 889 | default: |
mluis | 3:9c6f7f082151 | 890 | break; |
mluis | 3:9c6f7f082151 | 891 | } |
mluis | 0:92bca02df485 | 892 | } |
mluis | 3:9c6f7f082151 | 893 | break; |
mluis | 3:9c6f7f082151 | 894 | default: |
mluis | 3:9c6f7f082151 | 895 | break; |
mluis | 0:92bca02df485 | 896 | } |
mluis | 0:92bca02df485 | 897 | } |
mluis | 1:352f608c3337 | 898 | |
mluis | 3:9c6f7f082151 | 899 | // Switch LED 2 ON for each received downlink |
mluis | 3:9c6f7f082151 | 900 | Led2State = true; |
mluis | 3:9c6f7f082151 | 901 | Led2StateChanged = true; |
mluis | 3:9c6f7f082151 | 902 | TimerStart( &Led2Timer ); |
mluis | 3:9c6f7f082151 | 903 | DownlinkStatusUpdated = true; |
mluis | 3:9c6f7f082151 | 904 | } |
mluis | 3:9c6f7f082151 | 905 | |
mluis | 3:9c6f7f082151 | 906 | /*! |
mluis | 3:9c6f7f082151 | 907 | * \brief MLME-Confirm event function |
mluis | 3:9c6f7f082151 | 908 | * |
mluis | 5:1e9f6a365854 | 909 | * \param [IN] mlmeConfirm - Pointer to the confirm structure, |
mluis | 3:9c6f7f082151 | 910 | * containing confirm attributes. |
mluis | 3:9c6f7f082151 | 911 | */ |
mluis | 5:1e9f6a365854 | 912 | static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm ) |
mluis | 3:9c6f7f082151 | 913 | { |
mluis | 9:ee9dcbb9708d | 914 | switch( mlmeConfirm->MlmeRequest ) |
mluis | 3:9c6f7f082151 | 915 | { |
mluis | 9:ee9dcbb9708d | 916 | case MLME_JOIN: |
mluis | 3:9c6f7f082151 | 917 | { |
mluis | 9:ee9dcbb9708d | 918 | if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 919 | { |
mluis | 3:9c6f7f082151 | 920 | // Status is OK, node has joined the network |
mluis | 3:9c6f7f082151 | 921 | IsNetworkJoinedStatusUpdate = true; |
mluis | 7:3173f0508a98 | 922 | DeviceState = DEVICE_STATE_SEND; |
uss1994 | 10:9a4efdd07a77 | 923 | |
uss1994 | 10:9a4efdd07a77 | 924 | |
uss1994 | 10:9a4efdd07a77 | 925 | MessageType = MESSAGE_TYPE_HB; |
uss1994 | 10:9a4efdd07a77 | 926 | joining = false; |
uss1994 | 10:9a4efdd07a77 | 927 | next_stest = current_time + 5; |
uss1994 | 10:9a4efdd07a77 | 928 | next_ltest = current_time + long_interval; |
mluis | 9:ee9dcbb9708d | 929 | } |
mluis | 9:ee9dcbb9708d | 930 | else |
mluis | 9:ee9dcbb9708d | 931 | { |
mluis | 9:ee9dcbb9708d | 932 | // Join was not successful. Try to join again |
mluis | 9:ee9dcbb9708d | 933 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 3:9c6f7f082151 | 934 | } |
mluis | 9:ee9dcbb9708d | 935 | break; |
mluis | 9:ee9dcbb9708d | 936 | } |
mluis | 9:ee9dcbb9708d | 937 | case MLME_LINK_CHECK: |
mluis | 9:ee9dcbb9708d | 938 | { |
mluis | 9:ee9dcbb9708d | 939 | if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 940 | { |
mluis | 3:9c6f7f082151 | 941 | // Check DemodMargin |
mluis | 3:9c6f7f082151 | 942 | // Check NbGateways |
mluis | 3:9c6f7f082151 | 943 | if( ComplianceTest.Running == true ) |
mluis | 3:9c6f7f082151 | 944 | { |
mluis | 3:9c6f7f082151 | 945 | ComplianceTest.LinkCheck = true; |
mluis | 5:1e9f6a365854 | 946 | ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin; |
mluis | 5:1e9f6a365854 | 947 | ComplianceTest.NbGateways = mlmeConfirm->NbGateways; |
mluis | 3:9c6f7f082151 | 948 | } |
mluis | 3:9c6f7f082151 | 949 | } |
mluis | 9:ee9dcbb9708d | 950 | break; |
mluis | 3:9c6f7f082151 | 951 | } |
mluis | 9:ee9dcbb9708d | 952 | default: |
mluis | 9:ee9dcbb9708d | 953 | break; |
mluis | 3:9c6f7f082151 | 954 | } |
mluis | 3:9c6f7f082151 | 955 | NextTx = true; |
mluis | 3:9c6f7f082151 | 956 | UplinkStatusUpdated = true; |
mluis | 0:92bca02df485 | 957 | } |
mluis | 0:92bca02df485 | 958 | |
uss1994 | 10:9a4efdd07a77 | 959 | |
uss1994 | 10:9a4efdd07a77 | 960 | // **************************** TEST METHODS ******************************** // |
uss1994 | 10:9a4efdd07a77 | 961 | |
uss1994 | 10:9a4efdd07a77 | 962 | void startTest(uint8_t *measurements) { |
uss1994 | 10:9a4efdd07a77 | 963 | |
uss1994 | 16:e1d692816864 | 964 | // Determine test length |
uss1994 | 10:9a4efdd07a77 | 965 | switch (MessageType) |
uss1994 | 10:9a4efdd07a77 | 966 | { |
uss1994 | 10:9a4efdd07a77 | 967 | case MESSAGE_TYPE_SHORT_TEST: |
uss1994 | 10:9a4efdd07a77 | 968 | test_duration = 30; |
uss1994 | 10:9a4efdd07a77 | 969 | break; |
uss1994 | 10:9a4efdd07a77 | 970 | case MESSAGE_TYPE_LONG_TEST: |
uss1994 | 10:9a4efdd07a77 | 971 | test_duration = 5400; |
uss1994 | 10:9a4efdd07a77 | 972 | break; |
uss1994 | 10:9a4efdd07a77 | 973 | default: |
uss1994 | 10:9a4efdd07a77 | 974 | test_duration = 0; |
uss1994 | 10:9a4efdd07a77 | 975 | break; |
uss1994 | 10:9a4efdd07a77 | 976 | } |
uss1994 | 10:9a4efdd07a77 | 977 | |
uss1994 | 10:9a4efdd07a77 | 978 | // Start test |
uss1994 | 10:9a4efdd07a77 | 979 | relayPin = 1; |
uss1994 | 12:c1cf0e717684 | 980 | AppLedStateOn = 1; |
uss1994 | 12:c1cf0e717684 | 981 | Led3StateChanged = true; |
amirchaudhary | 30:c186bf174348 | 982 | |
amirchaudhary | 30:c186bf174348 | 983 | // Measure voltage preval |
amirchaudhary | 30:c186bf174348 | 984 | float battery_reading = BAT_PIN.read()*1000; |
amirchaudhary | 30:c186bf174348 | 985 | measurements[0] = (uint8_t) rintf(battery_reading/10); |
uss1994 | 10:9a4efdd07a77 | 986 | } |
uss1994 | 10:9a4efdd07a77 | 987 | |
uss1994 | 10:9a4efdd07a77 | 988 | void endTest(uint8_t *measurements) { |
uss1994 | 10:9a4efdd07a77 | 989 | // Measure endvals |
uss1994 | 20:e75b3340d1c7 | 990 | float battery_reading = BAT_PIN.read()*1000; |
uss1994 | 10:9a4efdd07a77 | 991 | int vce_reading = VCE_PIN.read()*1000; |
uss1994 | 10:9a4efdd07a77 | 992 | int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading; |
uss1994 | 10:9a4efdd07a77 | 993 | int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading; |
uss1994 | 19:6f88d1cabaca | 994 | int rh_reading = RH_PIN.read()*1000 - vce_reading; |
uss1994 | 10:9a4efdd07a77 | 995 | |
uss1994 | 20:e75b3340d1c7 | 996 | measurements[1] = (uint8_t) rintf(battery_reading/10); |
amirchaudhary | 30:c186bf174348 | 997 | measurements[2] = (uint8_t) (light_1_reading/2); |
amirchaudhary | 30:c186bf174348 | 998 | measurements[3] = (uint8_t) (light_2_reading/2); |
amirchaudhary | 30:c186bf174348 | 999 | measurements[4] = (uint8_t) (rh_reading/2); |
uss1994 | 10:9a4efdd07a77 | 1000 | |
amirchaudhary | 27:f024c8c51c6e | 1001 | // End test // |
uss1994 | 10:9a4efdd07a77 | 1002 | relayPin = 0; |
uss1994 | 12:c1cf0e717684 | 1003 | AppLedStateOn = 0; |
uss1994 | 12:c1cf0e717684 | 1004 | Led3StateChanged = true; |
uss1994 | 10:9a4efdd07a77 | 1005 | } |
uss1994 | 10:9a4efdd07a77 | 1006 | |
uss1994 | 11:e433cbbfd436 | 1007 | void button_options_handler(void) |
uss1994 | 11:e433cbbfd436 | 1008 | { |
uss1994 | 24:416a3fec29ea | 1009 | if (press_count >= 4) { |
uss1994 | 24:416a3fec29ea | 1010 | // Run long test if 4 or more clicks |
uss1994 | 11:e433cbbfd436 | 1011 | MessageType = MESSAGE_TYPE_LONG_TEST; |
uss1994 | 11:e433cbbfd436 | 1012 | |
uss1994 | 11:e433cbbfd436 | 1013 | startTest(measurements); |
uss1994 | 11:e433cbbfd436 | 1014 | |
uss1994 | 11:e433cbbfd436 | 1015 | running_test = true; |
uss1994 | 11:e433cbbfd436 | 1016 | test_start = current_time; |
uss1994 | 11:e433cbbfd436 | 1017 | } |
uss1994 | 24:416a3fec29ea | 1018 | if (press_count == 2 || press_count == 3) { |
uss1994 | 24:416a3fec29ea | 1019 | // Run short test if 2 or 3 clicks |
uss1994 | 11:e433cbbfd436 | 1020 | MessageType = MESSAGE_TYPE_SHORT_TEST; |
uss1994 | 11:e433cbbfd436 | 1021 | |
uss1994 | 11:e433cbbfd436 | 1022 | startTest(measurements); |
uss1994 | 11:e433cbbfd436 | 1023 | |
uss1994 | 11:e433cbbfd436 | 1024 | running_test = true; |
uss1994 | 11:e433cbbfd436 | 1025 | test_start = current_time; |
uss1994 | 11:e433cbbfd436 | 1026 | } |
uss1994 | 11:e433cbbfd436 | 1027 | |
uss1994 | 11:e433cbbfd436 | 1028 | // Reset count |
uss1994 | 11:e433cbbfd436 | 1029 | press_count = 0; |
uss1994 | 11:e433cbbfd436 | 1030 | } |
uss1994 | 11:e433cbbfd436 | 1031 | |
amirchaudhary | 13:831ffc0ea56c | 1032 | /** |
amirchaudhary | 13:831ffc0ea56c | 1033 | * Main application entry point. |
amirchaudhary | 13:831ffc0ea56c | 1034 | */ |
amirchaudhary | 13:831ffc0ea56c | 1035 | Serial pc(SERIAL_TX, SERIAL_RX,115200); |
amirchaudhary | 13:831ffc0ea56c | 1036 | int MY_SetSysClock_PLL_HSE(void) |
amirchaudhary | 13:831ffc0ea56c | 1037 | { |
amirchaudhary | 13:831ffc0ea56c | 1038 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
amirchaudhary | 13:831ffc0ea56c | 1039 | RCC_OscInitTypeDef RCC_OscInitStruct; |
amirchaudhary | 13:831ffc0ea56c | 1040 | |
amirchaudhary | 30:c186bf174348 | 1041 | /* configure RTC clock for HSE */ |
amirchaudhary | 30:c186bf174348 | 1042 | SET_BIT(PWR->CR, PWR_CR_DBP); // enable RTC register access as they are in backup domain |
amirchaudhary | 30:c186bf174348 | 1043 | __HAL_RCC_BACKUPRESET_FORCE(); // force reset RTC subsystem |
amirchaudhary | 30:c186bf174348 | 1044 | __HAL_RCC_BACKUPRESET_RELEASE(); |
amirchaudhary | 30:c186bf174348 | 1045 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_HSE_DIV8); // HSE=8MHZ and RTC clock input is 1MHz(when using HSE as source) |
amirchaudhary | 30:c186bf174348 | 1046 | |
amirchaudhary | 13:831ffc0ea56c | 1047 | /* Enable HSE and activate PLL with HSE as source */ |
amirchaudhary | 13:831ffc0ea56c | 1048 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
amirchaudhary | 13:831ffc0ea56c | 1049 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */ |
amirchaudhary | 13:831ffc0ea56c | 1050 | |
amirchaudhary | 13:831ffc0ea56c | 1051 | // PLLCLK = (8 MHz * 8)/2 = 32 MHz |
amirchaudhary | 13:831ffc0ea56c | 1052 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
amirchaudhary | 13:831ffc0ea56c | 1053 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
amirchaudhary | 13:831ffc0ea56c | 1054 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8; |
amirchaudhary | 13:831ffc0ea56c | 1055 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2; |
amirchaudhary | 13:831ffc0ea56c | 1056 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
amirchaudhary | 13:831ffc0ea56c | 1057 | return (-1); // FAIL |
amirchaudhary | 13:831ffc0ea56c | 1058 | } |
amirchaudhary | 13:831ffc0ea56c | 1059 | |
amirchaudhary | 13:831ffc0ea56c | 1060 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
amirchaudhary | 13:831ffc0ea56c | 1061 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
amirchaudhary | 13:831ffc0ea56c | 1062 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz |
amirchaudhary | 13:831ffc0ea56c | 1063 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz |
amirchaudhary | 13:831ffc0ea56c | 1064 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
amirchaudhary | 13:831ffc0ea56c | 1065 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
amirchaudhary | 13:831ffc0ea56c | 1066 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
amirchaudhary | 13:831ffc0ea56c | 1067 | return (-2); // FAIL |
amirchaudhary | 13:831ffc0ea56c | 1068 | } |
amirchaudhary | 13:831ffc0ea56c | 1069 | |
amirchaudhary | 13:831ffc0ea56c | 1070 | /* Enable HSE and activate PLL with HSE as source */ |
amirchaudhary | 13:831ffc0ea56c | 1071 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI; |
amirchaudhary | 13:831ffc0ea56c | 1072 | RCC_OscInitStruct.HSIState = RCC_HSI_OFF; |
amirchaudhary | 13:831ffc0ea56c | 1073 | RCC_OscInitStruct.MSIState = RCC_MSI_OFF; |
amirchaudhary | 13:831ffc0ea56c | 1074 | RCC_OscInitStruct.HSI48State = RCC_HSI48_OFF; |
amirchaudhary | 13:831ffc0ea56c | 1075 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
amirchaudhary | 13:831ffc0ea56c | 1076 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
amirchaudhary | 13:831ffc0ea56c | 1077 | return (-3); // FAIL |
amirchaudhary | 13:831ffc0ea56c | 1078 | } |
amirchaudhary | 30:c186bf174348 | 1079 | |
amirchaudhary | 30:c186bf174348 | 1080 | // enable rtc hardware |
amirchaudhary | 30:c186bf174348 | 1081 | __HAL_RCC_RTC_ENABLE(); |
amirchaudhary | 30:c186bf174348 | 1082 | wait(1.0); |
amirchaudhary | 30:c186bf174348 | 1083 | |
amirchaudhary | 13:831ffc0ea56c | 1084 | return 0; // OK |
amirchaudhary | 13:831ffc0ea56c | 1085 | } |
amirchaudhary | 13:831ffc0ea56c | 1086 | |
amirchaudhary | 13:831ffc0ea56c | 1087 | void my_patch(void) |
amirchaudhary | 13:831ffc0ea56c | 1088 | { |
amirchaudhary | 13:831ffc0ea56c | 1089 | int retVal; |
amirchaudhary | 13:831ffc0ea56c | 1090 | |
amirchaudhary | 13:831ffc0ea56c | 1091 | // Put device into default clock, i.e using MSI = 2MHz |
amirchaudhary | 13:831ffc0ea56c | 1092 | HAL_RCC_DeInit(); |
amirchaudhary | 13:831ffc0ea56c | 1093 | |
amirchaudhary | 13:831ffc0ea56c | 1094 | // Enable HSE clock |
amirchaudhary | 13:831ffc0ea56c | 1095 | retVal = MY_SetSysClock_PLL_HSE(); |
amirchaudhary | 13:831ffc0ea56c | 1096 | if(retVal< 0) |
amirchaudhary | 13:831ffc0ea56c | 1097 | { |
amirchaudhary | 13:831ffc0ea56c | 1098 | // fail |
amirchaudhary | 13:831ffc0ea56c | 1099 | //pc.printf("Failed to start HSE, ERR= %d\r\n", retVal); |
amirchaudhary | 13:831ffc0ea56c | 1100 | |
amirchaudhary | 13:831ffc0ea56c | 1101 | // indicate error |
amirchaudhary | 13:831ffc0ea56c | 1102 | while(1) |
amirchaudhary | 13:831ffc0ea56c | 1103 | { |
amirchaudhary | 13:831ffc0ea56c | 1104 | myled = 1; |
amirchaudhary | 13:831ffc0ea56c | 1105 | wait(0.2); |
amirchaudhary | 13:831ffc0ea56c | 1106 | myled = 0; |
amirchaudhary | 13:831ffc0ea56c | 1107 | wait(0.5); |
amirchaudhary | 13:831ffc0ea56c | 1108 | } |
amirchaudhary | 13:831ffc0ea56c | 1109 | } |
amirchaudhary | 13:831ffc0ea56c | 1110 | } |
amirchaudhary | 13:831ffc0ea56c | 1111 | |
mluis | 0:92bca02df485 | 1112 | int main( void ) |
mluis | 0:92bca02df485 | 1113 | { |
amirchaudhary | 30:c186bf174348 | 1114 | pc.printf("BUILD: %s mbed-os-rev: %d.%d.%d lib-rev: %d\r\n", \ |
amirchaudhary | 30:c186bf174348 | 1115 | __TIME__, MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION); |
amirchaudhary | 30:c186bf174348 | 1116 | pc.printf("OLD: SysClock= %d RCC= %08X CSR= %08X CIER= %08X\r\n", SystemCoreClock, RCC->CR, RCC->CSR, RCC->CIER); |
uss1994 | 17:23caa53e6ec1 | 1117 | my_patch(); |
amirchaudhary | 30:c186bf174348 | 1118 | pc.printf("\r\nNEW: SysClock= %d RCC= %08X CSR= %08X CIER= %08X\r\n", SystemCoreClock, RCC->CR, RCC->CSR, RCC->CIER); |
amirchaudhary | 30:c186bf174348 | 1119 | |
amirchaudhary | 13:831ffc0ea56c | 1120 | wait(3); |
mluis | 3:9c6f7f082151 | 1121 | LoRaMacPrimitives_t LoRaMacPrimitives; |
mluis | 3:9c6f7f082151 | 1122 | LoRaMacCallback_t LoRaMacCallbacks; |
mluis | 3:9c6f7f082151 | 1123 | MibRequestConfirm_t mibReq; |
mluis | 0:92bca02df485 | 1124 | |
mluis | 0:92bca02df485 | 1125 | BoardInit( ); |
mluis | 3:9c6f7f082151 | 1126 | SerialDisplayInit( ); |
mluis | 0:92bca02df485 | 1127 | |
mluis | 7:3173f0508a98 | 1128 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 7:3173f0508a98 | 1129 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 7:3173f0508a98 | 1130 | SerialDisplayUpdateKey( 7, AppKey ); |
mluis | 7:3173f0508a98 | 1131 | |
mluis | 7:3173f0508a98 | 1132 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 7:3173f0508a98 | 1133 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 7:3173f0508a98 | 1134 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 7:3173f0508a98 | 1135 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 7:3173f0508a98 | 1136 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 7:3173f0508a98 | 1137 | #endif |
mluis | 7:3173f0508a98 | 1138 | |
mluis | 3:9c6f7f082151 | 1139 | DeviceState = DEVICE_STATE_INIT; |
uss1994 | 10:9a4efdd07a77 | 1140 | set_time(1514764800); |
uss1994 | 10:9a4efdd07a77 | 1141 | relayPin = 0; |
uss1994 | 10:9a4efdd07a77 | 1142 | |
uss1994 | 10:9a4efdd07a77 | 1143 | running_test = false; |
uss1994 | 10:9a4efdd07a77 | 1144 | joining = true; |
uss1994 | 16:e1d692816864 | 1145 | //received_downlink = false; |
uss1994 | 10:9a4efdd07a77 | 1146 | time_t start_time = time(NULL); |
uss1994 | 10:9a4efdd07a77 | 1147 | |
uss1994 | 10:9a4efdd07a77 | 1148 | last_hb = start_time; |
uss1994 | 11:e433cbbfd436 | 1149 | |
uss1994 | 11:e433cbbfd436 | 1150 | //button1.mode(PullUp); // Activate pull-up |
uss1994 | 11:e433cbbfd436 | 1151 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
uss1994 | 12:c1cf0e717684 | 1152 | AppLedStateOn = 0; |
uss1994 | 12:c1cf0e717684 | 1153 | Led3StateChanged = true; |
amirchaudhary | 27:f024c8c51c6e | 1154 | |
amirchaudhary | 27:f024c8c51c6e | 1155 | myGreenLed = 0; // INITIAL OFF |
amirchaudhary | 27:f024c8c51c6e | 1156 | GreenLedState = GREEN_LED_STATE_INIT; |
amirchaudhary | 27:f024c8c51c6e | 1157 | TimerInit( &GreenLedTimer, OnGreenLedTimerEvent ); |
amirchaudhary | 27:f024c8c51c6e | 1158 | TimerSetValue( &GreenLedTimer, 100 ); |
amirchaudhary | 27:f024c8c51c6e | 1159 | TimerStart( &GreenLedTimer ); |
amirchaudhary | 27:f024c8c51c6e | 1160 | |
amirchaudhary | 27:f024c8c51c6e | 1161 | TimerInit( &BatteryCheckTimer, OnBatteryCheckTimerEvent ); |
amirchaudhary | 27:f024c8c51c6e | 1162 | TimerSetValue( &BatteryCheckTimer, 100 ); // 100msec |
amirchaudhary | 27:f024c8c51c6e | 1163 | TimerStart( &BatteryCheckTimer ); |
mluis | 0:92bca02df485 | 1164 | |
mluis | 0:92bca02df485 | 1165 | while( 1 ) |
mluis | 0:92bca02df485 | 1166 | { |
uss1994 | 10:9a4efdd07a77 | 1167 | current_time = time(NULL); |
mluis | 0:92bca02df485 | 1168 | SerialRxProcess( ); |
mluis | 0:92bca02df485 | 1169 | if( IsNetworkJoinedStatusUpdate == true ) |
mluis | 0:92bca02df485 | 1170 | { |
mluis | 0:92bca02df485 | 1171 | IsNetworkJoinedStatusUpdate = false; |
mluis | 3:9c6f7f082151 | 1172 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 1173 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1174 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:92bca02df485 | 1175 | } |
mluis | 0:92bca02df485 | 1176 | if( Led1StateChanged == true ) |
mluis | 0:92bca02df485 | 1177 | { |
mluis | 0:92bca02df485 | 1178 | Led1StateChanged = false; |
mluis | 1:352f608c3337 | 1179 | SerialDisplayUpdateLedState( 1, Led1State ); |
mluis | 0:92bca02df485 | 1180 | } |
mluis | 0:92bca02df485 | 1181 | if( Led2StateChanged == true ) |
mluis | 0:92bca02df485 | 1182 | { |
mluis | 0:92bca02df485 | 1183 | Led2StateChanged = false; |
mluis | 1:352f608c3337 | 1184 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:92bca02df485 | 1185 | } |
mluis | 0:92bca02df485 | 1186 | if( Led3StateChanged == true ) |
mluis | 0:92bca02df485 | 1187 | { |
mluis | 0:92bca02df485 | 1188 | Led3StateChanged = false; |
mluis | 0:92bca02df485 | 1189 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:92bca02df485 | 1190 | } |
mluis | 3:9c6f7f082151 | 1191 | if( UplinkStatusUpdated == true ) |
mluis | 0:92bca02df485 | 1192 | { |
mluis | 3:9c6f7f082151 | 1193 | UplinkStatusUpdated = false; |
mluis | 3:9c6f7f082151 | 1194 | SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize ); |
mluis | 3:9c6f7f082151 | 1195 | } |
mluis | 3:9c6f7f082151 | 1196 | if( DownlinkStatusUpdated == true ) |
mluis | 3:9c6f7f082151 | 1197 | { |
mluis | 3:9c6f7f082151 | 1198 | DownlinkStatusUpdated = false; |
mluis | 1:352f608c3337 | 1199 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:92bca02df485 | 1200 | SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize ); |
mluis | 0:92bca02df485 | 1201 | } |
mluis | 3:9c6f7f082151 | 1202 | |
amirchaudhary | 27:f024c8c51c6e | 1203 | if( BatteryCheckFlag == true) |
amirchaudhary | 27:f024c8c51c6e | 1204 | { |
amirchaudhary | 27:f024c8c51c6e | 1205 | BatteryCheckFlag = false; |
amirchaudhary | 27:f024c8c51c6e | 1206 | |
amirchaudhary | 27:f024c8c51c6e | 1207 | // Measure voltage preval |
amirchaudhary | 27:f024c8c51c6e | 1208 | float battery_reading = BAT_PIN.read()*3300*2; |
amirchaudhary | 27:f024c8c51c6e | 1209 | // pc.printf("VBAT= %d", (int)battery_reading); |
amirchaudhary | 27:f024c8c51c6e | 1210 | |
amirchaudhary | 28:eca152c69c9b | 1211 | // Setting min value 2.0V so if relay button is pressed without battery Green LED will be OFF. |
amirchaudhary | 29:ca6caa47ef38 | 1212 | // Set max value 4.45V as this voltage is seen at the battery pin. |
amirchaudhary | 28:eca152c69c9b | 1213 | if((battery_reading < 2000)||(battery_reading > 4450)) |
amirchaudhary | 27:f024c8c51c6e | 1214 | { |
amirchaudhary | 27:f024c8c51c6e | 1215 | BatteryLowFlag = true; |
amirchaudhary | 27:f024c8c51c6e | 1216 | } |
amirchaudhary | 27:f024c8c51c6e | 1217 | else |
amirchaudhary | 27:f024c8c51c6e | 1218 | { |
amirchaudhary | 27:f024c8c51c6e | 1219 | BatteryLowFlag = false; |
amirchaudhary | 27:f024c8c51c6e | 1220 | } |
amirchaudhary | 27:f024c8c51c6e | 1221 | } |
amirchaudhary | 27:f024c8c51c6e | 1222 | |
mluis | 3:9c6f7f082151 | 1223 | switch( DeviceState ) |
mluis | 0:92bca02df485 | 1224 | { |
mluis | 3:9c6f7f082151 | 1225 | case DEVICE_STATE_INIT: |
mluis | 3:9c6f7f082151 | 1226 | { |
mluis | 3:9c6f7f082151 | 1227 | LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; |
mluis | 3:9c6f7f082151 | 1228 | LoRaMacPrimitives.MacMcpsIndication = McpsIndication; |
mluis | 3:9c6f7f082151 | 1229 | LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; |
mluis | 3:9c6f7f082151 | 1230 | LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; |
mluis | 3:9c6f7f082151 | 1231 | LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
mluis | 3:9c6f7f082151 | 1232 | |
mluis | 3:9c6f7f082151 | 1233 | TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); |
mluis | 3:9c6f7f082151 | 1234 | |
mluis | 3:9c6f7f082151 | 1235 | TimerInit( &Led1Timer, OnLed1TimerEvent ); |
mluis | 9:ee9dcbb9708d | 1236 | TimerSetValue( &Led1Timer, 25 ); |
mluis | 3:9c6f7f082151 | 1237 | |
mluis | 3:9c6f7f082151 | 1238 | TimerInit( &Led2Timer, OnLed2TimerEvent ); |
mluis | 9:ee9dcbb9708d | 1239 | TimerSetValue( &Led2Timer, 25 ); |
mluis | 3:9c6f7f082151 | 1240 | |
mluis | 3:9c6f7f082151 | 1241 | mibReq.Type = MIB_ADR; |
mluis | 3:9c6f7f082151 | 1242 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 3:9c6f7f082151 | 1243 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1244 | |
mluis | 3:9c6f7f082151 | 1245 | mibReq.Type = MIB_PUBLIC_NETWORK; |
mluis | 3:9c6f7f082151 | 1246 | mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
mluis | 3:9c6f7f082151 | 1247 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1248 | |
amirchaudhary | 30:c186bf174348 | 1249 | // Limiting to just 2nd subband of 8 channels |
amirchaudhary | 30:c186bf174348 | 1250 | static uint16_t GatewayChannelsMask[] = {0xFF00, 0x0000, 0x0000, 0x0000, 0x0002, 0x0000}; |
amirchaudhary | 30:c186bf174348 | 1251 | mibReq.Type = MIB_CHANNELS_DEFAULT_MASK; |
amirchaudhary | 30:c186bf174348 | 1252 | mibReq.Param.ChannelsDefaultMask = GatewayChannelsMask; |
amirchaudhary | 30:c186bf174348 | 1253 | LoRaMacMibSetRequestConfirm( &mibReq ); |
amirchaudhary | 30:c186bf174348 | 1254 | |
amirchaudhary | 30:c186bf174348 | 1255 | mibReq.Type = MIB_CHANNELS_MASK; |
amirchaudhary | 30:c186bf174348 | 1256 | mibReq.Param.ChannelsMask = GatewayChannelsMask; |
amirchaudhary | 30:c186bf174348 | 1257 | LoRaMacMibSetRequestConfirm( &mibReq ); |
amirchaudhary | 30:c186bf174348 | 1258 | |
amirchaudhary | 30:c186bf174348 | 1259 | |
mluis | 3:9c6f7f082151 | 1260 | #if defined( USE_BAND_868 ) |
mluis | 3:9c6f7f082151 | 1261 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 3:9c6f7f082151 | 1262 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 5:1e9f6a365854 | 1263 | |
mluis | 9:ee9dcbb9708d | 1264 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 5:1e9f6a365854 | 1265 | LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); |
mluis | 5:1e9f6a365854 | 1266 | LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); |
mluis | 5:1e9f6a365854 | 1267 | LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); |
mluis | 5:1e9f6a365854 | 1268 | LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); |
mluis | 5:1e9f6a365854 | 1269 | LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); |
mluis | 5:1e9f6a365854 | 1270 | LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); |
mluis | 5:1e9f6a365854 | 1271 | LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 ); |
mluis | 7:3173f0508a98 | 1272 | |
mluis | 9:ee9dcbb9708d | 1273 | mibReq.Type = MIB_RX2_DEFAULT_CHANNEL; |
mluis | 9:ee9dcbb9708d | 1274 | mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; |
mluis | 9:ee9dcbb9708d | 1275 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 9:ee9dcbb9708d | 1276 | |
mluis | 7:3173f0508a98 | 1277 | mibReq.Type = MIB_RX2_CHANNEL; |
mluis | 7:3173f0508a98 | 1278 | mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; |
mluis | 7:3173f0508a98 | 1279 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 5:1e9f6a365854 | 1280 | #endif |
mluis | 5:1e9f6a365854 | 1281 | |
mluis | 3:9c6f7f082151 | 1282 | #endif |
mluis | 3:9c6f7f082151 | 1283 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 3:9c6f7f082151 | 1284 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 3:9c6f7f082151 | 1285 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
mluis | 3:9c6f7f082151 | 1286 | |
mluis | 3:9c6f7f082151 | 1287 | LoRaMacDownlinkStatus.DownlinkCounter = 0; |
mluis | 3:9c6f7f082151 | 1288 | |
mluis | 3:9c6f7f082151 | 1289 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 3:9c6f7f082151 | 1290 | break; |
mluis | 3:9c6f7f082151 | 1291 | } |
mluis | 3:9c6f7f082151 | 1292 | case DEVICE_STATE_JOIN: |
mluis | 3:9c6f7f082151 | 1293 | { |
mluis | 3:9c6f7f082151 | 1294 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
mluis | 3:9c6f7f082151 | 1295 | MlmeReq_t mlmeReq; |
mluis | 3:9c6f7f082151 | 1296 | |
mluis | 3:9c6f7f082151 | 1297 | mlmeReq.Type = MLME_JOIN; |
mluis | 3:9c6f7f082151 | 1298 | |
mluis | 3:9c6f7f082151 | 1299 | mlmeReq.Req.Join.DevEui = DevEui; |
mluis | 3:9c6f7f082151 | 1300 | mlmeReq.Req.Join.AppEui = AppEui; |
mluis | 3:9c6f7f082151 | 1301 | mlmeReq.Req.Join.AppKey = AppKey; |
mluis | 3:9c6f7f082151 | 1302 | |
mluis | 3:9c6f7f082151 | 1303 | if( NextTx == true ) |
mluis | 3:9c6f7f082151 | 1304 | { |
mluis | 3:9c6f7f082151 | 1305 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 3:9c6f7f082151 | 1306 | } |
mluis | 7:3173f0508a98 | 1307 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 3:9c6f7f082151 | 1308 | #else |
mluis | 3:9c6f7f082151 | 1309 | mibReq.Type = MIB_NET_ID; |
mluis | 3:9c6f7f082151 | 1310 | mibReq.Param.NetID = LORAWAN_NETWORK_ID; |
mluis | 3:9c6f7f082151 | 1311 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1312 | |
mluis | 3:9c6f7f082151 | 1313 | mibReq.Type = MIB_DEV_ADDR; |
mluis | 3:9c6f7f082151 | 1314 | mibReq.Param.DevAddr = DevAddr; |
mluis | 3:9c6f7f082151 | 1315 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1316 | |
mluis | 3:9c6f7f082151 | 1317 | mibReq.Type = MIB_NWK_SKEY; |
mluis | 3:9c6f7f082151 | 1318 | mibReq.Param.NwkSKey = NwkSKey; |
mluis | 3:9c6f7f082151 | 1319 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1320 | |
mluis | 3:9c6f7f082151 | 1321 | mibReq.Type = MIB_APP_SKEY; |
mluis | 3:9c6f7f082151 | 1322 | mibReq.Param.AppSKey = AppSKey; |
mluis | 3:9c6f7f082151 | 1323 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1324 | |
mluis | 3:9c6f7f082151 | 1325 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 1326 | mibReq.Param.IsNetworkJoined = true; |
mluis | 3:9c6f7f082151 | 1327 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1328 | |
mluis | 3:9c6f7f082151 | 1329 | DeviceState = DEVICE_STATE_SEND; |
mluis | 3:9c6f7f082151 | 1330 | #endif |
mluis | 3:9c6f7f082151 | 1331 | IsNetworkJoinedStatusUpdate = true; |
mluis | 3:9c6f7f082151 | 1332 | break; |
mluis | 3:9c6f7f082151 | 1333 | } |
mluis | 3:9c6f7f082151 | 1334 | case DEVICE_STATE_SEND: |
mluis | 3:9c6f7f082151 | 1335 | { |
mluis | 3:9c6f7f082151 | 1336 | if( NextTx == true ) |
mluis | 3:9c6f7f082151 | 1337 | { |
mluis | 3:9c6f7f082151 | 1338 | SerialDisplayUpdateUplinkAcked( false ); |
mluis | 3:9c6f7f082151 | 1339 | SerialDisplayUpdateDonwlinkRxData( false ); |
mluis | 3:9c6f7f082151 | 1340 | PrepareTxFrame( AppPort ); |
mluis | 3:9c6f7f082151 | 1341 | |
mluis | 3:9c6f7f082151 | 1342 | NextTx = SendFrame( ); |
mluis | 3:9c6f7f082151 | 1343 | } |
mluis | 3:9c6f7f082151 | 1344 | if( ComplianceTest.Running == true ) |
mluis | 3:9c6f7f082151 | 1345 | { |
mluis | 5:1e9f6a365854 | 1346 | // Schedule next packet transmission |
mluis | 9:ee9dcbb9708d | 1347 | TxDutyCycleTime = 5000; // 5000 ms |
mluis | 3:9c6f7f082151 | 1348 | } |
mluis | 3:9c6f7f082151 | 1349 | else |
mluis | 3:9c6f7f082151 | 1350 | { |
mluis | 3:9c6f7f082151 | 1351 | // Schedule next packet transmission |
mluis | 3:9c6f7f082151 | 1352 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
mluis | 3:9c6f7f082151 | 1353 | } |
uss1994 | 18:0a4de80c02a6 | 1354 | |
uss1994 | 18:0a4de80c02a6 | 1355 | if ( NextTx == true ) |
uss1994 | 18:0a4de80c02a6 | 1356 | { |
uss1994 | 18:0a4de80c02a6 | 1357 | DeviceState = DEVICE_STATE_SLEEP; |
uss1994 | 18:0a4de80c02a6 | 1358 | |
uss1994 | 18:0a4de80c02a6 | 1359 | // Schedule next packet transmission |
uss1994 | 18:0a4de80c02a6 | 1360 | TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); |
uss1994 | 18:0a4de80c02a6 | 1361 | TimerStart( &TxNextPacketTimer ); |
uss1994 | 18:0a4de80c02a6 | 1362 | |
uss1994 | 19:6f88d1cabaca | 1363 | // char my_str[17] = "Scheduled Resend"; |
uss1994 | 19:6f88d1cabaca | 1364 | // SerialDisplayPrintDebugLine( my_str, 17 ); |
uss1994 | 18:0a4de80c02a6 | 1365 | } |
uss1994 | 18:0a4de80c02a6 | 1366 | else |
uss1994 | 18:0a4de80c02a6 | 1367 | { |
uss1994 | 18:0a4de80c02a6 | 1368 | DeviceState = DEVICE_STATE_CYCLE; |
uss1994 | 18:0a4de80c02a6 | 1369 | } |
uss1994 | 18:0a4de80c02a6 | 1370 | |
mluis | 3:9c6f7f082151 | 1371 | break; |
mluis | 3:9c6f7f082151 | 1372 | } |
mluis | 3:9c6f7f082151 | 1373 | case DEVICE_STATE_CYCLE: |
mluis | 3:9c6f7f082151 | 1374 | { |
uss1994 | 10:9a4efdd07a77 | 1375 | // DeviceState = DEVICE_STATE_SLEEP; |
mluis | 5:1e9f6a365854 | 1376 | |
mluis | 3:9c6f7f082151 | 1377 | // Schedule next packet transmission |
uss1994 | 10:9a4efdd07a77 | 1378 | // TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); |
uss1994 | 10:9a4efdd07a77 | 1379 | // TimerStart( &TxNextPacketTimer ); |
uss1994 | 10:9a4efdd07a77 | 1380 | |
uss1994 | 10:9a4efdd07a77 | 1381 | |
uss1994 | 10:9a4efdd07a77 | 1382 | |
uss1994 | 10:9a4efdd07a77 | 1383 | // Is a test is running |
uss1994 | 10:9a4efdd07a77 | 1384 | if (running_test) { |
uss1994 | 10:9a4efdd07a77 | 1385 | // If it is |
uss1994 | 10:9a4efdd07a77 | 1386 | |
uss1994 | 10:9a4efdd07a77 | 1387 | //check if it's time to end the test |
uss1994 | 10:9a4efdd07a77 | 1388 | if (current_time - test_start >= test_duration) { |
uss1994 | 10:9a4efdd07a77 | 1389 | endTest(measurements); |
uss1994 | 10:9a4efdd07a77 | 1390 | // sendResult(measurements, ttype); |
uss1994 | 10:9a4efdd07a77 | 1391 | DeviceState = DEVICE_STATE_SEND; |
uss1994 | 10:9a4efdd07a77 | 1392 | last_hb = current_time; |
uss1994 | 10:9a4efdd07a77 | 1393 | |
uss1994 | 10:9a4efdd07a77 | 1394 | // Set the times for the next tests |
uss1994 | 10:9a4efdd07a77 | 1395 | switch ( MessageType ) |
uss1994 | 10:9a4efdd07a77 | 1396 | { |
uss1994 | 10:9a4efdd07a77 | 1397 | case MESSAGE_TYPE_SHORT_TEST: |
uss1994 | 10:9a4efdd07a77 | 1398 | { |
uss1994 | 10:9a4efdd07a77 | 1399 | next_stest = test_start + test_interval; |
uss1994 | 10:9a4efdd07a77 | 1400 | break; |
uss1994 | 10:9a4efdd07a77 | 1401 | } |
uss1994 | 10:9a4efdd07a77 | 1402 | default: |
uss1994 | 10:9a4efdd07a77 | 1403 | { |
uss1994 | 10:9a4efdd07a77 | 1404 | // If long test, reset both test types, |
uss1994 | 10:9a4efdd07a77 | 1405 | // to prevent two tests at the same time |
uss1994 | 10:9a4efdd07a77 | 1406 | next_stest = test_start + test_interval; |
uss1994 | 10:9a4efdd07a77 | 1407 | next_ltest = test_start + long_interval; |
uss1994 | 10:9a4efdd07a77 | 1408 | break; |
uss1994 | 10:9a4efdd07a77 | 1409 | } |
uss1994 | 10:9a4efdd07a77 | 1410 | } |
uss1994 | 10:9a4efdd07a77 | 1411 | |
uss1994 | 10:9a4efdd07a77 | 1412 | running_test = false; |
uss1994 | 10:9a4efdd07a77 | 1413 | } |
uss1994 | 10:9a4efdd07a77 | 1414 | } else { |
uss1994 | 10:9a4efdd07a77 | 1415 | // If it isn't |
uss1994 | 10:9a4efdd07a77 | 1416 | |
uss1994 | 10:9a4efdd07a77 | 1417 | // check if downlink action is required |
uss1994 | 10:9a4efdd07a77 | 1418 | |
uss1994 | 10:9a4efdd07a77 | 1419 | //if (received_downlink == true) { |
uss1994 | 10:9a4efdd07a77 | 1420 | // dl_handler(received_data); |
uss1994 | 10:9a4efdd07a77 | 1421 | //} |
uss1994 | 10:9a4efdd07a77 | 1422 | |
uss1994 | 11:e433cbbfd436 | 1423 | //check if button is pressed |
uss1994 | 11:e433cbbfd436 | 1424 | if (button1_pressed) { // Set when button is pressed |
uss1994 | 11:e433cbbfd436 | 1425 | if (press_count == 0) { |
uss1994 | 11:e433cbbfd436 | 1426 | buttonOptions_timeout.attach(callback(button_options_handler), 2); // Debounce time 300 ms |
uss1994 | 11:e433cbbfd436 | 1427 | } |
uss1994 | 11:e433cbbfd436 | 1428 | button1_pressed = false; |
uss1994 | 11:e433cbbfd436 | 1429 | press_count++; |
uss1994 | 11:e433cbbfd436 | 1430 | } |
uss1994 | 11:e433cbbfd436 | 1431 | |
uss1994 | 10:9a4efdd07a77 | 1432 | //check if it's time to run a test |
uss1994 | 10:9a4efdd07a77 | 1433 | if (current_time >= next_stest || current_time >= next_ltest) { |
uss1994 | 10:9a4efdd07a77 | 1434 | |
uss1994 | 10:9a4efdd07a77 | 1435 | // Check what kind of test to run |
uss1994 | 10:9a4efdd07a77 | 1436 | if (current_time >= next_ltest) { |
uss1994 | 10:9a4efdd07a77 | 1437 | MessageType = MESSAGE_TYPE_LONG_TEST; |
uss1994 | 10:9a4efdd07a77 | 1438 | } else { |
uss1994 | 10:9a4efdd07a77 | 1439 | MessageType = MESSAGE_TYPE_SHORT_TEST; |
uss1994 | 10:9a4efdd07a77 | 1440 | } |
uss1994 | 10:9a4efdd07a77 | 1441 | |
uss1994 | 10:9a4efdd07a77 | 1442 | startTest(measurements); |
uss1994 | 10:9a4efdd07a77 | 1443 | |
uss1994 | 10:9a4efdd07a77 | 1444 | running_test = true; |
uss1994 | 10:9a4efdd07a77 | 1445 | test_start = current_time; |
uss1994 | 10:9a4efdd07a77 | 1446 | |
uss1994 | 10:9a4efdd07a77 | 1447 | |
uss1994 | 10:9a4efdd07a77 | 1448 | //check if it's time for a heartbeat |
uss1994 | 10:9a4efdd07a77 | 1449 | } else if (current_time - last_hb >= hb_interval) { |
uss1994 | 10:9a4efdd07a77 | 1450 | // sendHb(); |
uss1994 | 10:9a4efdd07a77 | 1451 | DeviceState = DEVICE_STATE_SEND; |
uss1994 | 10:9a4efdd07a77 | 1452 | MessageType = MESSAGE_TYPE_HB; |
uss1994 | 10:9a4efdd07a77 | 1453 | last_hb = current_time; |
uss1994 | 10:9a4efdd07a77 | 1454 | } |
uss1994 | 10:9a4efdd07a77 | 1455 | } |
uss1994 | 10:9a4efdd07a77 | 1456 | |
uss1994 | 10:9a4efdd07a77 | 1457 | |
mluis | 3:9c6f7f082151 | 1458 | break; |
mluis | 3:9c6f7f082151 | 1459 | } |
mluis | 3:9c6f7f082151 | 1460 | case DEVICE_STATE_SLEEP: |
mluis | 3:9c6f7f082151 | 1461 | { |
mluis | 3:9c6f7f082151 | 1462 | // Wake up through events |
mluis | 3:9c6f7f082151 | 1463 | break; |
mluis | 3:9c6f7f082151 | 1464 | } |
mluis | 3:9c6f7f082151 | 1465 | default: |
mluis | 3:9c6f7f082151 | 1466 | { |
mluis | 3:9c6f7f082151 | 1467 | DeviceState = DEVICE_STATE_INIT; |
mluis | 3:9c6f7f082151 | 1468 | break; |
mluis | 3:9c6f7f082151 | 1469 | } |
mluis | 0:92bca02df485 | 1470 | } |
mluis | 0:92bca02df485 | 1471 | } |
mluis | 0:92bca02df485 | 1472 | } |