battery management

This commit is contained in:
2022-01-04 17:02:02 +01:00
parent 14b25a9459
commit d27b693db1
7 changed files with 33 additions and 29 deletions

View File

@@ -22,5 +22,5 @@ build_flags =
-DHARDWARE=2
-DCORE_DEBUG_LEVEL=4
-DNDEF_DEBUG=1
#upload_protocol = espota
#upload_port = muziekdoos.local
upload_protocol = espota
upload_port = muziekdoos.local

View File

@@ -29,7 +29,7 @@
#define LED_PIN 12
#define VBATTMIN 3600
#define VBATTMIN 3000 //before lowbatt cutoff
#define VBATTMAX 4180
#define VBATTREF 3300
#define R12 4.7

View File

@@ -25,7 +25,6 @@ void SetLedColor(CRGB color, bool blink)
setLedBlink(blink);
}
void initLed(void)
{
FastLED.addLeds<SK6812, LED_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
@@ -37,7 +36,9 @@ void handleLed(void)
uint32_t timeNow = millis();
if (timeNow - lastLedTime > LEDTIMEOUT)
{
if (blinkState && (timeNow - lastBlinkTime > LEDBLINKTIME))
if (blinkState)
{
if (timeNow - lastBlinkTime > LEDBLINKTIME)
{
if (!ledstate)
{
@@ -50,6 +51,7 @@ void handleLed(void)
ledstate = !ledstate;
lastBlinkTime = timeNow;
}
}
else
{
leds[0] = ledcolor;

View File

@@ -36,13 +36,13 @@ void loop()
looptime = millis();
handlePower();
handleBatterySensor();
handleLed();
if (getPowerState() == POWERSTATES::on)
{
handleAudio();
handleRfid();
handleSensor();
handleHallSensor();
handleGame();
}
@@ -55,5 +55,4 @@ void loop()
/* noting */
}
log_v("main: looptime = %d", millis() - looptime);
}

View File

@@ -130,7 +130,7 @@ void handlePowerState(void)
if (getLowBatt())
{
log_w("on: Lowbat");
// powerstate = lowBatt;
powerstate = lowBatt;
SetLedColor(CRGB::Red, true);
break;
}
@@ -184,10 +184,14 @@ void handlePowerState(void)
case lowBatt:
{
// add delay
powerstate = off;
for (int i = 0; i < 6; i++)
//powerstate = off;
if(!getLowBatt())
{
powerstate = on;
SetLedColor(CRGB::Green);
}
else
{
log_w("lowbatt");
SetLedColor(CRGB::Red, true);
}
}

View File

@@ -8,6 +8,7 @@ uint32_t lastVbatt = 0;
bool batteryLow = false;
uint16_t BatterySensor = 0;
uint16_t BatteryVoltage = 0;
uint16_t HallSensor = 0;
uint32_t last_hall_read = 0;
@@ -66,14 +67,14 @@ bool CheckBattery(void)
{
int16_t battticks = ADS.readADC(MEAS_ADC);
float vbattraw = ADS.toVoltage(battticks);
uint16_t vbatt = battery.voltage(vbattraw * 1000);
BatterySensor = battery.level(vbatt);
BatteryVoltage = battery.voltage(vbattraw * 1000);
BatterySensor = battery.level(BatteryVoltage);
digitalWrite(MEAS_EN, HIGH);
log_i("read batt, ticks=%d, raw=%4.2f, vbatt=%d, level=%d", battticks, vbattraw, vbatt, BatterySensor);
if (vbatt < VBATTMIN)
log_i("read batt, ticks=%d, raw=%4.2f, vbatt=%d, level=%d", battticks, vbattraw, BatteryVoltage, BatterySensor);
if (BatteryVoltage < VBATTMIN)
{
return true;
batteryLow = true;
return true;
}
batteryLow = false;
return false;
@@ -84,7 +85,7 @@ bool getLowBatt(void)
return batteryLow;
}
void handleSensor(void)
void handleBatterySensor(void)
{
uint32_t timeNow = millis();
@@ -103,8 +104,6 @@ void handleSensor(void)
else if(BatterySensor > 20) SetLedColor(CRGB::Red);
lastVbatt = timeNow;
}
log_v("Read sensor: Hall=%d, vbatt=%d", HallSensor, BatterySensor);
}
void handleHallSensor(void)

View File

@@ -16,7 +16,7 @@
void initSensor(void);
void handleSensor(void);
void handleBatterySensor(void);
void handleHallSensor(void);