Skip to content

Commit df07605

Browse files
author
Eric Jennings
committed
update fuel gauge to return proper battery alert flag now
1 parent fa5aeb7 commit df07605

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

Scout.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,34 @@ void PinoccioScout::setup(bool isForcedLeadScout) {
3333
forceLeadScout = isForcedLeadScout;
3434
PinoccioClass::setup();
3535

36-
RgbLed.turnOff();
37-
3836
pinMode(CHG_STATUS, INPUT_PULLUP);
3937
pinMode(BATT_ALARM, INPUT_PULLUP);
4038
pinMode(VCC_ENABLE, OUTPUT);
41-
enableBackpackVcc();
4239

43-
bp.begin(BACKPACK_BUS);
44-
if (!bp.enumerate()) {
45-
bp.printLastError(Serial);
46-
Serial.println();
47-
}
48-
Serial.println("Backpacks found:");
49-
for (uint8_t i = 0; i < bp.num_slaves; ++i) {
50-
for (uint8_t j = 0; j < UNIQUE_ID_LENGTH; ++j) {
51-
if (bp.slave_ids[i][j] < 0x10)
52-
Serial.print('0');
53-
Serial.print(bp.slave_ids[i][j]);
54-
}
55-
Serial.println();
56-
}
40+
RgbLed.turnOff();
5741

5842
handler.setup();
5943
Shell.setup();
6044

45+
enableBackpackVcc();
46+
bp.begin(BACKPACK_BUS);
47+
if (!bp.enumerate()) {
48+
bp.printLastError(Serial);
49+
Serial.println();
50+
}
51+
//Serial.println("Backpacks found:");
52+
for (uint8_t i = 0; i < bp.num_slaves; ++i) {
53+
for (uint8_t j = 0; j < UNIQUE_ID_LENGTH; ++j) {
54+
if (bp.slave_ids[i][j] < 0x10) {
55+
Serial.print('0');
56+
}
57+
//Serial.print(bp.slave_ids[i][j]);
58+
}
59+
//Serial.println();
60+
}
61+
6162
Wire.begin();
62-
delay(100);
6363
HAL_FuelGaugeConfig(20); // Configure the MAX17048G's alert percentage to 20%
64-
HAL_FuelGaugeQuickStart(); // Restart fuel-gauge calculations
6564

6665
saveState();
6766
startDigitalStateChangeEvents();
@@ -175,7 +174,7 @@ void PinoccioScout::saveState() {
175174
batteryPercentage = constrain(HAL_FuelGaugePercent(), 0, 100);
176175
batteryVoltage = HAL_FuelGaugeVoltage();
177176
isBattCharging = (digitalRead(CHG_STATUS) == LOW);
178-
isBattAlarmTriggered = (digitalRead(BATT_ALARM) == HIGH);
177+
isBattAlarmTriggered = (digitalRead(BATT_ALARM) == LOW);
179178
temperature = this->getTemperature();
180179
}
181180

@@ -262,7 +261,7 @@ static void scoutAnalogStateChangeTimerHandler(SYS_Timer_t *timer) {
262261
}
263262

264263
if (Scout.batteryAlarmTriggeredEventHandler != 0) {
265-
val = (digitalRead(BATT_ALARM) == HIGH);
264+
val = (digitalRead(BATT_ALARM) == LOW);
266265
if (Scout.isBattAlarmTriggered != val) {
267266
if (Scout.eventVerboseOutput == true) {
268267
Serial.print("Running: batteryAlarmTriggeredEventHandler(");

Shell.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static numvar goToSleep(void) {
192192
void powerReportHQ(void)
193193
{
194194
char report[100];
195-
sprintf(report,"{\"_\":\"pwr\",\"p\":%d,\"v\":%d,\"c\":%s,\"vcc\":%s}",Scout.getBatteryPercentage(),(int)Scout.getBatteryVoltage(),Scout.isBatteryCharging()?"true":"false",Scout.isBackpackVccEnabled()?"true":"false");
195+
sprintf(report,"{\"_\":\"pwr\",\"p\":%d,\"v\":%d,\"c\":%s,\"vcc\":%s,\"a\":%s}",Scout.getBatteryPercentage(),(int)Scout.getBatteryVoltage(),Scout.isBatteryCharging()?"true":"false",Scout.isBackpackVccEnabled()?"true":"false", Scout.isBatteryAlarmTriggered()?"true":"false");
196196
Scout.handler.fieldAnnounce(0xBEEF, report);
197197
}
198198

@@ -206,8 +206,8 @@ static numvar powerReport(void) {
206206
sp(Scout.isBatteryCharging());
207207
sp("\nvcc: ");
208208
sp(Scout.isBackpackVccEnabled());
209-
// sp("\nalarm:");
210-
// sp(Scout.isBatteryAlarmTriggered());
209+
sp("\nalarm: ");
210+
sp(Scout.isBatteryAlarmTriggered());
211211
sp("\n");
212212
return true;
213213
}

utility/halFuelGauge.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,23 @@ void HAL_FuelGaugeConfig(byte percent) {
3838
byte percentBits = 32 - percent;
3939
HAL_FuelGaugei2cWrite16((0x9700 | percentBits), 0x0C);
4040
}
41+
42+
// VALERT min and max voltage setting of 3.1V and 4.3V respectively (20mV per LSb)
43+
HAL_FuelGaugei2cWrite16(0x9BD7, 0x14);
4144
}
4245

4346
void HAL_FuelGaugeQuickStart() {
4447
HAL_FuelGaugei2cWrite16(0x4000, 0x06); // Write a 0x4000 to the MODE register
4548
}
4649

50+
byte HAL_FuelGaugeGetAlertReason() {
51+
return HAL_FuelGaugei2cRead16(0x1A >> 8);
52+
}
53+
54+
void HAL_FuelGaugeClearAlert() {
55+
//TODO
56+
}
57+
4758
unsigned int HAL_FuelGaugei2cRead16(unsigned char address) {
4859
int data = 0;
4960

utility/halFuelGauge.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int HAL_FuelGaugePercent();
3939

4040
/*
4141
configMAX17048G(byte percent) configures the config register of
42-
the MAX170143, specifically the alert threshold therein. Pass a
42+
the MAX170148, specifically the alert threshold therein. Pass a
4343
value between 1 and 32 to set the alert threshold to a value between
4444
1 and 32%. Any other values will set the threshold to 32%.
4545
*/
@@ -55,6 +55,23 @@ to reduce the error.
5555
*/
5656
void HAL_FuelGaugeQuickStart();
5757

58+
/*
59+
This lets you get the reason for the alert
60+
Masks:
61+
Low SOC - 0x10
62+
SOC 1% change - 0x20
63+
Reset - 0x08
64+
Overvoltage - 0x02
65+
Undervoltage - 0x04
66+
*/
67+
byte HAL_FuelGaugeGetAlertReason();
68+
69+
/*
70+
Clear alerts that have been triggered
71+
TODO
72+
*/
73+
void HAL_FuelGaugeClearAlert();
74+
5875
/*
5976
i2cRead16(unsigned char address) reads a 16-bit value beginning
6077
at the 8-bit address, and continuing to the next address. A 16-bit

0 commit comments

Comments
 (0)