3-phase PV router
Loading...
Searching...
No Matches
teleinfo.h
Go to the documentation of this file.
1
26#ifndef TELEINFO_H
27#define TELEINFO_H
28
29#include <stdint.h>
30
36namespace TeleInfo
37{
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 };
43
51constexpr uint8_t lineSize(uint8_t tagLength, uint8_t valueLength)
52{
53 return 1 + tagLength + 1 + valueLength + 1 + 1 + 1;
54}
55
60inline constexpr uint8_t MAX_STEP_LENGTH{ 1 + lineSize(4, 6) };
61
69inline char* writeDigits(char* dst, uint16_t value)
70{
71 char digits[5];
72 uint8_t count{ 0 };
73 do
74 {
75 digits[count++] = static_cast< char >('0' + value % 10);
76 value /= 10;
77 } while (value != 0);
78
79 do
80 {
81 *dst++ = digits[--count];
82 } while (count != 0);
83
84 return dst;
85}
86
100inline char* writeLine(char* dst, const char* tag, uint8_t index, uint16_t magnitude, bool negative)
101{
102 *dst++ = LF;
103
104 char* const start{ dst };
105 while (*tag)
106 {
107 *dst++ = *tag++;
108 }
109 if (index != 0)
110 {
111 *dst++ = static_cast< char >('0' + index);
112 }
113 *dst++ = TAB;
114
115 if (negative)
116 {
117 *dst++ = '-';
118 }
119 dst = writeDigits(dst, magnitude);
120 *dst++ = TAB;
121
122 uint8_t sum{ 0 };
123 for (const char* ptr = start; ptr != dst; ++ptr)
124 {
125 sum += static_cast< uint8_t >(*ptr);
126 }
127 *dst++ = static_cast< char >((sum & 0x3F) + 0x20);
128
129 *dst++ = CR;
130 return dst;
131}
132
142inline char* line(char* dst, const char* tag, int16_t value, uint8_t index = 0)
143{
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);
146}
147
157inline char* lineUnsigned(char* dst, const char* tag, uint16_t value, uint8_t index = 0)
158{
159 return writeLine(dst, tag, index, value, false);
160}
161
166template< typename Source >
167constexpr uint8_t frameSteps()
168{
169 uint8_t steps{ 1 }; // P
170
171 if constexpr (Source::PHASE_COUNT > 1)
172 {
173 steps += 2 * Source::PHASE_COUNT; // Pn, Vn ... P1, V1
174 }
175
176 if constexpr (Source::RELAY_DIVERSION)
177 {
178 steps += 1 + Source::RELAY_COUNT; // R, R1-Rn
179 }
180 steps += Source::LOAD_COUNT + Source::TEMP_COUNT; // D1-Dn, T1-Tn
181 steps += 1; // N
182 if constexpr (Source::DUAL_TARIFF)
183 {
184 steps += 1; // TA
185 }
186 steps += 1 + 1; // S, S_MC
187
188 return steps;
189}
190
198template< typename Source >
199char* frameLine(uint8_t step, char* dst)
200{
201 if (step == 0)
202 {
203 return line(dst, "P", Source::power());
204 }
205 --step;
206
207 if constexpr (Source::PHASE_COUNT > 1)
208 {
209 if (step < 2 * Source::PHASE_COUNT) // Pn, Vn first
210 {
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);
213 }
214 step -= 2 * Source::PHASE_COUNT;
215 }
216
217 if constexpr (Source::RELAY_DIVERSION)
218 {
219 if (step == 0)
220 {
221 return line(dst, "R", Source::relayAverage());
222 }
223 --step;
224
225 if (step < Source::RELAY_COUNT) // Rn first
226 {
227 const uint8_t index{ static_cast< uint8_t >(Source::RELAY_COUNT - step) };
228 return line(dst, "R", Source::relayOn(index - 1), index);
229 }
230 step -= Source::RELAY_COUNT;
231 }
232
233 if (step < Source::LOAD_COUNT) // Dn first
234 {
235 const uint8_t index{ static_cast< uint8_t >(Source::LOAD_COUNT - step) };
236 return line(dst, "D", Source::loadOnPercent(index - 1), index);
237 }
238 step -= Source::LOAD_COUNT;
239
240 if constexpr (Source::TEMP_COUNT != 0)
241 {
242 if (step < Source::TEMP_COUNT)
243 {
244 return Source::temperatureValid(step) ? line(dst, "T", Source::temperature(step), step + 1) : dst;
245 }
246 step -= Source::TEMP_COUNT;
247 }
248
249 if (step == 0)
250 {
251 return lineUnsigned(dst, "N", Source::absenceCount());
252 }
253 --step;
254
255 if constexpr (Source::DUAL_TARIFF)
256 {
257 if (step == 0)
258 {
259 return line(dst, "TA", Source::offPeak() ? 1 : 0);
260 }
261 --step;
262 }
263
264 if (step == 0)
265 {
266 return lineUnsigned(dst, "S", Source::sampleSets());
267 }
268 return line(dst, "S_MC", Source::lowestSampleSetsPerCycle());
269}
270
303template< typename Source >
304bool writeFrameStep(const uint8_t step, char* const text, uint8_t& length)
305{
306 char* end{ text };
307 if (step == 0)
308 {
309 *end++ = STX;
310 }
311
312 end = frameLine< Source >(step, end);
313
314 const bool last{ step == frameSteps< Source >() - 1 };
315 if (last)
316 {
317 *end++ = ETX;
318 }
319
320 length = static_cast< uint8_t >(end - text);
321 return !last;
322}
323} // namespace TeleInfo
324
325#endif /* TELEINFO_H */
The telemetry frame of the IoT output.
Definition teleinfo.h:37
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).
Definition teleinfo.h:157
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.
Definition teleinfo.h:304
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...
Definition teleinfo.h:60
constexpr uint8_t lineSize(uint8_t tagLength, uint8_t valueLength)
The size of a line: LF, tag, TAB, value, TAB, checksum and CR.
Definition teleinfo.h:51
constexpr char STX
Definition teleinfo.h:38
constexpr char ETX
Definition teleinfo.h:39
char * frameLine(uint8_t step, char *dst)
Writes the line of a step.
Definition teleinfo.h:199
constexpr char LF
Definition teleinfo.h:40
constexpr char CR
Definition teleinfo.h:41
constexpr char TAB
Definition teleinfo.h:42
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.
Definition teleinfo.h:100
constexpr uint8_t frameSteps()
The number of steps of a frame: one per line, invalid temperatures included (they write nothing).
Definition teleinfo.h:167
char * writeDigits(char *dst, uint16_t value)
Writes the decimal digits of a 16-bit value.
Definition teleinfo.h:69
char * line(char *dst, const char *tag, int16_t value, uint8_t index=0)
Writes the line of a signed value.
Definition teleinfo.h:142