<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/21270/best-way-to-send-float-over-ble</link><description>What is the Best way to send float over BLE? 
 Values range between -2.000volt and +2.000Volt (-2000mV / +2000mV)
Do I break it into two uint8_t and manipulate the +/- sign in separate 8-bit byte? 
 I took the ble_sdk_app_proximity example, and commented</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 17 Apr 2017 23:16:37 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/21270/best-way-to-send-float-over-ble" /><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83274?ContentTypeID=1</link><pubDate>Mon, 17 Apr 2017 23:16:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf654b74-7af2-4e0b-a8af-be857cfa6922</guid><dc:creator>endnode</dc:creator><description>&lt;p&gt;@awneil I was not precise. What I meant is that &amp;quot;float&amp;quot; is just interpretation during arithmetic. Unless you do such manipulations (and there is normally no reason to do it) there is no difference between float and int, just bunch of bytes. Also what I meant by &amp;quot;no float in digital system&amp;quot; is that normally there is no natural way of &amp;quot;creating&amp;quot; floats in digital system, there is simple A2D conversion which results in some sample with given bit-length, interpreting it as float doesn&amp;#39;t (normally) make sense unless you want to display it to humans or unless some very specific algorithm demands that instead of working on &amp;quot;raw&amp;quot; data (which are obvious integer). But I&amp;#39;m keen to learn if my experience is false and there are some legit and &amp;quot;natural&amp;quot; uses of floats during signal or other processing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83273?ContentTypeID=1</link><pubDate>Mon, 17 Apr 2017 22:54:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:13cbde1a-1c05-4d6b-b131-c76ba24c2aa3</guid><dc:creator>awneil</dc:creator><description>&lt;blockquote&gt;
&lt;p&gt;Values range between -2.000volt and +2.000Volt (-2000mV / +2000mV)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You have shown there yourself how to avoid the need for floating point at all: just express it in millivolts!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83272?ContentTypeID=1</link><pubDate>Mon, 17 Apr 2017 22:52:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:72a40642-325d-440c-87e9-baa41be273ba</guid><dc:creator>awneil</dc:creator><description>&lt;blockquote&gt;
&lt;p&gt;Normally there are no floats in digital &lt;em&gt;(sic?)&lt;/em&gt; system&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is clearly nonsense: many digital systems support - and use - floating point!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/IEEE_floating_point"&gt;en.wikipedia.org/.../IEEE_floating_point&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The ARM Cortex-M4F, on which the Nordic nRF52 is based, has a hardware floating-point unit built into the silicon.&lt;/p&gt;
&lt;p&gt;Perhaps you meant to say &amp;quot;&lt;em&gt;embedded&lt;/em&gt; system&amp;quot; - rather than &amp;quot;&lt;em&gt;digital&lt;/em&gt; system&amp;quot; ?&lt;/p&gt;
&lt;p&gt;Even then it is not true - especially in larger embedded systems.&lt;/p&gt;
&lt;p&gt;But it is quite often true of &lt;strong&gt;&lt;em&gt;small&lt;/em&gt;&lt;/strong&gt; embedded systems,  especially when using simple 8-bit microcontrollers;  possibly also when using a Cortex-Mo - such as the nRF51&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83279?ContentTypeID=1</link><pubDate>Mon, 17 Apr 2017 22:29:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e840fe4d-4053-41fb-8e6c-3f86a2056383</guid><dc:creator>ypearson</dc:creator><description>&lt;p&gt;I&amp;#39;m quite surprised that IAR doesn&amp;#39;t know what a union is.
Here is a link on IAR that uses union &lt;a href="https://www.iar.com/support/resources/articles/migration-techniques-for-different-endianness/"&gt;endianness&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is a IEEE 32b float. You just need to make sure the float get correctly re-assembled on the other side.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;float gsr = bytesToFloat(characteristic.getValue()[1],
                                 characteristic.getValue()[2],
                                 characteristic.getValue()[3],
                                 characteristic.getValue()[4]);

private float bytesToFloat(byte b0, byte b1, byte b2, byte b3) {
    int asInt = (b0 &amp;amp; 0xFF)
            |  ((b1 &amp;amp; 0xFF) &amp;lt;&amp;lt; 8)
            |  ((b2 &amp;amp; 0xFF) &amp;lt;&amp;lt; 16)
            |  ((b3 &amp;amp; 0xFF) &amp;lt;&amp;lt; 24);
    return  Float.intBitsToFloat(asInt);
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83278?ContentTypeID=1</link><pubDate>Mon, 17 Apr 2017 22:18:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:523301c9-9909-4bbf-a961-4b2d9a6c9cb0</guid><dc:creator>Renix</dc:creator><description>&lt;p&gt;Yvan, 2 questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The IAR compiler does not understand the union definition. Is there something missing. It is not clear how this gets implemented.&lt;/li&gt;
&lt;li&gt;It looks like the code simply breaks the 32-bits up into 4x bytes of 8-bits. It is not IEEE-754 32-bit Single-Precision Floating-Point Number conversion?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Any help appreciated&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83277?ContentTypeID=1</link><pubDate>Mon, 17 Apr 2017 20:29:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c38a24ae-7578-4be9-8228-5366d241df75</guid><dc:creator>ypearson</dc:creator><description>&lt;p&gt;I added the code, so you can just copy and paste it. I&amp;#39;m using SDK12.2.
Yvan Pearson&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83276?ContentTypeID=1</link><pubDate>Mon, 17 Apr 2017 20:18:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:33b07dca-7ac3-49dd-b6ea-eb52e0dbbd85</guid><dc:creator>Renix</dc:creator><description>&lt;p&gt;Thanks ypearson
I found the .h file, but no bds_float_encode function.&lt;/p&gt;
&lt;p&gt;Is this in an older SDK. I searched SDK 11, 12 and SDK13, but did not find a bds_float_encode function&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83275?ContentTypeID=1</link><pubDate>Fri, 14 Apr 2017 23:46:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4c221722-8955-4b85-a588-d6272e88cecf</guid><dc:creator>ypearson</dc:creator><description>&lt;p&gt;You can use this function that encodes a float (32 bits) into a uint8_t buffer&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;bds_float_encode(&amp;amp;float_data, encoded_buffer)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A float after all is just 4 bytes of data.&lt;/p&gt;
&lt;p&gt;The definition of the function is in this file&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./sdk/components/libraries/util/app_util_bds.h

static __INLINE uint8_t bds_float_encode(const float_t * p_value, uint8_t * p_encoded_data){
union { 
    float float_val;
    uint8_t char_val[4];
}encoder;

encoder.float_val = *p_value;
p_encoded_data[0] = encoder.char_val[0];
p_encoded_data[1] = encoder.char_val[1];
p_encoded_data[2] = encoder.char_val[2];
p_encoded_data[3] = encoder.char_val[3];
return(4);}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83271?ContentTypeID=1</link><pubDate>Mon, 10 Apr 2017 22:09:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05e08e39-83b9-40f2-a65c-be801aee2c77</guid><dc:creator>Renix</dc:creator><description>&lt;p&gt;OK. Thanks.
I will send 8-bit bytes, and track the sign.
So it seems one will send say 3 bytes to represent -1999 (0x7CF) [mV]
2 bytes for the value and 1-byte for the sign.
Then let the Client compute (if needed) say -1.999V
Rather than let the nRF52 calculate and track the decimal point, the sign, and the 2-byte value.
Makes sense.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to send float over BLE</title><link>https://devzone.nordicsemi.com/thread/83270?ContentTypeID=1</link><pubDate>Mon, 10 Apr 2017 20:47:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9832a79a-f847-4fd1-9202-92ae4c08188c</guid><dc:creator>endnode</dc:creator><description>&lt;p&gt;Hi. Normally there are no floats in digital system. Unless you really really really need to work with that because of sealed protocol specification just use native representation of that &amp;quot;type&amp;quot;, transport it and represent only on the final system where it needs to appear as float (GUI of mobile app, back end API/database...)&lt;/p&gt;
&lt;p&gt;So you get voltage as some digital sample of certain bit length (e.g. 14-bit sample by SAADC peripheral on nRF52), probably align it to next 8-bit byte (because BLE Link layer or any higher protocol is byte-oriented) and just send it to the other side.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>