11/*
2- Arduino.cpp
2+ Arduino.cpp
33 Copyright (c) 2025 Phil Schatzmann. All right reserved.
44
55 This library is free software; you can redistribute it and/or
1717 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818*/
1919#include " Arduino.h"
20+
2021#include < stdio.h>
22+
2123#include < chrono>
2224#include < cstring>
2325#include < ctime>
@@ -57,23 +59,39 @@ PluggableUSB_::PluggableUSB_() {}
5759
5860} // namespace arduino
5961
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+ */
6166void delay (unsigned long ms) {
6267 std::this_thread::sleep_for (std::chrono::milliseconds (ms));
6368}
6469
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+ */
6674void delayMicroseconds (unsigned int us) {
6775 std::this_thread::sleep_for (std::chrono::microseconds (us));
6876}
6977
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+ */
7186char * dtostrf (double val, signed char width, unsigned char prec, char * sout) {
7287 sprintf (sout, " %*.*lf" , width, prec, val);
7388 return sout;
7489}
7590
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+ */
7795unsigned long millis () {
7896 static uint64_t start = 0 ;
7997 using namespace std ::chrono;
@@ -89,7 +107,10 @@ unsigned long millis() {
89107 return result - start;
90108}
91109
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+ */
93114unsigned long micros (void ) {
94115 using namespace std ::chrono;
95116 // Get current time with precision of milliseconds
@@ -100,30 +121,114 @@ unsigned long micros(void) {
100121 return now.time_since_epoch ().count ();
101122}
102123
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+ */
103129void pinMode (pin_size_t pinNumber, PinMode pinMode) {
104130 GPIO.pinMode (pinNumber, pinMode);
105131}
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+ */
106138void digitalWrite (pin_size_t pinNumber, PinStatus status) {
107139 GPIO.digitalWrite (pinNumber, status);
108140}
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+ */
109147PinStatus digitalRead (pin_size_t pinNumber) {
110148 return GPIO.digitalRead (pinNumber);
111149}
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+ */
112156int 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+ */
113162void 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+ */
114169void analogWrite (pin_size_t pinNumber, int value) {
115170 GPIO.analogWrite (pinNumber, value);
116171}
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+ */
117180void tone (uint8_t pinNumber, unsigned int frequency, unsigned long duration) {
118181 GPIO.tone (pinNumber, frequency, duration);
119182}
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+ */
120188void 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+ */
121197unsigned long pulseIn (uint8_t pinNumber, uint8_t state, unsigned long timeout) {
122198 return GPIO.pulseIn (pinNumber, state, timeout);
123199}
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+ */
124208unsigned long pulseInLong (uint8_t pinNumber, uint8_t state,
125209 unsigned long timeout) {
126210 return GPIO.pulseInLong (pinNumber, state, timeout);
127211}
128212
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+ */
129234void yield () {}
0 commit comments