A simple example for analog input and EPD usage.

Dependencies:   GDEP015OC1 acn_nrf52_saadc aconno_bsp

Fork of acd52832_3_Analog_In by aconno dev team

Files at this revision

API Documentation at this revision

Comitter:
jurica238814
Date:
Tue Jul 04 17:55:04 2017 +0000
Parent:
5:6566725c8835
Child:
7:ecf8291ca82b
Commit message:
New values for battery voltage and current added.

Changed in this revision

GDEP015OC1.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GDEP015OC1.lib	Fri Jun 30 11:45:56 2017 +0000
+++ b/GDEP015OC1.lib	Tue Jul 04 17:55:04 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/jurica238814/code/GDEP015OC1/#55f053e7f087
+https://developer.mbed.org/users/jurica238814/code/GDEP015OC1/#9cf09f5a693a
--- a/main.cpp	Fri Jun 30 11:45:56 2017 +0000
+++ b/main.cpp	Tue Jul 04 17:55:04 2017 +0000
@@ -10,17 +10,28 @@
 #include "GDEP015OC1.h"
 #include "pictures.h"
 
-#define DEBUG                   (1)
-#define ADC_MAX_VALUE           (4092)
+#define DEBUG                   (0)
+#define ADC_MAX_VALUE           (4095)
 #define ADC_REF_VOLTAGE         (3.6)
 #define VOLTAGE_DIVIDER_RATION  (130.0/30)
 #define CURRENT_FACTOR          (36.0)
-#define BATTERY_MAX_V           (14.0)
+#define BATTERY_MAX_V           (12.8)
 #define BATTERY_MIN_V           (12.0)
-#define BATTERY_STEP            (0.4) 
-#define LOW_QUICK_CURRENT       (0.5)
+#define BATTERY_STEP            (0.2) 
+#define QUICK_CURRENT           (float)(0.8)
+#define LOW_CURRENT             (float)(0.4)
+#define NO_CURRENT              (float)(0.01)
+#define NUM_OF_MEASUREMENTS     (20)
+#define NO_CURRENT_STATE        (0)
+#define LOW_CURRENT_STATE       (1)
+#define QUICK_CURRENT_STATE     (2)
     
-
+#define _BS_5                   (5)
+#define _BS_4                   (4)
+#define _BS_3                   (3)
+#define _BS_2                   (2)
+#define _BS_1                   (1)
+#define _BS_E                   (0)
 
 SPI spi(PIN_EPD_MOSI, NC, PIN_EPD_SCK, NC);
 GDEP015OC1 epd = GDEP015OC1(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY);
@@ -29,109 +40,194 @@
 AnalogIn  usb1      (p29);
 AnalogIn  usb2      (p30);
 
+
+uint8_t get_battery_level(float battery_voltage){
+     if(battery_voltage > BATTERY_MAX_V - BATTERY_STEP){
+                return _BS_5;
+        }
+        else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 2) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 1)){
+                return _BS_4;
+        }
+        else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 3) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 2)){
+                return _BS_3;
+        }
+        else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 4) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 3)){
+                return _BS_2;
+        }
+        else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 5) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 4)){
+                return _BS_1;
+        }
+        else if(battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 5){
+                return _BS_E;
+        }
+    return 0;
+}
+
+uint8_t get_current_level(float current_value, uint8_t usb_state){
+    if(current_value < NO_CURRENT)
+        return NO_CURRENT_STATE;
+    else if(current_value > NO_CURRENT && current_value < LOW_CURRENT)
+        return LOW_CURRENT_STATE;
+    else if(current_value > LOW_CURRENT && current_value < QUICK_CURRENT){
+        // Determine current charger state depends on previous state
+        if(usb_state == LOW_CURRENT_STATE)
+            return LOW_CURRENT_STATE;
+        else if(usb_state == QUICK_CURRENT_STATE)
+            return QUICK_CURRENT_STATE;
+    }
+    else if(current_value > QUICK_CURRENT)
+        return QUICK_CURRENT_STATE;
+    return NO_CURRENT_STATE;
+}
+
 int main(){
-    char buffer[25] = {0};
     char low_string[25] = "LOW";
     char quick_string[25] = "QUICK";
     float adc1_mean=0, adc2_mean=0, adc3_mean=0;
     float battery_voltage = 0;
     float usb1_current = 0, usb2_current = 0;
     int count = 0;
+    uint8_t battery_level = 0;
+    uint8_t old_battery_level;
+    uint8_t usb1_current_state = 0, usb2_current_state = 0;
+    uint8_t usb1_old_state = 0, usb2_old_state = 0;
+    uint8_t change_flag = 1;
+    
+    #if DEBUG
+        char buffer[25] = {0};
+    #endif
     
     NRF_SAADC->RESOLUTION = 0x00000002;             // Set 12b resolution
     
     epd.empty();
     epd.writeFull(); 
-            
+        
     while(true){   
         
         adc1_mean += battery.read_u16();
         adc2_mean += usb1.read_u16();
         adc3_mean += usb2.read_u16();
         count ++;
-        
-        if (count == 10){
-            
-            adc1_mean /= 10;
-            adc2_mean /= 10;
-            adc3_mean /= 10;
+                
+        #if DEBUG
+            epd.empty();
+            epd.writeFull(); 
+        #endif            
+        if (count == NUM_OF_MEASUREMENTS){ 
+     
+            adc1_mean /= NUM_OF_MEASUREMENTS;
+            adc2_mean /= NUM_OF_MEASUREMENTS;
+            adc3_mean /= NUM_OF_MEASUREMENTS;
             count = 0;
             
             battery_voltage = adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION;
             usb1_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean;
             usb2_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean;
-            
-            if(battery_voltage > BATTERY_MAX_V - BATTERY_STEP){
-                //Load image
-                for(uint16_t x=0;x<5000;x++)
-                    epd.fill(BS_5[x], x);
-                epd.write();
-            }
-            else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 2) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 1)){
-                //Load image
-                for(uint16_t x=0;x<5000;x++)
-                    epd.fill(BS_4[x], x);
-                epd.write();
-            }
-            else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 3) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 2)){
-                //Load image
-                for(uint16_t x=0;x<5000;x++)
-                    epd.fill(BS_3[x], x);
-                epd.write();
-            }
-            else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 4) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 3)){
-                //Load image
-                for(uint16_t x=0;x<5000;x++)
-                    epd.fill(BS_2[x], x);
-                epd.write();
-            }
-            else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 5) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 4)){
-                //Load image
-                for(uint16_t x=0;x<5000;x++)
-                    epd.fill(BS_1[x], x);
-                epd.write();
-            }
-            else if(battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 5){
-                //Load image
-                for(uint16_t x=0;x<5000;x++)
-                    epd.fill(BS_E[x], x);
+                    
+            battery_level = get_battery_level(battery_voltage);
+            if(battery_level != old_battery_level){
+                old_battery_level = battery_level;
+                change_flag = 1;
+                switch(battery_level){
+                    case(0):{
+                            for(uint16_t x=0;x<5000;x++)
+                                epd.fill(BS_E[x], x);
+                            break;
+                            }
+                    case(1):{
+                            for(uint16_t x=0;x<5000;x++)
+                                epd.fill(BS_1[x], x);
+                            break;
+                            }
+                    case(2):{
+                            for(uint16_t x=0;x<5000;x++)
+                                epd.fill(BS_2[x], x);
+                            break;
+                            }
+                    case(3):{
+                            for(uint16_t x=0;x<5000;x++)
+                                epd.fill(BS_3[x], x);
+                            break;
+                            }
+                    case(4):{
+                            for(uint16_t x=0;x<5000;x++)
+                                epd.fill(BS_4[x], x);
+                            break;
+                            }
+                    case(5):{
+                            for(uint16_t x=0;x<5000;x++)
+                                epd.fill(BS_5[x], x);
+                            break;
+                            }
+                    default: break;
+                }
                 epd.write();
             }
             
-            if(usb1_current < (float)LOW_QUICK_CURRENT){
-                epd.writeString(low_string, 25, 180, 0);
-                epd.write();
-            }
-            else{
-                epd.writeString(quick_string, 25, 180, 0);
-                epd.write();
+            usb1_current_state = get_current_level(usb1_current, usb1_old_state);
+            usb2_current_state = get_current_level(usb2_current, usb2_old_state);
+            
+            if((usb1_old_state != usb1_current_state) || change_flag){
+                usb1_old_state = usb1_current_state;
+                switch(usb1_current_state){
+                    case(NO_CURRENT_STATE):{
+                        epd.writeString(quick_string, 25, 180, 1);  // Delete the old state
+                        epd.writeString(low_string, 25, 180, 1);
+                        break;
+                    }
+                    case(LOW_CURRENT_STATE):{
+                        epd.writeString(quick_string, 25, 180, 1);  // Delete the old state
+                        epd.writeString(low_string, 25, 180, 0);
+                        break;
+                    }
+                    case(QUICK_CURRENT_STATE):{
+                        epd.writeString(low_string, 25, 180, 1);    // Delete the old state
+                        epd.writeString(quick_string, 25, 180, 0);
+                        break;
+                    }
+                    default: break;
+                }
+                epd.write();                                            // Write string to the EPD
             }
             
-            if(usb2_current < (float)LOW_QUICK_CURRENT){
-                epd.writeString(low_string, 135, 180, 0);
-                epd.write();
+            if((usb2_old_state != usb2_current_state) || change_flag){
+                usb2_old_state = usb2_current_state;
+                switch(usb2_current_state){
+                    case(NO_CURRENT_STATE):{
+                        epd.writeString(low_string, 135, 180, 1);       // Delete the old state
+                        epd.writeString(quick_string, 135, 180, 1);     // Delete the old state
+                        break;
+                    }
+                    case(LOW_CURRENT_STATE):{
+                        epd.writeString(quick_string, 135, 180, 1);     // Delete the old state
+                        epd.writeString(low_string, 135, 180, 0);
+                        break;
+                    }
+                    case(QUICK_CURRENT_STATE):{
+                        epd.writeString(low_string, 135, 180, 1);       // Delete the old state
+                        epd.writeString(quick_string, 135, 180, 0);
+                        break;
+                    }
+                    default: break;
+                }
+                epd.write();                                            // Write string to the EPD
+                change_flag = 0;
             }
-            else{
-                epd.writeString(quick_string, 135, 180, 0);
-                epd.write();
-            }
+
             
-            
-            
-            #if DEBUG
-            /*
+            #if DEBUG            
                 // Print voltage and current values in debug mode
                 sprintf(buffer, "Battery: %5.5fV", adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION);     // Create a string
                 epd.writeString(buffer,25,95,0);                        // Write new data to the buffer    
                 epd.write();                                            // Write string to the EPD
-              */  
+                 
                 sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean);           // Create a string
                 epd.writeString(buffer,5,190,0);                        // Write new data to the buffer    
-                epd.write();                                            // Write string to the EPD
-                
+    
                 sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean);           // Create a string
                 epd.writeString(buffer,105,190,0);                       // Write new data to the buffer    
-                epd.write();                                            // Write string to the EPD
+            
+                epd.write(); 
             #endif
             
             adc1_mean = 0;