billroy/bitlash — pull request #31 by matthijskooijman (2014-02-05 18:17:31+00:00)
Title: Switch to using Print and Stream classes internally

Previously a mix of variables (pin number, override function) were used to determine where output should go to. These variables were checked whenever a byte was output to decide among a few possible output functions. The input stream to use was only selectable at compiletime, requiring changes to the library's files.

With these changes, the input and output are stored in a variable of type `Stream*` and `Print*` respectively. This allows users of the library to select the input stream to use from the sketch, at runtime, which makes things more flexible.

This pullrequest is based on top of pullrequest #23, but I couldn't find a way to tell github this. This means that the commits from "Remove conioh.h and mygetch.c, these were unused" up to and including "Replace char * with const char * where applicable" should be ignored when reviewing this pullrequest. This pullrequest has a cleaned up history, the original commit sequence before I squashed things together is still available here: https://github.com/Pinoccio/library-bitlash/tree/printstream-history

Changes in the API:
- A new `initBitlash(Stream&)` is added, to select a different stream to use as the main console. The existing `initBitlash(int baud)` still works, and sets up bitlash to use the default console (usuall just `Serial`).
- A new `setOutpuHandler(Print&)` is added, to redirect output to an arbitrary `Print` object. Again, the existing `setOutputHandler` that takes a function pointer still works.
- It is no longer possible to call `setOutputHandler` before `initBitlash`. If this is a problem, the old behaviour could be preserved, but requires a bit more complicated code due to optimizations.
- If you run "print #0, 123", the output will now always go to the pin (using the `Serial` object, assuming the `Serial` object transmits on pin 0), disregarding any output handler. Before, using pin number 0 would mean "don't redirect to a pin", so it would use the output handler. IMHO the new behaviour makes more sense, both in behaviour as well as making the code look more sane.

Furthermore, Arduino versions below 1.0 are no longer supported. I would say that 1.0 is old enough now (over 2 years) that almost all users will have upgraded and there is no point in supporting < 1.0 anymore. From your comments, it seems like you think otherwise. If you really think supporting older versions is necessary, we can see if we can do this, but it might require invasive changes that cancel some of the code clarity advantages of using the Stream and Print classes. In particular, it seems that the Stream class was only introduced in version 023, so just before 1.0. Perhaps we can get away by doing `#define Stream HardwareSerial` for older versions (and same for Print for even older versions).

Another thing that is different pre-1.0 seems to be `.print()` vs `.write()`. Apparently, pre-1.0 doing `.print(uint8_t)` wrote a raw single-byte instead of the number in ASCII digits. However, it seems that `.write(uint8_t)` has been available at least since 015, so it seems to me that just using that unconditionaly will work with version before and after 1.0 (or did they break this somewhere between 015 and 023, which I both checked?).

Finally, I still need to test and possibly update the examples. I'll add that later, but feel free to already review these commits :-)