[209] | 1 | // Copyright Benoit Blanchon 2014-2016
|
---|
| 2 | // MIT License
|
---|
| 3 | //
|
---|
| 4 | // Arduino JSON library
|
---|
| 5 | // https://github.com/bblanchon/ArduinoJson
|
---|
| 6 | // If you like this project, please add a star!
|
---|
| 7 |
|
---|
| 8 | #pragma once
|
---|
| 9 |
|
---|
| 10 | #ifndef ARDUINO
|
---|
| 11 |
|
---|
| 12 | #include "../Internals/JsonFloat.hpp"
|
---|
| 13 | #include "../Internals/JsonInteger.hpp"
|
---|
| 14 |
|
---|
| 15 | #include <stddef.h>
|
---|
| 16 | #include <stdint.h>
|
---|
| 17 | #include <stdio.h>
|
---|
| 18 |
|
---|
| 19 | #if defined(_MSC_VER) && _MSC_VER <= 1800
|
---|
| 20 | // snprintf has been added in Visual Studio 2015
|
---|
| 21 | #define ARDUINOJSON_SNPRINTF _snprintf
|
---|
| 22 | #else
|
---|
| 23 | #define ARDUINOJSON_SNPRINTF snprintf
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 | // This class reproduces Arduino's Print class
|
---|
| 27 | class Print {
|
---|
| 28 | public:
|
---|
| 29 | virtual ~Print() {}
|
---|
| 30 |
|
---|
| 31 | virtual size_t write(uint8_t) = 0;
|
---|
| 32 |
|
---|
| 33 | size_t print(const char* s) {
|
---|
| 34 | size_t n = 0;
|
---|
| 35 | while (*s) {
|
---|
| 36 | n += write(*s++);
|
---|
| 37 | }
|
---|
| 38 | return n;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | size_t print(ArduinoJson::Internals::JsonFloat value, int digits = 2) {
|
---|
| 42 | char tmp[32];
|
---|
| 43 |
|
---|
| 44 | // https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L220
|
---|
| 45 | bool isBigDouble = value > 4294967040.0 || value < -4294967040.0;
|
---|
| 46 |
|
---|
| 47 | if (isBigDouble) {
|
---|
| 48 | // Arduino's implementation prints "ovf"
|
---|
| 49 | // We prefer using the scientific notation, since we have sprintf
|
---|
| 50 | ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%g", value);
|
---|
| 51 | } else {
|
---|
| 52 | // Here we have the exact same output as Arduino's implementation
|
---|
| 53 | ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%.*f", digits, value);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | return print(tmp);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | size_t print(ArduinoJson::Internals::JsonInteger value) {
|
---|
| 60 | // see http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4
|
---|
| 61 | char buffer[22];
|
---|
| 62 |
|
---|
| 63 | size_t n = 0;
|
---|
| 64 | if (value < 0) {
|
---|
| 65 | value = -value;
|
---|
| 66 | n += write('-');
|
---|
| 67 | }
|
---|
| 68 | uint8_t i = 0;
|
---|
| 69 | do {
|
---|
| 70 | ArduinoJson::Internals::JsonInteger digit = value % 10;
|
---|
| 71 | value /= 10;
|
---|
| 72 | buffer[i++] = static_cast<char>(digit >= 0 ? '0' + digit : '0' - digit);
|
---|
| 73 | } while (value);
|
---|
| 74 |
|
---|
| 75 | while (i > 0) {
|
---|
| 76 | n += write(buffer[--i]);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | return n;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | size_t println() { return write('\r') + write('\n'); }
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | #else
|
---|
| 86 |
|
---|
| 87 | #include <Print.h>
|
---|
| 88 |
|
---|
| 89 | #endif
|
---|