libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
max17048.h
Go to the documentation of this file.
1 
2 #ifndef LIBPROPELLER_MAX17048_H_
3 #define LIBPROPELLER_MAX17048_H_
4 
5 #include "libpropeller/i2c/i2c.h"
6 
19 class MAX17048 {
20 public:
21 
25  bus_ = NULL;
26  status_ = false;
27  }
28 
34  bool Init(I2C * bus) {
35  bus_ = bus;
36  GetStatus();
37  return status_;
38  }
39 
44  bool GetStatus(void) {
45  status_ = bus_->Ping(kDeviceAddress);
46  return status_;
47  }
48 
57  int GetStateOfCharge(void) {
58  int soc = GetShort(kSOC);
59  return ((unsigned) soc) >> 8;
60  }
61 
66  int GetVoltage(void) {
67  int voltage = GetShort(kVCELL);
68 
69  //Originally units of 78.125uV/LSb,
70  //Convert to 1mV/LSb
71  voltage = ((voltage * 7812) / 100) / 1000;
72 
73  return voltage;
74  }
75 
84  int GetChargeRate(void) {
85  //SRLM doesn't know if this is signed or not, but if it is then sign extend
86  //And hopefully, it's not unsigned *and* 16 bits!
87  int rate = (GetShort(kCRATE) << 16) >> 16;
88 
89  return (rate * 208) / 100;
90  }
91 
96  int GetVersion(void) {
97  return GetShort(kVERSION);
98  }
99 
100 
101 private:
102 
103  const static unsigned char kDeviceAddress = 0b01101100;
104 
105  const static unsigned char kVCELL = 0x02;
106  const static unsigned char kSOC = 0x04;
107  const static unsigned char kVERSION = 0x08;
108  const static unsigned char kCRATE = 0x16;
109 
110  I2C * bus_;
111  bool status_;
112 
113  int GetShort(char address) {
114  char data[2];
115  bus_->Get(kDeviceAddress, address, data, 2);
116  int result = (data[0] << 8) | data[1];
117  return result;
118  }
119 
120 };
121 
122 
123 #endif // LIBPROPELLER_MAX17048_H_