Example program to demonstrate the use of the BatteryGaugeBQ27441 class.
Dependencies: battery-gauge-bq27441
main.cpp@1:7af88dc66286, 2017-06-06 (annotated)
- Committer:
- rob.meades@u-blox.com
- Date:
- Tue Jun 06 15:59:11 2017 +0100
- Revision:
- 1:7af88dc66286
- Parent:
- 0:bb556c2ee1f7
- Child:
- 6:61df9e6e2871
Flesh out the example properly.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:7af88dc66286 | 41 | printf ("BQ27441 battery gauge chip is initialised.\n"); |
rob.meades@u-blox.com | 1:7af88dc66286 | 42 | while (!stop) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 43 | if (gauge.isBatteryDetected()) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 44 | if (!gauge.isGaugeEnabled()) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 45 | gauge.enableGauge(); |
rob.meades@u-blox.com | 1:7af88dc66286 | 46 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 47 | if (gauge.isGaugeEnabled()) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 48 | // All is good, gauging is enabled, take a few readings |
rob.meades@u-blox.com | 1:7af88dc66286 | 49 | ledGreen = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 50 | ledRed = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 51 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 52 | if (gauge.getRemainingPercentage(&reading)) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 53 | printf("Remaining battery percentage: %d%%.\n", (int) reading); |
rob.meades@u-blox.com | 1:7af88dc66286 | 54 | } else { |
rob.meades@u-blox.com | 1:7af88dc66286 | 55 | ledGreen = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 56 | ledRed = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 57 | wait_ms(1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 58 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 59 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 60 | if (gauge.getRemainingCapacity(&reading)) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 61 | printf("Remaining battery capacity: %.3f Ah.\n", ((float) reading) / 1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 62 | } else { |
rob.meades@u-blox.com | 1:7af88dc66286 | 63 | ledGreen = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 64 | ledRed = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 65 | wait_ms(1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 66 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 67 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 68 | if (gauge.getVoltage(&reading)) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 69 | printf("Battery voltage: %.3f V.\n", ((float) reading) / 1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 70 | } else { |
rob.meades@u-blox.com | 1:7af88dc66286 | 71 | ledGreen = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 72 | ledRed = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 73 | wait_ms(1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 74 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 75 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 76 | if (gauge.getCurrent(&reading)) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 77 | printf("Current drawn from battery: %.3f A.\n", ((float) reading) / 1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 78 | } else { |
rob.meades@u-blox.com | 1:7af88dc66286 | 79 | ledGreen = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 80 | ledRed = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 81 | wait_ms(1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 82 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 83 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 84 | if (gauge.getTemperature(&reading)) { |
rob.meades@u-blox.com | 1:7af88dc66286 | 85 | printf("BQ27441 chip temperature: %d C.\n", (int) reading); |
rob.meades@u-blox.com | 1:7af88dc66286 | 86 | } else { |
rob.meades@u-blox.com | 1:7af88dc66286 | 87 | ledGreen = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 88 | ledRed = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 89 | wait_ms(1000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 90 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 91 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 92 | } else { |
rob.meades@u-blox.com | 1:7af88dc66286 | 93 | printf("Battery gauge could not be enabled.\n"); |
rob.meades@u-blox.com | 1:7af88dc66286 | 94 | stop = true; |
rob.meades@u-blox.com | 1:7af88dc66286 | 95 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 96 | } else { |
rob.meades@u-blox.com | 1:7af88dc66286 | 97 | ledGreen = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 98 | ledRed = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 99 | printf("Battery not detected.\n"); |
rob.meades@u-blox.com | 1:7af88dc66286 | 100 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 101 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 102 | wait_ms(5000); |
rob.meades@u-blox.com | 1:7af88dc66286 | 103 | } |
RobMeades | 0:bb556c2ee1f7 | 104 | } |
rob.meades@u-blox.com | 1:7af88dc66286 | 105 | |
rob.meades@u-blox.com | 1:7af88dc66286 | 106 | ledGreen = 1; |
rob.meades@u-blox.com | 1:7af88dc66286 | 107 | ledRed = 0; |
rob.meades@u-blox.com | 1:7af88dc66286 | 108 | printf("Should never get here.\n"); |
rob.meades@u-blox.com | 1:7af88dc66286 | 109 | MBED_ASSERT(false); |
RobMeades | 0:bb556c2ee1f7 | 110 | } |
RobMeades | 0:bb556c2ee1f7 | 111 | |
RobMeades | 0:bb556c2ee1f7 | 112 | // End Of File |