-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockArduino.h
285 lines (245 loc) · 8.99 KB
/
mockArduino.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @file mockArduino.h
* @author Felix Schuelke ([email protected])
* @brief
* @version 0.1
* @date 2024-08-18
*
* @copyright * Copyright (C) 2024 Felix Schuelke
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef MOCK_ARDUINO_H
#define MOCK_ARDUINO_H
#ifdef ARDUINO
#include <Arduino.h>
#else
#include <iostream>
#include <string>
#include <sstream>
#include <chrono>
#include <queue>
#include <functional>
#define OUTPUT 1 ///< Define constant for OUTPUT mode
#define INPUT 0 ///< Define constant for INPUT mode
/**
* @namespace arduinoMocking
* @brief Provides mock implementations of Arduino framework functions and classes for native builds.
*/
namespace arduinoMocking {
/**
* @typedef String
* @brief Alias for `std::string` to simulate Arduino's String type.
*/
typedef std::string String;
/**
* @brief Simulates the `pinMode` function for setting pin modes.
*
* This function is a placeholder for the Arduino `pinMode` function and does nothing in this mock implementation.
*
* @param pin The pin number to configure.
* @param mode The mode to set for the pin (INPUT or OUTPUT).
*/
void pinMode(int pin, int mode);
/**
* @class MockTime
* @brief Simulates Arduino's time functions for testing purposes.
*
* This class provides methods to get simulated time in milliseconds and microseconds.
*/
class MockTime {
public:
/**
* @brief Constructs a `MockTime` object and initializes the start time.
*/
MockTime();
/**
* @brief Returns the simulated time in milliseconds.
*
* @return The number of milliseconds since the start time, including any simulated time.
*/
unsigned long millis() const;
/**
* @brief Returns the simulated time in microseconds.
*
* @return The number of microseconds since the start time, including any simulated time.
*/
unsigned long micros() const;
private:
std::chrono::steady_clock::time_point startTime; ///< Start time for the simulation
unsigned long simulatedMillis; ///< Simulated milliseconds
unsigned long simulatedMicros; ///< Simulated microseconds
};
/**
* @brief Simulates the `millis` function to get the number of milliseconds since the program started.
*
* @return The number of milliseconds since the program started.
*/
unsigned long millis();
/**
* @brief Simulates the `micros` function to get the number of microseconds since the program started.
*
* @return The number of microseconds since the program started.
*/
unsigned long micros();
/**
* @class MockSerial
* @brief Simulates Arduino's `Serial` class for testing purposes.
*
* This class provides methods for serial communication, including print, println, and simulated input.
*/
class MockSerial {
public:
/**
* @brief Constructs a `MockSerial` object.
*/
MockSerial();
/**
* @brief Begins serial communication with the specified baud rate.
*
* Prints a message indicating the baud rate in the mock implementation.
*
* @param baudrate The baud rate for serial communication.
*/
void begin(unsigned long baudrate);
/**
* @brief Prints a string to the serial output.
*
* @param str The string to print.
*/
void print(const std::string& str);
/**
* @brief Prints a character to the serial output.
*
* @param c The character to print.
*/
void print(char c);
/**
* @brief Prints an integer to the serial output.
*
* @param num The integer to print.
*/
void print(int num);
/**
* @brief Prints an unsigned integer to the serial output.
*
* @param num The unsigned integer to print.
*/
void print(unsigned int num);
/**
* @brief Prints a long integer to the serial output.
*
* @param num The long integer to print.
*/
void print(long num);
/**
* @brief Prints an unsigned long integer to the serial output.
*
* @param num The unsigned long integer to print.
*/
void print(unsigned long num);
/**
* @brief Prints a double to the serial output.
*
* @param num The double to print.
*/
void print(double num);
/**
* @brief Prints a string followed by a newline to the serial output.
*
* @param str The string to print.
*/
void println(const std::string& str);
/**
* @brief Prints a character followed by a newline to the serial output.
*
* @param c The character to print.
*/
void println(char c);
/**
* @brief Prints an integer followed by a newline to the serial output.
*
* @param num The integer to print.
*/
void println(int num);
/**
* @brief Prints an unsigned integer followed by a newline to the serial output.
*
* @param num The unsigned integer to print.
*/
void println(unsigned int num);
/**
* @brief Prints a long integer followed by a newline to the serial output.
*
* @param num The long integer to print.
*/
void println(long num);
/**
* @brief Prints an unsigned long integer followed by a newline to the serial output.
*
* @param num The unsigned long integer to print.
*/
void println(unsigned long num);
/**
* @brief Prints a double followed by a newline to the serial output.
*
* @param num The double to print.
*/
void println(double num);
/**
* @brief Prints a newline to the serial output.
*/
void println();
/**
* @brief Returns the number of bytes available to read from the serial input buffer.
*
* @return The number of bytes available (size of input buffer).
*/
int available();
/**
* @brief Reads a byte from the serial input buffer.
*
* @return The byte read, or `-1` if no data is available.
*/
int read();
/**
* @brief Writes a byte to the serial output.
*
* @param byte The byte to write.
* @return The number of bytes written (always 1 in the mock implementation).
*/
size_t write(uint8_t byte);
/**
* @brief Simulates user input by adding characters to the input buffer.
*
* Prompts the user to enter a string, which is added to the buffer.
*/
void simulateInput();
/**
* @brief Simulates user input by adding a predefined string to the input buffer.
*
* @param input The string to add to the buffer.
*/
void simulateInput(std::string input);
private:
std::queue<uint8_t> inputBuffer; ///< Queue to store simulated input data.
};
/**
* @var Serial
* @brief Global instance of `MockSerial` to mimic Arduino's Serial object.
*/
extern arduinoMocking::MockSerial Serial;
} // namespace arduinoMocking
#endif // ARDUINO
#endif // MOCK_ARDUINO_H