1
1
/*
2
- Arduino.cpp
2
+ Arduino.cpp
3
3
Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
4
5
5
This library is free software; you can redistribute it and/or
17
17
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
18
*/
19
19
#include " Arduino.h"
20
+
20
21
#include < stdio.h>
22
+
21
23
#include < chrono>
22
24
#include < cstring>
23
25
#include < ctime>
@@ -57,23 +59,39 @@ PluggableUSB_::PluggableUSB_() {}
57
59
58
60
} // namespace arduino
59
61
60
- // sleep ms milliseconds
62
+ /* *
63
+ * @brief Pause the program for the amount of time (in milliseconds) specified as parameter
64
+ * @param ms The number of milliseconds to pause (unsigned long)
65
+ */
61
66
void delay (unsigned long ms) {
62
67
std::this_thread::sleep_for (std::chrono::milliseconds (ms));
63
68
}
64
69
65
- // sleep us milliseconds
70
+ /* *
71
+ * @brief Pause the program for the amount of time (in microseconds) specified as parameter
72
+ * @param us The number of microseconds to pause (unsigned int)
73
+ */
66
74
void delayMicroseconds (unsigned int us) {
67
75
std::this_thread::sleep_for (std::chrono::microseconds (us));
68
76
}
69
77
70
- // double to string conversion -> we can use sprintf which is complete in linux
78
+ /* *
79
+ * @brief Convert a floating point number to string
80
+ * @param val The double value to convert
81
+ * @param width The minimum field width (signed char)
82
+ * @param prec The precision (number of digits after decimal point)
83
+ * @param sout The output string buffer
84
+ * @return Pointer to the output string
85
+ */
71
86
char * dtostrf (double val, signed char width, unsigned char prec, char * sout) {
72
87
sprintf (sout, " %*.*lf" , width, prec, val);
73
88
return sout;
74
89
}
75
90
76
- // Returns the number of milliseconds passed since epich
91
+ /* *
92
+ * @brief Returns the number of milliseconds passed since the Arduino board began running the current program
93
+ * @return Number of milliseconds passed since the program started (unsigned long)
94
+ */
77
95
unsigned long millis () {
78
96
static uint64_t start = 0 ;
79
97
using namespace std ::chrono;
@@ -89,7 +107,10 @@ unsigned long millis() {
89
107
return result - start;
90
108
}
91
109
92
- // Returns the micros of milliseconds passed since epich
110
+ /* *
111
+ * @brief Returns the number of microseconds since the Arduino board began running the current program
112
+ * @return Number of microseconds passed since the program started (unsigned long)
113
+ */
93
114
unsigned long micros (void ) {
94
115
using namespace std ::chrono;
95
116
// Get current time with precision of milliseconds
@@ -100,30 +121,114 @@ unsigned long micros(void) {
100
121
return now.time_since_epoch ().count ();
101
122
}
102
123
124
+ /* *
125
+ * @brief Configure the specified pin to behave either as an input or an output
126
+ * @param pinNumber The pin whose mode you wish to set
127
+ * @param pinMode INPUT, OUTPUT, or INPUT_PULLUP
128
+ */
103
129
void pinMode (pin_size_t pinNumber, PinMode pinMode) {
104
130
GPIO.pinMode (pinNumber, pinMode);
105
131
}
132
+
133
+ /* *
134
+ * @brief Write a HIGH or a LOW value to a digital pin
135
+ * @param pinNumber The pin number
136
+ * @param status HIGH or LOW
137
+ */
106
138
void digitalWrite (pin_size_t pinNumber, PinStatus status) {
107
139
GPIO.digitalWrite (pinNumber, status);
108
140
}
141
+
142
+ /* *
143
+ * @brief Reads the value from a specified digital pin, either HIGH or LOW
144
+ * @param pinNumber The number of the digital pin you want to read
145
+ * @return HIGH or LOW
146
+ */
109
147
PinStatus digitalRead (pin_size_t pinNumber) {
110
148
return GPIO.digitalRead (pinNumber);
111
149
}
150
+
151
+ /* *
152
+ * @brief Reads the value from the specified analog pin
153
+ * @param pinNumber The number of the analog input pin to read from (0 to 5 on most boards)
154
+ * @return The analog reading on the pin (int 0 to 1023)
155
+ */
112
156
int analogRead (pin_size_t pinNumber) { return GPIO.analogRead (pinNumber); }
157
+
158
+ /* *
159
+ * @brief Configures the reference voltage used for analog input
160
+ * @param mode Which type of reference to use (DEFAULT, INTERNAL, EXTERNAL)
161
+ */
113
162
void analogReference (uint8_t mode) { GPIO.analogReference (mode); }
163
+
164
+ /* *
165
+ * @brief Writes an analog value (PWM wave) to a pin
166
+ * @param pinNumber The pin to write to
167
+ * @param value The duty cycle: between 0 (always off) and 255 (always on)
168
+ */
114
169
void analogWrite (pin_size_t pinNumber, int value) {
115
170
GPIO.analogWrite (pinNumber, value);
116
171
}
172
+
173
+
174
+ /* *
175
+ * @brief Generates a square wave of the specified frequency (and 50% duty cycle) on a pin
176
+ * @param pinNumber The pin on which to generate the tone
177
+ * @param frequency The frequency of the tone in hertz - unsigned int
178
+ * @param duration The duration of the tone in milliseconds (optional) - unsigned long
179
+ */
117
180
void tone (uint8_t pinNumber, unsigned int frequency, unsigned long duration) {
118
181
GPIO.tone (pinNumber, frequency, duration);
119
182
}
183
+
184
+ /* *
185
+ * @brief Stops the generation of a square wave triggered by tone()
186
+ * @param pinNumber The pin on which to stop generating the tone
187
+ */
120
188
void noTone (uint8_t pinNumber) { GPIO.noTone (pinNumber); }
189
+
190
+ /* *
191
+ * @brief Reads a pulse (either HIGH or LOW) on a pin
192
+ * @param pinNumber The number of the pin on which you want to read the pulse
193
+ * @param state Type of pulse to read: either HIGH or LOW
194
+ * @param timeout The number of microseconds to wait for the pulse to be completed (optional)
195
+ * @return The length of the pulse (in microseconds) or 0 if no complete pulse was received within the timeout
196
+ */
121
197
unsigned long pulseIn (uint8_t pinNumber, uint8_t state, unsigned long timeout) {
122
198
return GPIO.pulseIn (pinNumber, state, timeout);
123
199
}
200
+
201
+ /* *
202
+ * @brief Alternative to pulseIn() which is better at handling long pulses and interrupt affected systems
203
+ * @param pinNumber The number of the pin on which you want to read the pulse
204
+ * @param state Type of pulse to read: either HIGH or LOW
205
+ * @param timeout The number of microseconds to wait for the pulse to be completed (optional)
206
+ * @return The length of the pulse (in microseconds) or 0 if no complete pulse was received within the timeout
207
+ */
124
208
unsigned long pulseInLong (uint8_t pinNumber, uint8_t state,
125
209
unsigned long timeout) {
126
210
return GPIO.pulseInLong (pinNumber, state, timeout);
127
211
}
128
212
213
+ /* *
214
+ * @brief Set the PWM frequency for analogWrite() on the specified pin
215
+ * @param pin The pin number to configure PWM frequency for
216
+ * @param freq The desired PWM frequency in Hz
217
+ */
218
+ void analogWriteFrequency (pin_size_t pin, uint32_t freq) {
219
+ GPIO.analogWriteFrequency (pin, freq);
220
+ }
221
+
222
+ /* *
223
+ * @brief Set the resolution of the analogWrite() values
224
+ * @param bits The resolution in bits (e.g., 8 for 0-255, 10 for 0-1023)
225
+ */
226
+ void analogWriteResolution (uint8_t bits) {
227
+ GPIO.analogWriteResolution (bits);
228
+ }
229
+
230
+ /* *
231
+ * @brief Passes control to other tasks when called
232
+ * @note This is used to prevent watchdog timer resets in long-running loops
233
+ */
129
234
void yield () {}
0 commit comments