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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 
00002  * Copyright (c) 2017 Aconno. All Rights Reserved.
00003  *
00004  * Licensees are granted free, non-transferable use of the information. NO
00005  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00006  * the file.
00007  */
00008  
00009 #include "mbed.h"
00010 #include "acd52832_bsp.h"
00011 #include "GDEP015OC1.h"
00012 #include "pictures.h"
00013 #include "acd_nrf52_saadc.h"
00014 
00015 #define DEBUG                   (1)
00016 #define ADC_MAX_VALUE           (8192)
00017 #define ADC_REF_VOLTAGE         (3.3f)
00018 #define VOLTAGE_DIVIDER_RATION  (7.0606)
00019 #define VOLTAGE_LOSS            (0.4)
00020 #define CURRENT_FACTOR          (36.0)
00021 #define BATTERY_CHARGED_V       (13)
00022 #define BATTERY_MAX_V           (12.6)
00023 #define BATTERY_MIN_V           (11.85)
00024 #define BATTERY_STEP            (0.15) 
00025 #define QUICK_CURRENT           (float)(0.8)
00026 #define LOW_CURRENT             (float)(0.4)
00027 #define NO_CURRENT              (float)(0.01)
00028 #define NUM_OF_MEASUREMENTS     (1)
00029 #define NO_CURRENT_STATE        (0)
00030 #define LOW_CURRENT_STATE       (1)
00031 #define QUICK_CURRENT_STATE     (2)
00032     
00033 #define _CH_4                   (9)
00034 #define _CH_3                   (8)
00035 #define _CH_2                   (7)
00036 #define _CH_1                   (6)   
00037 #define _BS_5                   (5)
00038 #define _BS_4                   (4)
00039 #define _BS_3                   (3)
00040 #define _BS_2                   (2)
00041 #define _BS_1                   (1)
00042 #define _BS_E                   (0)
00043 
00044 #define ANALOG_PIN_P     (4)
00045 #define ANALOG_PIN_N     (5)
00046 #define NUM_OF_DIFF_ADCs (1)
00047 
00048 SPI spi(PIN_EPD_MOSI, NC, PIN_EPD_SCK, NC);
00049 GDEP015OC1 epd = GDEP015OC1(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY);    
00050 
00051 uint8_t get_battery_level(float battery_voltage){
00052         if (battery_voltage > BATTERY_CHARGED_V * 4){
00053                 return _CH_4;
00054         }
00055         else if ((battery_voltage > BATTERY_CHARGED_V + BATTERY_STEP * 3) && (battery_voltage > BATTERY_CHARGED_V + BATTERY_STEP  * 4)){
00056                 return _CH_3;
00057         }
00058         else if ((battery_voltage > BATTERY_CHARGED_V + BATTERY_STEP * 2) && (battery_voltage > BATTERY_CHARGED_V + BATTERY_STEP  * 3)){
00059                 return _CH_2;
00060         }
00061         else if ((battery_voltage > BATTERY_CHARGED_V + BATTERY_STEP * 1) && (battery_voltage > BATTERY_CHARGED_V + BATTERY_STEP * 2)){
00062                 return _CH_1;
00063         }
00064         else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP) && (battery_voltage < BATTERY_CHARGED_V)){
00065                 return _BS_5;
00066         }
00067         else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 2) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 1)){
00068                 return _BS_4;
00069         }
00070         else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 3) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 2)){
00071                 return _BS_3;
00072         }
00073         else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 4) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 3)){
00074                 return _BS_2;
00075         }
00076         else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 5) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 4)){
00077                 return _BS_1;
00078         }
00079         else if(battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 5){
00080                 return _BS_E;
00081         }
00082     return 0;
00083 }
00084 
00085 uint8_t get_current_level(float current_value, uint8_t usb_state){
00086     if(current_value < NO_CURRENT)
00087         return NO_CURRENT_STATE;
00088     else if(current_value > NO_CURRENT && current_value < LOW_CURRENT)
00089         return LOW_CURRENT_STATE;
00090     else if(current_value > LOW_CURRENT && current_value < QUICK_CURRENT){
00091         // Determine current charger state depends on previous state
00092         if(usb_state == LOW_CURRENT_STATE)
00093             return LOW_CURRENT_STATE;
00094         else if(usb_state == QUICK_CURRENT_STATE)
00095             return QUICK_CURRENT_STATE;
00096     }
00097     else if(current_value > QUICK_CURRENT)
00098         return QUICK_CURRENT_STATE;
00099     return NO_CURRENT_STATE;
00100 }
00101 
00102 int main(){
00103     NRF52_SAADC *diffADCs[NUM_OF_DIFF_ADCs];
00104     /* Declare your ADCs here */
00105     /* Change NUM_OF_DIFF_ADCs in the header */
00106     diffADCs[0] = new NRF52_SAADC(ANALOG_PIN_P, ANALOG_PIN_N);
00107     
00108     //NRF52_SAADC usb1(7); // P29, AIN5
00109     //NRF52_SAADC usb2(6); // p30, AIN6
00110 
00111     char low_string[25] = "LOW";
00112     char quick_string[25] = "QUICK";
00113     float adc1_mean=0, adc2_mean=0, adc3_mean=0;
00114     float battery_voltage = 0;
00115     float usb1_current = 0, usb2_current = 0;
00116     int count = 0;
00117     uint8_t battery_level = 0;
00118     uint8_t old_battery_level;
00119     uint8_t usb1_current_state = 0, usb2_current_state = 0;
00120     uint8_t usb1_old_state = 0, usb2_old_state = 0;
00121     uint8_t change_flag = 1;
00122     
00123     #if DEBUG
00124         char buffer[25] = {0};
00125         float old_battery_voltage = 0;
00126         float old_usb1_current = 0;
00127         float old_usb2_current = 0;
00128     #endif
00129         
00130     epd.empty();
00131     epd.writeFull(); 
00132         
00133     while(true){   
00134         
00135         adc1_mean += diffADCs[0]->read();
00136         adc2_mean += 1; //usb1.read();
00137         adc3_mean += 1; //usb2.read();
00138         count ++;
00139                             
00140         if (count == NUM_OF_MEASUREMENTS){ 
00141      
00142             adc1_mean /= NUM_OF_MEASUREMENTS;
00143             adc2_mean /= NUM_OF_MEASUREMENTS;
00144             adc3_mean /= NUM_OF_MEASUREMENTS;
00145             count = 0;
00146                 
00147             //voltage[i] = ((float)(diffADCs[i]->read()))*(3.3f/8192.0);    // Convert raw data into voltage
00148             battery_voltage = ((float)(diffADCs[0]->read()*(3.3f/8192.0)));
00149             //battery_voltage = ((float)(diffADCs[0]->read()*(3.3f/8192.0)));
00150             //battery_voltage = adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE);
00151             usb1_current = (adc2_mean*(ADC_REF_VOLTAGE/(ADC_MAX_VALUE*2))) * CURRENT_FACTOR;
00152             usb2_current = (adc3_mean*(ADC_REF_VOLTAGE/(ADC_MAX_VALUE*2))) * CURRENT_FACTOR;
00153                     
00154             battery_level = get_battery_level(battery_voltage);
00155             if(battery_level != old_battery_level || change_flag){
00156                 
00157                 old_battery_level = battery_level;
00158                 change_flag = 1;
00159                 switch(battery_level){
00160                     case(0):{
00161                             for(uint16_t x=0;x<5000;x++)
00162                                 epd.fill(BS_E[x], x);
00163                             break;
00164                             }
00165                     case(1):{
00166                             for(uint16_t x=0;x<5000;x++)
00167                                 epd.fill(BS_1[x], x);
00168                             break;
00169                             }
00170                     case(2):{
00171                             for(uint16_t x=0;x<5000;x++)
00172                                 epd.fill(BS_2[x], x);
00173                             break;
00174                             }
00175                     case(3):{
00176                             for(uint16_t x=0;x<5000;x++)
00177                                 epd.fill(BS_3[x], x);
00178                             break;
00179                             }
00180                     case(4):{
00181                             for(uint16_t x=0;x<5000;x++)
00182                                 epd.fill(BS_4[x], x);
00183                             break;
00184                             }
00185                     case(5):{
00186                             for(uint16_t x=0;x<5000;x++)
00187                                 epd.fill(BS_5[x], x);
00188                             break;
00189                             }
00190                     case(6):{
00191                             for(uint16_t x=0;x<5000;x++)
00192                                 epd.fill(charged_1[x], x);
00193                             break;
00194                             }
00195                     case(7):{
00196                             for(uint16_t x=0;x<5000;x++)
00197                                 epd.fill(charged_2[x], x);
00198                             break;
00199                             }
00200                     case(8):{
00201                             for(uint16_t x=0;x<5000;x++)
00202                                 epd.fill(charged_3[x], x);
00203                             break;
00204                             }
00205                     case(9):{
00206                             for(uint16_t x=0;x<5000;x++)
00207                                 epd.fill(charged_4[x], x);
00208                             break;
00209                             }
00210                     default: break;
00211                 }
00212                 epd.write();
00213             }
00214             
00215             usb1_current_state = get_current_level(usb1_current, usb1_old_state);
00216             usb2_current_state = get_current_level(usb2_current, usb2_old_state);
00217             
00218             if((usb1_old_state != usb1_current_state) || change_flag){
00219                 usb1_old_state = usb1_current_state;
00220                 switch(usb1_current_state){
00221                     case(NO_CURRENT_STATE):{
00222                         epd.writeString(quick_string, 25, 180, 1);  // Delete the old state
00223                         epd.writeString(low_string, 25, 180, 1);
00224                         break;
00225                     }
00226                     case(LOW_CURRENT_STATE):{
00227                         epd.writeString(quick_string, 25, 180, 1);  // Delete the old state
00228                         epd.writeString(low_string, 25, 180, 0);
00229                         break;
00230                     }
00231                     case(QUICK_CURRENT_STATE):{
00232                         epd.writeString(low_string, 25, 180, 1);    // Delete the old state
00233                         epd.writeString(quick_string, 25, 180, 0);
00234                         break;
00235                     }
00236                     default: break;
00237                 }
00238                 epd.write();                                            // Write string to the EPD
00239             }
00240             
00241             if((usb2_old_state != usb2_current_state) || change_flag){
00242                 usb2_old_state = usb2_current_state;
00243                 switch(usb2_current_state){
00244                     case(NO_CURRENT_STATE):{
00245                         epd.writeString(low_string, 135, 180, 1);       // Delete the old state
00246                         epd.writeString(quick_string, 135, 180, 1);     // Delete the old state
00247                         break;
00248                     }
00249                     case(LOW_CURRENT_STATE):{
00250                         epd.writeString(quick_string, 135, 180, 1);     // Delete the old state
00251                         epd.writeString(low_string, 135, 180, 0);
00252                         break;
00253                     }
00254                     case(QUICK_CURRENT_STATE):{
00255                         epd.writeString(low_string, 135, 180, 1);       // Delete the old state
00256                         epd.writeString(quick_string, 135, 180, 0);
00257                         break;
00258                     }
00259                     default: break;
00260                 }
00261                 epd.write();                                            // Write string to the EPD
00262                 change_flag = 0;
00263             }
00264 
00265             
00266             #if DEBUG    
00267                 // Print voltage and current values in debug mode
00268                 sprintf(buffer, "Battery: %5.5fV", old_battery_voltage);     // Create a string
00269                 epd.writeString(buffer,25,95,1);                        // Write new data to the buffer    
00270                 epd.write();                                            // Write string to the EPD
00271                 old_battery_voltage = battery_voltage;
00272                 
00273                 sprintf(buffer, "Battery: %5.5fV", battery_voltage);     // Create a string
00274                 epd.writeString(buffer,25,95,0);                        // Write new data to the buffer    
00275 
00276                 sprintf(buffer, "USB1: %5.5f", old_usb1_current);           // Create a string
00277                 epd.writeString(buffer,5,190,1);                        // Write new data to the buffer    
00278                 old_usb1_current = usb1_current;
00279                 sprintf(buffer, "USB1: %5.5f", usb1_current);           // Create a string
00280                 epd.writeString(buffer,5,190,0);                        // Write new data to the buffer    
00281     
00282                 sprintf(buffer, "USB2: %5.5fA", old_usb2_current);           // Create a string
00283                 epd.writeString(buffer,105,190,1);                        // Write new data to the buffer    
00284                 old_usb2_current = usb2_current;
00285                 sprintf(buffer, "USB2: %5.5fA", usb2_current);           // Create a string
00286                 epd.writeString(buffer,105,190,0);                       // Write new data to the buffer    
00287                 
00288                 epd.write(); 
00289             #endif
00290             
00291             adc1_mean = 0;
00292             adc2_mean = 0;
00293             adc3_mean = 0;
00294         }
00295     }
00296 }