Skip to content

Commit 2a5b1b1

Browse files
zfieldsrayozzie
andauthored
Zak txn (#92)
feat: NoteTxn * chore: clarify cts/rts to ctx/rtx * fix: add a slight delay before asking the i2c ISR for data, for i2c robustness * chore: Refactor NoteTxn impl * chore: Create NoteTxn Mock/Test Infrastructure * chore: expand Arduino mocks * chore: simplify compilation * chore: Remove unnecessary inclusion of `mock-note-c-note.c` from I2C unit-tests * fix: `launch.json` whitespace * feat: notetxn callback tests * feat: notecard::setTransactionPins tests * chore: optimize CTX pin handling * fix: Incorporate Ray's feedback - Update initial pin configuration - Remove superfluous pin instruction Co-authored-by: Ray Ozzie <[email protected]>
1 parent 9893305 commit 2a5b1b1

20 files changed

+1814
-125
lines changed

.devcontainer/devcontainer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515

1616
// Add the IDs of extensions you want installed when the container is created.
1717
"extensions": [
18-
"ms-vscode.cpptools",
19-
],
18+
"ms-vscode.cpptools"
19+
]
2020

2121
// Use 'forwardPorts' to make a list of ports inside the container available locally.
2222
// "forwardPorts": [],
23+
2324
// Uncomment the next line to run commands after the container is created - for example installing curl.
2425
// "postCreateCommand": "apt-get update && apt-get install -y curl",
26+
2527
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
2628
// "runArgs": [
2729
// "--device=/dev/bus/usb/"
2830
// ],
2931

3032
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
3133
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
34+
3235
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
3336
// "remoteUser": "blues"
3437
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*.gcno
66
*.gcov
77
vgcore.*
8+
*.tests
89

910
.vscode/
1011
build/

.vscode/launch.json

+55-43
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,86 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"name": "Debug Failed Unit-tests",
9-
"type": "cppdbg",
10-
"request": "launch",
11-
"program": "${workspaceRoot}/failed_test_run",
8+
"name": "Debug Failed Unit-tests",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${workspaceRoot}/failed_test_run",
1212
"args": [],
13-
"cwd": "${workspaceRoot}",
14-
"MIMode": "gdb",
13+
"cwd": "${workspaceRoot}",
14+
"MIMode": "gdb",
1515
"miDebuggerPath": "/usr/bin/gdb"
16-
},
16+
},
1717
{
18-
"name": "Debug Notecard Unit-tests",
19-
"type": "cppdbg",
20-
"request": "launch",
21-
"program": "${workspaceRoot}/notecard.tests",
18+
"name": "Debug Notecard Unit-tests",
19+
"type": "cppdbg",
20+
"request": "launch",
21+
"program": "${workspaceRoot}/notecard.tests",
2222
"args": [],
23-
"cwd": "${workspaceRoot}",
23+
"cwd": "${workspaceRoot}",
2424
"preLaunchTask": "Compile Notecard Tests",
2525
"postDebugTask": "Clear Test Binaries",
26-
"MIMode": "gdb",
26+
"MIMode": "gdb",
2727
"miDebuggerPath": "/usr/bin/gdb"
28-
},
28+
},
2929
{
30-
"name": "Debug NoteI2c Arduino Unit-tests",
31-
"type": "cppdbg",
32-
"request": "launch",
33-
"program": "${workspaceRoot}/notei2c_arduino.tests",
30+
"name": "Debug NoteI2c Arduino Unit-tests",
31+
"type": "cppdbg",
32+
"request": "launch",
33+
"program": "${workspaceRoot}/notei2c_arduino.tests",
3434
"args": [],
35-
"cwd": "${workspaceRoot}",
35+
"cwd": "${workspaceRoot}",
3636
"preLaunchTask": "Compile NoteI2c Arduino Tests",
3737
"postDebugTask": "Clear Test Binaries",
38-
"MIMode": "gdb",
38+
"MIMode": "gdb",
3939
"miDebuggerPath": "/usr/bin/gdb"
40-
},
40+
},
4141
{
42-
"name": "Debug NoteI2c Arduino Unit-tests (-DWIRE_HAS_END)",
43-
"type": "cppdbg",
44-
"request": "launch",
45-
"program": "${workspaceRoot}/notei2c_arduino_wire_has_end.tests",
42+
"name": "Debug NoteI2c Arduino Unit-tests (-DWIRE_HAS_END)",
43+
"type": "cppdbg",
44+
"request": "launch",
45+
"program": "${workspaceRoot}/notei2c_arduino_wire_has_end.tests",
4646
"args": [],
47-
"cwd": "${workspaceRoot}",
47+
"cwd": "${workspaceRoot}",
4848
"preLaunchTask": "Compile NoteI2c Arduino Tests (-DWIRE_HAS_END)",
4949
"postDebugTask": "Clear Test Binaries",
50-
"MIMode": "gdb",
50+
"MIMode": "gdb",
5151
"miDebuggerPath": "/usr/bin/gdb"
52-
},
52+
},
5353
{
54-
"name": "Debug NoteLog Arduino Unit-tests",
55-
"type": "cppdbg",
56-
"request": "launch",
57-
"program": "${workspaceRoot}/notelog_arduino.tests",
54+
"name": "Debug NoteLog Arduino Unit-tests",
55+
"type": "cppdbg",
56+
"request": "launch",
57+
"program": "${workspaceRoot}/notelog_arduino.tests",
5858
"args": [],
59-
"cwd": "${workspaceRoot}",
59+
"cwd": "${workspaceRoot}",
6060
"preLaunchTask": "Compile NoteLog Arduino Tests",
6161
"postDebugTask": "Clear Test Binaries",
62-
"MIMode": "gdb",
62+
"MIMode": "gdb",
6363
"miDebuggerPath": "/usr/bin/gdb"
64-
},
64+
},
6565
{
66-
"name": "Debug NoteSerial Arduino Unit-tests",
67-
"type": "cppdbg",
68-
"request": "launch",
69-
"program": "${workspaceRoot}/noteserial_arduino.tests",
66+
"name": "Debug NoteSerial Arduino Unit-tests",
67+
"type": "cppdbg",
68+
"request": "launch",
69+
"program": "${workspaceRoot}/noteserial_arduino.tests",
7070
"args": [],
71-
"cwd": "${workspaceRoot}",
71+
"cwd": "${workspaceRoot}",
7272
"preLaunchTask": "Compile NoteSerial Arduino Tests",
7373
"postDebugTask": "Clear Test Binaries",
74-
"MIMode": "gdb",
74+
"MIMode": "gdb",
7575
"miDebuggerPath": "/usr/bin/gdb"
76-
},
76+
},
77+
{
78+
"name": "Debug NoteTxn Arduino Unit-tests",
79+
"type": "cppdbg",
80+
"request": "launch",
81+
"program": "${workspaceRoot}/notetxn_arduino.tests",
82+
"args": [],
83+
"cwd": "${workspaceRoot}",
84+
"preLaunchTask": "Compile NoteTxn Arduino Tests",
85+
"postDebugTask": "Clear Test Binaries",
86+
"MIMode": "gdb",
87+
"miDebuggerPath": "/usr/bin/gdb"
88+
},
7789
]
78-
}
90+
}

.vscode/tasks.json

+33
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
"test/mock/NoteI2c_Mock.cpp",
3131
"test/mock/NoteLog_Mock.cpp",
3232
"test/mock/NoteSerial_Mock.cpp",
33+
"test/mock/NoteTime_Mock.cpp",
34+
"test/mock/NoteTxn_Mock.cpp",
3335
"src/Notecard.cpp",
3436
"test/Notecard.test.cpp",
3537
"-std=c++11",
@@ -176,6 +178,37 @@
176178
"isDefault": false
177179
}
178180
},
181+
{
182+
"type": "cppbuild",
183+
"label": "Compile NoteTxn Arduino Tests",
184+
"command": "g++",
185+
"args": [
186+
"-Wall",
187+
"-Wextra",
188+
"-Wpedantic",
189+
"test/mock/mock-arduino.cpp",
190+
"test/mock/mock-note-c-note.c",
191+
"src/NoteTxn_Arduino.cpp",
192+
"test/NoteTxn_Arduino.test.cpp",
193+
"-std=c++11",
194+
"-Itest",
195+
"-Isrc",
196+
"-DNOTE_MOCK",
197+
"-o",
198+
"notetxn_arduino.tests",
199+
"-g",
200+
],
201+
"options": {
202+
"cwd": "${workspaceFolder}"
203+
},
204+
"problemMatcher": [
205+
"$gcc"
206+
],
207+
"group": {
208+
"kind": "build",
209+
"isDefault": false
210+
}
211+
},
179212
{
180213
"type": "shell",
181214
"label": "Clear Test Binaries",

src/NoteI2c_Arduino.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ NoteI2c_Arduino::receive (
7474
}
7575
}
7676

77+
// Delay briefly ensuring that the Notecard can
78+
// deliver the data in real-time to the I2C ISR
79+
::delay(2);
80+
7781
// Read and cache response from Notecard
7882
if (!transmission_error) {
7983
const int request_length = requested_byte_count_ + NoteI2c::REQUEST_HEADER_SIZE;

src/NoteTxn.hpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef NOTE_TXN_HPP
2+
#define NOTE_TXN_HPP
3+
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
class NoteTxn
8+
{
9+
public:
10+
/**************************************************************************/
11+
/*!
12+
@brief Type used to abstract specific hardware implementation types.
13+
*/
14+
/**************************************************************************/
15+
typedef void * param_t;
16+
17+
virtual ~NoteTxn(void) {}
18+
19+
/**************************************************************************/
20+
/*!
21+
@brief A blocking call used to initiate a transaction with the Notecard
22+
@param[in] timeout_ms
23+
The number of milliseconds to wait before aborting the request
24+
@returns A boolean indicating success.
25+
*/
26+
/**************************************************************************/
27+
virtual bool start (uint32_t timeout_ms) = 0;
28+
29+
/**************************************************************************/
30+
/*!
31+
@brief Non-blocking call to terminate the transaction
32+
*/
33+
/**************************************************************************/
34+
virtual void stop (void) = 0;
35+
};
36+
37+
/******************************************************************************/
38+
/*!
39+
@brief Helper function to abstract, create and maintain a single instance
40+
of the NoteTxn interface implementation, as required by the underlying
41+
`note-c` library.
42+
@param[in] txn_parameters
43+
Pointer to the parameters required to instantiate
44+
the platform specific transaction implementation.
45+
*/
46+
/******************************************************************************/
47+
NoteTxn * make_note_txn (
48+
NoteTxn::param_t txn_parameters
49+
);
50+
51+
#endif // NOTE_TXN_HPP

src/NoteTxn_Arduino.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "NoteTxn_Arduino.hpp"
2+
3+
#ifndef NOTE_MOCK
4+
#include <Arduino.h>
5+
#else
6+
#include "mock/mock-arduino.hpp"
7+
#include "mock/mock-parameters.hpp"
8+
#endif
9+
10+
NoteTxn *
11+
make_note_txn (
12+
NoteTxn::param_t txn_parameters_
13+
)
14+
{
15+
static NoteTxn * note_txn = nullptr;
16+
if (!txn_parameters_) {
17+
if (note_txn) {
18+
delete note_txn;
19+
note_txn = nullptr;
20+
}
21+
} else if (!note_txn) {
22+
const uint8_t * txn_pins = reinterpret_cast<uint8_t *>(txn_parameters_);
23+
note_txn = new NoteTxn_Arduino(txn_pins[0], txn_pins[1]);
24+
}
25+
return note_txn;
26+
}
27+
28+
NoteTxn_Arduino::NoteTxn_Arduino
29+
(
30+
uint8_t ctx_pin_,
31+
uint8_t rtx_pin_
32+
) :
33+
_ctx_pin(ctx_pin_),
34+
_rtx_pin(rtx_pin_)
35+
{
36+
// Float CTX/RTX to conserve energy
37+
::pinMode(_ctx_pin, INPUT);
38+
::pinMode(_rtx_pin, INPUT);
39+
}
40+
41+
bool
42+
NoteTxn_Arduino::start (
43+
uint32_t timeout_ms_
44+
)
45+
{
46+
bool result = false;
47+
48+
// Signal Request To Transact
49+
::pinMode(_rtx_pin, OUTPUT);
50+
::digitalWrite(_rtx_pin, HIGH);
51+
52+
// Await Clear To Transact Signal
53+
::pinMode(_ctx_pin, INPUT_PULLUP);
54+
for (uint32_t timeout = (::millis() + timeout_ms_)
55+
; ::millis() < timeout
56+
; ::delay(1)
57+
) {
58+
if (HIGH == ::digitalRead(_ctx_pin)) {
59+
result = true;
60+
break;
61+
}
62+
}
63+
64+
// Float CTX to conserve energy
65+
::pinMode(_ctx_pin, INPUT);
66+
67+
// Abandon request on timeout
68+
if (!result) {
69+
stop();
70+
}
71+
72+
return result;
73+
}
74+
75+
void
76+
NoteTxn_Arduino::stop (
77+
void
78+
)
79+
{
80+
// Float RTX pin
81+
::pinMode(_rtx_pin, INPUT);
82+
}

src/NoteTxn_Arduino.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef NOTE_TXN_ARDUINO_HPP
2+
#define NOTE_TXN_ARDUINO_HPP
3+
4+
#include "NoteTxn.hpp"
5+
6+
class NoteTxn_Arduino final : public NoteTxn
7+
{
8+
public:
9+
NoteTxn_Arduino (uint8_t ctx_pin, uint8_t rtx_pin);
10+
bool start (uint32_t timeout_ms) override;
11+
void stop (void) override;
12+
13+
private:
14+
uint8_t _ctx_pin;
15+
uint8_t _rtx_pin;
16+
};
17+
18+
#endif // NOTE_TXN_ARDUINO_HPP

0 commit comments

Comments
 (0)