Implemented LED Indicator Patterns

Dependencies:   mbed

Revision:
27:f024c8c51c6e
Parent:
26:2cdfd2d309a6
Child:
28:eca152c69c9b
--- a/app/main.cpp	Wed Apr 03 11:30:23 2019 +0000
+++ b/app/main.cpp	Thu Apr 25 15:41:02 2019 +0000
@@ -149,6 +149,14 @@
 AnalogIn RH_PIN(PC_2);
 AnalogIn VCE_PIN(PB_1);
 
+/*
+D7 == Green
+D8 == Blue 
+ */
+
+DigitalOut myGreenLed(D7); // ON = Lora Connected
+DigitalOut myBlueLed(D8);  // Blink twice = Sends data or increments counter value Pelase proceed
+
 InterruptIn button1(USER_BUTTON);
 volatile bool button1_pressed = false; // Used in the main loop
 volatile bool button1_enabled = true; // Used for debouncing
@@ -201,6 +209,77 @@
 volatile bool Led2State = false;
 volatile bool Led2StateChanged = false;
 
+enum GreenLedState_e{
+    GREEN_LED_STATE_INIT=0,
+    GREEN_LED_STATE_JOINING,
+    GREEN_LED_STATE_JOINED
+}GreenLedState;
+volatile uint32_t GreenLedTickCounter=0;
+
+volatile bool BatteryLowFlag = true;
+
+volatile uint32_t GreenLedJoiningCounter=0;
+
+static TimerEvent_t GreenLedTimer;
+static void OnGreenLedTimerEvent( void )
+{
+    MibRequestConfirm_t mibReq;
+    LoRaMacStatus_t status;
+
+    if(BatteryLowFlag == true)
+    {
+        myGreenLed = 0; // OFF
+    }
+    else
+    {
+        // battery ok
+        GreenLedTickCounter++;        
+        switch(GreenLedState)
+        {
+            case GREEN_LED_STATE_INIT:
+            {
+                GreenLedState = GREEN_LED_STATE_JOINING;
+            }
+            break;
+            
+            case GREEN_LED_STATE_JOINING:
+            {
+                GreenLedJoiningCounter++;
+                if((GreenLedJoiningCounter % 2) == 0)
+                {
+                    GreenLedJoiningCounter = 0;
+                    myGreenLed = !myGreenLed; // toggle
+                    
+                    // check if we have joined the network
+                    mibReq.Type = MIB_NETWORK_JOINED;
+                    status = LoRaMacMibGetRequestConfirm( &mibReq );
+                    if( status == LORAMAC_STATUS_OK )
+                    {
+                        if( mibReq.Param.IsNetworkJoined == true )
+                        {
+                            GreenLedState = GREEN_LED_STATE_JOINED;
+                        }
+                    }
+                }
+            }
+            break;
+                    
+            case GREEN_LED_STATE_JOINED:
+            {
+                myGreenLed = 1;
+            }
+            break;
+        }
+    }
+}
+
+static TimerEvent_t BatteryCheckTimer;
+volatile bool BatteryCheckFlag=false;
+static void OnBatteryCheckTimerEvent( void )
+{
+    BatteryCheckFlag = true;
+}
+
 /*!
  * Indicates if a new packet can be sent
  */
@@ -209,6 +288,14 @@
 /*!
  * Device states
  */
+ 
+ 
+ // Application Globals
+
+
+
+ 
+ 
 static enum eDeviceState
 {
     DEVICE_STATE_INIT,
@@ -911,7 +998,7 @@
     measurements[3] = (uint8_t) light_2_reading/2;
     measurements[4] = (uint8_t) rh_reading/2;
     
-    // End test
+    // End test // 
     relayPin = 0;
     AppLedStateOn = 0;
     Led3StateChanged = true;
@@ -1013,8 +1100,6 @@
 }
 
 
-
-
 int main( void )
 {
     pc.printf("mbed-os-rev: %d.%d.%d   lib-rev: %d\r\n", \
@@ -1056,6 +1141,16 @@
     button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
     AppLedStateOn = 0;
     Led3StateChanged = true;
+    
+    myGreenLed = 0; // INITIAL OFF
+    GreenLedState = GREEN_LED_STATE_INIT;
+    TimerInit( &GreenLedTimer, OnGreenLedTimerEvent );
+    TimerSetValue( &GreenLedTimer, 100 );
+    TimerStart( &GreenLedTimer );
+    
+    TimerInit( &BatteryCheckTimer, OnBatteryCheckTimerEvent );
+    TimerSetValue( &BatteryCheckTimer, 100 ); // 100msec
+    TimerStart( &BatteryCheckTimer );
 
     while( 1 )
     {
@@ -1095,6 +1190,24 @@
             SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
         }
         
+        if( BatteryCheckFlag == true)
+        {
+            BatteryCheckFlag = false;
+
+            // Measure voltage preval
+            float battery_reading = BAT_PIN.read()*3300*2;
+ //           pc.printf("VBAT= %d", (int)battery_reading);
+            
+            if((battery_reading < 2000)||(battery_reading > 4450)) // set max voltage 4.55V
+            {
+                BatteryLowFlag = true;
+            }
+            else
+            {
+                BatteryLowFlag = false;
+            }
+        }
+        
         switch( DeviceState )
         {
             case DEVICE_STATE_INIT: