3#include <ArduinoJson.h>
22 size_t write(uint8_t c)
override
56 out.print(F(
"field"));
58 out.print(F(
" = some value; "));
61 out.println(F(
"end"));
82 TEST_ASSERT_EQUAL(0,
capture.length);
99 TEST_ASSERT_FALSE(
capture.overflow);
100 TEST_ASSERT_GREATER_THAN(1, polls);
101 TEST_ASSERT_EQUAL_STRING(
"field0 = some value; field1 = some value; field2 = some value; field3 = some value; "
102 "field4 = some value; field5 = some value; field6 = some value; field7 = some value; "
103 "field8 = some value; field9 = some value; end\r\n",
117 uint16_t mismatches{ 0 };
121 for (int32_t value = -5500; value <= 12500; ++value)
124 StaticJsonDocument< 16 > doc;
125 doc.set(
static_cast< float >(value) * 0.01F);
126 serializeJson(doc, json,
sizeof json);
131 const char* point{ strchr(
capture.text,
'.') };
132 const size_t decimals{ point ? strlen(point + 1) : 0 };
133 const bool shortest{ decimals <= 2 && (!point ||
capture.text[
capture.length - 1] !=
'0') };
134 const bool sameValue{ fabs(atof(json) - atof(
capture.text)) <= 1e-5 * (1 + fabs(atof(json))) };
136 noise += strcmp(json,
capture.text) != 0;
137 if (!(shortest && sameValue) && !mismatches++)
139 snprintf(message,
sizeof message,
"%ld: ArduinoJson '%s', printHundredths '%s'",
static_cast< long >(value), json,
143 TEST_ASSERT_EQUAL_MESSAGE(0, mismatches, message);
144 TEST_ASSERT_LESS_THAN(1000, noise);
150 TEST_ASSERT_EQUAL_STRING(
"-327.68",
capture.text);
154 TEST_ASSERT_EQUAL_STRING(
"327.67",
capture.text);
158 TEST_ASSERT_EQUAL_STRING(
"-0.05",
capture.text);
164 uint16_t mismatches{ 0 };
168 for (int32_t value = -5500; value <= 12500; ++value)
171 capture.print(
static_cast< float >(value) * 0.01F);
179 snprintf_P(message,
sizeof message, PSTR(
"%ld: print(float) '%s', printDecimals '%s'"),
static_cast< long >(value),
183 TEST_ASSERT_EQUAL_MESSAGE(0, mismatches, message);
190 strlcpy_P(text, expected,
sizeof text);
191 TEST_ASSERT_EQUAL_STRING(text,
capture.text);
224 constexpr float values[]{ 0.05F, 0.0612F };
226 static_assert(table.value[0] == 50000 && table.value[1] == 61200,
"array version");
297 out.write(text, length);
313 TEST_ASSERT_LESS_THAN(
sizeof whole,
capture.length);
315 TEST_ASSERT_EQUAL_CHAR(0x02, whole[0]);
316 TEST_ASSERT_EQUAL_CHAR(0x03, whole[
capture.length - 1]);
329 TEST_ASSERT_FALSE(
capture.overflow);
330 TEST_ASSERT_GREATER_THAN(1, polls);
331 TEST_ASSERT_EQUAL_STRING(whole,
capture.text);
340 const uint8_t startup{ UCSR0C };
342 const uint8_t iot{ UCSR0C };
346 const uint8_t other{ UCSR0C };
350 TEST_ASSERT_EQUAL_HEX8(SERIAL_8N1, startup);
351 TEST_ASSERT_EQUAL_HEX8(SERIAL_7E1, iot);
352 TEST_ASSERT_EQUAL_HEX8(SERIAL_8N1, other);
A Print that captures the text, with a transmit buffer that fills up as it is written.
int availableForWrite() override
size_t write(uint8_t c) override
void test_start_while_busy_is_refused(void)
void test_printHundredths_extremes(void)
void test_poll_never_overfills_and_completes(void)
void test_teleinfo_written_in_parts(void)
void test_printHundredths_matches_arduinojson(void)
void test_startup_text_serial_settings()
The startup text at 8N1, then 7E1 for the IoT output only (UART0 frame format in UCSR0C)
void test_toDecimals_at_compile_time(void)
void test_poll_waits_for_room(void)
void test_printDecimals_matches_print_float(void)
void assertCaptured(PGM_P expected)
bool fieldsStep(Print &out, uint8_t step)
void test_printDecimals_other_decimals_and_extremes(void)
bool frameStep(Print &out, uint8_t step)
Stops the MCU once an embedded test suite has run.
void printHundredths(Print &out, int16_t value_x100)
Print a value given in hundredths, in its shortest decimal form.
constexpr int32_t toDecimals(float value, uint8_t decimals)
A float constant as an integer with the given number of decimals, rounded.
void beginStartupText()
Serial settings for the startup text: 9600 baud 8N1, the default of serial monitors.
constexpr uint8_t MAX_STEP_LENGTH
bool start(Step step)
Start an output, written by later calls to poll()
void endStartupText()
Serial settings after the startup text: 7E1 for the IoT telemetry frames, as the mk2Wifi module expec...
bool busy()
Whether an output is still in progress.
void poll(Print &out=Serial)
Write as many steps as the transmit buffer can take, without waiting.
void printDecimals(Print &out, int32_t value, uint8_t decimals)
Print a value given as an integer with a fixed number of decimals (see toDecimals())
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...
static double reference(int32_t averagePower, float cal)
The float arithmetic being replaced, in bucket units.
Non-blocking serial output of the datalog.
The data of a small IoT frame: P, P1, V1, P2, V2, P3, V3, D1, T1, N, S, S_MC (3 phases,...
static bool temperatureValid(uint8_t)
static int16_t temperature(uint8_t)
static constexpr uint8_t LOAD_COUNT
static uint16_t phaseVoltage(uint8_t)
static uint8_t lowestSampleSetsPerCycle()
static constexpr uint8_t PHASE_COUNT
static int16_t phasePower(uint8_t)
static constexpr uint8_t TEMP_COUNT
static uint16_t sampleSets()
static uint16_t absenceCount()
static constexpr bool RELAY_DIVERSION
static int16_t relayAverage()
static int16_t loadOnPercent(uint8_t)
static bool relayOn(uint8_t)
static constexpr uint8_t RELAY_COUNT
static constexpr bool DUAL_TARIFF
The telemetry frame of the IoT output, written a line at a time.