├── .gitignore ├── library.properties ├── .github ├── workflows │ └── githubci.yml ├── PULL_REQUEST_TEMPLATE.md └── ISSUE_TEMPLATE.md ├── examples └── LC709203F_demo │ └── LC709203F_demo.ino ├── license.txt ├── Adafruit_LC709203F.h ├── code-of-conduct.md └── Adafruit_LC709203F.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | html/ 2 | Doxyfile 3 | .vscode/ 4 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit LC709203F 2 | version=1.3.4 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Arduino library for the LC709203F battery monitors in the Adafruit shop 6 | paragraph=Arduino library for the LC709203F battery monitors in the Adafruit shop 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_LC709203F 9 | architectures=* 10 | depends=Adafruit BusIO 11 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit LC709203F Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /examples/LC709203F_demo/LC709203F_demo.ino: -------------------------------------------------------------------------------- 1 | #include "Adafruit_LC709203F.h" 2 | 3 | Adafruit_LC709203F lc; 4 | 5 | void setup() { 6 | Serial.begin(115200); 7 | delay(10); 8 | Serial.println("\nAdafruit LC709203F demo"); 9 | 10 | // For the Feather ESP32-S2, we need to enable I2C power first! 11 | // this section can be deleted for other boards 12 | #if defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) 13 | // turn on the I2C power by setting pin to opposite of 'rest state' 14 | pinMode(PIN_I2C_POWER, INPUT); 15 | delay(1); 16 | bool polarity = digitalRead(PIN_I2C_POWER); 17 | pinMode(PIN_I2C_POWER, OUTPUT); 18 | digitalWrite(PIN_I2C_POWER, !polarity); 19 | #endif 20 | 21 | if (!lc.begin()) { 22 | Serial.println(F("Couldnt find Adafruit LC709203F?\nMake sure a battery is plugged in!")); 23 | while (1) delay(10); 24 | } 25 | Serial.println(F("Found LC709203F")); 26 | Serial.print("Version: 0x"); Serial.println(lc.getICversion(), HEX); 27 | 28 | lc.setThermistorB(3950); 29 | Serial.print("Thermistor B = "); Serial.println(lc.getThermistorB()); 30 | 31 | lc.setPackSize(LC709203F_APA_500MAH); 32 | 33 | lc.setAlarmVoltage(3.8); 34 | } 35 | 36 | void loop() { 37 | Serial.print("Batt_Voltage:"); 38 | Serial.print(lc.cellVoltage(), 3); 39 | Serial.print("\t"); 40 | Serial.print("Batt_Percent:"); 41 | Serial.print(lc.cellPercent(), 1); 42 | Serial.print("\t"); 43 | Serial.print("Batt_Temp:"); 44 | Serial.println(lc.getCellTemperature(), 1); 45 | 46 | delay(2000); // dont query too often! 47 | } 48 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2019 Limor Fried (Adafruit Industries) 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holders nor the 14 | names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /Adafruit_LC709203F.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_LC709203F.h 3 | * 4 | * I2C Driver for the Adafruit LC709203F Battery Monitor 5 | * 6 | * This is a library for the Adafruit LC709203F breakout: 7 | * https://www.adafruit.com/products/xxxx 8 | * 9 | * Adafruit invests time and resources providing this open source code, 10 | * please support Adafruit and open-source hardware by purchasing products from 11 | * Adafruit! 12 | * 13 | * 14 | * BSD license (see license.txt) 15 | */ 16 | 17 | #ifndef _ADAFRUIT_LC709203F_H 18 | #define _ADAFRUIT_LC709203F_H 19 | 20 | #include "Arduino.h" 21 | #include 22 | 23 | #define LC709203F_I2CADDR_DEFAULT 0x0B ///< LC709203F default i2c address 24 | #define LC709203F_CMD_THERMISTORB 0x06 ///< Read/write thermistor B 25 | #define LC709203F_CMD_INITRSOC 0x07 ///< Initialize RSOC calculation 26 | #define LC709203F_CMD_CELLTEMPERATURE 0x08 ///< Read/write batt temperature 27 | #define LC709203F_CMD_CELLVOLTAGE 0x09 ///< Read batt voltage 28 | #define LC709203F_CMD_APA 0x0B ///< Adjustment Pack Application 29 | #define LC709203F_CMD_RSOC 0x0D ///< Read state of charge 30 | #define LC709203F_CMD_CELLITE 0x0F ///< Read batt indicator to empty 31 | #define LC709203F_CMD_ICVERSION 0x11 ///< Read IC version 32 | #define LC709203F_CMD_BATTPROF 0x12 ///< Set the battery profile 33 | #define LC709203F_CMD_ALARMRSOC 0x13 ///< Alarm on percent threshold 34 | #define LC709203F_CMD_ALARMVOLT 0x14 ///< Alarm on voltage threshold 35 | #define LC709203F_CMD_POWERMODE 0x15 ///< Sets sleep/power mode 36 | #define LC709203F_CMD_STATUSBIT 0x16 ///< Temperature obtaining method 37 | #define LC709203F_CMD_PARAMETER 0x1A ///< Batt profile code 38 | 39 | /*! Battery temperature source */ 40 | typedef enum { 41 | LC709203F_TEMPERATURE_I2C = 0x0000, 42 | LC709203F_TEMPERATURE_THERMISTOR = 0x0001, 43 | } lc709203_tempmode_t; 44 | 45 | /*! Chip power state */ 46 | typedef enum { 47 | LC709203F_POWER_OPERATE = 0x0001, 48 | LC709203F_POWER_SLEEP = 0x0002, 49 | } lc709203_powermode_t; 50 | 51 | /*! Approx battery pack size */ 52 | typedef enum { 53 | LC709203F_APA_100MAH = 0x08, 54 | LC709203F_APA_200MAH = 0x0B, 55 | LC709203F_APA_500MAH = 0x10, 56 | LC709203F_APA_1000MAH = 0x19, 57 | LC709203F_APA_2000MAH = 0x2D, 58 | LC709203F_APA_3000MAH = 0x36, 59 | } lc709203_adjustment_t; 60 | 61 | /*! 62 | * @brief Class that stores state and functions for interacting with 63 | * the LC709203F I2C battery monitor 64 | */ 65 | class Adafruit_LC709203F { 66 | public: 67 | Adafruit_LC709203F(); 68 | ~Adafruit_LC709203F(); 69 | 70 | bool begin(TwoWire *wire = &Wire); 71 | bool initRSOC(void); 72 | 73 | bool setPowerMode(lc709203_powermode_t t); 74 | bool setPackSize(lc709203_adjustment_t apa); 75 | bool setPackAPA(uint8_t apa_value); 76 | 77 | uint16_t getICversion(void); 78 | float cellVoltage(void); 79 | float cellPercent(void); 80 | 81 | uint16_t getThermistorB(void); 82 | bool setThermistorB(uint16_t b); 83 | 84 | uint16_t getBattProfile(void); 85 | bool setBattProfile(uint16_t b); 86 | 87 | bool setTemperatureMode(lc709203_tempmode_t t); 88 | float getCellTemperature(void); 89 | 90 | bool setAlarmRSOC(uint8_t percent); 91 | bool setAlarmVoltage(float voltage); 92 | 93 | protected: 94 | Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface 95 | bool readWord(uint8_t address, uint16_t *data); 96 | bool writeWord(uint8_t command, uint16_t data); 97 | }; 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Adafruit Community Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and leaders pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level or type of 9 | experience, education, socio-economic status, nationality, personal appearance, 10 | race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | We are committed to providing a friendly, safe and welcoming environment for 15 | all. 16 | 17 | Examples of behavior that contributes to creating a positive environment 18 | include: 19 | 20 | * Be kind and courteous to others 21 | * Using welcoming and inclusive language 22 | * Being respectful of differing viewpoints and experiences 23 | * Collaborating with other community members 24 | * Gracefully accepting constructive criticism 25 | * Focusing on what is best for the community 26 | * Showing empathy towards other community members 27 | 28 | Examples of unacceptable behavior by participants include: 29 | 30 | * The use of sexualized language or imagery and sexual attention or advances 31 | * The use of inappropriate images, including in a community member's avatar 32 | * The use of inappropriate language, including in a community member's nickname 33 | * Any spamming, flaming, baiting or other attention-stealing behavior 34 | * Excessive or unwelcome helping; answering outside the scope of the question 35 | asked 36 | * Trolling, insulting/derogatory comments, and personal or political attacks 37 | * Public or private harassment 38 | * Publishing others' private information, such as a physical or electronic 39 | address, without explicit permission 40 | * Other conduct which could reasonably be considered inappropriate 41 | 42 | The goal of the standards and moderation guidelines outlined here is to build 43 | and maintain a respectful community. We ask that you don’t just aim to be 44 | "technically unimpeachable", but rather try to be your best self. 45 | 46 | We value many things beyond technical expertise, including collaboration and 47 | supporting others within our community. Providing a positive experience for 48 | other community members can have a much more significant impact than simply 49 | providing the correct answer. 50 | 51 | ## Our Responsibilities 52 | 53 | Project leaders are responsible for clarifying the standards of acceptable 54 | behavior and are expected to take appropriate and fair corrective action in 55 | response to any instances of unacceptable behavior. 56 | 57 | Project leaders have the right and responsibility to remove, edit, or 58 | reject messages, comments, commits, code, issues, and other contributions 59 | that are not aligned to this Code of Conduct, or to ban temporarily or 60 | permanently any community member for other behaviors that they deem 61 | inappropriate, threatening, offensive, or harmful. 62 | 63 | ## Moderation 64 | 65 | Instances of behaviors that violate the Adafruit Community Code of Conduct 66 | may be reported by any member of the community. Community members are 67 | encouraged to report these situations, including situations they witness 68 | involving other community members. 69 | 70 | You may report in the following ways: 71 | 72 | In any situation, you may send an email to . 73 | 74 | On the Adafruit Discord, you may send an open message from any channel 75 | to all Community Helpers by tagging @community helpers. You may also send an 76 | open message from any channel, or a direct message to @kattni#1507, 77 | @tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or 78 | @Andon#8175. 79 | 80 | Email and direct message reports will be kept confidential. 81 | 82 | In situations on Discord where the issue is particularly egregious, possibly 83 | illegal, requires immediate action, or violates the Discord terms of service, 84 | you should also report the message directly to Discord. 85 | 86 | These are the steps for upholding our community’s standards of conduct. 87 | 88 | 1. Any member of the community may report any situation that violates the 89 | Adafruit Community Code of Conduct. All reports will be reviewed and 90 | investigated. 91 | 2. If the behavior is an egregious violation, the community member who 92 | committed the violation may be banned immediately, without warning. 93 | 3. Otherwise, moderators will first respond to such behavior with a warning. 94 | 4. Moderators follow a soft "three strikes" policy - the community member may 95 | be given another chance, if they are receptive to the warning and change their 96 | behavior. 97 | 5. If the community member is unreceptive or unreasonable when warned by a 98 | moderator, or the warning goes unheeded, they may be banned for a first or 99 | second offense. Repeated offenses will result in the community member being 100 | banned. 101 | 102 | ## Scope 103 | 104 | This Code of Conduct and the enforcement policies listed above apply to all 105 | Adafruit Community venues. This includes but is not limited to any community 106 | spaces (both public and private), the entire Adafruit Discord server, and 107 | Adafruit GitHub repositories. Examples of Adafruit Community spaces include 108 | but are not limited to meet-ups, audio chats on the Adafruit Discord, or 109 | interaction at a conference. 110 | 111 | This Code of Conduct applies both within project spaces and in public spaces 112 | when an individual is representing the project or its community. As a community 113 | member, you are representing our community, and are expected to behave 114 | accordingly. 115 | 116 | ## Attribution 117 | 118 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 119 | version 1.4, available at 120 | , 121 | and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). 122 | 123 | For other projects adopting the Adafruit Community Code of 124 | Conduct, please contact the maintainers of those projects for enforcement. 125 | If you wish to use this code of conduct for your own project, consider 126 | explicitly mentioning your moderation policy or making a copy with your 127 | own moderation policy so as to avoid confusion. 128 | -------------------------------------------------------------------------------- /Adafruit_LC709203F.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_LC709203F.cpp 3 | * 4 | * @mainpage Adafruit LC709203F Battery Monitor library 5 | * 6 | * @section intro_sec Introduction 7 | * 8 | * I2C Driver for the Adafruit LC709203F Battery Monitor library 9 | * 10 | * This is a library for the Adafruit LC709203F breakout: 11 | * https://www.adafruit.com/product/4712 12 | * 13 | * Adafruit invests time and resources providing this open source code, 14 | * please support Adafruit and open-source hardware by purchasing products from 15 | * Adafruit! 16 | * 17 | * @section dependencies Dependencies 18 | * This library depends on the Adafruit BusIO library 19 | * 20 | * @section author Author 21 | * 22 | * Limor Fried (Adafruit Industries) 23 | * 24 | * @section license License 25 | * 26 | * BSD (see license.txt) 27 | * 28 | * @section HISTORY 29 | * 30 | * v1.0 - First release 31 | */ 32 | 33 | #include "Arduino.h" 34 | 35 | #include "Adafruit_LC709203F.h" 36 | 37 | static uint8_t lc709_crc8(uint8_t *data, int len); 38 | 39 | /*! 40 | * @brief Instantiates a new LC709203F class 41 | */ 42 | Adafruit_LC709203F::Adafruit_LC709203F(void) {} 43 | 44 | Adafruit_LC709203F::~Adafruit_LC709203F(void) {} 45 | 46 | /*! 47 | * @brief Sets up the hardware and initializes I2C 48 | * @param wire 49 | * The Wire object to be used for I2C connections. 50 | * @return True if initialization was successful, otherwise false. 51 | */ 52 | bool Adafruit_LC709203F::begin(TwoWire *wire) { 53 | if (i2c_dev) { 54 | delete i2c_dev; // remove old interface 55 | } 56 | 57 | i2c_dev = new Adafruit_I2CDevice(LC709203F_I2CADDR_DEFAULT, wire); 58 | 59 | if (!i2c_dev->begin()) { 60 | return false; 61 | } 62 | 63 | if (!setPowerMode(LC709203F_POWER_OPERATE)) 64 | return false; 65 | 66 | if (!setPackSize(LC709203F_APA_500MAH)) 67 | return false; 68 | 69 | /* 70 | uint16_t param ; 71 | readWord(LC709203F_CMD_PARAMETER, ¶m); 72 | Serial.print("Profile Param: "); 73 | Serial.println(param, HEX); 74 | */ 75 | 76 | // use 4.2V profile 77 | if (!setBattProfile(0x1)) 78 | return false; 79 | 80 | if (!setTemperatureMode(LC709203F_TEMPERATURE_THERMISTOR)) 81 | return false; 82 | 83 | return true; 84 | } 85 | 86 | /*! 87 | * @brief Get IC LSI version 88 | * @return 16-bit value read from LC709203F_CMD_ICVERSION register 89 | */ 90 | uint16_t Adafruit_LC709203F::getICversion(void) { 91 | uint16_t vers = 0; 92 | readWord(LC709203F_CMD_ICVERSION, &vers); 93 | return vers; 94 | } 95 | 96 | /*! 97 | * @brief Initialize the RSOC algorithm 98 | * @return True on I2C command success 99 | */ 100 | bool Adafruit_LC709203F::initRSOC(void) { 101 | return writeWord(LC709203F_CMD_INITRSOC, 0xAA55); 102 | } 103 | 104 | /*! 105 | * @brief Get battery voltage 106 | * @return Floating point value read in Volts 107 | */ 108 | float Adafruit_LC709203F::cellVoltage(void) { 109 | uint16_t voltage = 0; 110 | readWord(LC709203F_CMD_CELLVOLTAGE, &voltage); 111 | return voltage / 1000.0; 112 | } 113 | 114 | /*! 115 | * @brief Get battery state in percent (0-100%) 116 | * @return Floating point value from 0 to 100.0 117 | */ 118 | float Adafruit_LC709203F::cellPercent(void) { 119 | uint16_t percent = 0; 120 | readWord(LC709203F_CMD_CELLITE, &percent); 121 | return percent / 10.0; 122 | } 123 | 124 | /*! 125 | * @brief Get battery thermistor temperature 126 | * @return Floating point value from -20 to 60 *C 127 | */ 128 | float Adafruit_LC709203F::getCellTemperature(void) { 129 | uint16_t temp = 0; 130 | readWord(LC709203F_CMD_CELLTEMPERATURE, &temp); 131 | float tempf = map(temp, 0x9E4, 0xD04, -200, 600); 132 | return tempf / 10.0; 133 | } 134 | 135 | /*! 136 | * @brief Set the temperature mode (external or internal) 137 | * @param t The desired mode: LC709203F_TEMPERATURE_I2C or 138 | * LC709203F_TEMPERATURE_THERMISTOR 139 | * @return True on successful I2C write 140 | */ 141 | bool Adafruit_LC709203F::setTemperatureMode(lc709203_tempmode_t t) { 142 | return writeWord(LC709203F_CMD_STATUSBIT, (uint16_t)t); 143 | } 144 | 145 | /*! 146 | * @brief Set the approximate pack size, helps RSOC calculation 147 | * @param apa The lc709203_adjustment_t enumerated approximate cell size 148 | * @return True on successful I2C write 149 | */ 150 | bool Adafruit_LC709203F::setPackSize(lc709203_adjustment_t apa) { 151 | return writeWord(LC709203F_CMD_APA, (uint16_t)apa); 152 | } 153 | 154 | /*! 155 | * @brief Set battery APA value, per LC709203F datasheet 156 | * @param apa_value 8-bit APA value to use for the attached battery 157 | * @return True on successful I2C write 158 | */ 159 | bool Adafruit_LC709203F::setPackAPA(uint8_t apa_value) { 160 | return writeWord(LC709203F_CMD_APA, (uint16_t)apa_value); 161 | } 162 | 163 | /*! 164 | * @brief Set the alarm pin to respond to an RSOC percentage level 165 | * @param percent The threshold value, set to 0 to disable alarm 166 | * @return True on successful I2C write 167 | */ 168 | bool Adafruit_LC709203F::setAlarmRSOC(uint8_t percent) { 169 | return writeWord(LC709203F_CMD_ALARMRSOC, percent); 170 | } 171 | 172 | /*! 173 | * @brief Set the alarm pin to respond to a battery voltage level 174 | * @param voltage The threshold value, set to 0 to disable alarm 175 | * @return True on successful I2C write 176 | */ 177 | bool Adafruit_LC709203F::setAlarmVoltage(float voltage) { 178 | return writeWord(LC709203F_CMD_ALARMVOLT, voltage * 1000); 179 | } 180 | 181 | /*! 182 | * @brief Set the power mode, LC709203F_POWER_OPERATE or 183 | * LC709203F_POWER_SLEEP 184 | * @param t The power mode desired 185 | * @return True on successful I2C write 186 | */ 187 | bool Adafruit_LC709203F::setPowerMode(lc709203_powermode_t t) { 188 | return writeWord(LC709203F_CMD_POWERMODE, (uint16_t)t); 189 | } 190 | 191 | /*! 192 | * @brief Get the thermistor B value (e.g. 3950) 193 | * @return The uint16_t B value 194 | */ 195 | uint16_t Adafruit_LC709203F::getThermistorB(void) { 196 | uint16_t val = 0; 197 | readWord(LC709203F_CMD_THERMISTORB, &val); 198 | return val; 199 | } 200 | 201 | /*! 202 | * @brief Set the thermistor B value (e.g. 3950) 203 | * @param b The value to set it to 204 | * @return True on successful I2C write 205 | */ 206 | bool Adafruit_LC709203F::setThermistorB(uint16_t b) { 207 | return writeWord(LC709203F_CMD_THERMISTORB, b); 208 | } 209 | 210 | /*! 211 | * @brief Get the battery profile parameter 212 | * @return The uint16_t profile value (0 or 1) 213 | */ 214 | uint16_t Adafruit_LC709203F::getBattProfile(void) { 215 | uint16_t val = 0; 216 | readWord(LC709203F_CMD_BATTPROF, &val); 217 | return val; 218 | } 219 | 220 | /*! 221 | * @brief Set the battery profile parameter 222 | * @param b The value to set it to (0 or 1) 223 | * @return True on successful I2C write 224 | */ 225 | bool Adafruit_LC709203F::setBattProfile(uint16_t b) { 226 | return writeWord(LC709203F_CMD_BATTPROF, b); 227 | } 228 | 229 | /*! 230 | * @brief Helper that reads 16 bits of CRC data from the chip. Note 231 | * this function performs a CRC on data that includes the I2C 232 | * write address, command, read address and 2 bytes of response 233 | * @param command The I2C register/command 234 | * @param data Pointer to uint16_t value we will store response 235 | * @return True on successful I2C read 236 | */ 237 | bool Adafruit_LC709203F::readWord(uint8_t command, uint16_t *data) { 238 | uint8_t reply[6]; 239 | reply[0] = LC709203F_I2CADDR_DEFAULT * 2; // write byte 240 | reply[1] = command; // command / register 241 | reply[2] = reply[0] | 0x1; // read byte 242 | 243 | if (!i2c_dev->write_then_read(&command, 1, reply + 3, 3)) { 244 | return false; 245 | } 246 | 247 | uint8_t crc = lc709_crc8(reply, 5); 248 | // CRC failure? 249 | if (crc != reply[5]) 250 | return false; 251 | 252 | *data = reply[4]; 253 | *data <<= 8; 254 | *data |= reply[3]; 255 | 256 | return true; 257 | } 258 | 259 | /*! 260 | * @brief Helper that writes 16 bits of CRC data to the chip. Note 261 | * this function performs a CRC on data that includes the I2C 262 | * write address, command, and 2 bytes of response 263 | * @param command The I2C register/command 264 | * @param data Pointer to uint16_t value we will write to register 265 | * @return True on successful I2C write 266 | */ 267 | bool Adafruit_LC709203F::writeWord(uint8_t command, uint16_t data) { 268 | uint8_t send[5]; 269 | send[0] = LC709203F_I2CADDR_DEFAULT * 2; // write byte 270 | send[1] = command; // command / register 271 | send[2] = data & 0xFF; 272 | send[3] = data >> 8; 273 | send[4] = lc709_crc8(send, 4); 274 | 275 | return i2c_dev->write(send + 1, 4); 276 | } 277 | 278 | /** 279 | * Performs a CRC8 calculation on the supplied values. 280 | * 281 | * @param data Pointer to the data to use when calculating the CRC8. 282 | * @param len The number of bytes in 'data'. 283 | * 284 | * @return The computed CRC8 value. 285 | */ 286 | static uint8_t lc709_crc8(uint8_t *data, int len) { 287 | const uint8_t POLYNOMIAL(0x07); 288 | uint8_t crc(0x00); 289 | 290 | for (int j = len; j; --j) { 291 | crc ^= *data++; 292 | 293 | for (int i = 8; i; --i) { 294 | crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); 295 | } 296 | } 297 | return crc; 298 | } 299 | --------------------------------------------------------------------------------