This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

32-bit float transmission

I have 32-bit float numbers, sent to the BLE from SPI. I can manipulate this into x4 bytes, or actually use sprintf and get a real 6-byte representation of the number.

For debugging: So, to make debugging possible / easy, (I do not have a client side app yet), it is easier to send a text string, "-" "1" "." "999"

Longterm: It takes less time to transmit the x4 byte representation of the 32-bit float.

Practically: ADC measurements were used /converted in floating-point calculations. uController 1 has x3 floats that need to get sent over SPI to nRF52. I can convert to 4-byte int. I send to nRF52 over SPI. on nRF52 I have to convert to unit32_t to send with the example BLE Characteristics, a beginner's tutorial

Then on client, another conversion to real value is needed.

Questions: How can I change the example to send 4-bytes of ints (Then I can do 32-bit float encode and send as x4 bytes).

How can I change the example to send 6 bytes of char (Then I can use sprintf, send string, "-" "1" "." "999")

I changed the unit32_t to a int in the our_termperature_characteristic_update

But the values do not look correct. (I'm trying to figure out if there is a needed delay between characteristic updates). here

Any help appreciated

  • Hi. You can use "union" like this:

    union Data
    {
           float ff;
           int32_t ii;
           uint32_t uii;
           uint8_t cc[4];
    };
    
    //in one place
    ...
    Data your_data;
    your_data.ff = -1.999;
    int32_t i_value = your_data.ii;
    uint32_t ui_value = your_data.uii;
    ...
    
    
    //in second place
    ...
    Data your_data;
    your_data.ii = -1073750213; //this is a "-1.999" in signed integer representation
    //or
    //your_data.uii = 3221217083; //this is a "-1.999" in unsigned integer representation
    float f_value = your_data.ff;
    ...
    

    Best, VL.

Related