Implement to the requirements
A battery monitor specified by four numbered requirements, graded by tests named after them. A medium embedded C problem (Requirements and design notes), run…
medium · Requirements and design notes · requirements · state-machine · hysteresis · testing
Write bat_sample(b, mv), called with each new battery reading in millivolts, and bat_reset(b). bat_sample returns the status to report: 'C' critical, 'F' sensor fault, 'L' low or 'O' OK, checked in that order of priority. Implement exactly these requirements:
- REQ-1 The low-battery warning shall be raised when 3 consecutive valid samples are below 3400 mV.
- REQ-2 The low-battery warning shall clear only when a valid sample is above 3500 mV. A valid sample of 3400 mV or more resets the consecutive count.
- REQ-3 A valid sample below 3000 mV shall put the monitor into the critical state at once. Critical is latched: only
bat_resetleaves it, andbat_resetreturns the monitor to its initial state. - REQ-4 A sample of 0 mV or above 5000 mV is invalid. It shall be reported as a sensor fault until the next valid sample, and shall not change the consecutive count or the warning.
Starter code
char bat_sample(bat_t *b, uint16_t mv) {
if (mv < 3400) return 'L';
return 'O';
}
void bat_reset(bat_t *b) {
}Example tests
REQ-1 and REQ-2: three low samples, then hysteresis
bat_t b = {0};
uint16_t mv[] = { 3300, 3300, 3300, 3450, 3500, 3501 };
run(&b, mv, 6);Expected output:
OOLLLOREQ-3: critical latches until reset
bat_t b = {0};
uint16_t a[] = { 3600, 2999, 3600, 4200 };
run(&b, a, 4);
bat_reset(&b);
uint16_t c[] = { 3600 };
run(&b, c, 1);Expected output:
OCCC
OLearn it first
- Requirements and design notes · Testable requirements, traceability, one-page design notes and the documents a firmware team keeps.
More Requirements and design notes problems
- Check requirements traceability · Report requirements that no test verifies, and tests that point at requirements that do not exist.