Skip to content

Commit 6931a78

Browse files
committed
Remove all voltage & battery related stuff from main and use Battery class
1 parent 6bc568c commit 6931a78

File tree

3 files changed

+28
-108
lines changed

3 files changed

+28
-108
lines changed

eloc610LowPowerPartition/src/Battery.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ float Battery::getVoltage() {
9898
}
9999
}
100100

101+
// TODO: add calculation of the voltage divider
101102
mVoltage=accum/5.0;
102103

103104

104105
if (mChargingEnabled) {
105106
if (esp_err_t err = setChargingEnable(mChargingEnabled))
106107
ESP_LOGI(TAG, "Failed to enable charging %s", esp_err_to_name(err));
107108
}
109+
//TODO: set battery
110+
108111
return mVoltage;
109112
}
110113

@@ -162,3 +165,10 @@ esp_err_t Battery::setDefaultChargeEn(bool enable)
162165
return setChargingEnable(enable);
163166
}
164167

168+
bool Battery::isCalibrationDone() const {
169+
//TODO: check what kind of calibration to perform with ELOC
170+
//TODO: check if calibration is necessary of if it can be applied to
171+
// the esp32 internal calibration --> esp_adc_cal_check_efuse()
172+
return false;
173+
}
174+

eloc610LowPowerPartition/src/Battery.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class Battery
6565

6666
virtual esp_err_t setDefaultChargeEn(bool enable);
6767
bool isBatteryPresent();
68+
69+
bool isCalibrationDone() const;
6870
};
6971

7072

eloc610LowPowerPartition/src/main.cpp

+16-108
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ int gRawSampleBlockSize=1000;
6060
int gSampleBlockSize=16000; //must be factor of 1000 in samples
6161
int gBufferLen=1000; //in samples
6262
int gBufferCount=18; // so 6*4000 = 24k buf len
63-
float gVoltage[] = {0.0f, 0.0f, 0.0f, 0.0f};
6463
//always keep these in same order
6564
int gbitShift;
6665
bool gDisableBluetoothWhenRecording=false;
@@ -77,12 +76,6 @@ int64_t gTotalUPTimeSinceReboot=esp_timer_get_time(); //esp_timer_get_time retu
7776
int64_t gTotalRecordTimeSinceReboot=0;
7877
int64_t gSessionRecordTime=0;
7978

80-
//voltage
81-
bool gVoltageCalibrationDone=false;
82-
float gVoltageOffset=10.0; //read this in on startup
83-
float gvOff=2.7;
84-
float gvFull=3.3;
85-
float gvLow=3.18;
8679
int gMinutesWaitUntilDeepSleep=60; //change to 1 or 2 for testing
8780

8881

@@ -132,7 +125,6 @@ void readSettings();
132125
void writeSettings(String settings);
133126
void freeSpace();
134127
void doDeepSleep();
135-
float getVoltage();
136128
void setTime(long epoch, int ms);
137129

138130
// idf-wav-sdcard/lib/sd_card/src/SDCard.cpp m_host.max_freq_khz = 18000;
@@ -194,14 +186,6 @@ void readConfig() {
194186
ESP_LOGI(TAG, "gain override: %d", gbitShift);
195187
}
196188

197-
position = strstr(line, "VoltageOffset:");
198-
if (position != NULL)
199-
{
200-
position = strstr(line, ":");
201-
gVoltageOffset = atof(position + 1);
202-
ESP_LOGI(TAG, "voltage offset override: %f", gVoltageOffset);
203-
}
204-
205189
position = strstr(line, "SecondsPerFile:");
206190
if (position != NULL)
207191
{
@@ -574,14 +558,14 @@ void wait_for_button_push()
574558
//digitalWrite(BATTERY_LED,LOW);
575559
//bool gBatteryLEDToggle=false;
576560
float currentvolts;
577-
currentvolts= getVoltage()+ gVoltageOffset;
561+
currentvolts= Battery::GetInstance().getVoltage();
578562

579563
gRecording=false;
580-
//getVoltage();
564+
//Battery::GetInstance().getVoltage();
581565
boolean sentElocStatus=false;
582566
int loopcounter=0;
583567
Serial.println( "waiting for button or bluetooth");
584-
Serial.println( "voltage is "+String(getVoltage()+gVoltageOffset));
568+
Serial.println( "voltage is "+String(Battery::GetInstance().getVoltage()));
585569

586570

587571
int64_t timein= getSystemTimeMS();
@@ -599,7 +583,7 @@ void wait_for_button_push()
599583

600584
while (!gotrecord)
601585
{
602-
//getVoltage();
586+
//Battery::GetInstance().getVoltage();
603587
//gotrecord=false;
604588
//Serial.println( "waiting for buttonpress");
605589
//btwrite("Waiting for record button");
@@ -791,8 +775,9 @@ void wait_for_button_push()
791775
vTaskDelay(pdMS_TO_TICKS(30)); //so if we get record, max 10ms off
792776

793777

778+
/**************** NOTE: Status LED blinking must be handled in a separate task
794779
if (loopcounter==0) {
795-
currentvolts= getVoltage()+ gVoltageOffset;
780+
currentvolts= Battery::GetInstance().getVoltage();
796781
//currentvolts=0.1;
797782
if ((getSystemTimeMS()-timein) > (60000*gMinutesWaitUntilDeepSleep)) doDeepSleep(); // one hour to deep sleep
798783
if (currentvolts <= gvOff) doDeepSleep();
@@ -807,6 +792,7 @@ void wait_for_button_push()
807792
if (!SerialBT.connected()) digitalWrite(STATUS_LED,LOW);
808793
if (currentvolts <= gvLow) digitalWrite(BATTERY_LED,LOW);
809794
}
795+
****************************************************************************/
810796
}
811797

812798
//mountSDCard();
@@ -891,67 +877,6 @@ String getProperDateTime() {
891877

892878
}
893879

894-
895-
float IRAM_ATTR getVoltage() {
896-
//Statements;
897-
//printf(" two following are heap size, total and max alloc ");
898-
// Note: ADC2 pins cannot be used when Wi-Fi is used. So, if you’re using Wi-Fi and you’re having trouble
899-
//getting the value from an ADC2 GPIO, you may consider using an ADC1 GPIO instead, that should solve your problem.
900-
901-
902-
// we want to measure up to 4 volts.
903-
//ed's stuff https://www.youtube.com/watch?v=5srvxIm1mcQ 470k 1.47meg
904-
905-
//so the new vrange = 3.96v
906-
907-
908-
//3.25v on en pin corresponds to 1.83v on gpio34
909-
//so voltage = pinread/4095 *X where x is the multiplier varies from device to device and on input current.
910-
//3.29v =2979
911-
//so 3.29= 2979/4095 *3.96*X 3.29 =2.88*X so X = 1.142
912-
//pinMode(VOLTAGE_PIN,INPUT);
913-
//analogSetPinAttenuation(VOLTAGE_PIN, ADC_11db);
914-
//vTaskDelay(pdMS_TO_TICKS(500));
915-
//analogRead(VOLTAGE_PIN)
916-
917-
// return(2.6);
918-
919-
//uint16_t value=analogRead(VOLTAGE_PIN);
920-
921-
float accum=0.0;
922-
float avg;
923-
for (int i=0; i<5;i++) {
924-
accum+= gpio_get_level(VOLTAGE_PIN);
925-
}
926-
//return((float)accum/5.0);
927-
avg=accum/5.0;
928-
929-
930-
// printf("voltage raw, calc" );
931-
// printf(value);
932-
// printf(" ");
933-
// printf(((float)value/4095)*3.96*1.142 ); //see above calc
934-
// printf(" ");
935-
936-
//printf(analogReadMilliVolts(VOLTAGE_PIN));
937-
//delay(500);
938-
939-
return(10.0); //comment
940-
//return((avg/4095)*3.96*1.142); //uncomment for field versions
941-
942-
}
943-
944-
945-
float calculateVoltageOffset() {
946-
//gvoltageoffset will always be ADDED
947-
// assume battery is currently at voff =2.7
948-
//
949-
float temp= getVoltage()-gvLow; //if v = 2.6 offset will be neg so need to be added if 2.8, pos, so need to be sub
950-
gVoltageOffset=temp*-1.0;
951-
Serial.println ("voltage offset is "+String(gVoltageOffset));
952-
return gVoltageOffset;
953-
}
954-
955880
void doDeepSleep(){
956881

957882
esp_sleep_enable_ext0_wakeup(OTHER_GPIO_BUTTON, 0); //then try changing between 0 and 1.
@@ -1086,8 +1011,9 @@ void record(I2SSampler *input) {
10861011

10871012
//voltage check
10881013
if ((loopCounter % 50)==0 ) {
1089-
ESP_LOGI(TAG, "LOOPCOUNTER MOD 50 is 0" );
1090-
if ((getVoltage()+gVoltageOffset)<gvOff) {
1014+
ESP_LOGI(TAG, "Checking battery state" );
1015+
Battery::GetInstance().getVoltage();
1016+
if ((Battery::GetInstance().isEmpty())) {
10911017
stopit=true; loopCounter=10000001L;
10921018
ESP_LOGI(TAG, "Voltage LOW-OFF. Stopping record. " );
10931019
deepSleep=true;
@@ -1357,13 +1283,13 @@ void sendElocStatus() { //compiles and sends eloc config
13571283

13581284
sendstring=sendstring+ "!1!"+ gFirmwareVersion + "\n" ; //firmware
13591285

1360-
float tempvolts= getVoltage()+gVoltageOffset;
1286+
float tempvolts= Battery::GetInstance().getVoltage();
13611287
String temptemp= "FULL";
1362-
if (tempvolts <gvFull) temptemp="";
1363-
if (tempvolts <gvLow) temptemp="!!! LOW !!!";
1364-
if (tempvolts <gvOff) temptemp="turn off";
1288+
if (!Battery::GetInstance().isFull()) temptemp="";
1289+
if (Battery::GetInstance().isLow()) temptemp="!!! LOW !!!";
1290+
if (Battery::GetInstance().isEmpty()) temptemp="turn off";
13651291

1366-
if (gVoltageCalibrationDone) {
1292+
if (Battery::GetInstance().isCalibrationDone()) {
13671293
sendstring=sendstring+ "!2!" +String(tempvolts)+ " v # "+temptemp+"\n" ; //battery voltage
13681294
} else {
13691295
sendstring=sendstring+ "!2!" +String(tempvolts)+ " v "+temptemp+"\n" ;
@@ -1515,25 +1441,7 @@ readMicInfo();
15151441

15161442

15171443
readCurrentSession();
1518-
1519-
1520-
1521-
// read the voltage offset
1522-
FILE* f = fopen("/spiffs/voltageoffset.txt", "r");
1523-
char line[128]="";
1524-
if (f == NULL) {
1525-
ESP_LOGI(TAG, "no voltage offset");
1526-
//return;
1527-
} else {
1528-
fgets(line, sizeof(line), f);
1529-
gVoltageOffset=atof(line);
1530-
ESP_LOGI(TAG, "voltage offset %f", gVoltageOffset );
1531-
}
1532-
1533-
fclose(f);
1534-
1535-
1536-
1444+
15371445

15381446

15391447

0 commit comments

Comments
 (0)