constexpr-ify log field list (#2548)

* constexprify

* reclaim ram

* more constexpr

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2021-04-13 17:16:18 -07:00 committed by GitHub
parent 0180638a22
commit b110e01638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 18 deletions

View File

@ -18,7 +18,7 @@ static scaled_channel<uint32_t, TIME_PRECISION> packedTime;
// todo: we are at the edge of sdLogBuffer size and at the moment we have no code to make sure buffer does not overflow
// todo: make this logic smarter
static const LogField fields[] = {
static constexpr LogField fields[] = {
{tsOutputChannels.rpm, GAUGE_NAME_RPM, "rpm", 0},
{packedTime, GAUGE_NAME_TIME, "sec", 0},
{tsOutputChannels.totalTriggerErrorCounter, GAUGE_NAME_TRG_ERR, "err", 0},
@ -74,6 +74,17 @@ static const LogField fields[] = {
{tsOutputChannels.knockLevel, GAUGE_NAME_KNOCK_LEVEL, "dBv", 0},
};
static constexpr uint16_t computeFieldsRecordLength() {
uint16_t recLength = 0;
for (size_t i = 0; i < efi::size(fields); i++) {
recLength += fields[i].getSize();
}
return recLength;
}
static constexpr uint16_t recordLength = computeFieldsRecordLength();
void writeHeader(Writer& outBuffer) {
char buffer[MLQ_HEADER_SIZE];
// File format: MLVLG\0
@ -102,13 +113,8 @@ void writeHeader(Writer& outBuffer) {
buffer[17] = headerSize & 0xFF;
// Record length - length of a single data record: sum size of all fields
uint16_t recLength = 0;
for (size_t i = 0; i < efi::size(fields); i++) {
recLength += fields[i].getSize();
}
buffer[18] = recLength >> 8;
buffer[19] = recLength & 0xFF;
buffer[18] = recordLength >> 8;
buffer[19] = recordLength & 0xFF;
// Number of logger fields
buffer[20] = 0;

View File

@ -8,10 +8,10 @@ struct Writer;
class LogField {
public:
template <typename TValue, int TMult = 1>
LogField(const scaled_channel<TValue, TMult>& toRead, const char* name, const char* units, int8_t digits)
constexpr LogField(const scaled_channel<TValue, TMult>& toRead, const char* name, const char* units, int8_t digits)
: m_type(resolveType<TValue>())
, m_multiplier(TMult)
, m_addr(reinterpret_cast<const char*>(&toRead))
, m_addr(toRead.getFirstByteAddr())
, m_digits(digits)
, m_size(sizeForType(resolveType<TValue>()))
, m_name(name)
@ -30,7 +30,7 @@ public:
F32 = 7,
};
size_t getSize() const {
constexpr size_t getSize() const {
return m_size;
}
@ -43,9 +43,9 @@ public:
private:
template<typename T>
static Type resolveType();
static constexpr Type resolveType();
static size_t sizeForType(Type t) {
static constexpr size_t sizeForType(Type t) {
switch (t) {
case Type::U08:
case Type::S08:

View File

@ -709,7 +709,7 @@ void initEngineContoller(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX)
* UNUSED_SIZE constants.
*/
#ifndef RAM_UNUSED_SIZE
#define RAM_UNUSED_SIZE 2850
#define RAM_UNUSED_SIZE 4350
#endif
#ifndef CCM_UNUSED_SIZE
#define CCM_UNUSED_SIZE 2000

View File

@ -23,19 +23,26 @@
template <typename T, int mult = 1>
class scaled_channel {
public:
scaled_channel() : m_value(static_cast<T>(0)) { }
scaled_channel(float val)
constexpr scaled_channel() : m_value(static_cast<T>(0)) { }
constexpr scaled_channel(float val)
: m_value(val * mult)
{
}
// Allow reading back out as a float (note: this may be lossy!)
operator float() const {
constexpr operator float() const {
return m_value / (float)mult;
}
constexpr const char* getFirstByteAddr() const {
return &m_firstByte;
}
private:
T m_value;
union {
T m_value;
char m_firstByte;
};
};
// We need to guarantee that scaled values containing some type are the same size