Browse files

Added config display and alert threshold setting

  • Loading branch information...
1 parent e7ee43d commit 3d716384c3d794389d7603e338384568b0027d0c Matthew Newberry committed Dec 20, 2011
Showing with 32 additions and 6 deletions.
  1. +29 −5 MAX1704.cpp
  2. +3 −1 MAX1704.h
View
34 MAX1704.cpp
@@ -10,17 +10,35 @@
#include "MAX1704.h"
#include "Wire.h"
-float MAX1704::stateOfCharge(void){
+float MAX1704::stateOfCharge(){
int data[] = {};
readFrom(MAX1704_SOC,2,data);
-
+
float fraction = data[1] / 256.0;
float percentage = data[0] + fraction;
return percentage;
}
+void MAX1704::showConfig(){
+
+ int data[] = {};
+ readFrom(MAX1704_CONFIG,2,data);
+
+ byte sleep = (data[1] >>7) & 0x01;
+ byte alert = (data[1] >>5) & 0x01;
+ byte alertThreshold = data[1] & 0x1f;
+
+ Serial.print("Sleep = ");
+ Serial.println(sleep, HEX);
+ Serial.print("Alert = ");
+ Serial.println(alert, HEX);
+ Serial.print("Alert Threshold = ");
+ Serial.print(32-alertThreshold, DEC);
+ Serial.println("%");
+}
+
void MAX1704::powerOnReset(){
performCommand(MAX1704_POWER_ON_RESET, 0x00);
@@ -35,8 +53,13 @@ void MAX1704::version(){
}
-void MAX1704::setAlertLevel(int level){
-
+void MAX1704::setAlertLevel(uint8_t level){
+
+ Wire.beginTransmission(MAX1704_ADDR);
+ Wire.write(MAX1704_CONFIG);
+ Wire.write(MAX1704_ALERT_LEVEL);
+ Wire.write(32 - level);
+ Wire.endTransmission();
}
void MAX1704::performCommand(byte address, int value){
@@ -45,7 +68,6 @@ void MAX1704::performCommand(byte address, int value){
Wire.write(MAX1704_COMMAND);
Wire.write(address);
Wire.write(value);
-
Wire.endTransmission();
}
@@ -67,3 +89,5 @@ void MAX1704::readFrom(byte address, int number, int* data){
Wire.endTransmission();
}
+
+
View
4 MAX1704.h
@@ -18,15 +18,17 @@
#define MAX1704_QUICK_START 0x40
#define MAX1704_CONFIG 0x0C
#define MAX1704_COMMAND 0xFE
+#define MAX1704_ALERT_LEVEL 0x97
class MAX1704{
public:
float stateOfCharge();
+ void showConfig();
void powerOnReset();
void quickStart();
void version();
- void setAlertLevel(int level);
+ void setAlertLevel(uint8_t level);
private:
void performCommand(byte address, int value);

0 comments on commit 3d71638

Please sign in to comment.