mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.3.1 PrintSize
This commit is contained in:
parent
8a64b66732
commit
ea40d0ee93
@ -2,6 +2,13 @@ compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
- leonardo
|
||||
- due
|
||||
- zero
|
||||
- leonardo
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
||||
# Declaring Dependent Arduino Libraries (to be installed via the Arduino Library Manager)
|
||||
libraries:
|
||||
- "XMLWriter"
|
@ -4,10 +4,14 @@ name: Arduino CI
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
arduino_ci:
|
||||
runTest:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: Arduino-CI/action@master
|
||||
# Arduino-CI/action@v0.1.1
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
- run: |
|
||||
gem install arduino_ci
|
||||
arduino_ci.rb
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: PrintSize.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.0
|
||||
// VERSION: 0.3.1
|
||||
// PURPOSE: Class that determines printSize
|
||||
// DATE: 2017-12-09
|
||||
// URL: https://github.com/RobTillaart/PrintSize
|
||||
@ -12,11 +12,16 @@
|
||||
// 0.2.0 2020-04-30 add total counter to sum multiple print statements
|
||||
// 0.2.1 2020-05-26 fix #1 - URLS + centering example
|
||||
// 0.2.2 2020-06-19 fix library.json
|
||||
// 0.3.0 2021-01-06 arduino-CI + unit test
|
||||
// 0.3.0 2021-01-06 Arduino-CI + unit test
|
||||
// 0.3.1 2021-11-13 update Arduino-CI, readme.md, badges
|
||||
// add write(str, length) for Print interface.
|
||||
//
|
||||
|
||||
|
||||
#include "Print.h"
|
||||
|
||||
#define PRINTSIZE_VERSION (F("0.3.0"))
|
||||
#define PRINTSIZE_VERSION (F("0.3.1"))
|
||||
|
||||
|
||||
class PrintSize: public Print
|
||||
{
|
||||
@ -26,19 +31,31 @@ public:
|
||||
reset();
|
||||
};
|
||||
|
||||
// note: warning unused parameter - remove c to remove warning)
|
||||
size_t write(uint8_t c)
|
||||
|
||||
|
||||
size_t write(uint8_t c) // note: warning unused parameter
|
||||
{
|
||||
_total++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
size_t write(uint8_t * str, uint8_t length) // note: warning unused parameter
|
||||
{
|
||||
_total += length;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
void reset() { _total = 0; }
|
||||
|
||||
|
||||
uint32_t total() { return _total; };
|
||||
|
||||
|
||||
private:
|
||||
uint32_t _total = 0;
|
||||
|
||||
};
|
||||
|
||||
// -- END OF FILE --
|
||||
|
@ -5,15 +5,13 @@
|
||||
// PURPOSE: demo printSize
|
||||
// URL: https://github.com/RobTillaart/PrintSize
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2017-12-09 initial version
|
||||
// 0.1.1 2020-02-19 refactored, simpler printSpaces()
|
||||
//
|
||||
|
||||
|
||||
#include "PrintSize.h"
|
||||
|
||||
PrintSize ps;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -24,6 +22,7 @@ void setup()
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
@ -44,9 +43,12 @@ void loop()
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void printSpaces(uint8_t n)
|
||||
{
|
||||
while (n--) Serial.print(' ');
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,13 +1,10 @@
|
||||
//
|
||||
// FILE: PrintSize_centering.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo printSize centering
|
||||
// VERSION: 0.1.1
|
||||
// PURPOSE: demo printSize centring
|
||||
// URL: https://github.com/RobTillaart/PrintSize
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2020-04-30 initial version
|
||||
//
|
||||
|
||||
|
||||
#include "PrintSize.h"
|
||||
@ -17,6 +14,7 @@ int idx = 0;
|
||||
|
||||
PrintSize ps;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -30,13 +28,13 @@ void setup()
|
||||
ps.reset();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
|
||||
|
||||
Serial.println();
|
||||
Serial.println(" Centering");
|
||||
Serial.println(" Centring");
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
// create different order of magnitude random numbers
|
||||
@ -75,9 +73,12 @@ void loop()
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
void printSpaces(uint8_t n)
|
||||
{
|
||||
while (n--) Serial.print(' ');
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -2,6 +2,10 @@ compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
# - uno
|
||||
# - leonardo
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
# - m4
|
||||
- esp32 # only one supporting printf()
|
||||
- esp8266
|
||||
# - mega2560
|
@ -7,9 +7,7 @@
|
||||
// NOTE:
|
||||
// - UNO does not support printf, - ESP32 does
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2020-02-16 initial version
|
||||
//
|
||||
|
||||
|
||||
#include "PrintSize.h"
|
||||
|
||||
@ -53,4 +51,6 @@ void printSpaces(uint8_t n)
|
||||
while (n--) Serial.print(' ');
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,13 +1,10 @@
|
||||
//
|
||||
// FILE: PrintSize_total.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// VERSION: 0.1.1
|
||||
// PURPOSE: demo printSize total counter
|
||||
// URL: https://github.com/RobTillaart/PrintSize
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2020-04-30 initial version
|
||||
//
|
||||
|
||||
/*
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mattis eget odio ut
|
||||
@ -22,6 +19,8 @@
|
||||
nisl, porttitor malesuada erat urna eu neque. Phasellus ultricies ante tortor,
|
||||
ac facilisis diam dignissim sit amet. Donec accumsan ac orci a malesuada.
|
||||
*/
|
||||
|
||||
|
||||
#include "PrintSize.h"
|
||||
|
||||
char woord[24];
|
||||
@ -29,6 +28,7 @@ int idx = 0;
|
||||
|
||||
PrintSize ps;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -40,6 +40,7 @@ void setup()
|
||||
ps.reset();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (Serial.available() > 0)
|
||||
@ -59,8 +60,9 @@ void loop()
|
||||
}
|
||||
}
|
||||
|
||||
// split the stream of words in lines of maxlen
|
||||
void process(char * w, uint8_t maxlen)
|
||||
|
||||
// split the stream of words in lines of maxLength
|
||||
void process(char * w, uint8_t maxLength)
|
||||
{
|
||||
// skip empty words.
|
||||
if (strlen(w) == 0) return;
|
||||
@ -71,10 +73,10 @@ void process(char * w, uint8_t maxlen)
|
||||
// does the word fit on the line
|
||||
ps.print(w);
|
||||
ps.print(' ');
|
||||
if (ps.total() >= maxlen)
|
||||
if (ps.total() >= maxLength)
|
||||
{
|
||||
// if not, fill line with -
|
||||
for (; prev < maxlen; prev++) Serial.print('-');
|
||||
for (; prev < maxLength; prev++) Serial.print('-');
|
||||
Serial.println();
|
||||
|
||||
// start counter for new line.
|
||||
@ -86,4 +88,6 @@ void process(char * w, uint8_t maxlen)
|
||||
Serial.print(' ');
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
# Syntax Coloring Map For PrintSize
|
||||
# Syntax Colouring Map For PrintSize
|
||||
|
||||
# Datatypes (KEYWORD1)
|
||||
# Data types (KEYWORD1)
|
||||
PrintSize KEYWORD1
|
||||
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
write KEYWORD2
|
||||
reset KEYWORD2
|
||||
total KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
PRINTSIZE_VERSION LITERAL1
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/PrintSize.git"
|
||||
},
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=PrintSize
|
||||
version=0.3.0
|
||||
version=0.3.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library to determine size of a printed variable.
|
||||
|
@ -1,31 +1,52 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/PrintSize/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![Arduino-lint](https://github.com/RobTillaart/PrintSize/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PrintSize/actions/workflows/arduino-lint.yml)
|
||||
[![JSON check](https://github.com/RobTillaart/PrintSize/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PrintSize/actions/workflows/jsoncheck.yml)
|
||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PrintSize/blob/master/LICENSE)
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/PrintSize.svg?maxAge=3600)](https://github.com/RobTillaart/PrintSize/releases)
|
||||
|
||||
|
||||
# PrintSize
|
||||
|
||||
Arduino library to determine the length of print statements
|
||||
Arduino library to determine the length of print statements.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
PrintSize is a minimal library to determine the length of a variable when printed.
|
||||
This includes printing of floats, integers in decimal or hex notation.
|
||||
|
||||
Works for **print()**, **println()** and if supported **printf()** e.g. ESP32.
|
||||
Works for **print()**, **println()** and if supported **printf()** e.g. ESP32.
|
||||
|
||||
Finally since **0.2.0** it has a total counter to add up the characters "printed" since
|
||||
the last **reset()** call. (see example)
|
||||
|
||||
This library relates to https://github.com/RobTillaart/PrintCharArray which
|
||||
holds the printed data in a buffer for later processing.
|
||||
|
||||
|
||||
## Operational
|
||||
|
||||
Example shows the right alignment of 10 random numbers
|
||||
Example shows the right alignment of 10 random numbers.
|
||||
|
||||
Example shows (elementary) line fitting
|
||||
Example shows (elementary) line fitting.
|
||||
|
||||
|
||||
## Applications
|
||||
|
||||
Can be used to calculate the needed space.
|
||||
- to properly do a right alignment e.g. for numbers or variable text
|
||||
- do left alignement and overwrite previous output with just enough spaces.
|
||||
- centering of numbers
|
||||
- see if output will fit into a line / display
|
||||
- to properly do a right alignment e.g. for numbers or variable text.
|
||||
- do left alignment and overwrite previous output with just enough spaces.
|
||||
- centring of numbers.
|
||||
- see if output will fit into a line / display
|
||||
- see if a string fits in EEPROM or any other storage medium.
|
||||
- see if a string fits in a communication buffer.
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- add examples
|
||||
- add a function to handle **tab** char correctly e.g.
|
||||
could add more than one char. Interferes with the write(str, length).
|
||||
-
|
||||
|
||||
|
@ -59,23 +59,54 @@ unittest(test_new_operator)
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", PRINTSIZE_VERSION);
|
||||
fprintf(stderr, "VERSION: %s\n", (char *) PRINTSIZE_VERSION);
|
||||
|
||||
PrintSize ps;
|
||||
|
||||
assertEqual(0, ps.total());
|
||||
}
|
||||
|
||||
|
||||
|
||||
unittest(test_print)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", (char *) PRINTSIZE_VERSION);
|
||||
|
||||
PrintSize ps;
|
||||
|
||||
assertEqual(0, ps.total());
|
||||
assertEqual(11, ps.print("Hello World"));
|
||||
assertEqual(11, ps.total());
|
||||
assertEqual(6, ps.print(PI, 4));
|
||||
assertEqual(17, ps.total());
|
||||
|
||||
ps.reset();
|
||||
assertEqual(0, ps.total());
|
||||
assertEqual(13, ps.println("Hello World"));
|
||||
assertEqual(13, ps.total());
|
||||
assertEqual(8, ps.println(PI, 4));
|
||||
assertEqual(21, ps.total());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
unittest(test_write)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", (char *) PRINTSIZE_VERSION);
|
||||
|
||||
PrintSize ps;
|
||||
|
||||
assertEqual(0, ps.total());
|
||||
assertEqual(1, ps.write('c'));
|
||||
|
||||
ps.reset();
|
||||
assertEqual(0, ps.total());
|
||||
char str[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mattis eget odio ut";
|
||||
assertEqual(strlen(str), ps.write((uint8_t *)str, strlen(str)));
|
||||
assertEqual(strlen(str), ps.total());
|
||||
}
|
||||
|
||||
|
||||
unittest_main()
|
||||
|
||||
// --------
|
||||
|
Loading…
x
Reference in New Issue
Block a user