commit 002c24c1439e5acef1ffeeb296349749e87708e3
parent d88023097a7741f80475bd639eec265261c066f6
Author: Samdal <samdal@protonmail.com>
Date: Tue, 22 Jun 2021 12:50:41 +0200
fixed Serial.print, updated README
Diffstat:
M | ArduinoNative.hpp | | | 97 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- |
M | README.org | | | 32 | +++++++++++++++++++------------- |
2 files changed, 104 insertions(+), 25 deletions(-)
diff --git a/ArduinoNative.hpp b/ArduinoNative.hpp
@@ -1,27 +1,35 @@
#ifndef ArduinoNative
#define ArduinoNative
-#include <iostream>
#include <algorithm>
-#include <sstream>
-#include <chrono>
-#include <thread>
-#include <stdint.h>
+#include <bitset>
#include <cstring>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
+#include <chrono>
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <stdint.h>
#include <string>
+#include <thread>
/* CONSTANTS */
#define String std::string
#define LOW 0
#define HIGH 1
-enum {
+typedef enum {
INPUT,
OUTPUT,
INPUT_PULLUP
} an_pin_mode;
+typedef enum {
+ BIN,
+ OCT,
+ DEC,
+ HEX
+} an_print_format_t;
#define byte uint8_t
#define word uint16_t
@@ -35,7 +43,12 @@ enum {
#endif
/* BOARD DEFINITIONS */
-#if defined(AN_BOARD_NANO) || defined(AN_BOARD_PRO_MINI)
+
+#ifdef AN_BOARD_PRO_MINI
+#define AN_BOARD_PRO
+#endif
+
+#if defined(AN_BOARD_NANO) || defined(AN_BOARD_PRO)
#define MAX_PINS 21
@@ -71,7 +84,7 @@ float an_pin_voltage[MAX_PINS] = {0};
// Digital I/O
bool digitalRead(uint8_t pin);
void digitalWrite(uint8_t pin, bool value);
-#define pinMode(pin, mode)
+void pinMode(uint8_t pin, an_pin_mode mode);
// Analog I/O
uint16_t analogRead(uint8_t pin);
@@ -203,13 +216,61 @@ public:
return s.str().length();
}
template <typename T>
- size_t println(T val)
+ size_t print(T val, an_print_format_t format)
{
- std::cout << val << std::endl;
std::stringstream s;
- s << val;
- return s.str().length() + 1;
+ switch (format) {
+ case BIN: {
+ std::bitset<sizeof(val)*8> bits(val);
+ std::cout << bits;
+ s << bits;
+ return s.str().length();
+ } case DEC: {
+ long value = (long)val;
+ std::cout << value;
+ s << value;
+ return s.str().length();
+ } case HEX: {
+ long value = (long)val;
+ std::cout << std::hex << value;
+ s << std::hex << value;
+ return s.str().length();
+ } case OCT: {
+ long value = (long)val;
+ std::cout << std::oct << value;
+ s << std::oct << value;
+ return s.str().length();
+ }}
+ return 0;
+ }
+ size_t print(float val, uint8_t decimals)
+ {
+ std::cout << std::fixed << std::setprecision(decimals) << val;
+ std::stringstream s;
+ s << std::fixed << std::setprecision(decimals) << val;
+ return s.str().length();
+ }
+ template <typename T>
+ size_t println(T val)
+ {
+ size_t byteswritten = 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);
+ std::cout << "\n";
+ return byteswritten + 1;
+ }
+ size_t println(float val, uint8_t format)
+ {
+ size_t byteswritten = print(val, format);
+ std::cout << "\n";
+ return byteswritten + 1;
}
+ size_t println() {std::cout << std::endl; return 1;}
};
void setup();
@@ -256,6 +317,18 @@ void digitalWrite(uint8_t pin, bool val)
std::cout << "Pin: " << std::to_string(pin) << " is now " << std::to_string(an_pin_voltage[pin] > 3) << "\n";
#endif
}
+void pinMode(uint8_t pin, an_pin_mode mode)
+{
+ if (pin > MAX_PINS) {
+ std::cout << "ERROR: PIN " << std::to_string(pin) << " IS NOT DEFINED\n";
+ exit(1);
+ }
+ if (mode == INPUT_PULLUP) {
+ an_pin_voltage[pin] == 5.0;
+ an_pin_cycle[pin] == 255;
+ }
+
+}
// Analog I/O
uint16_t analogRead(uint8_t pin)
diff --git a/README.org b/README.org
@@ -30,14 +30,19 @@ void loop()
}
#+END_SRC
-#+RESULTS:
-
[[#more-examples][More Examples]]
+* Supported boards
+- Arduino Uno
+- Arduino Pro or Pro Mini
+- Arduino Nano
+Choose board by defining a macro
+#+BEGIN_SRC C++
+#define AN_BOARD_PRO
+#+END_SRC
+If no board is defined it will default to Arduino Uno
* Features
-Note that some functions are not implemented exactly like Arduino has them.
-For example Serial.print and println can't print in byte representation yet.
-** Implemented from Arduino library:
-Note that less important functions haven't been tested that much
+** Implemented from Arduino library
+Note that less important functions haven't been tested that much.
- [X] Digital I/O
+ [X] digitalRead()
+ [X] digitalWrite()
@@ -147,15 +152,15 @@ Serial.an_take_input()
#+END_SRC
** Extra debug features
Debug features can be enabled by defining the following macros
-*** AN_DEBUG_ALL
+- AN_DEBUG_ALL
Enables everything below
-*** AN_DEBUG_DIGITALREAD
+- AN_DEBUG_DIGITALREAD
Prints a message to console when digitalRead is called
-*** AN_DEBUG_DIGITALWRITE
+- AN_DEBUG_DIGITALWRITE
Prints a message to console when digitalWrite is called
-*** AN_DEBUG_ANALOGREAD
+- AN_DEBUG_ANALOGREAD
Prints a message to console when analogRead is called
-*** AN_DEBUG_ANALOGWRITE
+- AN_DEBUG_ANALOGWRITE
Prints a message to console when analogWrite is called
* More examples
** Serial and AnalogRead
@@ -182,7 +187,7 @@ void loop()
Serial.println(val);
float voltage = map(val, 0, 1023, 0.0, 5.0);
Serial.print("Voltage on pin is: ");
- Serial.println(voltage);
+ Serial.println(voltage, 2);
delay(30);
}
#+END_SRC
@@ -190,7 +195,8 @@ Output:
#+BEGIN_SRC
set voltage of pin 21 to: 3.2
Read value 654
-Voltage on pin is: 3.19648
+Voltage on pin is: 3.20
+...
#+END_SRC
** Serial read
#+BEGIN_SRC C++