commit 842899531d89ea281206b4659377ec511aaeb958
parent 002c24c1439e5acef1ffeeb296349749e87708e3
Author: Samdal <samdal@protonmail.com>
Date: Tue, 22 Jun 2021 13:29:45 +0200
new example in README, new debug option
Diffstat:
M | ArduinoNative.hpp | | | 31 | ++++++++++++++++++++++++------- |
M | README.org | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- |
2 files changed, 81 insertions(+), 17 deletions(-)
diff --git a/ArduinoNative.hpp b/ArduinoNative.hpp
@@ -40,6 +40,7 @@ typedef enum {
#define AN_DEBUG_DIGITALWRITE
#define AN_DEBUG_ANALOGREAD
#define AN_DEBUG_ANALOGWRITE
+#define AN_DEBUG_TIMESTAMP
#endif
/* BOARD DEFINITIONS */
@@ -253,20 +254,20 @@ public:
template <typename T>
size_t println(T val)
{
- size_t byteswritten = print(val);
+ size_t byteswritten = this->print(val);
std::cout << "\n";
return byteswritten + 1;
}
template <typename T>
size_t println(T val, an_print_format_t format)
{
- size_t byteswritten = print(val, format);
+ size_t byteswritten = this->print(val, format);
std::cout << "\n";
return byteswritten + 1;
}
size_t println(float val, uint8_t format)
{
- size_t byteswritten = print(val, format);
+ size_t byteswritten = this->print(val, format);
std::cout << "\n";
return byteswritten + 1;
}
@@ -300,7 +301,11 @@ bool digitalRead(uint8_t pin)
exit(1);
}
#ifdef AN_DEBUG_DIGITALREAD
- std::cout << "Read pin: " << std::to_string(pin) << " is " << std::to_string(an_pin_voltage[pin] > 3) << "\n";
+#ifdef AN_DEBUG_TIMESTAMP
+ std::cout << millis() << "ms | ";
+#endif
+ std::cout << "Read pin: " << std::to_string(pin) << " is "
+ << (an_pin_voltage[pin] > 3 ? "HIGH" : "LOW") << "\n";
#endif
return an_pin_voltage[pin] > 3;
}
@@ -314,7 +319,11 @@ void digitalWrite(uint8_t pin, bool val)
an_pin_cycle[pin] = val * 255;
an_pin_voltage[pin] = val * 5.0;
#ifdef AN_DEBUG_DIGITALWRITE
- std::cout << "Pin: " << std::to_string(pin) << " is now " << std::to_string(an_pin_voltage[pin] > 3) << "\n";
+#ifdef AN_DEBUG_TIMESTAMP
+ std::cout << millis() << "ms | ";
+#endif
+ std::cout << "Pin: " << std::to_string(pin) << " is now "
+ << (an_pin_voltage[pin] > 3 ? "HIGH" : "LOW") << "\n";
#endif
}
void pinMode(uint8_t pin, an_pin_mode mode)
@@ -340,7 +349,11 @@ uint16_t analogRead(uint8_t pin)
uint16_t val = map(an_pin_voltage[pin], 0.0, 5.0, 0, 1023);
val = constrain(val, 0, 1023);
#ifdef AN_DEBUG_ANALOGREAD
- std::cout << "Analog pin: " << std::to_string(pin) << " is " << std::to_string(val) << "\n";
+#ifdef AN_DEBUG_TIMESTAMP
+ std::cout << millis() << "ms | ";
+#endif
+ std::cout << "Analog pin: " << std::to_string(pin) << " is "
+ << std::to_string(val) << "\n";
#endif
return val;
}
@@ -355,7 +368,11 @@ void analogWrite(uint8_t pin, uint8_t val)
an_pin_cycle[pin] = val;
an_pin_voltage[pin] = map(val, 0, 255, 0.0, 5.0);
#ifdef AN_DEBUG_ANALOGWRITE
- std::cout << "Duty cycle on pin: " << std::to_string(pin) << " is now " << std::to_string(an_pin_cycle[pin]) << "\n";
+#ifdef AN_DEBUG_TIMESTAMP
+ std::cout << millis() << "ms | ";
+#endif
+ std::cout << "Duty cycle on pin: " << std::to_string(pin) << " is now "
+ << std::to_string(an_pin_cycle[pin]) << "\n";
#endif
}
diff --git a/README.org b/README.org
@@ -152,16 +152,12 @@ Serial.an_take_input()
#+END_SRC
** Extra debug features
Debug features can be enabled by defining the following macros
-- AN_DEBUG_ALL
-Enables everything below
-- AN_DEBUG_DIGITALREAD
-Prints a message to console when digitalRead is called
-- AN_DEBUG_DIGITALWRITE
-Prints a message to console when digitalWrite is called
-- AN_DEBUG_ANALOGREAD
-Prints a message to console when analogRead is called
-- AN_DEBUG_ANALOGWRITE
-Prints a message to console when analogWrite is called
+- AN_DEBUG_ALL: Enables everything below
+- AN_DEBUG_DIGITALREAD: Prints a message to console when digitalRead is called
+- AN_DEBUG_DIGITALWRITE: Prints a message to console when digitalWrite is called
+- AN_DEBUG_ANALOGREAD: Prints a message to console when analogRead is called
+- AN_DEBUG_ANALOGWRITE: Prints a message to console when analogWrite is called
+- AN_DEBUG_TIMESTAMP: Prints a timestamp in milliseconds in front of all debug messages
* More examples
** Serial and AnalogRead
#+BEGIN_SRC C++
@@ -226,3 +222,54 @@ l
l
o
#+END_SRC
+** millis()
+#+BEGIN_SRC C++
+#define AN_DEBUG_DIGITALWRITE
+#define AN_DEBUG_TIMESTAMP
+#define AN_IMPL
+#include "ArduinoNative.hpp"
+
+#define LED1 5
+#define LED2 6
+#define LED1_DELAY 1000 // delay in ms
+#define LED2_DELAY 2000 // delay in ms
+unsigned long previous_LED1_change;
+unsigned long previous_LED2_change;
+
+
+void setup()
+{
+ pinMode(LED1, OUTPUT);
+ pinMode(LED2, OUTPUT);
+}
+
+void loop()
+{
+ unsigned long t = millis();
+ // turn on LED1 every second
+ if (t - previous_LED1_change >= LED1_DELAY) {
+ digitalWrite(LED1, !digitalRead(LED1));
+ previous_LED1_change = t;
+ }
+ // turn on LED2 every other second
+ if (t - previous_LED2_change >= LED2_DELAY) {
+ digitalWrite(LED2, !digitalRead(LED2));
+ previous_LED2_change = t;
+ }
+}
+#+END_SRC
+Output:
+#+BEGIN_SRC
+500ms | Pin: 6 is now HIGH
+1000ms | Pin: 5 is now HIGH
+1000ms | Pin: 6 is now LOW
+1500ms | Pin: 6 is now HIGH
+2000ms | Pin: 5 is now LOW
+2000ms | Pin: 6 is now LOW
+2500ms | Pin: 6 is now HIGH
+3000ms | Pin: 5 is now HIGH
+3000ms | Pin: 6 is now LOW
+3500ms | Pin: 6 is now HIGH
+4000ms | Pin: 5 is now LOW
+4000ms | Pin: 6 is now LOW
+#+END_SRC