From d6869d1ec4bd24cd2c3eafa534f0849b25ec5607 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 28 Feb 2019 17:04:22 -0500 Subject: added basic code --- .../Peripheral/custom_htm/IEEE11073float.cpp | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100755 arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp (limited to 'arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp') diff --git a/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp new file mode 100755 index 0000000..6b372bd --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Peripheral/custom_htm/IEEE11073float.cpp @@ -0,0 +1,106 @@ +/**************************************************************************/ +/*! + @file IEEE11073float.h +*/ +/**************************************************************************/ + +/** + * \file bytelib.c + * \brief Byte manipulation module implementation. + * Copyright (C) 2010 Signove Tecnologia Corporation. + * All rights reserved. + * Contact: Signove Tecnologia Corporation (contact@signove.com) + * + * $LICENSE_TEXT:BEGIN$ + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation and appearing + * in the file LICENSE included in the packaging of this file; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * $LICENSE_TEXT:END$ + * + * \author Walter Guerra, Mateus Lima + * \date Jun 14, 2010 + */ + +#include +#include "IEEE11073float.h" + +uint32_t float2IEEE11073(double data, uint8_t output[4]) +{ + uint32_t result = MDER_NaN; + + + if (isnan(data)) { + goto finally; + }/* else if (data > MDER_FLOAT_MAX) { + result = MDER_POSITIVE_INFINITY; + goto finally; + } else if (data < MDER_FLOAT_MIN) { + result = MDER_NEGATIVE_INFINITY; + goto finally; + } else if (data >= -MDER_FLOAT_EPSILON && + data <= MDER_FLOAT_EPSILON) { + result = 0; + goto finally; + }*/ + + double sgn; sgn = data > 0 ? +1 : -1; + double mantissa; mantissa = fabs(data); + int32_t exponent; exponent = 0; // Note: 10**x exponent, not 2**x + + // scale up if number is too big + while (mantissa > MDER_FLOAT_MANTISSA_MAX) { + mantissa /= 10.0; + ++exponent; + if (exponent > MDER_FLOAT_EXPONENT_MAX) { + // argh, should not happen + if (sgn > 0) { + result = MDER_POSITIVE_INFINITY; + } else { + result = MDER_NEGATIVE_INFINITY; + } + goto finally; + } + } + + // scale down if number is too small + while (mantissa < 1) { + mantissa *= 10; + --exponent; + if (exponent < MDER_FLOAT_EXPONENT_MIN) { + // argh, should not happen + result = 0; + goto finally; + } + } + + // scale down if number needs more precision + double smantissa; smantissa = round(mantissa * MDER_FLOAT_PRECISION); + double rmantissa; rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; + double mdiff; mdiff = abs(smantissa - rmantissa); + while (mdiff > 0.5 && exponent > MDER_FLOAT_EXPONENT_MIN && + (mantissa * 10) <= MDER_FLOAT_MANTISSA_MAX) { + mantissa *= 10; + --exponent; + smantissa = round(mantissa * MDER_FLOAT_PRECISION); + rmantissa = round(mantissa) * MDER_FLOAT_PRECISION; + mdiff = abs(smantissa - rmantissa); + } + + uint32_t int_mantissa; int_mantissa = (int) round(sgn * mantissa); + result = (exponent << 24) | (int_mantissa & 0xFFFFFF); + +finally: + if ( output ) memcpy(output, &result, 4); + return result; +} -- cgit v1.2.3