<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Rescaling positive value</title><link>https://community.element14.com/technologies/code_exchange/w/documents/3840/rescaling-positive-value</link><description /><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>Rescaling positive value</title><link>https://community.element14.com/technologies/code_exchange/w/documents/3840/rescaling-positive-value</link><pubDate>Wed, 06 Oct 2021 21:42:06 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:ba492e24-1c2e-4c41-b73b-60ce970b098e</guid><dc:creator>jpnbino</dc:creator><comments>https://community.element14.com/technologies/code_exchange/w/documents/3840/rescaling-positive-value#comments</comments><description>Current Revision posted to Documents by jpnbino on 10/6/2021 9:42:06 PM&lt;br /&gt;
&lt;p style="margin:0;"&gt;Edit: I know its boring to take someone else&amp;#39;s code and set up an environment. For that, I&amp;#39;ve made the code available and ready to run in &lt;a class="jive-link-external-small" href="https://www.jdoodle.com/embed/v0/FeZ" rel="nofollow ugc noopener" target="_blank" title="https://www.jdoodle.com/embed/v0/FeZ"&gt;https://www.jdoodle.com/embed/v0/FeZ&lt;/a&gt; &lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;Hey Folks, many times we need to translate values to different ranges so I wrote this piece of code. I hope you enjoy and give feedback and ideas to improve:&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
  @Description
    This function performs a conversion from one scale X to scale Y. The relation between those scales is considered linear. Then, based on the parameters a known value x in scale X is converted to a value y in scale Y.
  @Preconditions
    None
  @Param
    x  - is the value to be converted to scale y
     xa - is the minimum scale X value.
     xb - is the maximum scale X value.
     ya - is the minimum scale Y value.
     yb - is the maximum scale Y value.
  @Returns
    The value converted from scale X to scale Y.
  @Comment
    This function uses the round half up method as an integer rounding method
  @Example
    &amp;lt;code&amp;gt;
     #define MIN_X 20
     #define MAX_X 50

     #define MIN_Y 1000
     #define MAX_Y 2000

     uint16_t y;
     uint16_t x = 30;

     y = Rescale_Value( x, MIN_X, MAX_X, MIN_Y, MAX_Y );

    &amp;lt;/code&amp;gt;
*/

uint16_t Rescale_Value (uint16_t x , uint16_t xa, uint16_t xb, uint16_t ya, uint16_t yb )
{
/**
Algorithm:
1 - Find a linear equation;
2 - Calculate the equation considering rounding;

Explanation:
- A equation of the line correlates two different range of values. The equation
is given by the following formula,

y = ((yb - ya)*(x - xa))/(xb - xa)) + ya;

where,
(xa,ya) and (xb,yb) -&amp;gt; are known points in the line.

OBS: to complete understand solution search equation of the line theory.
Considerations:
y will be written as a/b + ya
where,
a = (yb - ya)*(x - xa)
b = (xb - xa)
*/
//test input condition
    if(x &amp;lt; xa)
    {
        x = xa;
    }
    else if ( x &amp;gt; xb)
    {
        x = xb;
    }
  
uint16_t y;

//multiplication of two 16bits gives a 32bits integer
uint32_t a;
uint16_t b;


a = (yb - ya)*(x - xa);
b = (xb - xa);

/**
In the matter of integers, calculation rounding must be considered.
Then, y is calculated using the round half up method
So, a/b is aproximated as (a/b + 1/2), but as calculation is performed by integers
1/2 = 0 (zero), then (a/b + 1/2) is rewritten as((a + b/2)/b).
*/
y = ((a + b/2)/b) + ya;

    return y ; 
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;
</description></item></channel></rss>