-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:CivicTechTO/proj-noisemeter-device …
…into dns2
- Loading branch information
Showing
3 changed files
with
140 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef DATAPACKET_H | ||
#define DATAPACKET_H | ||
|
||
#include "timestamp.h" | ||
|
||
#include <algorithm> | ||
|
||
struct DataPacket | ||
{ | ||
constexpr DataPacket() = default; | ||
|
||
void add(float sample) noexcept { | ||
count++; | ||
minimum = std::min(minimum, sample); | ||
maximum = std::max(maximum, sample); | ||
average += (sample - average) / count; | ||
} | ||
|
||
int count = 0; | ||
float minimum = 999.f; | ||
float maximum = 0.f; | ||
float average = 0.f; | ||
Timestamp timestamp = Timestamp::invalidTimestamp(); | ||
}; | ||
|
||
#endif // DATAPACKET_H | ||
|
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,46 @@ | ||
#ifndef TIMESTAMP_H | ||
#define TIMESTAMP_H | ||
|
||
#include <Arduino.h> | ||
#include <ctime> | ||
|
||
class Timestamp | ||
{ | ||
public: | ||
Timestamp(std::time_t tm_ = std::time(nullptr)): | ||
tm(tm_) {} | ||
|
||
bool valid() const noexcept { | ||
return tm >= 8 * 3600 * 2; | ||
} | ||
|
||
operator String() const noexcept { | ||
char tsbuf[32]; | ||
const auto timeinfo = std::gmtime(&tm); | ||
const auto success = std::strftime(tsbuf, sizeof(tsbuf), "%c", timeinfo) > 0; | ||
|
||
return success ? tsbuf : "(error)"; | ||
} | ||
|
||
auto secondsBetween(Timestamp ts) const noexcept { | ||
return std::difftime(ts.tm, tm); | ||
} | ||
|
||
static void synchronize() { | ||
configTime(0, 0, "pool.ntp.org"); | ||
|
||
do { | ||
delay(1000); | ||
} while (!Timestamp().valid()); | ||
} | ||
|
||
static Timestamp invalidTimestamp() { | ||
return Timestamp(0); | ||
} | ||
|
||
private: | ||
std::time_t tm; | ||
}; | ||
|
||
#endif // TIMESTAMP_H | ||
|