Example program to demonstrate the use of the BatteryGaugeBQ27441 class.

Dependencies:   battery-gauge-bq27441

Revision:
1:7af88dc66286
Parent:
0:bb556c2ee1f7
Child:
6:61df9e6e2871
--- a/main.cpp	Tue Jun 06 08:28:41 2017 +0000
+++ b/main.cpp	Tue Jun 06 15:59:11 2017 +0100
@@ -22,23 +22,91 @@
 DigitalOut ledGreen(LED2, 1);
 
 /* This example program for the u-blox C030 board instantiates
- * the BQ27441 battery gauge interface and performs a few
- * example calls to the battery gauge API.  Progress may be
- * monitored with a serial terminal running at 9600 baud.  The
- * LED on the C030 board will turn green when this program is
- * operating correctly and will turn red if there is a failure.
+ * the BQ27441 battery gauge and performs a few example calls to
+ * the battery gauge API.  Progress may be monitored with a
+ * serial terminal running at 9600 baud.  The LED on the C030
+ * board will turn green when this program is operating correctly
+ * and will turn red if there is a failure.
  */
 
 int main()
 {
+    I2C i2C(I2C_SDA_B, I2C_SCL_B);
     BatteryGaugeBq27441 gauge;
-    I2C i2C(I2C_SDA_B, I2C_SCL_B);
+    int32_t reading;
+    bool stop = false;
 
-    gauge.init(&i2C);
-    while (1) {
-        ledGreen = 0;
-        // Do something
+    printf ("Starting up...\n");
+    if (gauge.init(&i2C)) {        
+        printf ("BQ27441 battery gauge chip is initialised.\n");        
+        while (!stop) {
+            if (gauge.isBatteryDetected()) {
+                if (!gauge.isGaugeEnabled()) {
+                    gauge.enableGauge();
+                }
+                if (gauge.isGaugeEnabled()) {
+                    // All is good, gauging is enabled, take a few readings
+                    ledGreen = 0;
+                    ledRed = 1;
+                    
+                    if (gauge.getRemainingPercentage(&reading)) {
+                        printf("Remaining battery percentage: %d%%.\n", (int) reading);
+                    } else {
+                        ledGreen = 1;
+                        ledRed = 0;
+                        wait_ms(1000);
+                    }
+                    
+                    if (gauge.getRemainingCapacity(&reading)) {
+                        printf("Remaining battery capacity: %.3f Ah.\n", ((float) reading) / 1000);
+                    } else {
+                        ledGreen = 1;
+                        ledRed = 0;
+                        wait_ms(1000);
+                    }
+                    
+                    if (gauge.getVoltage(&reading)) {
+                        printf("Battery voltage: %.3f V.\n", ((float) reading) / 1000);
+                    } else {
+                        ledGreen = 1;
+                        ledRed = 0;
+                        wait_ms(1000);
+                    }
+                    
+                    if (gauge.getCurrent(&reading)) {
+                        printf("Current drawn from battery: %.3f A.\n", ((float) reading) / 1000);
+                    } else {
+                        ledGreen = 1;
+                        ledRed = 0;
+                        wait_ms(1000);
+                    }
+                    
+                    if (gauge.getTemperature(&reading)) {
+                        printf("BQ27441 chip temperature: %d C.\n", (int) reading);
+                    } else {
+                        ledGreen = 1;
+                        ledRed = 0;
+                        wait_ms(1000);
+                    }
+                    
+                } else {
+                    printf("Battery gauge could not be enabled.\n");
+                    stop = true;
+                }
+            } else {
+                ledGreen = 1;
+                ledRed = 0;
+                printf("Battery not detected.\n");
+            }
+                
+            wait_ms(5000);
+        }
     }
+    
+    ledGreen = 1;
+    ledRed = 0;
+    printf("Should never get here.\n");
+    MBED_ASSERT(false);
 }
 
 // End Of File
\ No newline at end of file