Example program to demonstrate the use of the BatteryGaugeBQ35100 class on a C030 board.
Dependencies: battery-gauge-bq35100
main.cpp@1:390b13287bb7, 2017-11-10 (annotated)
- Committer:
- RobMeades
- Date:
- Fri Nov 10 17:31:58 2017 +0000
- Revision:
- 1:390b13287bb7
- Parent:
- 0:921ceae3cd78
Example to go with latest BQ35100 driver code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobMeades | 0:921ceae3cd78 | 1 | /* mbed Microcontroller Library |
RobMeades | 0:921ceae3cd78 | 2 | * Copyright (c) 2017 u-blox |
RobMeades | 0:921ceae3cd78 | 3 | * |
RobMeades | 0:921ceae3cd78 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
RobMeades | 0:921ceae3cd78 | 5 | * you may not use this file except in compliance with the License. |
RobMeades | 0:921ceae3cd78 | 6 | * You may obtain a copy of the License at |
RobMeades | 0:921ceae3cd78 | 7 | * |
RobMeades | 0:921ceae3cd78 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
RobMeades | 0:921ceae3cd78 | 9 | * |
RobMeades | 0:921ceae3cd78 | 10 | * Unless required by applicable law or agreed to in writing, software |
RobMeades | 0:921ceae3cd78 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
RobMeades | 0:921ceae3cd78 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
RobMeades | 0:921ceae3cd78 | 13 | * See the License for the specific language governing permissions and |
RobMeades | 0:921ceae3cd78 | 14 | * limitations under the License. |
RobMeades | 0:921ceae3cd78 | 15 | */ |
RobMeades | 0:921ceae3cd78 | 16 | |
RobMeades | 0:921ceae3cd78 | 17 | #include "mbed.h" |
RobMeades | 0:921ceae3cd78 | 18 | #include "battery_gauge_bq35100.h" |
RobMeades | 0:921ceae3cd78 | 19 | |
RobMeades | 0:921ceae3cd78 | 20 | // LEDs |
RobMeades | 0:921ceae3cd78 | 21 | DigitalOut ledRed(LED1, 1); |
RobMeades | 0:921ceae3cd78 | 22 | DigitalOut ledGreen(LED2, 1); |
RobMeades | 0:921ceae3cd78 | 23 | |
RobMeades | 0:921ceae3cd78 | 24 | /* This example program for the u-blox C030 primary battery |
RobMeades | 0:921ceae3cd78 | 25 | * shield instantiates the BQ35100 battery gauge and performs |
RobMeades | 0:921ceae3cd78 | 26 | * a few example calls to the battery gauge API. Progress may |
RobMeades | 0:921ceae3cd78 | 27 | * be monitored with a serial terminal running at 9600 baud. |
RobMeades | 0:921ceae3cd78 | 28 | * The LED on the C030 board will turn green when this program |
RobMeades | 0:921ceae3cd78 | 29 | * is operating correctly and will turn red if there is a failure. |
RobMeades | 0:921ceae3cd78 | 30 | */ |
RobMeades | 0:921ceae3cd78 | 31 | |
RobMeades | 0:921ceae3cd78 | 32 | int main() |
RobMeades | 0:921ceae3cd78 | 33 | { |
RobMeades | 0:921ceae3cd78 | 34 | I2C i2C(I2C_SDA, I2C_SCL); |
RobMeades | 0:921ceae3cd78 | 35 | BatteryGaugeBq35100 gauge; |
RobMeades | 0:921ceae3cd78 | 36 | int32_t reading; |
RobMeades | 1:390b13287bb7 | 37 | uint32_t capacity; |
RobMeades | 0:921ceae3cd78 | 38 | bool stop = false; |
RobMeades | 0:921ceae3cd78 | 39 | |
RobMeades | 0:921ceae3cd78 | 40 | printf ("Starting up...\n"); |
RobMeades | 1:390b13287bb7 | 41 | if (gauge.init(&i2C) && gauge.setDesignCapacity(6500)) { |
RobMeades | 1:390b13287bb7 | 42 | printf ("BQ35100 battery gauge chip is initialised assuming a 6500 mAh battery.\n"); |
RobMeades | 1:390b13287bb7 | 43 | while (!stop) { |
RobMeades | 1:390b13287bb7 | 44 | if (!gauge.isGaugeEnabled()) { |
RobMeades | 1:390b13287bb7 | 45 | gauge.enableGauge(); |
RobMeades | 1:390b13287bb7 | 46 | } |
RobMeades | 1:390b13287bb7 | 47 | if (gauge.isGaugeEnabled()) { |
RobMeades | 1:390b13287bb7 | 48 | // All is good, gauging is enabled, take a few readings |
RobMeades | 1:390b13287bb7 | 49 | ledGreen = 0; |
RobMeades | 1:390b13287bb7 | 50 | ledRed = 1; |
RobMeades | 1:390b13287bb7 | 51 | |
RobMeades | 1:390b13287bb7 | 52 | if (gauge.getRemainingPercentage(&reading)) { |
RobMeades | 1:390b13287bb7 | 53 | printf("Remaining battery percentage: %d%%.\n", (int) reading); |
RobMeades | 1:390b13287bb7 | 54 | } else { |
RobMeades | 1:390b13287bb7 | 55 | ledGreen = 1; |
RobMeades | 1:390b13287bb7 | 56 | ledRed = 0; |
RobMeades | 1:390b13287bb7 | 57 | wait_ms(1000); |
RobMeades | 1:390b13287bb7 | 58 | } |
RobMeades | 1:390b13287bb7 | 59 | |
RobMeades | 1:390b13287bb7 | 60 | if (gauge.getRemainingCapacity(&capacity)) { |
RobMeades | 1:390b13287bb7 | 61 | printf("Remaining battery capacity: %.3f Ah.\n", ((float) capacity) / 1000); |
RobMeades | 1:390b13287bb7 | 62 | } else { |
RobMeades | 1:390b13287bb7 | 63 | ledGreen = 1; |
RobMeades | 1:390b13287bb7 | 64 | ledRed = 0; |
RobMeades | 1:390b13287bb7 | 65 | wait_ms(1000); |
RobMeades | 1:390b13287bb7 | 66 | } |
RobMeades | 1:390b13287bb7 | 67 | |
RobMeades | 1:390b13287bb7 | 68 | if (gauge.getVoltage(&reading)) { |
RobMeades | 1:390b13287bb7 | 69 | printf("Battery voltage: %.3f V.\n", ((float) reading) / 1000); |
RobMeades | 1:390b13287bb7 | 70 | } else { |
RobMeades | 1:390b13287bb7 | 71 | ledGreen = 1; |
RobMeades | 1:390b13287bb7 | 72 | ledRed = 0; |
RobMeades | 1:390b13287bb7 | 73 | wait_ms(1000); |
RobMeades | 1:390b13287bb7 | 74 | } |
RobMeades | 1:390b13287bb7 | 75 | |
RobMeades | 1:390b13287bb7 | 76 | if (gauge.getCurrent(&reading)) { |
RobMeades | 1:390b13287bb7 | 77 | printf("Current drawn from battery: %.3f A.\n", ((float) reading) / 1000); |
RobMeades | 1:390b13287bb7 | 78 | } else { |
RobMeades | 1:390b13287bb7 | 79 | ledGreen = 1; |
RobMeades | 1:390b13287bb7 | 80 | ledRed = 0; |
RobMeades | 1:390b13287bb7 | 81 | wait_ms(1000); |
RobMeades | 1:390b13287bb7 | 82 | } |
RobMeades | 1:390b13287bb7 | 83 | |
RobMeades | 1:390b13287bb7 | 84 | if (gauge.getTemperature(&reading)) { |
RobMeades | 1:390b13287bb7 | 85 | printf("BQ27441 chip temperature: %d C.\n", (int) reading); |
RobMeades | 1:390b13287bb7 | 86 | } else { |
RobMeades | 1:390b13287bb7 | 87 | ledGreen = 1; |
RobMeades | 1:390b13287bb7 | 88 | ledRed = 0; |
RobMeades | 1:390b13287bb7 | 89 | wait_ms(1000); |
RobMeades | 1:390b13287bb7 | 90 | } |
RobMeades | 1:390b13287bb7 | 91 | |
RobMeades | 1:390b13287bb7 | 92 | } else { |
RobMeades | 1:390b13287bb7 | 93 | printf("Battery gauging could not be enabled.\n"); |
RobMeades | 1:390b13287bb7 | 94 | stop = true; |
RobMeades | 1:390b13287bb7 | 95 | } |
RobMeades | 1:390b13287bb7 | 96 | wait_ms(5000); |
RobMeades | 1:390b13287bb7 | 97 | } |
RobMeades | 0:921ceae3cd78 | 98 | } |
RobMeades | 0:921ceae3cd78 | 99 | |
RobMeades | 0:921ceae3cd78 | 100 | ledGreen = 1; |
RobMeades | 0:921ceae3cd78 | 101 | ledRed = 0; |
RobMeades | 0:921ceae3cd78 | 102 | printf("Should never get here.\n"); |
RobMeades | 0:921ceae3cd78 | 103 | MBED_ASSERT(false); |
RobMeades | 0:921ceae3cd78 | 104 | } |
RobMeades | 0:921ceae3cd78 | 105 | |
RobMeades | 0:921ceae3cd78 | 106 | // End Of File |