LCOV - code coverage report
Current view: top level - AP_PiccoloCAN/piccolo_protocol - scaledencode.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 488 0.0 %
Date: 2026-09-09 11:02:08 Functions: 0 65 0.0 %

          Line data    Source code
       1             : // scaledencode.c was generated by ProtoGen version 3.5.c
       2             : 
       3             : /*
       4             :  * This file is free software: you can redistribute it and/or modify it
       5             :  * under the terms of the GNU General Public License as published by the
       6             :  * Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This file is distributed in the hope that it will be useful, but
      10             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      12             :  * See the GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License along
      15             :  * with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  *
      17             :  * Author: Oliver Walters / Currawong Engineering Pty Ltd
      18             :  */
      19             : 
      20             : #include "scaledencode.h"
      21             : #include "fieldencode.h"
      22             : 
      23             : 
      24             : /*!
      25             :  * Scale a float using floating point scaling to the base integer type used for
      26             :  * bitfields.
      27             :  * \param value is the number to scale.
      28             :  * \param min is the minimum value that can be encoded.
      29             :  * \param scaler is multiplied by value to create the encoded integer.
      30             :  * \param bits is the number of bits in the bitfield, used to limit the returned value.
      31             :  * \return (value-min)*scaler.
      32             :  */
      33           0 : unsigned int float32ScaledToBitfield(float value, float min, float scaler, int bits)
      34             : {
      35             :     // The largest integer the bitfield can hold
      36           0 :     unsigned int max = (0x1u << bits) - 1;
      37             : 
      38             :     // Protect from underflow
      39           0 :     if(value < min)
      40           0 :         return 0;
      41             : 
      42             :     // Scale the number
      43           0 :     value = (value - min)*scaler;
      44             : 
      45             :     // Protect from overflow
      46           0 :     if(value > max)
      47           0 :         return max;
      48             : 
      49             :     // Account for fractional truncation
      50           0 :     return (unsigned int)(value + 0.5f);
      51             : }
      52             : 
      53             : 
      54             : /*!
      55             :  * Encode a float on a byte stream by floating point scaling to fit in 4
      56             :  * unsigned bytes in big endian order.
      57             :  * \param value is the number to encode.
      58             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
      59             :  * \param index gives the location of the first byte in the byte stream, and
      60             :  *        will be incremented by 4 when this function is complete.
      61             :  * \param min is the minimum value that can be encoded.
      62             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
      63             :  */
      64           0 : void float32ScaledTo4UnsignedBeBytes(float value, uint8_t* bytes, int* index, float min, float scaler)
      65             : {
      66             :     // scale the number
      67           0 :     float scaledvalue = (float)((value - min)*scaler);
      68             :     uint32_t number;
      69             : 
      70             :     // Make sure number fits in the range
      71           0 :     if(scaledvalue >= 4294967295ul)
      72           0 :         number = 4294967295ul;
      73           0 :     else if(scaledvalue <= 0)
      74           0 :         number = 0;
      75             :     else
      76           0 :         number = (uint32_t)(scaledvalue + 0.5f); // account for fractional truncation
      77             : 
      78           0 :     uint32ToBeBytes(number, bytes, index);
      79           0 : }
      80             : 
      81             : 
      82             : /*!
      83             :  * Encode a float on a byte stream by floating point scaling to fit in 4
      84             :  * unsigned bytes in little endian order.
      85             :  * \param value is the number to encode.
      86             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
      87             :  * \param index gives the location of the first byte in the byte stream, and
      88             :  *        will be incremented by 4 when this function is complete.
      89             :  * \param min is the minimum value that can be encoded.
      90             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
      91             :  */
      92           0 : void float32ScaledTo4UnsignedLeBytes(float value, uint8_t* bytes, int* index, float min, float scaler)
      93             : {
      94             :     // scale the number
      95           0 :     float scaledvalue = (float)((value - min)*scaler);
      96             :     uint32_t number;
      97             : 
      98             :     // Make sure number fits in the range
      99           0 :     if(scaledvalue >= 4294967295ul)
     100           0 :         number = 4294967295ul;
     101           0 :     else if(scaledvalue <= 0)
     102           0 :         number = 0;
     103             :     else
     104           0 :         number = (uint32_t)(scaledvalue + 0.5f); // account for fractional truncation
     105             : 
     106           0 :     uint32ToLeBytes(number, bytes, index);
     107           0 : }
     108             : 
     109             : 
     110             : /*!
     111             :  * Encode a float on a byte stream by floating point scaling to fit in 4 signed
     112             :  * bytes in big endian order.
     113             :  * \param value is the number to encode.
     114             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     115             :  * \param index gives the location of the first byte in the byte stream, and
     116             :  *        will be incremented by 4 when this function is complete.
     117             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     118             :  */
     119           0 : void float32ScaledTo4SignedBeBytes(float value, uint8_t* bytes, int* index, float scaler)
     120             : {
     121             :     // scale the number
     122           0 :     float scaledvalue = (float)(value*scaler);
     123             :     int32_t number;
     124             : 
     125             :     // Make sure number fits in the range
     126           0 :     if(scaledvalue >= 0)
     127             :     {
     128           0 :         if(scaledvalue >= 2147483647l)
     129           0 :             number = 2147483647l;
     130             :         else
     131           0 :             number = (int32_t)(scaledvalue + 0.5f); // account for fractional truncation
     132             :     }
     133             :     else
     134             :     {
     135           0 :         if(scaledvalue <= (-2147483647l - 1))
     136           0 :             number = (-2147483647l - 1);
     137             :         else
     138           0 :             number = (int32_t)(scaledvalue - 0.5f); // account for fractional truncation
     139             :     }
     140             : 
     141           0 :     int32ToBeBytes(number, bytes, index);
     142           0 : }
     143             : 
     144             : 
     145             : /*!
     146             :  * Encode a float on a byte stream by floating point scaling to fit in 4 signed
     147             :  * bytes in little endian order.
     148             :  * \param value is the number to encode.
     149             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     150             :  * \param index gives the location of the first byte in the byte stream, and
     151             :  *        will be incremented by 4 when this function is complete.
     152             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     153             :  */
     154           0 : void float32ScaledTo4SignedLeBytes(float value, uint8_t* bytes, int* index, float scaler)
     155             : {
     156             :     // scale the number
     157           0 :     float scaledvalue = (float)(value*scaler);
     158             :     int32_t number;
     159             : 
     160             :     // Make sure number fits in the range
     161           0 :     if(scaledvalue >= 0)
     162             :     {
     163           0 :         if(scaledvalue >= 2147483647l)
     164           0 :             number = 2147483647l;
     165             :         else
     166           0 :             number = (int32_t)(scaledvalue + 0.5f); // account for fractional truncation
     167             :     }
     168             :     else
     169             :     {
     170           0 :         if(scaledvalue <= (-2147483647l - 1))
     171           0 :             number = (-2147483647l - 1);
     172             :         else
     173           0 :             number = (int32_t)(scaledvalue - 0.5f); // account for fractional truncation
     174             :     }
     175             : 
     176           0 :     int32ToLeBytes(number, bytes, index);
     177           0 : }
     178             : 
     179             : 
     180             : /*!
     181             :  * Encode a float on a byte stream by floating point scaling to fit in 3
     182             :  * unsigned bytes in big endian order.
     183             :  * \param value is the number to encode.
     184             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     185             :  * \param index gives the location of the first byte in the byte stream, and
     186             :  *        will be incremented by 3 when this function is complete.
     187             :  * \param min is the minimum value that can be encoded.
     188             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     189             :  */
     190           0 : void float32ScaledTo3UnsignedBeBytes(float value, uint8_t* bytes, int* index, float min, float scaler)
     191             : {
     192             :     // scale the number
     193           0 :     float scaledvalue = (float)((value - min)*scaler);
     194             :     uint32_t number;
     195             : 
     196             :     // Make sure number fits in the range
     197           0 :     if(scaledvalue >= 16777215ul)
     198           0 :         number = 16777215ul;
     199           0 :     else if(scaledvalue <= 0)
     200           0 :         number = 0;
     201             :     else
     202           0 :         number = (uint32_t)(scaledvalue + 0.5f); // account for fractional truncation
     203             : 
     204           0 :     uint24ToBeBytes(number, bytes, index);
     205           0 : }
     206             : 
     207             : 
     208             : /*!
     209             :  * Encode a float on a byte stream by floating point scaling to fit in 3
     210             :  * unsigned bytes in little endian order.
     211             :  * \param value is the number to encode.
     212             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     213             :  * \param index gives the location of the first byte in the byte stream, and
     214             :  *        will be incremented by 3 when this function is complete.
     215             :  * \param min is the minimum value that can be encoded.
     216             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     217             :  */
     218           0 : void float32ScaledTo3UnsignedLeBytes(float value, uint8_t* bytes, int* index, float min, float scaler)
     219             : {
     220             :     // scale the number
     221           0 :     float scaledvalue = (float)((value - min)*scaler);
     222             :     uint32_t number;
     223             : 
     224             :     // Make sure number fits in the range
     225           0 :     if(scaledvalue >= 16777215ul)
     226           0 :         number = 16777215ul;
     227           0 :     else if(scaledvalue <= 0)
     228           0 :         number = 0;
     229             :     else
     230           0 :         number = (uint32_t)(scaledvalue + 0.5f); // account for fractional truncation
     231             : 
     232           0 :     uint24ToLeBytes(number, bytes, index);
     233           0 : }
     234             : 
     235             : 
     236             : /*!
     237             :  * Encode a float on a byte stream by floating point scaling to fit in 3 signed
     238             :  * bytes in big endian order.
     239             :  * \param value is the number to encode.
     240             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     241             :  * \param index gives the location of the first byte in the byte stream, and
     242             :  *        will be incremented by 3 when this function is complete.
     243             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     244             :  */
     245           0 : void float32ScaledTo3SignedBeBytes(float value, uint8_t* bytes, int* index, float scaler)
     246             : {
     247             :     // scale the number
     248           0 :     float scaledvalue = (float)(value*scaler);
     249             :     int32_t number;
     250             : 
     251             :     // Make sure number fits in the range
     252           0 :     if(scaledvalue >= 0)
     253             :     {
     254           0 :         if(scaledvalue >= 8388607)
     255           0 :             number = 8388607;
     256             :         else
     257           0 :             number = (int32_t)(scaledvalue + 0.5f); // account for fractional truncation
     258             :     }
     259             :     else
     260             :     {
     261           0 :         if(scaledvalue <= (-8388607l - 1))
     262           0 :             number = (-8388607l - 1);
     263             :         else
     264           0 :             number = (int32_t)(scaledvalue - 0.5f); // account for fractional truncation
     265             :     }
     266             : 
     267           0 :     int24ToBeBytes(number, bytes, index);
     268           0 : }
     269             : 
     270             : 
     271             : /*!
     272             :  * Encode a float on a byte stream by floating point scaling to fit in 3 signed
     273             :  * bytes in little endian order.
     274             :  * \param value is the number to encode.
     275             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     276             :  * \param index gives the location of the first byte in the byte stream, and
     277             :  *        will be incremented by 3 when this function is complete.
     278             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     279             :  */
     280           0 : void float32ScaledTo3SignedLeBytes(float value, uint8_t* bytes, int* index, float scaler)
     281             : {
     282             :     // scale the number
     283           0 :     float scaledvalue = (float)(value*scaler);
     284             :     int32_t number;
     285             : 
     286             :     // Make sure number fits in the range
     287           0 :     if(scaledvalue >= 0)
     288             :     {
     289           0 :         if(scaledvalue >= 8388607)
     290           0 :             number = 8388607;
     291             :         else
     292           0 :             number = (int32_t)(scaledvalue + 0.5f); // account for fractional truncation
     293             :     }
     294             :     else
     295             :     {
     296           0 :         if(scaledvalue <= (-8388607l - 1))
     297           0 :             number = (-8388607l - 1);
     298             :         else
     299           0 :             number = (int32_t)(scaledvalue - 0.5f); // account for fractional truncation
     300             :     }
     301             : 
     302           0 :     int24ToLeBytes(number, bytes, index);
     303           0 : }
     304             : 
     305             : 
     306             : /*!
     307             :  * Encode a float on a byte stream by floating point scaling to fit in 2
     308             :  * unsigned bytes in big endian order.
     309             :  * \param value is the number to encode.
     310             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     311             :  * \param index gives the location of the first byte in the byte stream, and
     312             :  *        will be incremented by 2 when this function is complete.
     313             :  * \param min is the minimum value that can be encoded.
     314             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     315             :  */
     316           0 : void float32ScaledTo2UnsignedBeBytes(float value, uint8_t* bytes, int* index, float min, float scaler)
     317             : {
     318             :     // scale the number
     319           0 :     float scaledvalue = (float)((value - min)*scaler);
     320             :     uint16_t number;
     321             : 
     322             :     // Make sure number fits in the range
     323           0 :     if(scaledvalue >= 65535u)
     324           0 :         number = 65535u;
     325           0 :     else if(scaledvalue <= 0)
     326           0 :         number = 0;
     327             :     else
     328           0 :         number = (uint16_t)(scaledvalue + 0.5f); // account for fractional truncation
     329             : 
     330           0 :     uint16ToBeBytes(number, bytes, index);
     331           0 : }
     332             : 
     333             : 
     334             : /*!
     335             :  * Encode a float on a byte stream by floating point scaling to fit in 2
     336             :  * unsigned bytes in little endian order.
     337             :  * \param value is the number to encode.
     338             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     339             :  * \param index gives the location of the first byte in the byte stream, and
     340             :  *        will be incremented by 2 when this function is complete.
     341             :  * \param min is the minimum value that can be encoded.
     342             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     343             :  */
     344           0 : void float32ScaledTo2UnsignedLeBytes(float value, uint8_t* bytes, int* index, float min, float scaler)
     345             : {
     346             :     // scale the number
     347           0 :     float scaledvalue = (float)((value - min)*scaler);
     348             :     uint16_t number;
     349             : 
     350             :     // Make sure number fits in the range
     351           0 :     if(scaledvalue >= 65535u)
     352           0 :         number = 65535u;
     353           0 :     else if(scaledvalue <= 0)
     354           0 :         number = 0;
     355             :     else
     356           0 :         number = (uint16_t)(scaledvalue + 0.5f); // account for fractional truncation
     357             : 
     358           0 :     uint16ToLeBytes(number, bytes, index);
     359           0 : }
     360             : 
     361             : 
     362             : /*!
     363             :  * Encode a float on a byte stream by floating point scaling to fit in 2 signed
     364             :  * bytes in big endian order.
     365             :  * \param value is the number to encode.
     366             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     367             :  * \param index gives the location of the first byte in the byte stream, and
     368             :  *        will be incremented by 2 when this function is complete.
     369             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     370             :  */
     371           0 : void float32ScaledTo2SignedBeBytes(float value, uint8_t* bytes, int* index, float scaler)
     372             : {
     373             :     // scale the number
     374           0 :     float scaledvalue = (float)(value*scaler);
     375             :     int16_t number;
     376             : 
     377             :     // Make sure number fits in the range
     378           0 :     if(scaledvalue >= 0)
     379             :     {
     380           0 :         if(scaledvalue >= 32767)
     381           0 :             number = 32767;
     382             :         else
     383           0 :             number = (int16_t)(scaledvalue + 0.5f); // account for fractional truncation
     384             :     }
     385             :     else
     386             :     {
     387           0 :         if(scaledvalue <= (-32767 - 1))
     388           0 :             number = (-32767 - 1);
     389             :         else
     390           0 :             number = (int16_t)(scaledvalue - 0.5f); // account for fractional truncation
     391             :     }
     392             : 
     393           0 :     int16ToBeBytes(number, bytes, index);
     394           0 : }
     395             : 
     396             : 
     397             : /*!
     398             :  * Encode a float on a byte stream by floating point scaling to fit in 2 signed
     399             :  * bytes in little endian order.
     400             :  * \param value is the number to encode.
     401             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     402             :  * \param index gives the location of the first byte in the byte stream, and
     403             :  *        will be incremented by 2 when this function is complete.
     404             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     405             :  */
     406           0 : void float32ScaledTo2SignedLeBytes(float value, uint8_t* bytes, int* index, float scaler)
     407             : {
     408             :     // scale the number
     409           0 :     float scaledvalue = (float)(value*scaler);
     410             :     int16_t number;
     411             : 
     412             :     // Make sure number fits in the range
     413           0 :     if(scaledvalue >= 0)
     414             :     {
     415           0 :         if(scaledvalue >= 32767)
     416           0 :             number = 32767;
     417             :         else
     418           0 :             number = (int16_t)(scaledvalue + 0.5f); // account for fractional truncation
     419             :     }
     420             :     else
     421             :     {
     422           0 :         if(scaledvalue <= (-32767 - 1))
     423           0 :             number = (-32767 - 1);
     424             :         else
     425           0 :             number = (int16_t)(scaledvalue - 0.5f); // account for fractional truncation
     426             :     }
     427             : 
     428           0 :     int16ToLeBytes(number, bytes, index);
     429           0 : }
     430             : 
     431             : 
     432             : /*!
     433             :  * Encode a float on a byte stream by floating point scaling to fit in 1
     434             :  * unsigned byte.
     435             :  * \param value is the number to encode.
     436             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     437             :  * \param index gives the location of the first byte in the byte stream, and
     438             :  *        will be incremented by 1 when this function is complete.
     439             :  * \param min is the minimum value that can be encoded.
     440             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     441             :  */
     442           0 : void float32ScaledTo1UnsignedBytes(float value, uint8_t* bytes, int* index, float min, float scaler)
     443             : {
     444             :     // scale the number
     445           0 :     float scaledvalue = (float)((value - min)*scaler);
     446             :     uint8_t number;
     447             : 
     448             :     // Make sure number fits in the range
     449           0 :     if(scaledvalue >= 255u)
     450           0 :         number = 255u;
     451           0 :     else if(scaledvalue <= 0)
     452           0 :         number = 0;
     453             :     else
     454           0 :         number = (uint8_t)(scaledvalue + 0.5f); // account for fractional truncation
     455             : 
     456           0 :     uint8ToBytes(number, bytes, index);
     457           0 : }
     458             : 
     459             : 
     460             : /*!
     461             :  * Encode a float on a byte stream by floating point scaling to fit in 1 signed
     462             :  * byte.
     463             :  * \param value is the number to encode.
     464             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     465             :  * \param index gives the location of the first byte in the byte stream, and
     466             :  *        will be incremented by 1 when this function is complete.
     467             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     468             :  */
     469           0 : void float32ScaledTo1SignedBytes(float value, uint8_t* bytes, int* index, float scaler)
     470             : {
     471             :     // scale the number
     472           0 :     float scaledvalue = (float)(value*scaler);
     473             :     int8_t number;
     474             : 
     475             :     // Make sure number fits in the range
     476           0 :     if(scaledvalue >= 0)
     477             :     {
     478           0 :         if(scaledvalue >= 127)
     479           0 :             number = 127;
     480             :         else
     481           0 :             number = (int8_t)(scaledvalue + 0.5f); // account for fractional truncation
     482             :     }
     483             :     else
     484             :     {
     485           0 :         if(scaledvalue <= (-127 - 1))
     486           0 :             number = (-127 - 1);
     487             :         else
     488           0 :             number = (int8_t)(scaledvalue - 0.5f); // account for fractional truncation
     489             :     }
     490             : 
     491           0 :     int8ToBytes(number, bytes, index);
     492           0 : }
     493             : 
     494             : 
     495             : /*!
     496             :  * Scale a uint32_t using integer scaling to the base integer type used for
     497             :  * bitfields.
     498             :  * \param value is the number to scale.
     499             :  * \param min is the minimum value that can be encoded.
     500             :  * \param scaler is multiplied by value to create the encoded integer.
     501             :  * \param bits is the number of bits in the bitfield, used to limit the returned value.
     502             :  * \return (value-min)*scaler.
     503             :  */
     504           0 : unsigned int uint32ScaledToBitfield(uint32_t value, int32_t min, uint32_t scaler, int bits)
     505             : {
     506             :     // The largest integer the bitfield can hold
     507           0 :     unsigned int max = (0x1u << bits) - 1;
     508             : 
     509             :     // Scale the number
     510           0 :     unsigned int number = (unsigned int)((value - min)*scaler);
     511             : 
     512             :     // Protect from underflow
     513           0 :     if(((int32_t)value) < min)
     514           0 :         return 0;
     515             : 
     516             :     // Protect from overflow
     517           0 :     if(number > max)
     518           0 :         return max;
     519             : 
     520           0 :     return number;
     521             : }
     522             : 
     523             : 
     524             : /*!
     525             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 4 unsigned
     526             :  * bytes in big endian order.
     527             :  * \param value is the number to encode.
     528             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     529             :  * \param index gives the location of the first byte in the byte stream, and
     530             :  *        will be incremented by 4 when this function is complete.
     531             :  * \param min is the minimum value that can be encoded.
     532             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     533             :  */
     534           0 : void uint32ScaledTo4UnsignedBeBytes(uint32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     535             : {
     536             :     // scale the number
     537           0 :     uint32_t number = 0;
     538             : 
     539             :     // Make sure number fits in the range
     540           0 :     if(((int32_t)value) > min)
     541             :     {
     542           0 :         number = (uint32_t)((value - min)*scaler);
     543             :     }
     544             : 
     545           0 :     uint32ToBeBytes(number, bytes, index);
     546           0 : }
     547             : 
     548             : 
     549             : /*!
     550             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 4 unsigned
     551             :  * bytes in little endian order.
     552             :  * \param value is the number to encode.
     553             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     554             :  * \param index gives the location of the first byte in the byte stream, and
     555             :  *        will be incremented by 4 when this function is complete.
     556             :  * \param min is the minimum value that can be encoded.
     557             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     558             :  */
     559           0 : void uint32ScaledTo4UnsignedLeBytes(uint32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     560             : {
     561             :     // scale the number
     562           0 :     uint32_t number = 0;
     563             : 
     564             :     // Make sure number fits in the range
     565           0 :     if(((int32_t)value) > min)
     566             :     {
     567           0 :         number = (uint32_t)((value - min)*scaler);
     568             :     }
     569             : 
     570           0 :     uint32ToLeBytes(number, bytes, index);
     571           0 : }
     572             : 
     573             : 
     574             : /*!
     575             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 4 signed
     576             :  * bytes in big endian order.
     577             :  * \param value is the number to encode.
     578             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     579             :  * \param index gives the location of the first byte in the byte stream, and
     580             :  *        will be incremented by 4 when this function is complete.
     581             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     582             :  */
     583           0 : void uint32ScaledTo4SignedBeBytes(uint32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     584             : {
     585             :     // scale the number
     586           0 :     int32_t number = (int32_t)(value*scaler);
     587             : 
     588           0 :     int32ToBeBytes(number, bytes, index);
     589           0 : }
     590             : 
     591             : 
     592             : /*!
     593             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 4 signed
     594             :  * bytes in little endian order.
     595             :  * \param value is the number to encode.
     596             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     597             :  * \param index gives the location of the first byte in the byte stream, and
     598             :  *        will be incremented by 4 when this function is complete.
     599             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     600             :  */
     601           0 : void uint32ScaledTo4SignedLeBytes(uint32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     602             : {
     603             :     // scale the number
     604           0 :     int32_t number = (int32_t)(value*scaler);
     605             : 
     606           0 :     int32ToLeBytes(number, bytes, index);
     607           0 : }
     608             : 
     609             : 
     610             : /*!
     611             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 3 unsigned
     612             :  * bytes in big endian order.
     613             :  * \param value is the number to encode.
     614             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     615             :  * \param index gives the location of the first byte in the byte stream, and
     616             :  *        will be incremented by 3 when this function is complete.
     617             :  * \param min is the minimum value that can be encoded.
     618             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     619             :  */
     620           0 : void uint32ScaledTo3UnsignedBeBytes(uint32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     621             : {
     622             :     // scale the number
     623           0 :     uint32_t number = 0;
     624             : 
     625             :     // Make sure number fits in the range
     626           0 :     if(((int32_t)value) > min)
     627             :     {
     628           0 :         number = (uint32_t)((value - min)*scaler);
     629           0 :         if(number > 16777215ul)
     630           0 :             number = 16777215ul;
     631             :     }
     632             : 
     633           0 :     uint24ToBeBytes((uint32_t)number, bytes, index);
     634           0 : }
     635             : 
     636             : 
     637             : /*!
     638             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 3 unsigned
     639             :  * bytes in little endian order.
     640             :  * \param value is the number to encode.
     641             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     642             :  * \param index gives the location of the first byte in the byte stream, and
     643             :  *        will be incremented by 3 when this function is complete.
     644             :  * \param min is the minimum value that can be encoded.
     645             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     646             :  */
     647           0 : void uint32ScaledTo3UnsignedLeBytes(uint32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     648             : {
     649             :     // scale the number
     650           0 :     uint32_t number = 0;
     651             : 
     652             :     // Make sure number fits in the range
     653           0 :     if(((int32_t)value) > min)
     654             :     {
     655           0 :         number = (uint32_t)((value - min)*scaler);
     656           0 :         if(number > 16777215ul)
     657           0 :             number = 16777215ul;
     658             :     }
     659             : 
     660           0 :     uint24ToLeBytes((uint32_t)number, bytes, index);
     661           0 : }
     662             : 
     663             : 
     664             : /*!
     665             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 3 signed
     666             :  * bytes in big endian order.
     667             :  * \param value is the number to encode.
     668             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     669             :  * \param index gives the location of the first byte in the byte stream, and
     670             :  *        will be incremented by 3 when this function is complete.
     671             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     672             :  */
     673           0 : void uint32ScaledTo3SignedBeBytes(uint32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     674             : {
     675             :     // scale the number
     676           0 :     int32_t number = (int32_t)(value*scaler);
     677             : 
     678             :     // Make sure number fits in the range
     679           0 :     if(number > 8388607l)
     680           0 :         number = 8388607l;
     681           0 :     else if(number < (-8388607l - 1))
     682           0 :         number = (-8388607l - 1);
     683             : 
     684           0 :     int24ToBeBytes((int32_t)number, bytes, index);
     685           0 : }
     686             : 
     687             : 
     688             : /*!
     689             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 3 signed
     690             :  * bytes in little endian order.
     691             :  * \param value is the number to encode.
     692             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     693             :  * \param index gives the location of the first byte in the byte stream, and
     694             :  *        will be incremented by 3 when this function is complete.
     695             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     696             :  */
     697           0 : void uint32ScaledTo3SignedLeBytes(uint32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     698             : {
     699             :     // scale the number
     700           0 :     int32_t number = (int32_t)(value*scaler);
     701             : 
     702             :     // Make sure number fits in the range
     703           0 :     if(number > 8388607l)
     704           0 :         number = 8388607l;
     705           0 :     else if(number < (-8388607l - 1))
     706           0 :         number = (-8388607l - 1);
     707             : 
     708           0 :     int24ToLeBytes((int32_t)number, bytes, index);
     709           0 : }
     710             : 
     711             : 
     712             : /*!
     713             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 2 unsigned
     714             :  * bytes in big endian order.
     715             :  * \param value is the number to encode.
     716             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     717             :  * \param index gives the location of the first byte in the byte stream, and
     718             :  *        will be incremented by 2 when this function is complete.
     719             :  * \param min is the minimum value that can be encoded.
     720             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     721             :  */
     722           0 : void uint32ScaledTo2UnsignedBeBytes(uint32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     723             : {
     724             :     // scale the number
     725           0 :     uint32_t number = 0;
     726             : 
     727             :     // Make sure number fits in the range
     728           0 :     if(((int32_t)value) > min)
     729             :     {
     730           0 :         number = (uint32_t)((value - min)*scaler);
     731           0 :         if(number > 65535u)
     732           0 :             number = 65535u;
     733             :     }
     734             : 
     735           0 :     uint16ToBeBytes((uint16_t)number, bytes, index);
     736           0 : }
     737             : 
     738             : 
     739             : /*!
     740             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 2 unsigned
     741             :  * bytes in little endian order.
     742             :  * \param value is the number to encode.
     743             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     744             :  * \param index gives the location of the first byte in the byte stream, and
     745             :  *        will be incremented by 2 when this function is complete.
     746             :  * \param min is the minimum value that can be encoded.
     747             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     748             :  */
     749           0 : void uint32ScaledTo2UnsignedLeBytes(uint32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     750             : {
     751             :     // scale the number
     752           0 :     uint32_t number = 0;
     753             : 
     754             :     // Make sure number fits in the range
     755           0 :     if(((int32_t)value) > min)
     756             :     {
     757           0 :         number = (uint32_t)((value - min)*scaler);
     758           0 :         if(number > 65535u)
     759           0 :             number = 65535u;
     760             :     }
     761             : 
     762           0 :     uint16ToLeBytes((uint16_t)number, bytes, index);
     763           0 : }
     764             : 
     765             : 
     766             : /*!
     767             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 2 signed
     768             :  * bytes in big endian order.
     769             :  * \param value is the number to encode.
     770             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     771             :  * \param index gives the location of the first byte in the byte stream, and
     772             :  *        will be incremented by 2 when this function is complete.
     773             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     774             :  */
     775           0 : void uint32ScaledTo2SignedBeBytes(uint32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     776             : {
     777             :     // scale the number
     778           0 :     int32_t number = (int32_t)(value*scaler);
     779             : 
     780             :     // Make sure number fits in the range
     781           0 :     if(number > 32767)
     782           0 :         number = 32767;
     783           0 :     else if(number < (-32767 - 1))
     784           0 :         number = (-32767 - 1);
     785             : 
     786           0 :     int16ToBeBytes((int16_t)number, bytes, index);
     787           0 : }
     788             : 
     789             : 
     790             : /*!
     791             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 2 signed
     792             :  * bytes in little endian order.
     793             :  * \param value is the number to encode.
     794             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     795             :  * \param index gives the location of the first byte in the byte stream, and
     796             :  *        will be incremented by 2 when this function is complete.
     797             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     798             :  */
     799           0 : void uint32ScaledTo2SignedLeBytes(uint32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     800             : {
     801             :     // scale the number
     802           0 :     int32_t number = (int32_t)(value*scaler);
     803             : 
     804             :     // Make sure number fits in the range
     805           0 :     if(number > 32767)
     806           0 :         number = 32767;
     807           0 :     else if(number < (-32767 - 1))
     808           0 :         number = (-32767 - 1);
     809             : 
     810           0 :     int16ToLeBytes((int16_t)number, bytes, index);
     811           0 : }
     812             : 
     813             : 
     814             : /*!
     815             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 1 unsigned
     816             :  * byte.
     817             :  * \param value is the number to encode.
     818             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     819             :  * \param index gives the location of the first byte in the byte stream, and
     820             :  *        will be incremented by 1 when this function is complete.
     821             :  * \param min is the minimum value that can be encoded.
     822             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     823             :  */
     824           0 : void uint32ScaledTo1UnsignedBytes(uint32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     825             : {
     826             :     // scale the number
     827           0 :     uint32_t number = 0;
     828             : 
     829             :     // Make sure number fits in the range
     830           0 :     if(((int32_t)value) > min)
     831             :     {
     832           0 :         number = (uint32_t)((value - min)*scaler);
     833           0 :         if(number > 255u)
     834           0 :             number = 255u;
     835             :     }
     836             : 
     837           0 :     uint8ToBytes((uint8_t)number, bytes, index);
     838           0 : }
     839             : 
     840             : 
     841             : /*!
     842             :  * Encode a uint32_t on a byte stream by integer scaling to fit in 1 signed
     843             :  * byte.
     844             :  * \param value is the number to encode.
     845             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     846             :  * \param index gives the location of the first byte in the byte stream, and
     847             :  *        will be incremented by 1 when this function is complete.
     848             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     849             :  */
     850           0 : void uint32ScaledTo1SignedBytes(uint32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     851             : {
     852             :     // scale the number
     853           0 :     int32_t number = (int32_t)(value*scaler);
     854             : 
     855             :     // Make sure number fits in the range
     856           0 :     if(number > 127)
     857           0 :         number = 127;
     858           0 :     else if(number < (-127 - 1))
     859           0 :         number = (-127 - 1);
     860             : 
     861           0 :     int8ToBytes((int8_t)number, bytes, index);
     862           0 : }
     863             : 
     864             : 
     865             : /*!
     866             :  * Scale a int32_t using integer scaling to the base integer type used for
     867             :  * bitfields.
     868             :  * \param value is the number to scale.
     869             :  * \param min is the minimum value that can be encoded.
     870             :  * \param scaler is multiplied by value to create the encoded integer.
     871             :  * \param bits is the number of bits in the bitfield, used to limit the returned value.
     872             :  * \return (value-min)*scaler.
     873             :  */
     874           0 : unsigned int int32ScaledToBitfield(int32_t value, int32_t min, uint32_t scaler, int bits)
     875             : {
     876             :     // The largest integer the bitfield can hold
     877           0 :     unsigned int max = (0x1u << bits) - 1;
     878             : 
     879             :     // Scale the number
     880           0 :     unsigned int number = (unsigned int)((value - min)*scaler);
     881             : 
     882             :     // Protect from underflow
     883           0 :     if(((int32_t)value) < min)
     884           0 :         return 0;
     885             : 
     886             :     // Protect from overflow
     887           0 :     if(number > max)
     888           0 :         return max;
     889             : 
     890           0 :     return number;
     891             : }
     892             : 
     893             : 
     894             : /*!
     895             :  * Encode a int32_t on a byte stream by integer scaling to fit in 4 unsigned
     896             :  * bytes in big endian order.
     897             :  * \param value is the number to encode.
     898             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     899             :  * \param index gives the location of the first byte in the byte stream, and
     900             :  *        will be incremented by 4 when this function is complete.
     901             :  * \param min is the minimum value that can be encoded.
     902             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     903             :  */
     904           0 : void int32ScaledTo4UnsignedBeBytes(int32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     905             : {
     906             :     // scale the number
     907           0 :     uint32_t number = 0;
     908             : 
     909             :     // Make sure number fits in the range
     910           0 :     if(((int32_t)value) > min)
     911             :     {
     912           0 :         number = (uint32_t)((value - min)*scaler);
     913             :     }
     914             : 
     915           0 :     uint32ToBeBytes(number, bytes, index);
     916           0 : }
     917             : 
     918             : 
     919             : /*!
     920             :  * Encode a int32_t on a byte stream by integer scaling to fit in 4 unsigned
     921             :  * bytes in little endian order.
     922             :  * \param value is the number to encode.
     923             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     924             :  * \param index gives the location of the first byte in the byte stream, and
     925             :  *        will be incremented by 4 when this function is complete.
     926             :  * \param min is the minimum value that can be encoded.
     927             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     928             :  */
     929           0 : void int32ScaledTo4UnsignedLeBytes(int32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     930             : {
     931             :     // scale the number
     932           0 :     uint32_t number = 0;
     933             : 
     934             :     // Make sure number fits in the range
     935           0 :     if(((int32_t)value) > min)
     936             :     {
     937           0 :         number = (uint32_t)((value - min)*scaler);
     938             :     }
     939             : 
     940           0 :     uint32ToLeBytes(number, bytes, index);
     941           0 : }
     942             : 
     943             : 
     944             : /*!
     945             :  * Encode a int32_t on a byte stream by integer scaling to fit in 4 signed
     946             :  * bytes in big endian order.
     947             :  * \param value is the number to encode.
     948             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     949             :  * \param index gives the location of the first byte in the byte stream, and
     950             :  *        will be incremented by 4 when this function is complete.
     951             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     952             :  */
     953           0 : void int32ScaledTo4SignedBeBytes(int32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     954             : {
     955             :     // scale the number
     956           0 :     int32_t number = (int32_t)(value*scaler);
     957             : 
     958           0 :     int32ToBeBytes(number, bytes, index);
     959           0 : }
     960             : 
     961             : 
     962             : /*!
     963             :  * Encode a int32_t on a byte stream by integer scaling to fit in 4 signed
     964             :  * bytes in little endian order.
     965             :  * \param value is the number to encode.
     966             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     967             :  * \param index gives the location of the first byte in the byte stream, and
     968             :  *        will be incremented by 4 when this function is complete.
     969             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
     970             :  */
     971           0 : void int32ScaledTo4SignedLeBytes(int32_t value, uint8_t* bytes, int* index, uint32_t scaler)
     972             : {
     973             :     // scale the number
     974           0 :     int32_t number = (int32_t)(value*scaler);
     975             : 
     976           0 :     int32ToLeBytes(number, bytes, index);
     977           0 : }
     978             : 
     979             : 
     980             : /*!
     981             :  * Encode a int32_t on a byte stream by integer scaling to fit in 3 unsigned
     982             :  * bytes in big endian order.
     983             :  * \param value is the number to encode.
     984             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     985             :  * \param index gives the location of the first byte in the byte stream, and
     986             :  *        will be incremented by 3 when this function is complete.
     987             :  * \param min is the minimum value that can be encoded.
     988             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
     989             :  */
     990           0 : void int32ScaledTo3UnsignedBeBytes(int32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
     991             : {
     992             :     // scale the number
     993           0 :     uint32_t number = 0;
     994             : 
     995             :     // Make sure number fits in the range
     996           0 :     if(((int32_t)value) > min)
     997             :     {
     998           0 :         number = (uint32_t)((value - min)*scaler);
     999           0 :         if(number > 16777215ul)
    1000           0 :             number = 16777215ul;
    1001             :     }
    1002             : 
    1003           0 :     uint24ToBeBytes((uint32_t)number, bytes, index);
    1004           0 : }
    1005             : 
    1006             : 
    1007             : /*!
    1008             :  * Encode a int32_t on a byte stream by integer scaling to fit in 3 unsigned
    1009             :  * bytes in little endian order.
    1010             :  * \param value is the number to encode.
    1011             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1012             :  * \param index gives the location of the first byte in the byte stream, and
    1013             :  *        will be incremented by 3 when this function is complete.
    1014             :  * \param min is the minimum value that can be encoded.
    1015             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1016             :  */
    1017           0 : void int32ScaledTo3UnsignedLeBytes(int32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
    1018             : {
    1019             :     // scale the number
    1020           0 :     uint32_t number = 0;
    1021             : 
    1022             :     // Make sure number fits in the range
    1023           0 :     if(((int32_t)value) > min)
    1024             :     {
    1025           0 :         number = (uint32_t)((value - min)*scaler);
    1026           0 :         if(number > 16777215ul)
    1027           0 :             number = 16777215ul;
    1028             :     }
    1029             : 
    1030           0 :     uint24ToLeBytes((uint32_t)number, bytes, index);
    1031           0 : }
    1032             : 
    1033             : 
    1034             : /*!
    1035             :  * Encode a int32_t on a byte stream by integer scaling to fit in 3 signed
    1036             :  * bytes in big endian order.
    1037             :  * \param value is the number to encode.
    1038             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1039             :  * \param index gives the location of the first byte in the byte stream, and
    1040             :  *        will be incremented by 3 when this function is complete.
    1041             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1042             :  */
    1043           0 : void int32ScaledTo3SignedBeBytes(int32_t value, uint8_t* bytes, int* index, uint32_t scaler)
    1044             : {
    1045             :     // scale the number
    1046           0 :     int32_t number = (int32_t)(value*scaler);
    1047             : 
    1048             :     // Make sure number fits in the range
    1049           0 :     if(number > 8388607l)
    1050           0 :         number = 8388607l;
    1051           0 :     else if(number < (-8388607l - 1))
    1052           0 :         number = (-8388607l - 1);
    1053             : 
    1054           0 :     int24ToBeBytes((int32_t)number, bytes, index);
    1055           0 : }
    1056             : 
    1057             : 
    1058             : /*!
    1059             :  * Encode a int32_t on a byte stream by integer scaling to fit in 3 signed
    1060             :  * bytes in little endian order.
    1061             :  * \param value is the number to encode.
    1062             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1063             :  * \param index gives the location of the first byte in the byte stream, and
    1064             :  *        will be incremented by 3 when this function is complete.
    1065             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1066             :  */
    1067           0 : void int32ScaledTo3SignedLeBytes(int32_t value, uint8_t* bytes, int* index, uint32_t scaler)
    1068             : {
    1069             :     // scale the number
    1070           0 :     int32_t number = (int32_t)(value*scaler);
    1071             : 
    1072             :     // Make sure number fits in the range
    1073           0 :     if(number > 8388607l)
    1074           0 :         number = 8388607l;
    1075           0 :     else if(number < (-8388607l - 1))
    1076           0 :         number = (-8388607l - 1);
    1077             : 
    1078           0 :     int24ToLeBytes((int32_t)number, bytes, index);
    1079           0 : }
    1080             : 
    1081             : 
    1082             : /*!
    1083             :  * Encode a int32_t on a byte stream by integer scaling to fit in 2 unsigned
    1084             :  * bytes in big endian order.
    1085             :  * \param value is the number to encode.
    1086             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1087             :  * \param index gives the location of the first byte in the byte stream, and
    1088             :  *        will be incremented by 2 when this function is complete.
    1089             :  * \param min is the minimum value that can be encoded.
    1090             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1091             :  */
    1092           0 : void int32ScaledTo2UnsignedBeBytes(int32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
    1093             : {
    1094             :     // scale the number
    1095           0 :     uint32_t number = 0;
    1096             : 
    1097             :     // Make sure number fits in the range
    1098           0 :     if(((int32_t)value) > min)
    1099             :     {
    1100           0 :         number = (uint32_t)((value - min)*scaler);
    1101           0 :         if(number > 65535u)
    1102           0 :             number = 65535u;
    1103             :     }
    1104             : 
    1105           0 :     uint16ToBeBytes((uint16_t)number, bytes, index);
    1106           0 : }
    1107             : 
    1108             : 
    1109             : /*!
    1110             :  * Encode a int32_t on a byte stream by integer scaling to fit in 2 unsigned
    1111             :  * bytes in little endian order.
    1112             :  * \param value is the number to encode.
    1113             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1114             :  * \param index gives the location of the first byte in the byte stream, and
    1115             :  *        will be incremented by 2 when this function is complete.
    1116             :  * \param min is the minimum value that can be encoded.
    1117             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1118             :  */
    1119           0 : void int32ScaledTo2UnsignedLeBytes(int32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
    1120             : {
    1121             :     // scale the number
    1122           0 :     uint32_t number = 0;
    1123             : 
    1124             :     // Make sure number fits in the range
    1125           0 :     if(((int32_t)value) > min)
    1126             :     {
    1127           0 :         number = (uint32_t)((value - min)*scaler);
    1128           0 :         if(number > 65535u)
    1129           0 :             number = 65535u;
    1130             :     }
    1131             : 
    1132           0 :     uint16ToLeBytes((uint16_t)number, bytes, index);
    1133           0 : }
    1134             : 
    1135             : 
    1136             : /*!
    1137             :  * Encode a int32_t on a byte stream by integer scaling to fit in 2 signed
    1138             :  * bytes in big endian order.
    1139             :  * \param value is the number to encode.
    1140             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1141             :  * \param index gives the location of the first byte in the byte stream, and
    1142             :  *        will be incremented by 2 when this function is complete.
    1143             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1144             :  */
    1145           0 : void int32ScaledTo2SignedBeBytes(int32_t value, uint8_t* bytes, int* index, uint32_t scaler)
    1146             : {
    1147             :     // scale the number
    1148           0 :     int32_t number = (int32_t)(value*scaler);
    1149             : 
    1150             :     // Make sure number fits in the range
    1151           0 :     if(number > 32767)
    1152           0 :         number = 32767;
    1153           0 :     else if(number < (-32767 - 1))
    1154           0 :         number = (-32767 - 1);
    1155             : 
    1156           0 :     int16ToBeBytes((int16_t)number, bytes, index);
    1157           0 : }
    1158             : 
    1159             : 
    1160             : /*!
    1161             :  * Encode a int32_t on a byte stream by integer scaling to fit in 2 signed
    1162             :  * bytes in little endian order.
    1163             :  * \param value is the number to encode.
    1164             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1165             :  * \param index gives the location of the first byte in the byte stream, and
    1166             :  *        will be incremented by 2 when this function is complete.
    1167             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1168             :  */
    1169           0 : void int32ScaledTo2SignedLeBytes(int32_t value, uint8_t* bytes, int* index, uint32_t scaler)
    1170             : {
    1171             :     // scale the number
    1172           0 :     int32_t number = (int32_t)(value*scaler);
    1173             : 
    1174             :     // Make sure number fits in the range
    1175           0 :     if(number > 32767)
    1176           0 :         number = 32767;
    1177           0 :     else if(number < (-32767 - 1))
    1178           0 :         number = (-32767 - 1);
    1179             : 
    1180           0 :     int16ToLeBytes((int16_t)number, bytes, index);
    1181           0 : }
    1182             : 
    1183             : 
    1184             : /*!
    1185             :  * Encode a int32_t on a byte stream by integer scaling to fit in 1 unsigned
    1186             :  * byte.
    1187             :  * \param value is the number to encode.
    1188             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1189             :  * \param index gives the location of the first byte in the byte stream, and
    1190             :  *        will be incremented by 1 when this function is complete.
    1191             :  * \param min is the minimum value that can be encoded.
    1192             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1193             :  */
    1194           0 : void int32ScaledTo1UnsignedBytes(int32_t value, uint8_t* bytes, int* index, int32_t min, uint32_t scaler)
    1195             : {
    1196             :     // scale the number
    1197           0 :     uint32_t number = 0;
    1198             : 
    1199             :     // Make sure number fits in the range
    1200           0 :     if(((int32_t)value) > min)
    1201             :     {
    1202           0 :         number = (uint32_t)((value - min)*scaler);
    1203           0 :         if(number > 255u)
    1204           0 :             number = 255u;
    1205             :     }
    1206             : 
    1207           0 :     uint8ToBytes((uint8_t)number, bytes, index);
    1208           0 : }
    1209             : 
    1210             : 
    1211             : /*!
    1212             :  * Encode a int32_t on a byte stream by integer scaling to fit in 1 signed
    1213             :  * byte.
    1214             :  * \param value is the number to encode.
    1215             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1216             :  * \param index gives the location of the first byte in the byte stream, and
    1217             :  *        will be incremented by 1 when this function is complete.
    1218             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1219             :  */
    1220           0 : void int32ScaledTo1SignedBytes(int32_t value, uint8_t* bytes, int* index, uint32_t scaler)
    1221             : {
    1222             :     // scale the number
    1223           0 :     int32_t number = (int32_t)(value*scaler);
    1224             : 
    1225             :     // Make sure number fits in the range
    1226           0 :     if(number > 127)
    1227           0 :         number = 127;
    1228           0 :     else if(number < (-127 - 1))
    1229           0 :         number = (-127 - 1);
    1230             : 
    1231           0 :     int8ToBytes((int8_t)number, bytes, index);
    1232           0 : }
    1233             : 
    1234             : 
    1235             : /*!
    1236             :  * Scale a uint16_t using integer scaling to the base integer type used for
    1237             :  * bitfields.
    1238             :  * \param value is the number to scale.
    1239             :  * \param min is the minimum value that can be encoded.
    1240             :  * \param scaler is multiplied by value to create the encoded integer.
    1241             :  * \param bits is the number of bits in the bitfield, used to limit the returned value.
    1242             :  * \return (value-min)*scaler.
    1243             :  */
    1244           0 : unsigned int uint16ScaledToBitfield(uint16_t value, int16_t min, uint16_t scaler, int bits)
    1245             : {
    1246             :     // The largest integer the bitfield can hold
    1247           0 :     unsigned int max = (0x1u << bits) - 1;
    1248             : 
    1249             :     // Scale the number
    1250           0 :     unsigned int number = (unsigned int)((value - min)*scaler);
    1251             : 
    1252             :     // Protect from underflow
    1253           0 :     if(((int32_t)value) < min)
    1254           0 :         return 0;
    1255             : 
    1256             :     // Protect from overflow
    1257           0 :     if(number > max)
    1258           0 :         return max;
    1259             : 
    1260           0 :     return number;
    1261             : }
    1262             : 
    1263             : 
    1264             : /*!
    1265             :  * Encode a uint16_t on a byte stream by integer scaling to fit in 2 unsigned
    1266             :  * bytes in big endian order.
    1267             :  * \param value is the number to encode.
    1268             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1269             :  * \param index gives the location of the first byte in the byte stream, and
    1270             :  *        will be incremented by 2 when this function is complete.
    1271             :  * \param min is the minimum value that can be encoded.
    1272             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1273             :  */
    1274           0 : void uint16ScaledTo2UnsignedBeBytes(uint16_t value, uint8_t* bytes, int* index, int16_t min, uint16_t scaler)
    1275             : {
    1276             :     // scale the number
    1277           0 :     uint16_t number = 0;
    1278             : 
    1279             :     // Make sure number fits in the range
    1280           0 :     if(((int16_t)value) > min)
    1281             :     {
    1282           0 :         number = (uint16_t)((value - min)*scaler);
    1283             :     }
    1284             : 
    1285           0 :     uint16ToBeBytes(number, bytes, index);
    1286           0 : }
    1287             : 
    1288             : 
    1289             : /*!
    1290             :  * Encode a uint16_t on a byte stream by integer scaling to fit in 2 unsigned
    1291             :  * bytes in little endian order.
    1292             :  * \param value is the number to encode.
    1293             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1294             :  * \param index gives the location of the first byte in the byte stream, and
    1295             :  *        will be incremented by 2 when this function is complete.
    1296             :  * \param min is the minimum value that can be encoded.
    1297             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1298             :  */
    1299           0 : void uint16ScaledTo2UnsignedLeBytes(uint16_t value, uint8_t* bytes, int* index, int16_t min, uint16_t scaler)
    1300             : {
    1301             :     // scale the number
    1302           0 :     uint16_t number = 0;
    1303             : 
    1304             :     // Make sure number fits in the range
    1305           0 :     if(((int16_t)value) > min)
    1306             :     {
    1307           0 :         number = (uint16_t)((value - min)*scaler);
    1308             :     }
    1309             : 
    1310           0 :     uint16ToLeBytes(number, bytes, index);
    1311           0 : }
    1312             : 
    1313             : 
    1314             : /*!
    1315             :  * Encode a uint16_t on a byte stream by integer scaling to fit in 2 signed
    1316             :  * bytes in big endian order.
    1317             :  * \param value is the number to encode.
    1318             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1319             :  * \param index gives the location of the first byte in the byte stream, and
    1320             :  *        will be incremented by 2 when this function is complete.
    1321             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1322             :  */
    1323           0 : void uint16ScaledTo2SignedBeBytes(uint16_t value, uint8_t* bytes, int* index, uint16_t scaler)
    1324             : {
    1325             :     // scale the number
    1326           0 :     int16_t number = (int16_t)(value*scaler);
    1327             : 
    1328           0 :     int16ToBeBytes(number, bytes, index);
    1329           0 : }
    1330             : 
    1331             : 
    1332             : /*!
    1333             :  * Encode a uint16_t on a byte stream by integer scaling to fit in 2 signed
    1334             :  * bytes in little endian order.
    1335             :  * \param value is the number to encode.
    1336             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1337             :  * \param index gives the location of the first byte in the byte stream, and
    1338             :  *        will be incremented by 2 when this function is complete.
    1339             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1340             :  */
    1341           0 : void uint16ScaledTo2SignedLeBytes(uint16_t value, uint8_t* bytes, int* index, uint16_t scaler)
    1342             : {
    1343             :     // scale the number
    1344           0 :     int16_t number = (int16_t)(value*scaler);
    1345             : 
    1346           0 :     int16ToLeBytes(number, bytes, index);
    1347           0 : }
    1348             : 
    1349             : 
    1350             : /*!
    1351             :  * Encode a uint16_t on a byte stream by integer scaling to fit in 1 unsigned
    1352             :  * byte.
    1353             :  * \param value is the number to encode.
    1354             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1355             :  * \param index gives the location of the first byte in the byte stream, and
    1356             :  *        will be incremented by 1 when this function is complete.
    1357             :  * \param min is the minimum value that can be encoded.
    1358             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1359             :  */
    1360           0 : void uint16ScaledTo1UnsignedBytes(uint16_t value, uint8_t* bytes, int* index, int16_t min, uint16_t scaler)
    1361             : {
    1362             :     // scale the number
    1363           0 :     uint16_t number = 0;
    1364             : 
    1365             :     // Make sure number fits in the range
    1366           0 :     if(((int16_t)value) > min)
    1367             :     {
    1368           0 :         number = (uint16_t)((value - min)*scaler);
    1369           0 :         if(number > 255u)
    1370           0 :             number = 255u;
    1371             :     }
    1372             : 
    1373           0 :     uint8ToBytes((uint8_t)number, bytes, index);
    1374           0 : }
    1375             : 
    1376             : 
    1377             : /*!
    1378             :  * Encode a uint16_t on a byte stream by integer scaling to fit in 1 signed
    1379             :  * byte.
    1380             :  * \param value is the number to encode.
    1381             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1382             :  * \param index gives the location of the first byte in the byte stream, and
    1383             :  *        will be incremented by 1 when this function is complete.
    1384             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1385             :  */
    1386           0 : void uint16ScaledTo1SignedBytes(uint16_t value, uint8_t* bytes, int* index, uint16_t scaler)
    1387             : {
    1388             :     // scale the number
    1389           0 :     int16_t number = (int16_t)(value*scaler);
    1390             : 
    1391             :     // Make sure number fits in the range
    1392           0 :     if(number > 127)
    1393           0 :         number = 127;
    1394           0 :     else if(number < (-127 - 1))
    1395           0 :         number = (-127 - 1);
    1396             : 
    1397           0 :     int8ToBytes((int8_t)number, bytes, index);
    1398           0 : }
    1399             : 
    1400             : 
    1401             : /*!
    1402             :  * Scale a int16_t using integer scaling to the base integer type used for
    1403             :  * bitfields.
    1404             :  * \param value is the number to scale.
    1405             :  * \param min is the minimum value that can be encoded.
    1406             :  * \param scaler is multiplied by value to create the encoded integer.
    1407             :  * \param bits is the number of bits in the bitfield, used to limit the returned value.
    1408             :  * \return (value-min)*scaler.
    1409             :  */
    1410           0 : unsigned int int16ScaledToBitfield(int16_t value, int16_t min, uint16_t scaler, int bits)
    1411             : {
    1412             :     // The largest integer the bitfield can hold
    1413           0 :     unsigned int max = (0x1u << bits) - 1;
    1414             : 
    1415             :     // Scale the number
    1416           0 :     unsigned int number = (unsigned int)((value - min)*scaler);
    1417             : 
    1418             :     // Protect from underflow
    1419           0 :     if(((int32_t)value) < min)
    1420           0 :         return 0;
    1421             : 
    1422             :     // Protect from overflow
    1423           0 :     if(number > max)
    1424           0 :         return max;
    1425             : 
    1426           0 :     return number;
    1427             : }
    1428             : 
    1429             : 
    1430             : /*!
    1431             :  * Encode a int16_t on a byte stream by integer scaling to fit in 2 unsigned
    1432             :  * bytes in big endian order.
    1433             :  * \param value is the number to encode.
    1434             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1435             :  * \param index gives the location of the first byte in the byte stream, and
    1436             :  *        will be incremented by 2 when this function is complete.
    1437             :  * \param min is the minimum value that can be encoded.
    1438             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1439             :  */
    1440           0 : void int16ScaledTo2UnsignedBeBytes(int16_t value, uint8_t* bytes, int* index, int16_t min, uint16_t scaler)
    1441             : {
    1442             :     // scale the number
    1443           0 :     uint16_t number = 0;
    1444             : 
    1445             :     // Make sure number fits in the range
    1446           0 :     if(((int16_t)value) > min)
    1447             :     {
    1448           0 :         number = (uint16_t)((value - min)*scaler);
    1449             :     }
    1450             : 
    1451           0 :     uint16ToBeBytes(number, bytes, index);
    1452           0 : }
    1453             : 
    1454             : 
    1455             : /*!
    1456             :  * Encode a int16_t on a byte stream by integer scaling to fit in 2 unsigned
    1457             :  * bytes in little endian order.
    1458             :  * \param value is the number to encode.
    1459             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1460             :  * \param index gives the location of the first byte in the byte stream, and
    1461             :  *        will be incremented by 2 when this function is complete.
    1462             :  * \param min is the minimum value that can be encoded.
    1463             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1464             :  */
    1465           0 : void int16ScaledTo2UnsignedLeBytes(int16_t value, uint8_t* bytes, int* index, int16_t min, uint16_t scaler)
    1466             : {
    1467             :     // scale the number
    1468           0 :     uint16_t number = 0;
    1469             : 
    1470             :     // Make sure number fits in the range
    1471           0 :     if(((int16_t)value) > min)
    1472             :     {
    1473           0 :         number = (uint16_t)((value - min)*scaler);
    1474             :     }
    1475             : 
    1476           0 :     uint16ToLeBytes(number, bytes, index);
    1477           0 : }
    1478             : 
    1479             : 
    1480             : /*!
    1481             :  * Encode a int16_t on a byte stream by integer scaling to fit in 2 signed
    1482             :  * bytes in big endian order.
    1483             :  * \param value is the number to encode.
    1484             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1485             :  * \param index gives the location of the first byte in the byte stream, and
    1486             :  *        will be incremented by 2 when this function is complete.
    1487             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1488             :  */
    1489           0 : void int16ScaledTo2SignedBeBytes(int16_t value, uint8_t* bytes, int* index, uint16_t scaler)
    1490             : {
    1491             :     // scale the number
    1492           0 :     int16_t number = (int16_t)(value*scaler);
    1493             : 
    1494           0 :     int16ToBeBytes(number, bytes, index);
    1495           0 : }
    1496             : 
    1497             : 
    1498             : /*!
    1499             :  * Encode a int16_t on a byte stream by integer scaling to fit in 2 signed
    1500             :  * bytes in little endian order.
    1501             :  * \param value is the number to encode.
    1502             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1503             :  * \param index gives the location of the first byte in the byte stream, and
    1504             :  *        will be incremented by 2 when this function is complete.
    1505             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1506             :  */
    1507           0 : void int16ScaledTo2SignedLeBytes(int16_t value, uint8_t* bytes, int* index, uint16_t scaler)
    1508             : {
    1509             :     // scale the number
    1510           0 :     int16_t number = (int16_t)(value*scaler);
    1511             : 
    1512           0 :     int16ToLeBytes(number, bytes, index);
    1513           0 : }
    1514             : 
    1515             : 
    1516             : /*!
    1517             :  * Encode a int16_t on a byte stream by integer scaling to fit in 1 unsigned
    1518             :  * byte.
    1519             :  * \param value is the number to encode.
    1520             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1521             :  * \param index gives the location of the first byte in the byte stream, and
    1522             :  *        will be incremented by 1 when this function is complete.
    1523             :  * \param min is the minimum value that can be encoded.
    1524             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1525             :  */
    1526           0 : void int16ScaledTo1UnsignedBytes(int16_t value, uint8_t* bytes, int* index, int16_t min, uint16_t scaler)
    1527             : {
    1528             :     // scale the number
    1529           0 :     uint16_t number = 0;
    1530             : 
    1531             :     // Make sure number fits in the range
    1532           0 :     if(((int16_t)value) > min)
    1533             :     {
    1534           0 :         number = (uint16_t)((value - min)*scaler);
    1535           0 :         if(number > 255u)
    1536           0 :             number = 255u;
    1537             :     }
    1538             : 
    1539           0 :     uint8ToBytes((uint8_t)number, bytes, index);
    1540           0 : }
    1541             : 
    1542             : 
    1543             : /*!
    1544             :  * Encode a int16_t on a byte stream by integer scaling to fit in 1 signed
    1545             :  * byte.
    1546             :  * \param value is the number to encode.
    1547             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1548             :  * \param index gives the location of the first byte in the byte stream, and
    1549             :  *        will be incremented by 1 when this function is complete.
    1550             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1551             :  */
    1552           0 : void int16ScaledTo1SignedBytes(int16_t value, uint8_t* bytes, int* index, uint16_t scaler)
    1553             : {
    1554             :     // scale the number
    1555           0 :     int16_t number = (int16_t)(value*scaler);
    1556             : 
    1557             :     // Make sure number fits in the range
    1558           0 :     if(number > 127)
    1559           0 :         number = 127;
    1560           0 :     else if(number < (-127 - 1))
    1561           0 :         number = (-127 - 1);
    1562             : 
    1563           0 :     int8ToBytes((int8_t)number, bytes, index);
    1564           0 : }
    1565             : 
    1566             : 
    1567             : /*!
    1568             :  * Scale a uint8_t using integer scaling to the base integer type used for
    1569             :  * bitfields.
    1570             :  * \param value is the number to scale.
    1571             :  * \param min is the minimum value that can be encoded.
    1572             :  * \param scaler is multiplied by value to create the encoded integer.
    1573             :  * \param bits is the number of bits in the bitfield, used to limit the returned value.
    1574             :  * \return (value-min)*scaler.
    1575             :  */
    1576           0 : unsigned int uint8ScaledToBitfield(uint8_t value, int8_t min, uint8_t scaler, int bits)
    1577             : {
    1578             :     // The largest integer the bitfield can hold
    1579           0 :     unsigned int max = (0x1u << bits) - 1;
    1580             : 
    1581             :     // Scale the number
    1582           0 :     unsigned int number = (unsigned int)((value - min)*scaler);
    1583             : 
    1584             :     // Protect from underflow
    1585           0 :     if(((int32_t)value) < min)
    1586           0 :         return 0;
    1587             : 
    1588             :     // Protect from overflow
    1589           0 :     if(number > max)
    1590           0 :         return max;
    1591             : 
    1592           0 :     return number;
    1593             : }
    1594             : 
    1595             : 
    1596             : /*!
    1597             :  * Encode a uint8_t on a byte stream by integer scaling to fit in 1 unsigned
    1598             :  * byte.
    1599             :  * \param value is the number to encode.
    1600             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1601             :  * \param index gives the location of the first byte in the byte stream, and
    1602             :  *        will be incremented by 1 when this function is complete.
    1603             :  * \param min is the minimum value that can be encoded.
    1604             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1605             :  */
    1606           0 : void uint8ScaledTo1UnsignedBytes(uint8_t value, uint8_t* bytes, int* index, int8_t min, uint8_t scaler)
    1607             : {
    1608             :     // scale the number
    1609           0 :     uint8_t number = 0;
    1610             : 
    1611             :     // Make sure number fits in the range
    1612           0 :     if(((int8_t)value) > min)
    1613             :     {
    1614           0 :         number = (uint8_t)((value - min)*scaler);
    1615             :     }
    1616             : 
    1617           0 :     uint8ToBytes(number, bytes, index);
    1618           0 : }
    1619             : 
    1620             : 
    1621             : /*!
    1622             :  * Encode a uint8_t on a byte stream by integer scaling to fit in 1 signed
    1623             :  * byte.
    1624             :  * \param value is the number to encode.
    1625             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1626             :  * \param index gives the location of the first byte in the byte stream, and
    1627             :  *        will be incremented by 1 when this function is complete.
    1628             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1629             :  */
    1630           0 : void uint8ScaledTo1SignedBytes(uint8_t value, uint8_t* bytes, int* index, uint8_t scaler)
    1631             : {
    1632             :     // scale the number
    1633           0 :     int8_t number = (int8_t)(value*scaler);
    1634             : 
    1635           0 :     int8ToBytes(number, bytes, index);
    1636           0 : }
    1637             : 
    1638             : 
    1639             : /*!
    1640             :  * Scale a int8_t using integer scaling to the base integer type used for
    1641             :  * bitfields.
    1642             :  * \param value is the number to scale.
    1643             :  * \param min is the minimum value that can be encoded.
    1644             :  * \param scaler is multiplied by value to create the encoded integer.
    1645             :  * \param bits is the number of bits in the bitfield, used to limit the returned value.
    1646             :  * \return (value-min)*scaler.
    1647             :  */
    1648           0 : unsigned int int8ScaledToBitfield(int8_t value, int8_t min, uint8_t scaler, int bits)
    1649             : {
    1650             :     // The largest integer the bitfield can hold
    1651           0 :     unsigned int max = (0x1u << bits) - 1;
    1652             : 
    1653             :     // Scale the number
    1654           0 :     unsigned int number = (unsigned int)((value - min)*scaler);
    1655             : 
    1656             :     // Protect from underflow
    1657           0 :     if(((int32_t)value) < min)
    1658           0 :         return 0;
    1659             : 
    1660             :     // Protect from overflow
    1661           0 :     if(number > max)
    1662           0 :         return max;
    1663             : 
    1664           0 :     return number;
    1665             : }
    1666             : 
    1667             : 
    1668             : /*!
    1669             :  * Encode a int8_t on a byte stream by integer scaling to fit in 1 unsigned
    1670             :  * byte.
    1671             :  * \param value is the number to encode.
    1672             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1673             :  * \param index gives the location of the first byte in the byte stream, and
    1674             :  *        will be incremented by 1 when this function is complete.
    1675             :  * \param min is the minimum value that can be encoded.
    1676             :  * \param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.
    1677             :  */
    1678           0 : void int8ScaledTo1UnsignedBytes(int8_t value, uint8_t* bytes, int* index, int8_t min, uint8_t scaler)
    1679             : {
    1680             :     // scale the number
    1681           0 :     uint8_t number = 0;
    1682             : 
    1683             :     // Make sure number fits in the range
    1684           0 :     if(((int8_t)value) > min)
    1685             :     {
    1686           0 :         number = (uint8_t)((value - min)*scaler);
    1687             :     }
    1688             : 
    1689           0 :     uint8ToBytes(number, bytes, index);
    1690           0 : }
    1691             : 
    1692             : 
    1693             : /*!
    1694             :  * Encode a int8_t on a byte stream by integer scaling to fit in 1 signed byte.
    1695             :  * \param value is the number to encode.
    1696             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
    1697             :  * \param index gives the location of the first byte in the byte stream, and
    1698             :  *        will be incremented by 1 when this function is complete.
    1699             :  * \param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.
    1700             :  */
    1701           0 : void int8ScaledTo1SignedBytes(int8_t value, uint8_t* bytes, int* index, uint8_t scaler)
    1702             : {
    1703             :     // scale the number
    1704           0 :     int8_t number = (int8_t)(value*scaler);
    1705             : 
    1706           0 :     int8ToBytes(number, bytes, index);
    1707           0 : }
    1708             : 
    1709             : 
    1710             : // end of scaledencode.c

Generated by: LCOV version 1.14