122 lines
3.1 KiB
C
Raw Normal View History

2021-01-29 12:31:58 +01:00
#pragma once
//
// FILE: printHelpers.h
// AUTHOR: Rob Tillaart
// DATE: 2018-01-21
2023-07-15 09:44:38 +02:00
// VERSION: 0.4.1
2021-12-24 13:26:40 +01:00
// PUPROSE: Arduino library to help formatting for printing.
2021-01-29 12:31:58 +01:00
// URL: https://github.com/RobTillaart/printHelpers
#include "Arduino.h"
#include "stdlib.h"
2023-07-15 09:44:38 +02:00
#define PRINTHELPERS_VERSION (F("0.4.1"))
2021-01-29 12:31:58 +01:00
2022-11-22 15:52:32 +01:00
// global buffer used by all functions so no static buffer in every function
// is needed ==> results need to be printed/copied asap
// not usable in multi-threading environments (use with care)
2022-04-15 20:50:26 +02:00
//
2022-11-22 15:52:32 +01:00
// 24 is a pretty safe minimum
2023-01-28 13:52:36 +01:00
//
2022-04-15 20:50:26 +02:00
#ifndef PRINTBUFFERSIZE
#define PRINTBUFFERSIZE 66
#endif
2021-01-29 12:31:58 +01:00
////////////////////////////////////////////////////////////
//
2023-01-28 13:52:36 +01:00
// print64()
2021-01-29 12:31:58 +01:00
//
2022-11-22 15:52:32 +01:00
// print64 note
// buffer size 66 will work for base 2 -36
// buffer size 34 will work for base 4 -36
// buffer size 24 will work for base 8 -36
// buffer size 22 will work for base 10 - 36
2021-01-29 12:31:58 +01:00
2022-04-15 20:50:26 +02:00
char * print64(int64_t value, uint8_t base = 10);
2021-01-29 12:31:58 +01:00
2022-04-15 20:50:26 +02:00
char * print64(uint64_t value, uint8_t base = 10);
2021-01-29 12:31:58 +01:00
////////////////////////////////////////////////////////////
//
2023-01-28 13:52:36 +01:00
// Scientific + Engineering notation
2021-01-29 12:31:58 +01:00
//
2022-11-22 15:52:32 +01:00
// typical buffer size for 8 byte double is 22 bytes
// 15 bytes mantissa, sign dot E-xxx
2021-11-13 18:15:22 +01:00
// em = exponentMultiple.
2022-04-15 20:50:26 +02:00
char * scieng(double value, uint8_t decimals, uint8_t em);
2021-01-29 12:31:58 +01:00
2023-01-28 13:52:36 +01:00
char * eng(double value, uint8_t decimals); // em == 3
2021-01-29 12:31:58 +01:00
2023-01-28 13:52:36 +01:00
char * sci(double value, uint8_t decimals); // em == 1
2021-01-29 12:31:58 +01:00
2023-07-15 09:44:38 +02:00
size_t sci(Stream &str, double value, uint8_t decimals);
2021-01-29 12:31:58 +01:00
2021-12-24 13:26:40 +01:00
2021-01-29 12:31:58 +01:00
////////////////////////////////////////////////////////////
//
2023-01-28 13:52:36 +01:00
// toBytes()
2021-01-29 12:31:58 +01:00
//
2022-11-22 15:52:32 +01:00
// official support to UDA == 1024^12
// kilo mega giga tera peta exa (1024^6)
// zetta yotta xona weka vunda uda (1024^12)
2021-01-29 12:31:58 +01:00
//
2022-11-22 15:52:32 +01:00
// (treda Byte == TDB is the next one and it is 2 char
// so code wise difficult and as it is seldom used, support stops there.
2021-12-24 13:26:40 +01:00
//
2022-11-22 15:52:32 +01:00
// To have some support the code uses lowercase for the next 8 levels
// treda sorta rinta quexa pepta ocha nena minga luma (1024 ^21 ~~ 10^63)
2022-04-15 20:50:26 +02:00
char * toBytes(double value, uint8_t decimals = 2);
2021-01-29 12:31:58 +01:00
2022-11-29 17:12:53 +01:00
////////////////////////////////////////////////////////////
//
2023-01-28 13:52:36 +01:00
// hex()
2022-11-29 17:12:53 +01:00
//
2023-01-28 13:52:36 +01:00
// always leading zero's - no prefix - no separators
2022-11-29 17:12:53 +01:00
// cast if needed.
char * hex(uint64_t value, uint8_t digits = 16);
2023-01-28 13:52:36 +01:00
char * hex(uint32_t value, uint8_t digits = 8);
char * hex(uint16_t value, uint8_t digits = 4);
char * hex(uint8_t value, uint8_t digits = 2);
2022-11-29 17:12:53 +01:00
////////////////////////////////////////////////////////////
//
2023-01-28 13:52:36 +01:00
// BIN
2022-11-29 17:12:53 +01:00
//
2023-01-28 13:52:36 +01:00
// always leading zero's - no prefix - no separators
2022-11-29 17:12:53 +01:00
// cast if needed.
char * bin(uint64_t value, uint8_t digits = 64);
char * bin(uint32_t value, uint8_t digits = 32);
char * bin(uint16_t value, uint8_t digits = 16);
char * bin(uint8_t value, uint8_t digits = 8);
2023-01-29 11:51:20 +01:00
////////////////////////////////////////////////////////////
//
// toRoman()
//
// value should be in range 1..9999
2023-07-15 09:44:38 +02:00
// values 10K-100M are experimental in lower case (see readme.md)
2023-01-29 11:51:20 +01:00
char * toRoman(uint32_t value);
2023-07-15 09:44:38 +02:00
////////////////////////////////////////////////////////////
//
// Distances
// Experimental
//
// step == 2,4,8,16,32,64,128,256 (default 16)
char * printInch(float inch, uint16_t step = 16);
char * printFeet(float feet);
2022-11-22 15:52:32 +01:00
// -- END OF FILE --
2021-12-24 13:26:40 +01:00