|
1 |
| -Arduino_AVRSTL |
2 |
| -=============== |
3 |
| -Arduino library providing STL for ArduinoCore-avr and ArduinoCore-megaavr boards. |
| 1 | +# ArduinoSTL |
4 | 2 |
|
5 |
| -This library is based on [ciband/avr_stl](https://github.com/ciband/avr_stl) as well as [mike-matera/ArduinoSTL](https://github.com/mike-matera/ArduinoSTL). |
| 3 | +This is an implementation of a C++ standard library packaged as an Arduino library. The library supports teaching my CS-11M class by adding key C++ features into the Arduino environment. |
6 | 4 |
|
| 5 | +The library is ported from uClibc++: |
| 6 | + |
| 7 | +http://git.uclibc.org/uClibc++ |
| 8 | + |
| 9 | +With a streams implementation from Andy Brown's Arduino Library: |
| 10 | + |
| 11 | +http://andybrown.me.uk/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/ |
| 12 | + |
| 13 | + |
| 14 | +## Using printf() and scanf() |
| 15 | +The ArduinoSTL header file contains code to bind a serial port of your choice to |
| 16 | +the stdio primitives. This happens automatically but the user must still call |
| 17 | +Serial.begin() |
| 18 | + |
| 19 | +```c++ |
| 20 | +#include <ArduinoSTL.h> |
| 21 | + |
| 22 | +void setup() { |
| 23 | + Serial.begin(9600); |
| 24 | + printf("Hello World\n"); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +## Using ```cin``` an ```cout``` |
| 29 | +When you include this header file you automatically get cin and cout based on ```Serial```. See below for how to specify your own device. Here's an example sketch using ```cin``` and ```cout``` . |
| 30 | + |
| 31 | +```c++ |
| 32 | +#include <ArduinoSTL.h> |
| 33 | + |
| 34 | +using namespace std; |
| 35 | + |
| 36 | +void setup() { |
| 37 | + Serial.begin(9600); |
| 38 | + cout << "Feed me an integers." << endl; |
| 39 | +} |
| 40 | + |
| 41 | +void loop() { |
| 42 | + int foo; |
| 43 | + if (cin >> foo) { |
| 44 | + cout << "You fed me " << foo << endl; |
| 45 | + }else{ |
| 46 | + cin.clear(); |
| 47 | + cin.ignore(); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | +## Changing the Serial Port |
| 52 | +You can change what serial port that ```cin```, ```cout``` and ```printf()``` use. You can use built-in serial ports (e.g. ```Serial1``` on Leonardo) or you can use software serial ports that implement ```Stream```. |
| 53 | + |
| 54 | +### Using a Built-in Port |
| 55 | +In ```src/ArduinoSTL.cpp``` change the value of ```ARDUINOSTL_DEFAULT_SERIAL```. Leave the other defaults uncommented. |
| 56 | + |
| 57 | +### Using a SoftwareSerial port. |
| 58 | +Set ```ARDUINO_DEFAULT_SERAL``` to ```NULL```. Comment out the other defaults. |
| 59 | + |
| 60 | +Here's an example sketch that uses SofwareSerial: |
| 61 | + |
| 62 | +```c++ |
| 63 | +#include <ArduinoSTL.h> |
| 64 | +#include <SoftwareSerial.h> |
| 65 | + |
| 66 | +SoftwareSerial mySerial(0, 1); |
| 67 | + |
| 68 | +namespace std { |
| 69 | + ohserialstream cout(mySerial); |
| 70 | + ihserialstream cin(mySerial); |
| 71 | +} |
| 72 | + |
| 73 | +void setup() { |
| 74 | + mySerial.begin(9600); |
| 75 | + ArduinoSTL_Serial.connect(mySerial); |
| 76 | +} |
| 77 | +``` |
| 78 | +
|
| 79 | +## Avoiding Instantiation of ```cin``` and ```cout``` |
| 80 | +Comment out ```ARDUINOSTL_DEFAULT_CIN_COUT``` and nothing will be instantiated. You must comment out this flag if you intend to select a non-default serial port. There's no appreciable overhead for using ```printf()``` so you cannot currently avoid initializaing it. |
| 81 | +
|
| 82 | +## Known Issues |
| 83 | +
|
| 84 | +Printing of floats and doubles using ```cout``` ignores format specifiers. |
| 85 | +
|
| 86 | +uClibc seems to be fairly complete. Strings and vectors both work, even with the limited amount of heap available to Arduino. The uClibc++ status page can be found here: |
| 87 | +
|
| 88 | +https://cxx.uclibc.org/status.html |
| 89 | +
|
| 90 | +Always use the latest Arduino IDE. This library uses the Arduino IDE Library Specification rev.2.1 with features only available on Arduino 1.6.10 and higher. The specification can be found here: |
| 91 | +
|
| 92 | +https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification |
| 93 | +
|
| 94 | +## License |
| 95 | +
|
| 96 | +The uClibc++ library is licensed under the LGPL. This project adopts the LGPL to be compatible with the bulk of the code that it uses. Unless otherwise noted all code is licensed under the LGPL. There's one exception: |
| 97 | +
|
| 98 | + - src/serstream is licensed under the BSD license according to Andy Brown's wishes here: http://andybrown.me.uk/terms-and-conditions/ |
0 commit comments