Skip to content

Commit e8e1cda

Browse files
authored
chore: migrate Arduino API calls to note-c abstractions (#150)
* chore: migrate Arduino API calls to note-c abstractions * chore: fix all broken test Several test broke when moving from native Arduino calls to based abstractions
1 parent 6a362dc commit e8e1cda

12 files changed

+127
-43
lines changed

src/NoteI2c_Arduino.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "NoteI2c_Arduino.hpp"
22

3+
#include "Notecard.h"
4+
35
#if defined(NOTE_C_LOW_MEM)
46
static const char *i2cerr = "i2c {io}";
57
#endif
@@ -105,7 +107,7 @@ NoteI2c_Arduino::receive (
105107
if (!transmission_error) {
106108
// Delay briefly ensuring that the Notecard can
107109
// deliver the data in real-time to the I2C ISR
108-
::delay(2);
110+
NoteDelayMs(2);
109111

110112
const int request_length = requested_byte_count_ + NoteI2c::REQUEST_HEADER_SIZE;
111113
const int response_length = _i2cPort.requestFrom((int)device_address_, request_length);
@@ -139,7 +141,7 @@ NoteI2c_Arduino::receive (
139141
// Flash stalls have been observed on the Notecard ESP. Delaying
140142
// between retries provides time for the Notecard to recover from
141143
// the resource contention.
142-
::delay(1000);
144+
NoteDelayMs(1000);
143145
NOTE_C_LOG_ERROR(result);
144146
NOTE_C_LOG_WARN("serial-over-i2c: reattempting to read Notecard response");
145147
} while (result && (i++ < retry_count));

src/NoteI2c_Arduino.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include "NoteI2c.hpp"
55

6-
#include "Notecard.h"
7-
86
#ifndef NOTE_MOCK
97
#include <Wire.h>
108
#else

src/NoteSerial_Arduino.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#include "NoteSerial_Arduino.hpp"
22

3-
#include "NoteDefines.h"
3+
#include "Notecard.h"
44

55
#ifndef NOTE_MOCK
66
#ifdef NOTE_ARDUINO_SOFTWARE_SERIAL_SUPPORT
77
#include <SoftwareSerial.h>
88
#endif
99
#else
1010
#include "mock/mock-arduino.hpp"
11+
#include "mock/mock-parameters.hpp"
1112
#endif
1213

1314
#define NOTE_C_SERIAL_TIMEOUT_MS 3500
@@ -68,7 +69,10 @@ NoteSerial_Arduino<T>::NoteSerial_Arduino
6869
_notecardSerial.begin(_notecardSerialSpeed);
6970

7071
// Wait for the serial port to be ready
71-
for (const size_t startMs = ::millis() ; !_notecardSerial && ((::millis() - startMs) < NOTE_C_SERIAL_TIMEOUT_MS) ;);
72+
for (const size_t startMs = NoteGetMs()
73+
; !_notecardSerial && ((NoteGetMs() - startMs) < NOTE_C_SERIAL_TIMEOUT_MS)
74+
;
75+
);
7276
}
7377

7478
template <typename T>

src/NoteTxn_Arduino.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "NoteTxn_Arduino.hpp"
22

3+
#include "Notecard.h"
4+
35
#ifndef NOTE_MOCK
46
#include <Arduino.h>
57
#else
@@ -78,9 +80,9 @@ NoteTxn_Arduino::start (
7880

7981
// Await Clear To Transact Signal
8082
::pinMode(_ctx_pin, INPUT_PULLUP);
81-
for (uint32_t timeout = (::millis() + timeout_ms_)
82-
; ::millis() < timeout
83-
; ::delay(1)
83+
for (uint32_t timeout = (NoteGetMs() + timeout_ms_)
84+
; NoteGetMs() < timeout
85+
; NoteDelayMs(1)
8486
) {
8587
if (HIGH == ::digitalRead(_ctx_pin)) {
8688
result = true;

test/NoteI2c_Arduino.test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include "NoteI2c_Arduino.hpp"
22
#include "TestFunction.hpp"
3-
#include "mock/mock-arduino.hpp"
4-
#include "mock/mock-parameters.hpp"
53

64
#include <cassert>
75
#include <cstring>
86

9-
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteI2c_Arduino.cpp NoteI2c_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteI2c_arduino.tests && ./noteI2c_arduino.tests || echo "Tests Result: $?"
7+
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteI2c_Arduino.cpp NoteI2c_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteI2c_arduino.tests && ./noteI2c_arduino.tests || echo "Tests Result: $?"
108

119
int test_make_note_i2c_instantiates_notei2c_object()
1210
{

test/NoteLog_Arduino.test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "NoteLog_Arduino.hpp"
22
#include "TestFunction.hpp"
3-
#include "mock/mock-arduino.hpp"
4-
#include "mock/mock-parameters.hpp"
53

64
#include <cassert>
75
#include <cstring>
6+
#include <iostream>
87

98
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteLog_Arduino.cpp NoteLog_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteLog_arduino.tests && ./noteLog_arduino.tests || echo "Tests Result: $?"
109

test/NoteSerial_Arduino.test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include "NoteSerial_Arduino.hpp"
22
#include "TestFunction.hpp"
3-
#include "mock/mock-arduino.hpp"
4-
#include "mock/mock-parameters.hpp"
53

64
#include <cassert>
75
#include <cstring>
86
#include <iostream>
97

10-
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteSerial_Arduino.cpp NoteSerial_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteSerial_arduino.tests && ./noteSerial_arduino.tests || echo "Tests Result: $?"
8+
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteSerial_Arduino.cpp NoteSerial_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteSerial_arduino.tests && ./noteSerial_arduino.tests || echo "Tests Result: $?"
119

1210
int test_make_note_serial_instantiates_noteserial_object()
1311
{

test/NoteTxn_Arduino.test.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <cassert>
77

8-
// Compile command: g++ -std=c++11 -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteTxn_Arduino.cpp NoteTxn_Arduino.test.cpp -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteTxn_arduino.tests && ./noteTxn_arduino.tests || echo "Tests Result: $?"
8+
// Compile command: g++ -std=c++11 -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteTxn_Arduino.cpp NoteTxn_Arduino.test.cpp -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteTxn_arduino.tests && ./noteTxn_arduino.tests || echo "Tests Result: $?"
99

1010
int test_make_note_txn_instantiates_notetxn_object()
1111
{
@@ -288,8 +288,8 @@ int test_notetxn_arduino_start_blocks_until_ctx_pin_goes_high()
288288
digitalRead_Parameters.result[CTX_PIN].push_back(LOW);
289289
digitalRead_Parameters.result[CTX_PIN].push_back(LOW);
290290
digitalRead_Parameters.result[CTX_PIN].push_back(HIGH);
291-
millis_Parameters.reset();
292-
millis_Parameters.default_result = (TIMEOUT_MS - 1);
291+
noteGetMs_Parameters.reset();
292+
noteGetMs_Parameters.default_result = (TIMEOUT_MS - 1);
293293

294294
// Action
295295
notetxn.start(TIMEOUT_MS);
@@ -298,7 +298,7 @@ int test_notetxn_arduino_start_blocks_until_ctx_pin_goes_high()
298298
if (
299299
digitalRead_Parameters.invoked[CTX_PIN] == digitalRead_Parameters.result[CTX_PIN].size()
300300
&& digitalRead_Parameters.result[CTX_PIN][(digitalRead_Parameters.invoked[CTX_PIN] - 1)] == HIGH
301-
&& millis_Parameters.result.empty() // Only returns value less than `TIMEOUT_MS`
301+
&& noteGetMs_Parameters.result.empty() // Only returns value less than `TIMEOUT_MS`
302302
) {
303303
result = 0;
304304
}
@@ -330,12 +330,13 @@ int test_notetxn_arduino_start_blocks_until_timeout_ms()
330330
NoteTxn_Arduino notetxn(CTX_PIN, RTX_PIN);
331331
digitalRead_Parameters.reset();
332332
digitalRead_Parameters.default_result[CTX_PIN] = LOW;
333-
millis_Parameters.reset();
334-
millis_Parameters.result.push_back(36);
335-
millis_Parameters.result.push_back(274);
336-
millis_Parameters.result.push_back(515);
337-
millis_Parameters.result.push_back(801);
338-
millis_Parameters.result.push_back(TIMEOUT_MS + millis_Parameters.result[0]);
333+
noteDelayMs_Parameters.reset();
334+
noteGetMs_Parameters.reset();
335+
noteGetMs_Parameters.result.push_back(36);
336+
noteGetMs_Parameters.result.push_back(274);
337+
noteGetMs_Parameters.result.push_back(515);
338+
noteGetMs_Parameters.result.push_back(801);
339+
noteGetMs_Parameters.result.push_back(TIMEOUT_MS + noteGetMs_Parameters.result[0]);
339340

340341
// Action
341342
notetxn.start(TIMEOUT_MS);
@@ -344,7 +345,7 @@ int test_notetxn_arduino_start_blocks_until_timeout_ms()
344345
if (
345346
digitalRead_Parameters.invoked[CTX_PIN] // Called at least once
346347
&& digitalRead_Parameters.result[CTX_PIN].empty() // Only returns `LOW`
347-
&& millis_Parameters.invoked == millis_Parameters.result.size()
348+
&& noteGetMs_Parameters.invoked == noteGetMs_Parameters.result.size()
348349
) {
349350
result = 0;
350351
}
@@ -412,7 +413,7 @@ int test_notetxn_arduino_start_leaves_rtx_pin_high_when_ctx_responds_high()
412413
digitalRead_Parameters.reset();
413414
digitalRead_Parameters.default_result[CTX_PIN] = HIGH;
414415
digitalWrite_Parameters.reset();
415-
millis_Parameters.reset();
416+
noteGetMs_Parameters.reset();
416417

417418
// Action
418419
notetxn.start(TIMEOUT_MS);
@@ -486,12 +487,12 @@ int test_notetxn_arduino_start_leaves_ctx_pin_floating_on_timeout()
486487
NoteTxn_Arduino notetxn(CTX_PIN, RTX_PIN);
487488
digitalRead_Parameters.reset();
488489
digitalRead_Parameters.default_result[CTX_PIN] = LOW;
489-
millis_Parameters.reset();
490-
millis_Parameters.result.push_back(36);
491-
millis_Parameters.result.push_back(274);
492-
millis_Parameters.result.push_back(515);
493-
millis_Parameters.result.push_back(801);
494-
millis_Parameters.result.push_back(TIMEOUT_MS + millis_Parameters.result[0]);
490+
noteGetMs_Parameters.reset();
491+
noteGetMs_Parameters.result.push_back(36);
492+
noteGetMs_Parameters.result.push_back(274);
493+
noteGetMs_Parameters.result.push_back(515);
494+
noteGetMs_Parameters.result.push_back(801);
495+
noteGetMs_Parameters.result.push_back(TIMEOUT_MS + noteGetMs_Parameters.result[0]);
495496
pinMode_Parameters.reset();
496497

497498
// Action
@@ -501,7 +502,7 @@ int test_notetxn_arduino_start_leaves_ctx_pin_floating_on_timeout()
501502
if (
502503
digitalRead_Parameters.invoked[CTX_PIN] // Called at least once
503504
&& digitalRead_Parameters.result[CTX_PIN].empty() // Only returns `LOW`
504-
&& millis_Parameters.invoked == millis_Parameters.result.size()
505+
&& noteGetMs_Parameters.invoked == noteGetMs_Parameters.result.size()
505506
&& pinMode_Parameters.invoked[CTX_PIN] > 1 // Called at least twice
506507
&& pinMode_Parameters.pin_mode[CTX_PIN][(pinMode_Parameters.invoked[CTX_PIN] - 1)] == INPUT
507508
) {
@@ -532,12 +533,12 @@ int test_notetxn_arduino_start_floats_rtx_pin_on_timeout()
532533
NoteTxn_Arduino notetxn(CTX_PIN, RTX_PIN);
533534
digitalRead_Parameters.reset();
534535
digitalRead_Parameters.default_result[CTX_PIN] = LOW;
535-
millis_Parameters.reset();
536-
millis_Parameters.result.push_back(36);
537-
millis_Parameters.result.push_back(274);
538-
millis_Parameters.result.push_back(515);
539-
millis_Parameters.result.push_back(801);
540-
millis_Parameters.result.push_back(TIMEOUT_MS + millis_Parameters.result[0]);
536+
noteGetMs_Parameters.reset();
537+
noteGetMs_Parameters.result.push_back(36);
538+
noteGetMs_Parameters.result.push_back(274);
539+
noteGetMs_Parameters.result.push_back(515);
540+
noteGetMs_Parameters.result.push_back(801);
541+
noteGetMs_Parameters.result.push_back(TIMEOUT_MS + noteGetMs_Parameters.result[0]);
541542
pinMode_Parameters.reset();
542543

543544
// Action
@@ -547,7 +548,7 @@ int test_notetxn_arduino_start_floats_rtx_pin_on_timeout()
547548
if (
548549
digitalRead_Parameters.invoked[CTX_PIN] // Called at least once
549550
&& digitalRead_Parameters.result[CTX_PIN].empty() // Only returns `LOW`
550-
&& millis_Parameters.invoked == millis_Parameters.result.size()
551+
&& noteGetMs_Parameters.invoked == noteGetMs_Parameters.result.size()
551552
&& pinMode_Parameters.pin_mode[RTX_PIN][(pinMode_Parameters.invoked[RTX_PIN] - 1)] == INPUT
552553
) {
553554
result = 0;

test/mock/mock-arduino.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct Delay_Parameters {
3939
void
4040
) {
4141
invoked = 0;
42+
mock_time = false;
4243
ms = 0;
4344
}
4445
size_t invoked;

test/mock/mock-note-c-note.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
JAddIntToObject_Parameters jAddIntToObject_Parameters;
44
NoteDebug_Parameters noteDebug_Parameters;
55
NoteDebugSyncStatus_Parameters noteDebugSyncStatus_Parameters;
6+
NoteDelayMs_Parameters noteDelayMs_Parameters;
67
NoteDeleteResponse_Parameters noteDeleteResponse_Parameters;
78
NoteGetFnI2C_Parameters noteGetFnI2C_Parameters;
89
NoteGetFnSerial_Parameters noteGetFnSerial_Parameters;
10+
NoteGetMs_Parameters noteGetMs_Parameters;
911
NoteNewCommand_Parameters noteNewCommand_Parameters;
1012
NoteNewRequest_Parameters noteNewRequest_Parameters;
1113
NoteRequest_Parameters noteRequest_Parameters;
@@ -104,6 +106,22 @@ NoteDebugSyncStatus(
104106
return noteDebugSyncStatus_Parameters.result;
105107
}
106108

109+
void
110+
NoteDelayMs (
111+
uint32_t ms_
112+
) {
113+
// Record invocation(s)
114+
++noteDelayMs_Parameters.invoked;
115+
116+
// Stash parameter(s)
117+
noteDelayMs_Parameters.ms = ms_;
118+
119+
// Emulate the passing time for timeout loops
120+
if (noteDelayMs_Parameters.mock_time) {
121+
noteGetMs_Parameters.default_result += ms_;
122+
}
123+
}
124+
107125
void NoteGetFnI2C(uint32_t *notecardAddr, uint32_t *maxTransmitSize,
108126
i2cResetFn *resetFn, i2cTransmitFn *transmitFn,
109127
i2cReceiveFn *receiveFn
@@ -163,6 +181,21 @@ void NoteGetFnSerial(serialResetFn *resetFn, serialTransmitFn *transmitFn,
163181
}
164182
}
165183

184+
uint32_t
185+
NoteGetMs(
186+
void
187+
) {
188+
// Record invocation(s)
189+
++noteGetMs_Parameters.invoked;
190+
191+
// Return user-supplied result
192+
if (noteGetMs_Parameters.result.size() < noteGetMs_Parameters.invoked) {
193+
return noteGetMs_Parameters.default_result;
194+
} else {
195+
return noteGetMs_Parameters.result[(noteGetMs_Parameters.invoked - 1)];
196+
}
197+
}
198+
166199
J *
167200
NoteNewCommand(
168201
const char * request_

0 commit comments

Comments
 (0)