Skip to content

Commit

Permalink
Fix led and refactor helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mp-se committed Dec 25, 2023
1 parent 4b585e2 commit 2618c10
Show file tree
Hide file tree
Showing 18 changed files with 892 additions and 607 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ build_flags =
-D BAUD=${common_env_data.monitor_speed}
#-D SKIP_SLEEPMODE
#-D FORCE_GRAVITY_MODE
#-D COLLECT_PERFDATA
-D ACTIVATE_OTA
-D CFG_DISABLE_LOGGING # Turn off verbose/notice logging to reduce size and dont overload uart (applies to LOG_LEVEL6)
-D GYRO_DISABLE_LOGGING
Expand Down Expand Up @@ -53,7 +54,6 @@ lib_deps =
https://github.com/mp-se/arduino-mqtt#v2.5.1
https://github.com/mp-se/ESPAsyncWebServer#0.1.1
https://github.com/mp-se/ESPAsyncTCP#0.1.0
https://github.com/mp-se/Adafruit_NeoPixel#1.12.0
lib_deps32 =
https://github.com/mp-se/NimBLE-Arduino#1.4.1
extra_scripts =
Expand Down
57 changes: 57 additions & 0 deletions src/battery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
MIT License
Copyright (c) 2021-2023 Magnus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <battery.hpp>
#include <config.hpp>
#include <helper.hpp>

BatteryVoltage::BatteryVoltage() {
#if defined(ESP8266)
pinMode(PIN_VOLT, INPUT);
#else
pinMode(PIN_VOLT, INPUT_PULLDOWN);
#endif
}

void BatteryVoltage::read() {
// The analog pin can only handle 3.3V maximum voltage so we need to reduce
// the voltage (from max 5V)
float factor = myConfig.getVoltageFactor(); // Default value is 1.63
int v = analogRead(PIN_VOLT);

// An ESP8266 has a ADC range of 0-1023 and a maximum voltage of 3.3V
// An ESP32 has an ADC range of 0-4095 and a maximum voltage of 3.3V

#if defined(ESP8266)
_batteryLevel = ((3.3 / 1023) * v) * factor;
#else // defined (ESP32)
_batteryLevel = ((3.3 / 4095) * v) * factor;
#endif
#if LOG_LEVEL == 6 && !defined(HELPER_DISABLE_LOGGING)
Log.verbose(
F("BATT: Reading voltage level. Factor=%F Value=%d, Voltage=%F." CR),
factor, v, _batteryLevel);
#endif
}

// EOF
41 changes: 41 additions & 0 deletions src/battery.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
MIT License
Copyright (c) 2021-2023 Magnus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef SRC_BATTERY_HPP_
#define SRC_BATTERY_HPP_

class BatteryVoltage {
private:
float _batteryLevel = 0;

public:
BatteryVoltage();
void read();
float getVoltage() { return _batteryLevel; }
};

extern BatteryVoltage myBatteryVoltage;

#endif // SRC_BATTERY_HPP_

// EOF
Loading

0 comments on commit 2618c10

Please sign in to comment.