Skip to content

Commit

Permalink
Merge pull request #187: Issue 181: Implement battery voltage measure…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
boerge1 authored Mar 27, 2024
2 parents e0a81fc + 5abb096 commit 7521efc
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 49 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ Die SD Karte (Ordner mp3 und advert) hat sich gegenüber der Version 3.1.6 geän

# Change Log

## Version 3.1.7 (28.03.2024)
## Version 3.1.7 (29.03.2024)
- [Issue 184](https://github.com/tonuino/TonUINO-TNG/issues/184): #define DONT_ACCEPT_SAME_RFID_TWICE makes the error: 'class Tonuino' has no member named 'getCard'
- [Issue 181](https://github.com/tonuino/TonUINO-TNG/issues/181): Implement battery voltage measurement
- [Issue 180](https://github.com/tonuino/TonUINO-TNG/issues/180): Play special shortcut on startup if a GPIO is set
- [Issue 056](https://github.com/tonuino/TonUINO-TNG/issues/56): Implement headphone jack detection
- [Issue 178](https://github.com/tonuino/TonUINO-TNG/issues/178): Use Nano Every optional with HW Serial connection to the DfPlayer
Expand Down
2 changes: 1 addition & 1 deletion TonUINO-TNG.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void setup()
LOG(init_log, s_error, F("TonUINO Version 3.1 - refactored by Boerge1\n"));
LOG(init_log, s_error, F("created by Thorsten Voß and licensed under GNU/GPL."));
LOG(init_log, s_error, F("Information and contribution at https://tonuino.de.\n"));
LOG(init_log, s_error, F("V3.1.7 28.03.24\n"));
LOG(init_log, s_error, F("V3.1.7 29.03.24\n"));

Tonuino::getTonuino().setup();
}
Expand Down
90 changes: 90 additions & 0 deletions src/batVoltage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "batVoltage.hpp"

#include "constants.hpp"

#ifdef BAT_VOLTAGE_MEASUREMENT
#include "logger.hpp"
#include "mp3.hpp"

namespace {

#ifndef TonUINO_Classic
inline constexpr uint16_t voltageMeasurementRefVoltage = 2500; // reference voltage mV
#endif
inline constexpr int16_t voltageMeasurementMaxLevel = 1023;
inline constexpr int16_t voltageMeasurementLowLevel = batVoltageLow /voltageMeasurementCorrection*voltageMeasurementMaxLevel;
inline constexpr int16_t voltageMeasurementEmptyLevel = batVoltageEmpty/voltageMeasurementCorrection*voltageMeasurementMaxLevel;
inline constexpr unsigned long batLowMessageIntervall = 30*1000; // 30 seconds
inline constexpr unsigned long batEmptyTimer = 10*1000; // 10 seconds

#ifdef TonUINO_Classic
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1125300L / result; // Back-calculate AVcc in mV
return result;
}
#endif
}

BatVoltage::BatVoltage(Mp3& mp3)
: mp3(mp3)
{
pinMode(voltageMeasurementPin, INPUT);
logTimer.start(2000);
}

bool BatVoltage::check() {
#ifdef TonUINO_Classic
const uint16_t voltageMeasurementRefVoltage = readVcc();
#endif
const int16_t value = analogRead(voltageMeasurementPin)*static_cast<long>(voltageMeasurementRefVoltage)/1000;

LOG_CODE(batvol_log, s_debug, { \
if (logTimer.isExpired()) { \
logTimer.start(2000); \
LOG(batvol_log, s_debug, F("BatVoltage: "), value*voltageMeasurementCorrection/voltageMeasurementMaxLevel); \
} \
} );

if (value < voltageMeasurementEmptyLevel) {
if (emptyTimer.isActive()) {
if (emptyTimer.isExpired()) {
LOG(batvol_log, s_error, F("BatVoltage empty"));
return true;
}
}
else {
emptyTimer.start(batEmptyTimer);
}
}
else
emptyTimer.stop();

if (value < voltageMeasurementLowLevel) {
if (lowTimer.isActive()) {
if (lowTimer.isExpired()) {
lowMessage();
}
}
else {
lowTimer.start(batLowMessageIntervall);
}
}
else
lowTimer.stop();

return false;
}

void BatVoltage::lowMessage() {
mp3.playAdvertisement(advertTracks::t_262_pling, false /*olnyIfIsPlaying*/);
}

#endif // BAT_VOLTAGE_MEASUREMENT
25 changes: 25 additions & 0 deletions src/batVoltage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef SRC_BATVOLTAGE_HPP_
#define SRC_BATVOLTAGE_HPP_

#include <Arduino.h>

#include "timer.hpp"

class Mp3;

class BatVoltage {
public:

BatVoltage(Mp3& mp3);
bool check();

private:
void lowMessage();

Timer logTimer{};
Timer lowTimer{};
Timer emptyTimer{};
Mp3& mp3;
};

#endif /* SRC_BATVOLTAGE_HPP_ */
Loading

0 comments on commit 7521efc

Please sign in to comment.