Example program to demonstrate the use of the BatteryGaugeBQ27441 class.

Dependencies:   battery-gauge-bq27441

Committer:
Bilal Qamar
Date:
Mon Apr 09 17:03:36 2018 +0500
Revision:
8:ea7defa37e8a
Parent:
7:9ccf046018f7
Updated library to the latest commit resolving current=0 issue

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RobMeades 0:bb556c2ee1f7 1 /* mbed Microcontroller Library
RobMeades 0:bb556c2ee1f7 2 * Copyright (c) 2017 u-blox
RobMeades 0:bb556c2ee1f7 3 *
RobMeades 0:bb556c2ee1f7 4 * Licensed under the Apache License, Version 2.0 (the "License");
RobMeades 0:bb556c2ee1f7 5 * you may not use this file except in compliance with the License.
RobMeades 0:bb556c2ee1f7 6 * You may obtain a copy of the License at
RobMeades 0:bb556c2ee1f7 7 *
RobMeades 0:bb556c2ee1f7 8 * http://www.apache.org/licenses/LICENSE-2.0
RobMeades 0:bb556c2ee1f7 9 *
RobMeades 0:bb556c2ee1f7 10 * Unless required by applicable law or agreed to in writing, software
RobMeades 0:bb556c2ee1f7 11 * distributed under the License is distributed on an "AS IS" BASIS,
RobMeades 0:bb556c2ee1f7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
RobMeades 0:bb556c2ee1f7 13 * See the License for the specific language governing permissions and
RobMeades 0:bb556c2ee1f7 14 * limitations under the License.
RobMeades 0:bb556c2ee1f7 15 */
RobMeades 0:bb556c2ee1f7 16
RobMeades 0:bb556c2ee1f7 17 #include "mbed.h"
RobMeades 0:bb556c2ee1f7 18 #include "battery_gauge_bq27441.h"
RobMeades 0:bb556c2ee1f7 19
RobMeades 0:bb556c2ee1f7 20 // LEDs
RobMeades 0:bb556c2ee1f7 21 DigitalOut ledRed(LED1, 1);
RobMeades 0:bb556c2ee1f7 22 DigitalOut ledGreen(LED2, 1);
RobMeades 0:bb556c2ee1f7 23
RobMeades 0:bb556c2ee1f7 24 /* This example program for the u-blox C030 board instantiates
rob.meades@u-blox.com 1:7af88dc66286 25 * the BQ27441 battery gauge and performs a few example calls to
rob.meades@u-blox.com 1:7af88dc66286 26 * the battery gauge API. Progress may be monitored with a
rob.meades@u-blox.com 1:7af88dc66286 27 * serial terminal running at 9600 baud. The LED on the C030
rob.meades@u-blox.com 1:7af88dc66286 28 * board will turn green when this program is operating correctly
rob.meades@u-blox.com 1:7af88dc66286 29 * and will turn red if there is a failure.
RobMeades 0:bb556c2ee1f7 30 */
RobMeades 0:bb556c2ee1f7 31
RobMeades 0:bb556c2ee1f7 32 int main()
RobMeades 0:bb556c2ee1f7 33 {
rob.meades@u-blox.com 1:7af88dc66286 34 I2C i2C(I2C_SDA_B, I2C_SCL_B);
RobMeades 0:bb556c2ee1f7 35 BatteryGaugeBq27441 gauge;
rob.meades@u-blox.com 1:7af88dc66286 36 int32_t reading;
rob.meades@u-blox.com 1:7af88dc66286 37 bool stop = false;
RobMeades 0:bb556c2ee1f7 38
rob.meades@u-blox.com 1:7af88dc66286 39 printf ("Starting up...\n");
rob.meades@u-blox.com 1:7af88dc66286 40 if (gauge.init(&i2C)) {
rob.meades@u-blox.com 6:61df9e6e2871 41 printf ("BQ27441 battery gauge chip is initialised.\n");
rob.meades@u-blox.com 6:61df9e6e2871 42 #ifdef TARGET_UBLOX_C030
rob.meades@u-blox.com 6:61df9e6e2871 43 if (gauge.disableBatteryDetect()) {
rob.meades@u-blox.com 6:61df9e6e2871 44 printf ("Battery detection input disabled (it is not connected on C030).\n");
rob.meades@u-blox.com 6:61df9e6e2871 45 }
rob.meades@u-blox.com 6:61df9e6e2871 46 #endif
rob.meades@u-blox.com 1:7af88dc66286 47 while (!stop) {
rob.meades@u-blox.com 1:7af88dc66286 48 if (gauge.isBatteryDetected()) {
rob.meades@u-blox.com 1:7af88dc66286 49 if (!gauge.isGaugeEnabled()) {
rob.meades@u-blox.com 1:7af88dc66286 50 gauge.enableGauge();
rob.meades@u-blox.com 1:7af88dc66286 51 }
rob.meades@u-blox.com 1:7af88dc66286 52 if (gauge.isGaugeEnabled()) {
rob.meades@u-blox.com 1:7af88dc66286 53 // All is good, gauging is enabled, take a few readings
rob.meades@u-blox.com 1:7af88dc66286 54 ledGreen = 0;
rob.meades@u-blox.com 1:7af88dc66286 55 ledRed = 1;
rob.meades@u-blox.com 1:7af88dc66286 56
rob.meades@u-blox.com 1:7af88dc66286 57 if (gauge.getRemainingPercentage(&reading)) {
rob.meades@u-blox.com 1:7af88dc66286 58 printf("Remaining battery percentage: %d%%.\n", (int) reading);
rob.meades@u-blox.com 1:7af88dc66286 59 } else {
rob.meades@u-blox.com 1:7af88dc66286 60 ledGreen = 1;
rob.meades@u-blox.com 1:7af88dc66286 61 ledRed = 0;
rob.meades@u-blox.com 1:7af88dc66286 62 wait_ms(1000);
rob.meades@u-blox.com 1:7af88dc66286 63 }
rob.meades@u-blox.com 1:7af88dc66286 64
rob.meades@u-blox.com 1:7af88dc66286 65 if (gauge.getRemainingCapacity(&reading)) {
rob.meades@u-blox.com 1:7af88dc66286 66 printf("Remaining battery capacity: %.3f Ah.\n", ((float) reading) / 1000);
rob.meades@u-blox.com 1:7af88dc66286 67 } else {
rob.meades@u-blox.com 1:7af88dc66286 68 ledGreen = 1;
rob.meades@u-blox.com 1:7af88dc66286 69 ledRed = 0;
rob.meades@u-blox.com 1:7af88dc66286 70 wait_ms(1000);
rob.meades@u-blox.com 1:7af88dc66286 71 }
rob.meades@u-blox.com 1:7af88dc66286 72
rob.meades@u-blox.com 1:7af88dc66286 73 if (gauge.getVoltage(&reading)) {
rob.meades@u-blox.com 1:7af88dc66286 74 printf("Battery voltage: %.3f V.\n", ((float) reading) / 1000);
rob.meades@u-blox.com 1:7af88dc66286 75 } else {
rob.meades@u-blox.com 1:7af88dc66286 76 ledGreen = 1;
rob.meades@u-blox.com 1:7af88dc66286 77 ledRed = 0;
rob.meades@u-blox.com 1:7af88dc66286 78 wait_ms(1000);
rob.meades@u-blox.com 1:7af88dc66286 79 }
rob.meades@u-blox.com 1:7af88dc66286 80
rob.meades@u-blox.com 1:7af88dc66286 81 if (gauge.getCurrent(&reading)) {
rob.meades@u-blox.com 1:7af88dc66286 82 printf("Current drawn from battery: %.3f A.\n", ((float) reading) / 1000);
rob.meades@u-blox.com 1:7af88dc66286 83 } else {
rob.meades@u-blox.com 1:7af88dc66286 84 ledGreen = 1;
rob.meades@u-blox.com 1:7af88dc66286 85 ledRed = 0;
rob.meades@u-blox.com 1:7af88dc66286 86 wait_ms(1000);
rob.meades@u-blox.com 1:7af88dc66286 87 }
rob.meades@u-blox.com 1:7af88dc66286 88
rob.meades@u-blox.com 1:7af88dc66286 89 if (gauge.getTemperature(&reading)) {
rob.meades@u-blox.com 1:7af88dc66286 90 printf("BQ27441 chip temperature: %d C.\n", (int) reading);
rob.meades@u-blox.com 1:7af88dc66286 91 } else {
rob.meades@u-blox.com 1:7af88dc66286 92 ledGreen = 1;
rob.meades@u-blox.com 1:7af88dc66286 93 ledRed = 0;
rob.meades@u-blox.com 1:7af88dc66286 94 wait_ms(1000);
rob.meades@u-blox.com 1:7af88dc66286 95 }
Bilal Qamar 8:ea7defa37e8a 96
Bilal Qamar 8:ea7defa37e8a 97 if (gauge.getPower(&reading)) {
Bilal Qamar 8:ea7defa37e8a 98 printf("Power consumption : %.3f W.\n", ((float) reading) / 1000);
Bilal Qamar 8:ea7defa37e8a 99 } else {
Bilal Qamar 8:ea7defa37e8a 100 ledGreen = 1;
Bilal Qamar 8:ea7defa37e8a 101 ledRed = 0;
Bilal Qamar 8:ea7defa37e8a 102 wait_ms(1000);
Bilal Qamar 8:ea7defa37e8a 103 }
rob.meades@u-blox.com 1:7af88dc66286 104
rob.meades@u-blox.com 1:7af88dc66286 105 } else {
rob.meades@u-blox.com 1:7af88dc66286 106 printf("Battery gauge could not be enabled.\n");
rob.meades@u-blox.com 1:7af88dc66286 107 stop = true;
rob.meades@u-blox.com 1:7af88dc66286 108 }
rob.meades@u-blox.com 1:7af88dc66286 109 } else {
rob.meades@u-blox.com 1:7af88dc66286 110 ledGreen = 1;
rob.meades@u-blox.com 1:7af88dc66286 111 ledRed = 0;
rob.meades@u-blox.com 1:7af88dc66286 112 printf("Battery not detected.\n");
rob.meades@u-blox.com 1:7af88dc66286 113 }
rob.meades@u-blox.com 1:7af88dc66286 114
rob.meades@u-blox.com 1:7af88dc66286 115 wait_ms(5000);
rob.meades@u-blox.com 1:7af88dc66286 116 }
RobMeades 0:bb556c2ee1f7 117 }
rob.meades@u-blox.com 1:7af88dc66286 118
rob.meades@u-blox.com 1:7af88dc66286 119 ledGreen = 1;
rob.meades@u-blox.com 1:7af88dc66286 120 ledRed = 0;
rob.meades@u-blox.com 7:9ccf046018f7 121 printf("Should never get here: do you have a battery connected?\n");
rob.meades@u-blox.com 1:7af88dc66286 122 MBED_ASSERT(false);
RobMeades 0:bb556c2ee1f7 123 }
RobMeades 0:bb556c2ee1f7 124
RobMeades 0:bb556c2ee1f7 125 // End Of File