-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187: Issue 181: Implement battery voltage measure…
…ment
- Loading branch information
Showing
11 changed files
with
275 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
Oops, something went wrong.