Skip to content

Commit 667dba5

Browse files
committed
add some information about mocking
1 parent 2187a9b commit 667dba5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,55 @@ int main(int argc, char **argv)
117117
}
118118
```
119119
120+
## Mocking sensors in development
121+
122+
The library provides a mock implementation of the `ProximityConnection` class that can be used for testing and development purposes. This allows you to simulate the behavior of a real sensor without needing to connect to an actual device.
123+
124+
In this snippet, we create a mock connection, construct a mock message, and set the mock data to be returned by the mock connection. The mock message is then encoded and used to simulate the behavior of a real sensor.
125+
126+
```cpp
127+
ProximityConnection* conn;
128+
129+
if (mock) {
130+
ProximityMockConnection* mock_conn = new ProximityMockConnection();
131+
132+
// Create a mock message header
133+
ProximitySensorMessageHeader mock_header {};
134+
memcpy(mock_header.preamble, MESSAGE_HEADER_PREAMBLE_C, sizeof(mock_header.preamble));
135+
mock_header.order = 69;
136+
mock_header.serial = 12345;
137+
mock_header.channels = 0x0000'0000'0000'0001u << ((channel-1)*2);
138+
mock_header.status = 0;
139+
mock_header.frame_count = 1;
140+
mock_header.bytes_per_frame = 4;
141+
mock_header.start_sample_number = 1;
142+
143+
// Create a buffer to hold the encoded message
144+
size_t payload_size = mock_header.payload_size();
145+
char* messages = new char[payload_size];
146+
147+
// Create a mock message with the header
148+
auto header_ptr = std::make_unique<ProximitySensorMessageHeader>(mock_header);
149+
ProximitySensorMessage mock_message = ProximitySensorMessage(std::move(header_ptr));
150+
mock_message.set_channel_value(1, ChannelValue(
151+
mock_header.start_sample_number,
152+
// This is the value that will be returned by the mock sensor
153+
ChannelValue::from_micrometers(2000.0, range)
154+
));
155+
156+
// Encode the mock message to the messages buffer
157+
mock_message.encode(messages, payload_size);
158+
// Set the mock data to return on recv calls
159+
mock_conn->set_recv_data(messages, payload_size);
160+
// Assign the mock connection to the abstract connection
161+
conn = mock_conn;
162+
} else {
163+
conn = new ProximitySocketConnection(ip, port);
164+
}
165+
166+
ProximitySensor* proximity_sensor = new ProximitySensor(conn, rate, 2.0);
167+
```
168+
120169
## Provided verification binary
121170

122171
The library provides a standalone binary that can be used to read data from the capa NCDT control unit and output the measurements to the console. After installing the library, the binary can be found in the `bin` directory. The binary is called `capancdt_read_sensor` amd can be used as follows:

0 commit comments

Comments
 (0)