Sensor Demo for CTIA
Dependencies: LoRaWAN-lib SX1272Lib lib_gps lib_mma8451q lib_mpl3115a2 mbed
Fork of LoRaWAN-NAMote72-Application-Demo by
app/LoRaApp.cpp@13:238b23d4025c, 2016-08-19 (annotated)
- Committer:
- ubhat
- Date:
- Fri Aug 19 00:29:34 2016 +0000
- Revision:
- 13:238b23d4025c
- Parent:
- 7:92f4f419f91f
- Child:
- 14:172b8e37168c
CTIA Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ubhat | 0:69f2e28d12c1 | 1 | /* |
ubhat | 0:69f2e28d12c1 | 2 | / _____) _ | | |
ubhat | 0:69f2e28d12c1 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
ubhat | 0:69f2e28d12c1 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
ubhat | 0:69f2e28d12c1 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
ubhat | 0:69f2e28d12c1 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
ubhat | 0:69f2e28d12c1 | 7 | (C)2015 Semtech |
ubhat | 0:69f2e28d12c1 | 8 | |
ubhat | 0:69f2e28d12c1 | 9 | Description: User-defined applications such as GPS, Temp, Accelerometer, LED indications etc. |
ubhat | 0:69f2e28d12c1 | 10 | Event based actions such as LED blink on Tx, LED toggle on downlink etc |
ubhat | 0:69f2e28d12c1 | 11 | |
ubhat | 0:69f2e28d12c1 | 12 | License: Revised BSD License, see LICENSE.TXT file include in the project |
ubhat | 0:69f2e28d12c1 | 13 | |
ubhat | 0:69f2e28d12c1 | 14 | Maintainer: Uttam Bhat |
ubhat | 0:69f2e28d12c1 | 15 | */ |
ubhat | 0:69f2e28d12c1 | 16 | |
ubhat | 0:69f2e28d12c1 | 17 | #include "LoRaApp.h" |
ubhat | 0:69f2e28d12c1 | 18 | |
ubhat | 7:92f4f419f91f | 19 | bool VerticalStatus = false; |
ubhat | 7:92f4f419f91f | 20 | |
ubhat | 0:69f2e28d12c1 | 21 | /*! |
ubhat | 0:69f2e28d12c1 | 22 | * Red LED timer event |
ubhat | 0:69f2e28d12c1 | 23 | */ |
ubhat | 0:69f2e28d12c1 | 24 | TimerLed RedLedTimer( Red ); |
ubhat | 0:69f2e28d12c1 | 25 | |
ubhat | 0:69f2e28d12c1 | 26 | /*! |
ubhat | 0:69f2e28d12c1 | 27 | * Yellow LED timer event |
ubhat | 0:69f2e28d12c1 | 28 | */ |
ubhat | 0:69f2e28d12c1 | 29 | TimerLed YellowLedTimer( Yellow ); |
ubhat | 0:69f2e28d12c1 | 30 | |
ubhat | 0:69f2e28d12c1 | 31 | /*! |
ubhat | 0:69f2e28d12c1 | 32 | * Green LED timer event |
ubhat | 0:69f2e28d12c1 | 33 | */ |
ubhat | 0:69f2e28d12c1 | 34 | TimerLed GreenLedTimer( Green ); |
ubhat | 0:69f2e28d12c1 | 35 | |
ubhat | 0:69f2e28d12c1 | 36 | Application::Application( uint8_t * memptr ) |
ubhat | 0:69f2e28d12c1 | 37 | { |
ubhat | 0:69f2e28d12c1 | 38 | BuffAddr = memptr; |
ubhat | 0:69f2e28d12c1 | 39 | memset( BuffAddr, 0, LORAWAN_APP_DATA_MAX_SIZE ); |
ubhat | 6:f8194e691dd4 | 40 | BuffPtr = 0; |
ubhat | 0:69f2e28d12c1 | 41 | } |
ubhat | 0:69f2e28d12c1 | 42 | |
ubhat | 0:69f2e28d12c1 | 43 | Application::~Application( ) |
ubhat | 0:69f2e28d12c1 | 44 | { |
ubhat | 0:69f2e28d12c1 | 45 | } |
ubhat | 0:69f2e28d12c1 | 46 | |
ubhat | 0:69f2e28d12c1 | 47 | void Application::ApplicationAppendData( uint8_t *pData, uint8_t len ) |
ubhat | 0:69f2e28d12c1 | 48 | { |
ubhat | 0:69f2e28d12c1 | 49 | memcpy( BuffAddr + BuffPtr, pData, len ); |
ubhat | 0:69f2e28d12c1 | 50 | BuffPtr += len; |
ubhat | 0:69f2e28d12c1 | 51 | } |
ubhat | 0:69f2e28d12c1 | 52 | |
ubhat | 0:69f2e28d12c1 | 53 | void Application::ApplicationPtrPos( uint8_t ptrPos ) |
ubhat | 0:69f2e28d12c1 | 54 | { |
ubhat | 0:69f2e28d12c1 | 55 | BuffPtr = ptrPos; |
ubhat | 0:69f2e28d12c1 | 56 | } |
ubhat | 0:69f2e28d12c1 | 57 | |
ubhat | 0:69f2e28d12c1 | 58 | void Application::ApplicationCall( eAppType App ) |
ubhat | 0:69f2e28d12c1 | 59 | { |
ubhat | 0:69f2e28d12c1 | 60 | switch( App ) |
ubhat | 0:69f2e28d12c1 | 61 | { |
ubhat | 0:69f2e28d12c1 | 62 | // Appends 8 Bytes (3 bytes longitude, 3 bytes latitude, 2 bytes altitude) to TX buffer |
ubhat | 0:69f2e28d12c1 | 63 | case AppGps: |
ubhat | 0:69f2e28d12c1 | 64 | { |
ubhat | 0:69f2e28d12c1 | 65 | Gps.service( ); |
ubhat | 0:69f2e28d12c1 | 66 | |
ubhat | 0:69f2e28d12c1 | 67 | uint16_t altitudeGps = atoi( Gps.NmeaGpsData.NmeaAltitude ); |
ubhat | 0:69f2e28d12c1 | 68 | |
ubhat | 0:69f2e28d12c1 | 69 | if( ( BuffPtr + 8 ) <= LORAWAN_APP_DATA_SIZE ) |
ubhat | 0:69f2e28d12c1 | 70 | { |
ubhat | 0:69f2e28d12c1 | 71 | BuffAddr[BuffPtr++] = ( Gps.LatitudeBinary >> 16 ) & 0xFF; |
ubhat | 0:69f2e28d12c1 | 72 | BuffAddr[BuffPtr++] = ( Gps.LatitudeBinary >> 8 ) & 0xFF; |
ubhat | 0:69f2e28d12c1 | 73 | BuffAddr[BuffPtr++] = Gps.LatitudeBinary & 0xFF; |
ubhat | 0:69f2e28d12c1 | 74 | BuffAddr[BuffPtr++] = ( Gps.LongitudeBinary >> 16 ) & 0xFF; |
ubhat | 0:69f2e28d12c1 | 75 | BuffAddr[BuffPtr++] = ( Gps.LongitudeBinary >> 8 ) & 0xFF; |
ubhat | 0:69f2e28d12c1 | 76 | BuffAddr[BuffPtr++] = Gps.LongitudeBinary & 0xFF; |
ubhat | 0:69f2e28d12c1 | 77 | BuffAddr[BuffPtr++] = ( altitudeGps >> 8 ) & 0xFF; |
ubhat | 0:69f2e28d12c1 | 78 | BuffAddr[BuffPtr++] = altitudeGps & 0xFF; |
ubhat | 0:69f2e28d12c1 | 79 | } |
ubhat | 0:69f2e28d12c1 | 80 | break; |
ubhat | 0:69f2e28d12c1 | 81 | } |
ubhat | 0:69f2e28d12c1 | 82 | |
ubhat | 0:69f2e28d12c1 | 83 | // Appends 1 Byte to TX buffer |
ubhat | 13:238b23d4025c | 84 | case AppPrsr: |
ubhat | 13:238b23d4025c | 85 | { |
ubhat | 13:238b23d4025c | 86 | if( ( BuffPtr + 2 ) <= LORAWAN_APP_DATA_SIZE ) |
ubhat | 13:238b23d4025c | 87 | { |
ubhat | 13:238b23d4025c | 88 | volatile uint8_t stat; |
ubhat | 13:238b23d4025c | 89 | |
ubhat | 13:238b23d4025c | 90 | Mpl3115a2.SetModeBarometer(); |
ubhat | 13:238b23d4025c | 91 | Mpl3115a2.ToggleOneShot( ); |
ubhat | 13:238b23d4025c | 92 | |
ubhat | 13:238b23d4025c | 93 | stat = Mpl3115a2.read(STATUS_REG); |
ubhat | 13:238b23d4025c | 94 | while( (stat & 0x04) != 0x04 ) { |
ubhat | 13:238b23d4025c | 95 | wait(0.01); |
ubhat | 13:238b23d4025c | 96 | stat = Mpl3115a2.read(STATUS_REG); |
ubhat | 13:238b23d4025c | 97 | } |
ubhat | 13:238b23d4025c | 98 | |
ubhat | 13:238b23d4025c | 99 | BuffAddr[BuffPtr++] = Mpl3115a2.read(OUT_P_MSB_REG); |
ubhat | 13:238b23d4025c | 100 | BuffAddr[BuffPtr++] = Mpl3115a2.read(OUT_P_CSB_REG); |
ubhat | 13:238b23d4025c | 101 | } |
ubhat | 13:238b23d4025c | 102 | break; |
ubhat | 13:238b23d4025c | 103 | } |
ubhat | 13:238b23d4025c | 104 | |
ubhat | 13:238b23d4025c | 105 | // Appends 1 Byte to TX buffer |
ubhat | 0:69f2e28d12c1 | 106 | case AppTemp: |
ubhat | 0:69f2e28d12c1 | 107 | { |
ubhat | 0:69f2e28d12c1 | 108 | Mpl3115a2.ReadTemperature( ); |
ubhat | 0:69f2e28d12c1 | 109 | if( ( BuffPtr + 1 ) <= LORAWAN_APP_DATA_SIZE ) |
ubhat | 0:69f2e28d12c1 | 110 | { |
ubhat | 13:238b23d4025c | 111 | BuffAddr[BuffPtr++] = ( int32_t )Mpl3115a2.Temperature; // Signed degrees Celcius in half degree units. So, +/-63 °C |
ubhat | 0:69f2e28d12c1 | 112 | } |
ubhat | 0:69f2e28d12c1 | 113 | break; |
ubhat | 13:238b23d4025c | 114 | } |
ubhat | 0:69f2e28d12c1 | 115 | |
ubhat | 0:69f2e28d12c1 | 116 | // Appends 1 Byte to TX buffer |
ubhat | 0:69f2e28d12c1 | 117 | case AppBat: |
ubhat | 0:69f2e28d12c1 | 118 | { |
ubhat | 0:69f2e28d12c1 | 119 | if( ( BuffPtr + 1 ) <= LORAWAN_APP_DATA_SIZE ) |
ubhat | 0:69f2e28d12c1 | 120 | { |
ubhat | 0:69f2e28d12c1 | 121 | BuffAddr[BuffPtr++] = BoardGetBatteryLevel( ); // Per LoRaWAN spec; 0 = Charging; 1...254 = level, 255 = N/A |
ubhat | 0:69f2e28d12c1 | 122 | } |
ubhat | 0:69f2e28d12c1 | 123 | break; |
ubhat | 0:69f2e28d12c1 | 124 | } |
ubhat | 0:69f2e28d12c1 | 125 | |
ubhat | 0:69f2e28d12c1 | 126 | // Appends incremental values of 1 Byte each to TX buffer until Full |
ubhat | 0:69f2e28d12c1 | 127 | case AppRamp: |
ubhat | 0:69f2e28d12c1 | 128 | { |
ubhat | 0:69f2e28d12c1 | 129 | int32_t i, j; |
ubhat | 0:69f2e28d12c1 | 130 | |
ubhat | 0:69f2e28d12c1 | 131 | // Populate Tx Buffer with increasing byte values starting from 0x00, 0x01, 0x02 ... |
ubhat | 0:69f2e28d12c1 | 132 | for( i = BuffPtr, j = 0; i < LORAWAN_APP_DATA_SIZE; i++ ) |
ubhat | 0:69f2e28d12c1 | 133 | { |
ubhat | 0:69f2e28d12c1 | 134 | BuffAddr[i] = j++; |
ubhat | 0:69f2e28d12c1 | 135 | } |
ubhat | 0:69f2e28d12c1 | 136 | BuffPtr = LORAWAN_APP_DATA_SIZE; |
ubhat | 0:69f2e28d12c1 | 137 | break; |
ubhat | 0:69f2e28d12c1 | 138 | } |
ubhat | 0:69f2e28d12c1 | 139 | |
ubhat | 0:69f2e28d12c1 | 140 | // Appends 2 Bytes to TX buffer |
ubhat | 0:69f2e28d12c1 | 141 | case AppAccl: |
ubhat | 0:69f2e28d12c1 | 142 | { |
ubhat | 0:69f2e28d12c1 | 143 | uint8_t statusReg; |
ubhat | 0:69f2e28d12c1 | 144 | |
ubhat | 0:69f2e28d12c1 | 145 | // Read the PS_STATUS register |
ubhat | 0:69f2e28d12c1 | 146 | statusReg = Mma8451q.read_single( MMA8451_PL_STATUS ); |
ubhat | 0:69f2e28d12c1 | 147 | |
ubhat | 0:69f2e28d12c1 | 148 | /* Display Orientation of NAMote on Serial Port */ |
ubhat | 0:69f2e28d12c1 | 149 | SerialAcclMetrDisplay( statusReg ); |
ubhat | 0:69f2e28d12c1 | 150 | |
ubhat | 0:69f2e28d12c1 | 151 | // If Orientation of the Mote changed then let Green LED ON |
ubhat | 0:69f2e28d12c1 | 152 | if( ( statusReg & 0x80 ) != 0 ) |
ubhat | 0:69f2e28d12c1 | 153 | { |
ubhat | 0:69f2e28d12c1 | 154 | AppLed = 1; |
ubhat | 0:69f2e28d12c1 | 155 | CtrlLED( Green, LED_ON ); |
ubhat | 0:69f2e28d12c1 | 156 | } |
ubhat | 0:69f2e28d12c1 | 157 | |
ubhat | 0:69f2e28d12c1 | 158 | // Read and populate device orientation in Tx Buffer |
ubhat | 0:69f2e28d12c1 | 159 | if( ( BuffPtr + 2 ) <= LORAWAN_APP_DATA_SIZE ) |
ubhat | 0:69f2e28d12c1 | 160 | { |
ubhat | 0:69f2e28d12c1 | 161 | if( statusReg & 0x40 ) |
ubhat | 0:69f2e28d12c1 | 162 | { |
ubhat | 0:69f2e28d12c1 | 163 | if( statusReg & 0x01 ) |
ubhat | 0:69f2e28d12c1 | 164 | { |
ubhat | 0:69f2e28d12c1 | 165 | BuffAddr[BuffPtr++] = 0x66; // horizontal + faceup |
ubhat | 0:69f2e28d12c1 | 166 | } |
ubhat | 0:69f2e28d12c1 | 167 | else |
ubhat | 0:69f2e28d12c1 | 168 | { |
ubhat | 0:69f2e28d12c1 | 169 | BuffAddr[BuffPtr++] = 0x99; // horizontal + facedown |
ubhat | 0:69f2e28d12c1 | 170 | } |
ubhat | 0:69f2e28d12c1 | 171 | |
ubhat | 0:69f2e28d12c1 | 172 | BuffAddr[BuffPtr++] = 0; // vertical = false |
ubhat | 0:69f2e28d12c1 | 173 | } |
ubhat | 0:69f2e28d12c1 | 174 | else |
ubhat | 0:69f2e28d12c1 | 175 | { |
ubhat | 0:69f2e28d12c1 | 176 | BuffAddr[BuffPtr++] = 0; // horizontal = false |
ubhat | 0:69f2e28d12c1 | 177 | BuffAddr[BuffPtr++] = 0x11; // vertical = true |
ubhat | 0:69f2e28d12c1 | 178 | } |
ubhat | 0:69f2e28d12c1 | 179 | } |
ubhat | 0:69f2e28d12c1 | 180 | |
ubhat | 0:69f2e28d12c1 | 181 | break; |
ubhat | 6:f8194e691dd4 | 182 | } |
ubhat | 0:69f2e28d12c1 | 183 | |
ubhat | 0:69f2e28d12c1 | 184 | case AppAcclSenet: |
ubhat | 0:69f2e28d12c1 | 185 | { |
ubhat | 0:69f2e28d12c1 | 186 | uint8_t statusReg; |
ubhat | 0:69f2e28d12c1 | 187 | |
ubhat | 0:69f2e28d12c1 | 188 | // Read the PS_STATUS register |
ubhat | 0:69f2e28d12c1 | 189 | statusReg = Mma8451q.read_single( MMA8451_PL_STATUS ); |
ubhat | 0:69f2e28d12c1 | 190 | |
ubhat | 0:69f2e28d12c1 | 191 | /* Display Orientation of NAMote on Serial Port */ |
ubhat | 0:69f2e28d12c1 | 192 | SerialAcclMetrDisplay( statusReg ); |
ubhat | 0:69f2e28d12c1 | 193 | |
ubhat | 0:69f2e28d12c1 | 194 | // If Orientation of the Mote changed then populate Upper Nibble of 0th Byte of Tx Buffer |
ubhat | 0:69f2e28d12c1 | 195 | if( ( statusReg & 0x40 ) != 0 ) |
ubhat | 0:69f2e28d12c1 | 196 | { |
ubhat | 0:69f2e28d12c1 | 197 | AppLed = 0; |
ubhat | 0:69f2e28d12c1 | 198 | CtrlLED( Green, LED_OFF ); |
ubhat | 0:69f2e28d12c1 | 199 | BuffAddr[BuffPtr++] = 0; // horizontal |
ubhat | 0:69f2e28d12c1 | 200 | } |
ubhat | 0:69f2e28d12c1 | 201 | else |
ubhat | 0:69f2e28d12c1 | 202 | { |
ubhat | 0:69f2e28d12c1 | 203 | AppLed = 1; |
ubhat | 0:69f2e28d12c1 | 204 | CtrlLED( Green, LED_ON ); |
ubhat | 0:69f2e28d12c1 | 205 | BuffAddr[BuffPtr++] = 10; // vertical |
ubhat | 0:69f2e28d12c1 | 206 | } |
ubhat | 0:69f2e28d12c1 | 207 | |
ubhat | 0:69f2e28d12c1 | 208 | break; |
ubhat | 0:69f2e28d12c1 | 209 | } |
ubhat | 0:69f2e28d12c1 | 210 | |
ubhat | 13:238b23d4025c | 211 | case AppAcclSensor: |
ubhat | 13:238b23d4025c | 212 | { |
ubhat | 13:238b23d4025c | 213 | uint8_t statusReg; |
ubhat | 13:238b23d4025c | 214 | |
ubhat | 13:238b23d4025c | 215 | // Read the PS_STATUS register |
ubhat | 13:238b23d4025c | 216 | statusReg = Mma8451q.read_single( MMA8451_PL_STATUS ); |
ubhat | 13:238b23d4025c | 217 | |
ubhat | 13:238b23d4025c | 218 | /* Display Orientation of NAMote on Serial Port */ |
ubhat | 13:238b23d4025c | 219 | SerialAcclMetrDisplay( statusReg ); |
ubhat | 13:238b23d4025c | 220 | |
ubhat | 13:238b23d4025c | 221 | // If Orientation of the Mote changed then populate Upper Nibble of 0th Byte of Tx Buffer |
ubhat | 13:238b23d4025c | 222 | if( ( statusReg & 0x80 ) != 0 ) |
ubhat | 13:238b23d4025c | 223 | { |
ubhat | 13:238b23d4025c | 224 | CtrlLED( Green, LED_ON ); |
ubhat | 13:238b23d4025c | 225 | } |
ubhat | 13:238b23d4025c | 226 | else |
ubhat | 13:238b23d4025c | 227 | { |
ubhat | 13:238b23d4025c | 228 | CtrlLED( Green, LED_OFF ); |
ubhat | 13:238b23d4025c | 229 | } |
ubhat | 13:238b23d4025c | 230 | |
ubhat | 13:238b23d4025c | 231 | // Read and populate device orientation in Tx Buffer |
ubhat | 13:238b23d4025c | 232 | if( ( BuffPtr + 2 ) <= LORAWAN_APP_DATA_SIZE ) |
ubhat | 13:238b23d4025c | 233 | { |
ubhat | 13:238b23d4025c | 234 | Mma8451q.read(MMA8451_OUT_X_MSB, &BuffAddr[BuffPtr], 6); |
ubhat | 13:238b23d4025c | 235 | BuffPtr += 6; |
ubhat | 13:238b23d4025c | 236 | } |
ubhat | 13:238b23d4025c | 237 | |
ubhat | 13:238b23d4025c | 238 | break; |
ubhat | 13:238b23d4025c | 239 | } |
ubhat | 13:238b23d4025c | 240 | |
ubhat | 6:f8194e691dd4 | 241 | case AppPushButton: |
ubhat | 6:f8194e691dd4 | 242 | { |
ubhat | 6:f8194e691dd4 | 243 | uint16_t PushButtonCnt; |
ubhat | 6:f8194e691dd4 | 244 | uint8_t *p = (uint8_t *) &PushButtonCnt; |
ubhat | 6:f8194e691dd4 | 245 | |
ubhat | 6:f8194e691dd4 | 246 | PushButtonCnt = LoRaMacUplinkStatus.UplinkCounter; |
ubhat | 6:f8194e691dd4 | 247 | |
ubhat | 6:f8194e691dd4 | 248 | memcpy( &BuffAddr[BuffPtr], p, sizeof(uint16_t) ); |
ubhat | 6:f8194e691dd4 | 249 | |
ubhat | 6:f8194e691dd4 | 250 | break; |
ubhat | 6:f8194e691dd4 | 251 | } |
ubhat | 6:f8194e691dd4 | 252 | |
ubhat | 0:69f2e28d12c1 | 253 | default: |
ubhat | 0:69f2e28d12c1 | 254 | { |
ubhat | 0:69f2e28d12c1 | 255 | break; |
ubhat | 0:69f2e28d12c1 | 256 | } |
ubhat | 0:69f2e28d12c1 | 257 | } |
ubhat | 0:69f2e28d12c1 | 258 | } |
ubhat | 0:69f2e28d12c1 | 259 | |
ubhat | 0:69f2e28d12c1 | 260 | static void OnRedLedTimerEvent( void ) |
ubhat | 0:69f2e28d12c1 | 261 | { |
ubhat | 0:69f2e28d12c1 | 262 | TimerStop( &RedLedTimer.LedTimer ); |
ubhat | 0:69f2e28d12c1 | 263 | |
ubhat | 0:69f2e28d12c1 | 264 | if( RedLed == LED_OFF ) |
ubhat | 0:69f2e28d12c1 | 265 | { |
ubhat | 0:69f2e28d12c1 | 266 | RedLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 267 | } |
ubhat | 0:69f2e28d12c1 | 268 | else |
ubhat | 0:69f2e28d12c1 | 269 | { |
ubhat | 0:69f2e28d12c1 | 270 | RedLed = LED_OFF; |
ubhat | 0:69f2e28d12c1 | 271 | } |
ubhat | 0:69f2e28d12c1 | 272 | } |
ubhat | 0:69f2e28d12c1 | 273 | |
ubhat | 0:69f2e28d12c1 | 274 | static void OnYellowLedTimerEvent( void ) |
ubhat | 0:69f2e28d12c1 | 275 | { |
ubhat | 0:69f2e28d12c1 | 276 | TimerStop( &YellowLedTimer.LedTimer ); |
ubhat | 0:69f2e28d12c1 | 277 | |
ubhat | 0:69f2e28d12c1 | 278 | if( YellowLed == LED_OFF ) |
ubhat | 0:69f2e28d12c1 | 279 | { |
ubhat | 0:69f2e28d12c1 | 280 | YellowLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 281 | } |
ubhat | 0:69f2e28d12c1 | 282 | else |
ubhat | 0:69f2e28d12c1 | 283 | { |
ubhat | 0:69f2e28d12c1 | 284 | YellowLed = LED_OFF; |
ubhat | 0:69f2e28d12c1 | 285 | } |
ubhat | 0:69f2e28d12c1 | 286 | } |
ubhat | 0:69f2e28d12c1 | 287 | |
ubhat | 0:69f2e28d12c1 | 288 | static void OnGreenLedTimerEvent( void ) |
ubhat | 0:69f2e28d12c1 | 289 | { |
ubhat | 0:69f2e28d12c1 | 290 | TimerStop( &GreenLedTimer.LedTimer ); |
ubhat | 0:69f2e28d12c1 | 291 | |
ubhat | 0:69f2e28d12c1 | 292 | if( GreenLed == LED_OFF ) |
ubhat | 0:69f2e28d12c1 | 293 | { |
ubhat | 0:69f2e28d12c1 | 294 | GreenLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 295 | } |
ubhat | 0:69f2e28d12c1 | 296 | else |
ubhat | 0:69f2e28d12c1 | 297 | { |
ubhat | 0:69f2e28d12c1 | 298 | GreenLed = LED_OFF; |
ubhat | 0:69f2e28d12c1 | 299 | } |
ubhat | 0:69f2e28d12c1 | 300 | } |
ubhat | 0:69f2e28d12c1 | 301 | |
ubhat | 0:69f2e28d12c1 | 302 | TimerLed::TimerLed( eLedType led ) |
ubhat | 0:69f2e28d12c1 | 303 | { |
ubhat | 0:69f2e28d12c1 | 304 | switch( led ) |
ubhat | 0:69f2e28d12c1 | 305 | { |
ubhat | 0:69f2e28d12c1 | 306 | case Red: |
ubhat | 0:69f2e28d12c1 | 307 | { |
ubhat | 0:69f2e28d12c1 | 308 | TimerInit( &LedTimer, OnRedLedTimerEvent ); |
ubhat | 0:69f2e28d12c1 | 309 | break; |
ubhat | 0:69f2e28d12c1 | 310 | } |
ubhat | 0:69f2e28d12c1 | 311 | |
ubhat | 0:69f2e28d12c1 | 312 | case Yellow: |
ubhat | 0:69f2e28d12c1 | 313 | { |
ubhat | 0:69f2e28d12c1 | 314 | TimerInit( &LedTimer, OnYellowLedTimerEvent ); |
ubhat | 0:69f2e28d12c1 | 315 | break; |
ubhat | 0:69f2e28d12c1 | 316 | } |
ubhat | 0:69f2e28d12c1 | 317 | |
ubhat | 0:69f2e28d12c1 | 318 | case Green: |
ubhat | 0:69f2e28d12c1 | 319 | { |
ubhat | 0:69f2e28d12c1 | 320 | TimerInit( &LedTimer, OnGreenLedTimerEvent ); |
ubhat | 0:69f2e28d12c1 | 321 | break; |
ubhat | 0:69f2e28d12c1 | 322 | } |
ubhat | 0:69f2e28d12c1 | 323 | } |
ubhat | 0:69f2e28d12c1 | 324 | |
ubhat | 0:69f2e28d12c1 | 325 | } |
ubhat | 0:69f2e28d12c1 | 326 | |
ubhat | 0:69f2e28d12c1 | 327 | TimerLed::~TimerLed( ) |
ubhat | 0:69f2e28d12c1 | 328 | { |
ubhat | 0:69f2e28d12c1 | 329 | } |
ubhat | 0:69f2e28d12c1 | 330 | |
ubhat | 0:69f2e28d12c1 | 331 | void BlinkLED( eLedType led, uint32_t time ) |
ubhat | 0:69f2e28d12c1 | 332 | { |
ubhat | 0:69f2e28d12c1 | 333 | switch( led ) |
ubhat | 0:69f2e28d12c1 | 334 | { |
ubhat | 0:69f2e28d12c1 | 335 | case Red: |
ubhat | 0:69f2e28d12c1 | 336 | { |
ubhat | 0:69f2e28d12c1 | 337 | TimerSetValue( &RedLedTimer.LedTimer, time ); |
ubhat | 0:69f2e28d12c1 | 338 | TimerStart( &RedLedTimer.LedTimer ); |
ubhat | 0:69f2e28d12c1 | 339 | RedLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 340 | break; |
ubhat | 0:69f2e28d12c1 | 341 | } |
ubhat | 0:69f2e28d12c1 | 342 | |
ubhat | 0:69f2e28d12c1 | 343 | case Yellow: |
ubhat | 0:69f2e28d12c1 | 344 | { |
ubhat | 0:69f2e28d12c1 | 345 | TimerSetValue( &YellowLedTimer.LedTimer, time ); |
ubhat | 0:69f2e28d12c1 | 346 | TimerStart( &YellowLedTimer.LedTimer ); |
ubhat | 0:69f2e28d12c1 | 347 | YellowLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 348 | break; |
ubhat | 0:69f2e28d12c1 | 349 | } |
ubhat | 0:69f2e28d12c1 | 350 | |
ubhat | 0:69f2e28d12c1 | 351 | case Green: |
ubhat | 0:69f2e28d12c1 | 352 | { |
ubhat | 0:69f2e28d12c1 | 353 | TimerSetValue( &GreenLedTimer.LedTimer, time ); |
ubhat | 0:69f2e28d12c1 | 354 | TimerStart( &GreenLedTimer.LedTimer ); |
ubhat | 0:69f2e28d12c1 | 355 | GreenLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 356 | break; |
ubhat | 0:69f2e28d12c1 | 357 | } |
ubhat | 0:69f2e28d12c1 | 358 | } |
ubhat | 0:69f2e28d12c1 | 359 | } |
ubhat | 0:69f2e28d12c1 | 360 | |
ubhat | 0:69f2e28d12c1 | 361 | void ToggleLED( eLedType led ) |
ubhat | 0:69f2e28d12c1 | 362 | { |
ubhat | 0:69f2e28d12c1 | 363 | switch( led ) |
ubhat | 0:69f2e28d12c1 | 364 | { |
ubhat | 0:69f2e28d12c1 | 365 | case Red: |
ubhat | 0:69f2e28d12c1 | 366 | { |
ubhat | 0:69f2e28d12c1 | 367 | if( RedLed == LED_OFF ) |
ubhat | 0:69f2e28d12c1 | 368 | { |
ubhat | 0:69f2e28d12c1 | 369 | RedLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 370 | } |
ubhat | 0:69f2e28d12c1 | 371 | else |
ubhat | 0:69f2e28d12c1 | 372 | { |
ubhat | 0:69f2e28d12c1 | 373 | RedLed = LED_OFF; |
ubhat | 0:69f2e28d12c1 | 374 | } |
ubhat | 0:69f2e28d12c1 | 375 | break; |
ubhat | 0:69f2e28d12c1 | 376 | } |
ubhat | 0:69f2e28d12c1 | 377 | |
ubhat | 0:69f2e28d12c1 | 378 | case Yellow: |
ubhat | 0:69f2e28d12c1 | 379 | { |
ubhat | 0:69f2e28d12c1 | 380 | if( YellowLed == LED_OFF ) |
ubhat | 0:69f2e28d12c1 | 381 | { |
ubhat | 0:69f2e28d12c1 | 382 | YellowLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 383 | } |
ubhat | 0:69f2e28d12c1 | 384 | else |
ubhat | 0:69f2e28d12c1 | 385 | { |
ubhat | 0:69f2e28d12c1 | 386 | YellowLed = LED_OFF; |
ubhat | 0:69f2e28d12c1 | 387 | } |
ubhat | 0:69f2e28d12c1 | 388 | break; |
ubhat | 0:69f2e28d12c1 | 389 | } |
ubhat | 0:69f2e28d12c1 | 390 | |
ubhat | 0:69f2e28d12c1 | 391 | case Green: |
ubhat | 0:69f2e28d12c1 | 392 | { |
ubhat | 0:69f2e28d12c1 | 393 | if( GreenLed == LED_OFF ) |
ubhat | 0:69f2e28d12c1 | 394 | { |
ubhat | 0:69f2e28d12c1 | 395 | GreenLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 396 | } |
ubhat | 0:69f2e28d12c1 | 397 | else |
ubhat | 0:69f2e28d12c1 | 398 | { |
ubhat | 0:69f2e28d12c1 | 399 | GreenLed = LED_OFF; |
ubhat | 0:69f2e28d12c1 | 400 | } |
ubhat | 0:69f2e28d12c1 | 401 | break; |
ubhat | 0:69f2e28d12c1 | 402 | } |
ubhat | 0:69f2e28d12c1 | 403 | } |
ubhat | 0:69f2e28d12c1 | 404 | } |
ubhat | 0:69f2e28d12c1 | 405 | |
ubhat | 0:69f2e28d12c1 | 406 | void CtrlLED( eLedType led, uint8_t state ) |
ubhat | 0:69f2e28d12c1 | 407 | { |
ubhat | 0:69f2e28d12c1 | 408 | switch( led ) |
ubhat | 0:69f2e28d12c1 | 409 | { |
ubhat | 0:69f2e28d12c1 | 410 | case Red: |
ubhat | 0:69f2e28d12c1 | 411 | { |
ubhat | 0:69f2e28d12c1 | 412 | RedLed = state; |
ubhat | 0:69f2e28d12c1 | 413 | break; |
ubhat | 0:69f2e28d12c1 | 414 | } |
ubhat | 0:69f2e28d12c1 | 415 | |
ubhat | 0:69f2e28d12c1 | 416 | case Yellow: |
ubhat | 0:69f2e28d12c1 | 417 | { |
ubhat | 0:69f2e28d12c1 | 418 | YellowLed = state; |
ubhat | 0:69f2e28d12c1 | 419 | break; |
ubhat | 0:69f2e28d12c1 | 420 | } |
ubhat | 0:69f2e28d12c1 | 421 | |
ubhat | 0:69f2e28d12c1 | 422 | case Green: |
ubhat | 0:69f2e28d12c1 | 423 | { |
ubhat | 0:69f2e28d12c1 | 424 | GreenLed = state; |
ubhat | 0:69f2e28d12c1 | 425 | break; |
ubhat | 0:69f2e28d12c1 | 426 | } |
ubhat | 0:69f2e28d12c1 | 427 | |
ubhat | 0:69f2e28d12c1 | 428 | case Usr: |
ubhat | 0:69f2e28d12c1 | 429 | { |
ubhat | 0:69f2e28d12c1 | 430 | if( state ) |
ubhat | 0:69f2e28d12c1 | 431 | { |
ubhat | 0:69f2e28d12c1 | 432 | UsrLed = LED_ON; |
ubhat | 0:69f2e28d12c1 | 433 | } |
ubhat | 0:69f2e28d12c1 | 434 | else |
ubhat | 0:69f2e28d12c1 | 435 | { |
ubhat | 0:69f2e28d12c1 | 436 | UsrLed = LED_OFF; |
ubhat | 0:69f2e28d12c1 | 437 | } |
ubhat | 0:69f2e28d12c1 | 438 | break; |
ubhat | 0:69f2e28d12c1 | 439 | } |
ubhat | 0:69f2e28d12c1 | 440 | } |
ubhat | 0:69f2e28d12c1 | 441 | } |
ubhat | 7:92f4f419f91f | 442 | |
ubhat | 7:92f4f419f91f | 443 | void CheckOrientation( void ) |
ubhat | 7:92f4f419f91f | 444 | { |
ubhat | 7:92f4f419f91f | 445 | uint8_t statusReg; |
ubhat | 7:92f4f419f91f | 446 | |
ubhat | 7:92f4f419f91f | 447 | // Read the PS_STATUS register |
ubhat | 7:92f4f419f91f | 448 | statusReg = Mma8451q.read_single( MMA8451_PL_STATUS ); |
ubhat | 7:92f4f419f91f | 449 | |
ubhat | 7:92f4f419f91f | 450 | // If Orientation of the Mote changed then populate Upper Nibble of 0th Byte of Tx Buffer |
ubhat | 7:92f4f419f91f | 451 | if( ( statusReg & 0x40 ) != 0 ) |
ubhat | 7:92f4f419f91f | 452 | { |
ubhat | 7:92f4f419f91f | 453 | CtrlLED( Green, LED_OFF ); |
ubhat | 7:92f4f419f91f | 454 | VerticalStatus = false; // horizontal |
ubhat | 7:92f4f419f91f | 455 | } |
ubhat | 7:92f4f419f91f | 456 | else |
ubhat | 7:92f4f419f91f | 457 | { |
ubhat | 7:92f4f419f91f | 458 | CtrlLED( Green, LED_ON ); |
ubhat | 7:92f4f419f91f | 459 | VerticalStatus = true; // vertical |
ubhat | 7:92f4f419f91f | 460 | } |
ubhat | 7:92f4f419f91f | 461 | } |