u-blox / Mbed OS example-battery-gauge-bq35100

Dependencies:   battery-gauge-bq35100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 u-blox
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "battery_gauge_bq35100.h"
00019 
00020 // LEDs
00021 DigitalOut ledRed(LED1, 1);
00022 DigitalOut ledGreen(LED2, 1);
00023 
00024 /* This example program for the u-blox C030 primary battery
00025  * shield instantiates the BQ35100 battery gauge and performs
00026  * a few example calls to the battery gauge API.  Progress may
00027  * be monitored with a serial terminal running at 9600 baud.
00028  * The LED on the C030 board will turn green when this program
00029  * is operating correctly and will turn red if there is a failure.
00030  */
00031 
00032 int main()
00033 {
00034     I2C i2C(I2C_SDA, I2C_SCL);
00035     BatteryGaugeBq35100 gauge;
00036     int32_t reading;
00037     uint32_t capacity;
00038     bool stop = false;
00039 
00040     printf ("Starting up...\n");
00041     if (gauge.init(&i2C) && gauge.setDesignCapacity(6500)) {        
00042         printf ("BQ35100 battery gauge chip is initialised assuming a 6500 mAh battery.\n");
00043         while (!stop) {
00044             if (!gauge.isGaugeEnabled()) {
00045                 gauge.enableGauge();
00046             }
00047             if (gauge.isGaugeEnabled()) {
00048                 // All is good, gauging is enabled, take a few readings
00049                 ledGreen = 0;
00050                 ledRed = 1;
00051                 
00052                 if (gauge.getRemainingPercentage(&reading)) {
00053                     printf("Remaining battery percentage: %d%%.\n", (int) reading);
00054                 } else {
00055                     ledGreen = 1;
00056                     ledRed = 0;
00057                     wait_ms(1000);
00058                 }
00059                 
00060                 if (gauge.getRemainingCapacity(&capacity)) {
00061                     printf("Remaining battery capacity: %.3f Ah.\n", ((float) capacity) / 1000);
00062                 } else {
00063                     ledGreen = 1;
00064                     ledRed = 0;
00065                     wait_ms(1000);
00066                 }
00067                 
00068                 if (gauge.getVoltage(&reading)) {
00069                     printf("Battery voltage: %.3f V.\n", ((float) reading) / 1000);
00070                 } else {
00071                     ledGreen = 1;
00072                     ledRed = 0;
00073                     wait_ms(1000);
00074                 }
00075                 
00076                 if (gauge.getCurrent(&reading)) {
00077                     printf("Current drawn from battery: %.3f A.\n", ((float) reading) / 1000);
00078                 } else {
00079                     ledGreen = 1;
00080                     ledRed = 0;
00081                     wait_ms(1000);
00082                 }
00083                 
00084                 if (gauge.getTemperature(&reading)) {
00085                     printf("BQ27441 chip temperature: %d C.\n", (int) reading);
00086                 } else {
00087                     ledGreen = 1;
00088                     ledRed = 0;
00089                     wait_ms(1000);
00090                 }
00091                 
00092             } else {
00093                 printf("Battery gauging could not be enabled.\n");
00094                 stop = true;
00095             }
00096             wait_ms(5000);
00097         }
00098     }
00099     
00100     ledGreen = 1;
00101     ledRed = 0;
00102     printf("Should never get here.\n");
00103     MBED_ASSERT(false);
00104 }
00105 
00106 // End Of File