Christoph Schneeberger
23579c677f
|
6 years ago | |
---|---|---|
docs/images | 6 years ago | |
examples | ||
LICENSE | ||
README.md | 6 years ago | |
TerSer.h |
README.md
TerSer
A much smaller and more efficient serial library than Arduinos Serial. Use an include file to replace the Serial.print from Arduino library.
Table of Contents
|
What is the problem with Arduino Serial.print ?
Example comparison between Didel TerSer and Arduino Serial
Using tabular data
TerSer Limitations
Code size and timing comparison
|
Also when Serial debugging is used to display sensor values, as shown next, suppressing non significative zeros is not adequate for tabular data, specially when the screen is scrolling. Serial.print is a good example of the "Law of instrument": One just uses a bad but widely available tool, and is not willing to find or invent a better one.
We propose to use a compact C portable library. Compatibility with an Oled display is a further plus. The SerTerm.h and OledTerm.h are demonstrated on the popular Arduino environment, but it is just plain C.
It is admittedly very convenient to just use Serial.print(var); since it doesn't require you to specify the type of variable. Though when trying for example to output data in tabular style the processor needs to know the data type used so that it can reserve the adequate space for it in its output. It might seem like a big inconvenience to specify the data type with any statement that outputs data over serial but then again for debugging purposes it makes perfectly sense in terms of speed AND size of the resulting code. By the same reasons we also do not use a buffer as it is needed only in specific situations.
### Example comparison between Didel TerSer and Arduino Serial | TerSer | Output TerSer | Output Serial | Arduino Serial | | ------------- | ------------- | ------------- | ------------- | | Car('Z'); | Z | Z | Serial.print('Z'); | | Text("Test"); | Test | Test | Serial.print("Test"); | | Bin16(v16); | 0000010001100000 | 10001100000 | Serial.print(v16,BIN); | | Hex16(v16); | 08c0 | 8c0 | Serial.print(v16,HEX); | | Dec16(v16); | 1120 | 1120 | Serial.print(v16); | | Dec16(v16b); | 0020 or 20 | 20 | Serial.print(v16b); | | Dec16(v16s); | + 20 or +0020 | 20 | Serial.print(v16s); | | Dec16(-v16s); | 20 or -0020 | -20 | Serial.print(-v16s); |uint16_t v16=1120; uint16_t v16b=20; int16_t v16s=20;
### Using tabular dataFilling the non significative digits with zero or space is a general options, named ShowZ(); and HideZ();
The Tab(nn); function, allows to align data of different types.
e.g. Table next had to be written:
Text ("Mode HideZ();") SetTab(15);
Text ("Mode ShowZ();");NL();
HideZ();Dec8s(3); SetTab(15);
ShowZ();Dec8s(3); NL();
. . .
It is up to you to add functions like TextNL(); if you need to keep your Serial.println() habits.
### TerSer Limitations TerSer does not print floating point numbers. The may happen some day with one more file to import, e.g. names TerFloat.h.Dec8(); and Dec16(); use a tricky macro to recognize the signed or unsigned data type. The parameter must be a single variable, e.g. Dec8(var); Dec8(var+2); will give wrong results. Code will be shorter and there will be no limitation if you use Dec8u(any unsigned expr); and Dec8s(any signed expr);, same of course Dec16u(); and Dec16s();
### Code size and timing comparison Code size has been obtained by calling one function at a time, compiler under the usual -0s mode. Size is the difference with the empty file size. TerSer.h can of course be used with setup() and loop(). It add the ~300 bytes Arduino initializations. Execution time has been measured with a Nop replacing the SendCar function. The time depends on the baud rate, ~1 ms per character displayed at 9600 bits/s.TerSer.ino Terminal 174b 0v
Adds 174b for total size:
#include "TerSer.h"
int main() {
SetupTerSer();
while(1) {
// Text("Test"); // 20b
// Bin8(v8); // 16b 3us
// Bin16(v16); // 42b 6,8us
// Hex8(v8); // 52b 4us
// Hex16(v16); // 82b 11.5us
// Dec8(vs8); //116b 13us
// Dec16(vs16); //200b 52us 14v
// max 14 variables
} // end while(1)
} // end main()
PrintArduino empty 1426b 184v
Adds 1426b for program size:
void setup() {
Serial.begin(9600);
}
void loop() {
// Serial.print("Text"); // 22 190v
// Serial.println("text"); // 40 192v
// Serial.print(123,HEX); // 60 184v
// Serial.print(12335,HEX);// 120 184v
// Serial.print(-123); // 266 184v
// Serial.print(-12345); // 266 184v
// Serial.println(12345); // 286 188v
for(;;);
}