38inline constexpr char STX{ 0x02 };
39inline constexpr char ETX{ 0x03 };
40inline constexpr char LF{ 0x0A };
41inline constexpr char CR{ 0x0D };
42inline constexpr char TAB{ 0x09 };
51constexpr uint8_t
lineSize(uint8_t tagLength, uint8_t valueLength)
53 return 1 + tagLength + 1 + valueLength + 1 + 1 + 1;
75 digits[count++] =
static_cast< char >(
'0' + value % 10);
81 *dst++ = digits[--count];
100inline char*
writeLine(
char* dst,
const char* tag, uint8_t index, uint16_t magnitude,
bool negative)
104 char*
const start{ dst };
111 *dst++ =
static_cast< char >(
'0' + index);
123 for (
const char* ptr = start; ptr != dst; ++ptr)
125 sum +=
static_cast< uint8_t
>(*ptr);
127 *dst++ =
static_cast< char >((sum & 0x3F) + 0x20);
142inline char*
line(
char* dst,
const char* tag, int16_t value, uint8_t index = 0)
144 const uint16_t magnitude{ value < 0 ? static_cast< uint16_t >(0U -
static_cast< uint16_t
>(value)) :
static_cast< uint16_t
>(value) };
145 return writeLine(dst, tag, index, magnitude, value < 0);
157inline char*
lineUnsigned(
char* dst,
const char* tag, uint16_t value, uint8_t index = 0)
159 return writeLine(dst, tag, index, value,
false);
166template<
typename Source >
171 if constexpr (Source::PHASE_COUNT > 1)
173 steps += 2 * Source::PHASE_COUNT;
176 if constexpr (Source::RELAY_DIVERSION)
178 steps += 1 + Source::RELAY_COUNT;
180 steps += Source::LOAD_COUNT + Source::TEMP_COUNT;
182 if constexpr (Source::DUAL_TARIFF)
198template<
typename Source >
203 return line(dst,
"P", Source::power());
207 if constexpr (Source::PHASE_COUNT > 1)
209 if (step < 2 * Source::PHASE_COUNT)
211 const uint8_t phase{
static_cast< uint8_t
>(Source::PHASE_COUNT - 1 - step / 2) };
212 return (step & 1) == 0 ?
line(dst,
"P", Source::phasePower(phase), phase + 1) :
lineUnsigned(dst,
"V", Source::phaseVoltage(phase), phase + 1);
214 step -= 2 * Source::PHASE_COUNT;
217 if constexpr (Source::RELAY_DIVERSION)
221 return line(dst,
"R", Source::relayAverage());
225 if (step < Source::RELAY_COUNT)
227 const uint8_t index{
static_cast< uint8_t
>(Source::RELAY_COUNT - step) };
228 return line(dst,
"R", Source::relayOn(index - 1), index);
230 step -= Source::RELAY_COUNT;
233 if (step < Source::LOAD_COUNT)
235 const uint8_t index{
static_cast< uint8_t
>(Source::LOAD_COUNT - step) };
236 return line(dst,
"D", Source::loadOnPercent(index - 1), index);
238 step -= Source::LOAD_COUNT;
240 if constexpr (Source::TEMP_COUNT != 0)
242 if (step < Source::TEMP_COUNT)
244 return Source::temperatureValid(step) ?
line(dst,
"T", Source::temperature(step), step + 1) : dst;
246 step -= Source::TEMP_COUNT;
255 if constexpr (Source::DUAL_TARIFF)
259 return line(dst,
"TA", Source::offPeak() ? 1 : 0);
268 return line(dst,
"S_MC", Source::lowestSampleSetsPerCycle());
303template<
typename Source >
320 length =
static_cast< uint8_t
>(end - text);
The telemetry frame of the IoT output.
char * lineUnsigned(char *dst, const char *tag, uint16_t value, uint8_t index=0)
Writes the line of an unsigned value (above 32767, line() would wrap).
bool writeFrameStep(const uint8_t step, char *const text, uint8_t &length)
Writes a step of the frame: one line, STX before the first one, ETX after the last.
constexpr uint8_t MAX_STEP_LENGTH
The most a step writes: STX or ETX, and the longest line, a 4-character tag ("S_MC") with a signed 16...
constexpr uint8_t lineSize(uint8_t tagLength, uint8_t valueLength)
The size of a line: LF, tag, TAB, value, TAB, checksum and CR.
char * frameLine(uint8_t step, char *dst)
Writes the line of a step.
char * writeLine(char *dst, const char *tag, uint8_t index, uint16_t magnitude, bool negative)
Writes a whole line: LF, tag and index, TAB, value, TAB, checksum and CR.
constexpr uint8_t frameSteps()
The number of steps of a frame: one per line, invalid temperatures included (they write nothing).
char * writeDigits(char *dst, uint16_t value)
Writes the decimal digits of a 16-bit value.
char * line(char *dst, const char *tag, int16_t value, uint8_t index=0)
Writes the line of a signed value.