ArduinoNative

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

README.org (6874B)


      1 #+AUTHOR: Halvard Samdal
      2 
      3 * What is ArduinoNative?
      4 ArduinoNative allows you to run Arduino code without Arduino.
      5 ArduinoNative does not attempt to emulate or simulate an Arduino, it is a simple header-only implementation of the Arduino library in order to test and debug Arduino code.
      6 Not only do you have the ability to use your favorite IDE with fancy code suggestions, you can even use a debugger to step through your code.
      7 * Getting started
      8 1. Download and install required tools to build C++ on your machine
      9 2. Create a new C++ project and place ArduinoNative.hpp in that directory
     10 3. Define AN_IMPL in one and only one of your source files
     11 4. Include ArduinoNative.hpp
     12 ** A basic blink example
     13 #+BEGIN_SRC C++
     14 #define AN_DEBUG_ALL
     15 #define AN_IMPL
     16 #include "ArduinoNative.hpp"
     17 
     18 void setup()
     19 {
     20         pinMode(LED_BUILTIN, OUTPUT);
     21 }
     22 
     23 void loop()
     24 {
     25         digitalWrite(LED_BUILTIN, HIGH);
     26         delay(1000);
     27         digitalWrite(LED_BUILTIN, LOW);
     28         delay(1000);
     29 }
     30 #+END_SRC
     31 
     32 [[#more-examples][More Examples]]
     33 * Supported boards
     34 - Arduino Uno
     35 - Arduino Pro or Pro Mini
     36 - Arduino Nano
     37 Choose board by defining a macro
     38 #+BEGIN_SRC C++
     39 #define AN_BOARD_PRO
     40 #+END_SRC
     41 If no board is defined it will default to Arduino Uno
     42 * Features
     43 ** Implemented from Arduino library
     44 [[https://www.arduino.cc/reference/en/][Arduino Library Reference]]. Note that less used functions haven't been tested that much.
     45 *** Exceptions
     46 - HIGH and LOW interrupt modes don’t work, only CHANGE, RISING and FALLING
     47 - serialEvent() is only supported on GCC and Clang, as it uses a GCC extension.
     48 - PROGMEM, USB and Stream aren't implemented and likely never will be
     49 ** Other functions
     50 It is recommended that you encapsulate these non-Arduino functions with some macro guards.
     51 This prevents you from having to remove them when actually compile for an Arduino.
     52 #+BEGIN_SRC C++
     53 #ifdef ArduinoNative
     54 // this will only be compiled if ArduinoNative is used
     55 #endif
     56 #+END_SRC
     57 - Set pin voltage
     58 #+BEGIN_SRC C++
     59 an_set_voltage(pin, voltage)
     60 #+END_SRC
     61 - Set pin voltage from console input
     62 #+BEGIN_SRC C++
     63 an_request_voltage(pin)
     64 #+END_SRC
     65 - Take input from console and put it in Serial buffer
     66 #+BEGIN_SRC C++
     67 Serial.an_take_input()
     68 #+END_SRC
     69 - Print timestamp in ms
     70 #+BEGIN_SRC C++
     71 an_print_timestamp(); // example "1530ms | "
     72 #+END_SRC
     73 - Attach sine wave to pin
     74 #+BEGIN_SRC C++
     75 an_attach_sine(pin, hz = 1, amplitude = 2.5, dc_offset = 2.5, abs = false)
     76 #+END_SRC
     77 - Remove sine wave on pin
     78 #+BEGIN_SRC C++
     79 an_remove_sine(pin)
     80 #+END_SRC
     81 - Attach square wave to pin
     82 #+BEGIN_SRC C++
     83 an_attach_square(pin, hz = 1, duty_cycle = 0.5);
     84 #+END_SRC
     85 - Remove square wave on pin
     86 #+BEGIN_SRC C++
     87 an_remove_square(pin)
     88 #+END_SRC
     89 ** Extra debug features
     90 Debug features can be enabled by defining the following macros
     91 - *AN_DEBUG_TIMESTAMP*: Prints a timestamp in milliseconds in front of all debug messages
     92 - *AN_DEBUG_ALL*: Enables everything below
     93 - *AN_DEBUG_DIGITALREAD*: Prints a message to console when digitalRead is called
     94 - *AN_DEBUG_DIGITALWRITE*: Prints a message to console when digitalWrite is called
     95 - *AN_DEBUG_ANALOGREAD*: Prints a message to console when analogRead is called
     96 - *AN_DEBUG_ANALOGWRITE*: Prints a message to console when analogWrite is called
     97 * Roadmap
     98 - [ ] Check for pin type
     99 - [ ] Attach simulated hardware on pins
    100 - [ ] Move examples to their own folder
    101 - [ ] Debug viewer to show pin status instead of Serial
    102 - [ ] Support more boards
    103 - [ ] Implement extra libraries (Servo.h, FastLED, etc)
    104 * More examples
    105 ** Serial and AnalogRead
    106 #+BEGIN_SRC C++
    107 #define AN_BOARD_NANO
    108 #define AN_IMPL
    109 #include "ArduinoNative.hpp"
    110 
    111 int analogpin = A7;
    112 
    113 void setup()
    114 {
    115         Serial.begin(9600);
    116         pinMode(analogpin, INPUT);
    117 }
    118 
    119 void loop()
    120 {
    121 #ifdef ArduinoNative
    122         an_request_voltage(analogpin);
    123 #endif
    124         unsigned val = analogRead(analogpin);
    125         Serial.print("Read value ");
    126         Serial.println(val);
    127         float voltage = map(val, 0, 1023, 0.0, 5.0);
    128         Serial.print("Voltage on pin is: ");
    129         Serial.println(voltage, 2);
    130         delay(30);
    131 }
    132 #+END_SRC
    133 Output:
    134 #+BEGIN_SRC
    135 set voltage of pin 21 to: 3.2
    136 Read value 654
    137 Voltage on pin is: 3.20
    138 ...
    139 #+END_SRC
    140 ** Serial read
    141 #+BEGIN_SRC C++
    142 #define AN_IMPL
    143 #include "ArduinoNative.hpp"
    144 
    145 void setup()
    146 {
    147         Serial.begin(9600);
    148 #ifdef ArduinoNative
    149         Serial.an_take_input();
    150 #endif
    151 }
    152 
    153 void loop()
    154 {
    155         while(Serial.available())
    156                 Serial.println((char)Serial.read());
    157 }
    158 #+END_SRC
    159 Output:
    160 #+BEGIN_SRC
    161 ArduinoNative is requesting Serial input: hello
    162 h
    163 e
    164 l
    165 l
    166 o
    167 #+END_SRC
    168 ** millis() and specific debug modes
    169 #+BEGIN_SRC C++
    170 #define AN_DEBUG_DIGITALWRITE
    171 #define AN_DEBUG_TIMESTAMP
    172 #define AN_IMPL
    173 #include "ArduinoNative.hpp"
    174 
    175 #define LED1 5
    176 #define LED2 6
    177 #define LED1_DELAY 1000 // delay in ms
    178 #define LED2_DELAY 2000 // delay in ms
    179 unsigned long previous_LED1_change;
    180 unsigned long previous_LED2_change;
    181 
    182 
    183 void setup()
    184 {
    185         pinMode(LED1, OUTPUT);
    186         pinMode(LED2, OUTPUT);
    187 }
    188 
    189 void loop()
    190 {
    191         unsigned long t = millis();
    192         // turn on LED1 every second
    193         if (t - previous_LED1_change >= LED1_DELAY) {
    194                 digitalWrite(LED1, !digitalRead(LED1));
    195                 previous_LED1_change = t;
    196         }
    197         // turn on LED2 every half a second
    198         if (t - previous_LED2_change >= LED2_DELAY) {
    199                 digitalWrite(LED2, !digitalRead(LED2));
    200                 previous_LED2_change = t;
    201         }
    202 }
    203 #+END_SRC
    204 Output:
    205 #+BEGIN_SRC
    206 500ms | Pin: 6 is now HIGH
    207 1000ms | Pin: 5 is now HIGH
    208 1000ms | Pin: 6 is now LOW
    209 1500ms | Pin: 6 is now HIGH
    210 2000ms | Pin: 5 is now LOW
    211 2000ms | Pin: 6 is now LOW
    212 2500ms | Pin: 6 is now HIGH
    213 3000ms | Pin: 5 is now HIGH
    214 3000ms | Pin: 6 is now LOW
    215 3500ms | Pin: 6 is now HIGH
    216 4000ms | Pin: 5 is now LOW
    217 4000ms | Pin: 6 is now LOW
    218 ...
    219 #+END_SRC
    220 ** Interrupts and an_attach_square
    221 #+BEGIN_SRC C++
    222 #define AN_DEBUG_TIMESTAMP
    223 #define AN_IMPL
    224 #include "ArduinoNative.hpp"
    225 
    226 unsigned long switchdelay;
    227 unsigned short count;
    228 
    229 void interrupt()
    230 {
    231 #ifdef ArduinoNative
    232         an_print_timestamp();
    233 #endif
    234         Serial.print("INTERRUPT");
    235         Serial.println(++count);
    236         if (count >= 5)
    237                 detachInterrupt(2);
    238 }
    239 
    240 void setup() {
    241 #ifdef ArduinoNative
    242         an_attach_square(2);
    243 #endif
    244         Serial.begin(9600);
    245         attachInterrupt(digitalPinToInterrupt(2), interrupt, CHANGE);
    246 }
    247 
    248 void loop() {}
    249 #+END_SRC
    250 Output:
    251 #+BEGIN_SRC
    252 500ms | INTERRUPT1
    253 1001ms | INTERRUPT2
    254 1500ms | INTERRUPT3
    255 2001ms | INTERRUPT4
    256 2500ms | INTERRUPT5
    257 #+END_SRC
    258 ** AnalogReference()
    259 #+BEGIN_SRC C++
    260 #define AN_IMPL
    261 #include "ArduinoNative.hpp"
    262 
    263 void setup()
    264 {
    265         Serial.begin(9600);
    266         analogReference(EXTERNAL);
    267 #ifdef ArduinoNative
    268         an_set_voltage(AREF, 3.3);
    269         an_set_voltage(A2, 1.65);
    270 #endif
    271         Serial.println(analogRead(A2));
    272 }
    273 
    274 void loop() {}
    275 #+END_SRC
    276 Output:
    277 #+BEGIN_SRC
    278 512
    279 #+END_SRC