acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

Pangodream_18650_CL.cpp (5301B)

      1 /*
      2   MIT License
      3   
      4   Copyright (c) 2019 Pangodream
      5   
      6   Permission is hereby granted, free of charge, to any person obtaining a copy
      7   of this software and associated documentation files (the "Software"), to deal
      8   in the Software without restriction, including without limitation the rights
      9   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10   copies of the Software, and to permit persons to whom the Software is
     11   furnished to do so, subject to the following conditions:
     12   
     13   The above copyright notice and this permission notice shall be included in all
     14   copies or substantial portions of the Software.
     15   
     16   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22   SOFTWARE.
     23 */
     24 #include "Arduino.h"
     25 #include "Pangodream_18650_CL.h"
     26 
     27 Pangodream_18650_CL::Pangodream_18650_CL(int addressPin, double convFactor, int reads)
     28 {
     29     _reads = reads;
     30     _convFactor = convFactor;
     31     _addressPin = addressPin;
     32     _initVoltsArray();
     33 }
     34 
     35 Pangodream_18650_CL::Pangodream_18650_CL(int addressPin, double convFactor)
     36 {
     37     _reads = DEF_READS;
     38     _convFactor = convFactor;
     39     _addressPin = addressPin;
     40     _initVoltsArray();
     41 }
     42 
     43 Pangodream_18650_CL::Pangodream_18650_CL(int addressPin)
     44 {
     45     _reads = DEF_READS;
     46     _convFactor = DEF_CONV_FACTOR;
     47     _addressPin = addressPin;
     48     _initVoltsArray();
     49 }
     50 
     51 Pangodream_18650_CL::Pangodream_18650_CL()
     52 {
     53     _reads = DEF_READS;
     54     _convFactor = DEF_CONV_FACTOR;
     55     _addressPin = DEF_PIN;
     56     _initVoltsArray();
     57 }
     58 
     59 int Pangodream_18650_CL::getAnalogPin()
     60 {
     61     return _addressPin;
     62 }
     63 double Pangodream_18650_CL::getConvFactor()
     64 {
     65     return _convFactor;
     66 }
     67     
     68 /**
     69  * Loads each voltage value in its matching charge element (index)
     70  */
     71 void Pangodream_18650_CL::_initVoltsArray(){
     72     _vs[0] = 3.200; 
     73     _vs[1] = 3.250; _vs[2] = 3.300; _vs[3] = 3.350; _vs[4] = 3.400; _vs[5] = 3.450;
     74     _vs[6] = 3.500; _vs[7] = 3.550; _vs[8] = 3.600; _vs[9] = 3.650; _vs[10] = 3.700;
     75     _vs[11] = 3.703; _vs[12] = 3.706; _vs[13] = 3.710; _vs[14] = 3.713; _vs[15] = 3.716;
     76     _vs[16] = 3.719; _vs[17] = 3.723; _vs[18] = 3.726; _vs[19] = 3.729; _vs[20] = 3.732;
     77     _vs[21] = 3.735; _vs[22] = 3.739; _vs[23] = 3.742; _vs[24] = 3.745; _vs[25] = 3.748;
     78     _vs[26] = 3.752; _vs[27] = 3.755; _vs[28] = 3.758; _vs[29] = 3.761; _vs[30] = 3.765;
     79     _vs[31] = 3.768; _vs[32] = 3.771; _vs[33] = 3.774; _vs[34] = 3.777; _vs[35] = 3.781;
     80     _vs[36] = 3.784; _vs[37] = 3.787; _vs[38] = 3.790; _vs[39] = 3.794; _vs[40] = 3.797;
     81     _vs[41] = 3.800; _vs[42] = 3.805; _vs[43] = 3.811; _vs[44] = 3.816; _vs[45] = 3.821;
     82     _vs[46] = 3.826; _vs[47] = 3.832; _vs[48] = 3.837; _vs[49] = 3.842; _vs[50] = 3.847;
     83     _vs[51] = 3.853; _vs[52] = 3.858; _vs[53] = 3.863; _vs[54] = 3.868; _vs[55] = 3.874;
     84     _vs[56] = 3.879; _vs[57] = 3.884; _vs[58] = 3.889; _vs[59] = 3.895; _vs[60] = 3.900;
     85     _vs[61] = 3.906; _vs[62] = 3.911; _vs[63] = 3.917; _vs[64] = 3.922; _vs[65] = 3.928;
     86     _vs[66] = 3.933; _vs[67] = 3.939; _vs[68] = 3.944; _vs[69] = 3.950; _vs[70] = 3.956;
     87     _vs[71] = 3.961; _vs[72] = 3.967; _vs[73] = 3.972; _vs[74] = 3.978; _vs[75] = 3.983;
     88     _vs[76] = 3.989; _vs[77] = 3.994; _vs[78] = 4.000; _vs[79] = 4.008; _vs[80] = 4.015;
     89     _vs[81] = 4.023; _vs[82] = 4.031; _vs[83] = 4.038; _vs[84] = 4.046; _vs[85] = 4.054;
     90     _vs[86] = 4.062; _vs[87] = 4.069; _vs[88] = 4.077; _vs[89] = 4.085; _vs[90] = 4.092;
     91     _vs[91] = 4.100; _vs[92] = 4.111; _vs[93] = 4.122; _vs[94] = 4.133; _vs[95] = 4.144;
     92     _vs[96] = 4.156; _vs[97] = 4.167; _vs[98] = 4.178; _vs[99] = 4.189; _vs[100] = 4.200;
     93 }
     94 
     95 int Pangodream_18650_CL::getBatteryChargeLevel()
     96 {
     97     int readValue = _analogRead(_addressPin);
     98     double volts = _analogReadToVolts(readValue);
     99     int chargeLevel = _getChargeLevel(volts);
    100     return chargeLevel;
    101 }
    102 
    103 int Pangodream_18650_CL::pinRead(){
    104     return _analogRead(_addressPin); 
    105 }
    106 
    107 int Pangodream_18650_CL::_analogRead(int pinNumber){
    108     int totalValue = 0;
    109     int averageValue = 0;
    110     for(int i = 0; i < _reads; i++){
    111        totalValue += analogRead(pinNumber);
    112     }
    113     averageValue = totalValue / _reads;
    114     return averageValue; 
    115 }
    116 /**
    117  * Performs a binary search to find the index corresponding to a voltage.
    118  * The index of the array is the charge %
    119 */
    120 int Pangodream_18650_CL::_getChargeLevel(double volts){
    121   int idx = 50;
    122   int prev = 0;
    123   int half = 0;
    124   if (volts >= 4.2){
    125     return 100;
    126   }
    127   if (volts <= 3.2){
    128     return 0;
    129   }
    130   while(true){
    131     half = abs(idx - prev) / 2;
    132     prev = idx;
    133     if(volts >= _vs[idx]){
    134       idx = idx + half;
    135     }else{
    136       idx = idx - half;
    137     }
    138     if (prev == idx){
    139       break;
    140     }
    141   }
    142   return idx;
    143 }
    144 
    145 double Pangodream_18650_CL::_analogReadToVolts(int readValue){
    146   double volts; 
    147   volts = readValue * _convFactor / 1000;
    148   return volts;
    149 }
    150 
    151 double Pangodream_18650_CL::getBatteryVolts(){
    152     int readValue = analogRead(_addressPin);
    153     return _analogReadToVolts(readValue);
    154 }