Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated readFrom and added new methods

  • Loading branch information...
commit 9f2e18edbfe976e118708737dda1aa43fae31b97 1 parent e1bc4f2
Matthew Newberry authored
Showing with 110 additions and 52 deletions.
  1. +80 −39 MAX1704.cpp
  2. +22 −10 MAX1704.h
  3. +8 −3 MAX1704.ino
View
119 MAX1704.cpp
@@ -1,10 +1,20 @@
-//
-// MAX1704.cpp
-//
-//
-// Created by Matthew Newberry on 12/18/11.
-// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
-//
+/**************************************************************************
+ * *
+ * MAX1704* Driver for Arduino *
+ * *
+ * Matthew Newberry *
+ * mattnewberry@me.com *
+ * *
+ ***************************************************************************
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU License. *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU License V2 for more details. *
+ * *
+ ***************************************************************************/
#include "Arduino.h"
#include "MAX1704.h"
@@ -12,11 +22,13 @@
float MAX1704::stateOfCharge(){
- int data[] = {};
- readFrom(MAX1704_SOC,2,data);
+ byte msb = 0;
+ byte lsb = 0;
- float fraction = data[1] / 256.0;
- float percentage = data[0] + fraction;
+ readFrom(MAX1704_SOC,msb,lsb);
+
+ float fraction = lsb / 256.0;
+ float percentage = msb + fraction;
return percentage;
}
@@ -26,7 +38,10 @@ void MAX1704::showConfig(){
int threshold = alertThreshold();
boolean sleep = isSleeping();
boolean alert = isAlerting();
-
+ byte v = version();
+
+ Serial.print("Version = ");
+ Serial.println(v, HEX);
Serial.print("Sleep = ");
Serial.println(sleep, HEX);
Serial.print("Alert = ");
@@ -36,7 +51,7 @@ void MAX1704::showConfig(){
Serial.println("%");
}
-void MAX1704::powerOnReset(){
+void MAX1704::reset(){
performCommand(MAX1704_POWER_ON_RESET, 0x00);
}
@@ -46,8 +61,17 @@ void MAX1704::quickStart(){
performCommand(MAX1704_QUICK_START, 0x00);
}
-void MAX1704::version(){
+char MAX1704::version(){
+
+ int value = 0;
+ byte msb = 0;
+ byte lsb = 0;
+ readFrom(MAX1704_VERSION, msb, lsb);
+
+ value = 0xFF00 & (msb<<8);
+ value |= 0xFF & lsb;
+ return value;
}
void MAX1704::setAlertThreshold(uint8_t level){
@@ -60,35 +84,52 @@ void MAX1704::setAlertThreshold(uint8_t level){
}
int MAX1704::alertThreshold(){
-
- int data[] = {};
- readFrom(MAX1704_CONFIG,2,data);
- byte alertThreshold = data[1] & 0x1f;
-
- return int(32 - alertThreshold);
+ byte msb = 0;
+ byte lsb = 0;
+
+ readFrom(MAX1704_CONFIG,msb,lsb);
+ byte threshold = lsb & 0x1f;
+
+ return 32 - threshold;
}
boolean MAX1704::isAlerting(){
-
- int data[] = {};
- readFrom(MAX1704_CONFIG,2,data);
- byte alert = (data[1] >>5) & 0x01;
-
+ byte msb = 0;
+ byte lsb = 0;
+
+ readFrom(MAX1704_CONFIG,msb,lsb);
+ byte alert = (lsb >>5) & 0x01;
+
return int(alert) == 1;
}
boolean MAX1704::isSleeping(){
-
- int data[] = {};
- readFrom(MAX1704_CONFIG,2,data);
- byte sleep = (data[1] >>7) & 0x01;
-
+ byte msb = 0;
+ byte lsb = 0;
+
+ readFrom(MAX1704_CONFIG,msb,lsb);
+ byte sleep = (lsb >>7) & 0x01;
+
return int(sleep) == 1;
}
+void MAX1704::sleep(){
+
+ Wire.beginTransmission(MAX1704_ADDR);
+ Wire.write(MAX1704_CONFIG);
+ Wire.write(MAX1704_ALERT_LEVEL);
+ Wire.write(32 - level);
+ Wire.endTransmission();
+}
+
+void MAX1704:awake(){
+
+
+}
+
void MAX1704::performCommand(byte address, int value){
Wire.beginTransmission(MAX1704_ADDR);
@@ -98,23 +139,23 @@ void MAX1704::performCommand(byte address, int value){
Wire.endTransmission();
}
-void MAX1704::readFrom(byte address, int number, int* data){
+void MAX1704::readFrom(byte address, byte &msb, byte &lsb){
Wire.beginTransmission(MAX1704_ADDR);
Wire.write(address);
Wire.endTransmission();
- Wire.requestFrom(MAX1704_ADDR, number);
-
- int i = 0;
- while(Wire.available()){
-
- data[i] = Wire.read();
- i++;
- }
+ Wire.requestFrom(MAX1704_ADDR, 2);
+ int numread = Wire.available();
+ //if (numread == 2) {
+ msb = Wire.read();
+ lsb = Wire.read();
+ //}
Wire.endTransmission();
}
+
+
View
32 MAX1704.h
@@ -1,10 +1,20 @@
-//
-// MAX1704.h
-//
-//
-// Created by Matthew Newberry on 12/18/11.
-// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
-//
+/**************************************************************************
+ * *
+ * MAX1704* Driver for Arduino *
+ * *
+ * Matthew Newberry *
+ * mattnewberry@me.com *
+ * *
+ ***************************************************************************
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU License. *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU License V2 for more details. *
+ * *
+ ***************************************************************************/
#include "Arduino.h"
@@ -25,18 +35,20 @@ class MAX1704{
public:
float stateOfCharge();
void showConfig();
- void powerOnReset();
+ void reset();
void quickStart();
- void version();
+ char version();
void setAlertThreshold(uint8_t level);
int alertThreshold();
boolean isSleeping();
boolean isAlerting();
+ void sleep();
+ void awake();
private:
void performCommand(byte address, int value);
- void readFrom(byte address, int number, int* data);
+ void readFrom(byte address, byte &msb, byte &lsb);
};
#endif
View
11 MAX1704.ino
@@ -2,6 +2,9 @@
* *
* MAX1704* Driver for Arduino *
* *
+ * Matthew Newberry *
+ * mattnewberry@me.com *
+ * *
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
@@ -26,14 +29,16 @@ void setup(){
Serial.println("Starting up...");
delay(1000);
+ fuelGauge.reset();
fuelGauge.quickStart();
- fuelGauge.powerOnReset();
fuelGauge.showConfig();
+ delay(1000);
}
void loop(){
-
+
+ delay(1000);
float charge = fuelGauge.stateOfCharge();
Serial.println(charge);
- delay(5000);
+ delay(1000);
}
Please sign in to comment.
Something went wrong with that request. Please try again.