Node code for sx1272 LoRa transciever

Dependencies:   mbed BMP085 BufferedSerial DHT Sds021 Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72-bootcamp by Semtech

Revision:
2:48d8d4806d48
Parent:
0:45496a70a8a5
Child:
3:d4142832d5af
--- a/app/main.cpp	Thu Feb 18 07:49:29 2016 +0000
+++ b/app/main.cpp	Mon Mar 07 13:54:25 2016 +0000
@@ -19,7 +19,8 @@
 #include "LoRaMac.h"
 #include "Comissioning.h"
 #include "SerialDisplay.h"
-
+#include "DigitDisplay.h"
+#include "ChainableLED.h"
 /*!
  * Join requests trials duty cycle.
  */
@@ -39,12 +40,12 @@
 /*!
  * Default mote datarate
  */
-#define LORAWAN_DEFAULT_DATARATE                    DR_0
+#define LORAWAN_DEFAULT_DATARATE                    DR_5
 
 /*!
  * LoRaWAN confirmed messages
  */
-#define LORAWAN_CONFIRMED_MSG_ON                    true
+#define LORAWAN_CONFIRMED_MSG_ON                    false
 
 /*!
  * LoRaWAN Adaptive Data Rate
@@ -62,7 +63,7 @@
  *
  * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
  */
-#define LORAWAN_DUTYCYCLE_ON                        true
+#define LORAWAN_DUTYCYCLE_ON                        false
 
 #endif
 
@@ -158,6 +159,25 @@
  */
 static bool NextTx = true;
 
+
+static float LightValue = 0.0; 
+static uint8_t LightMode = 0;  // 0:automatic, 1:manual
+Ticker BuzTimer;
+
+#define NUM_LED 3
+
+
+AnalogIn LightSens( A1 ); 
+ChainableLED color_led(D6, D7, NUM_LED);
+DigitDisplay display(A2, A3);
+DigitalOut buzzer(A2);
+
+
+static void OnBuzTimerEvent( void )
+{
+    buzzer = 0;
+    BuzTimer.detach( );
+}
 /*!
  * Device states
  */
@@ -282,6 +302,22 @@
 {
     switch( port )
     {
+     
+    case 10:
+        {  
+        uint32_t tempValue = (uint32_t)(LightValue*1000000.0);
+        
+        debug( "[Tx] LightValue=%d 0x%x\n\r" , tempValue, tempValue);
+        
+        AppData[0] = LightMode;
+        AppData[1] = ((tempValue&0xFF000000)>>24)&0xFF;
+        AppData[2] = ((tempValue&0x00FF0000)>>16)&0xFF;
+        AppData[3] = ((tempValue&0x0000FF00)>>8)&0xFF;
+        AppData[4] = (tempValue&0x000000FF);    
+        }
+        break;
+        
+        
     case 15:
         {
             AppData[0] = AppLedStateOn;
@@ -553,6 +589,28 @@
                 Led3StateChanged = true;
             }
             break;
+            
+        case 10: 
+            display.write( 0, McpsIndication->Buffer[0] );
+            display.write( 1, McpsIndication->Buffer[1] );
+            display.write( 2, McpsIndication->Buffer[2] );
+            display.write( 3, McpsIndication->Buffer[3] ); 
+            break;
+            
+        case 20:
+            LightMode = McpsIndication->Buffer[0];
+            debug( "[Rx] LightMode=%x - R G B= 0x%x 0x%x 0x%x\n\r" , LightMode, McpsIndication->Buffer[1], McpsIndication->Buffer[2], McpsIndication->Buffer[3]);
+            if( LightMode )
+            {
+                color_led.setColorRGB(0, McpsIndication->Buffer[1], McpsIndication->Buffer[2], McpsIndication->Buffer[3] );
+            }
+            break;
+            
+        case 30:
+            BuzTimer.attach_us( &OnBuzTimerEvent, 200000 );
+            buzzer = 1;
+            break;
+            
         case 224:
             if( ComplianceTest.Running == false )
             {
@@ -690,6 +748,11 @@
  */
 int main( void )
 {
+    float tempLightValue = 0;   
+    
+    LightMode = 0;      // 0: manual,   1: automatic
+    buzzer = 0;         // 0: OFF,      1: ON
+    
     LoRaMacPrimitives_t LoRaMacPrimitives;
     LoRaMacCallback_t LoRaMacCallbacks;
     MibRequestConfirm_t mibReq;
@@ -877,6 +940,18 @@
                 DeviceState = DEVICE_STATE_INIT;
                 break;
             }
+            
+        }
+        
+        // Read light sensor
+        tempLightValue = LightSens.read( ) * 1.65;
+        
+        LightValue = ( 1 - tempLightValue );
+        
+        // Set automatic RGB from light sensor
+        if( LightMode == 0 )
+        {
+            color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
         }
     }
 }