Single precision floating point conversion

ID: LPCBARESDK-699

Status: Open

First reported: 6.0.14.1114

Fixed in: TBD

Description

Profile such as HTPT needs to represent floating point data. However, in the current SDK6, floating point precision library is not supported. Usually, a floating point library will consume system RAM space during the run time. To minimize the system RAM usage a minimal code can be added in the application project to use the single precision floating data.

Workaround

Example is shared based on the Health Thermometer Profile Thermometer HTPT profile example with Single precision floating point.

//@file user_htpt_application.c or user_proxr.c in the user space

// conversion to an IEEE format
static uint32_t convert_to_IEEE_format_from_float(float temperature)
{
 uint8_t exponent = 0xFF;
 uint32_t mantissa = (uint32_t)(temperature*10);
 return ( ((uint32_t)exponent) << 24) | mantissa;
}

void user_send_temperature_ntf(void)
{
 uint8_t conidx = 0;
 uint8_t exponent = 0xFF;
 // used for testing purposes - MCP9808_get_temperature();
 float temperature = 10.1;


 //Allocate a new message
 struct htpt_env_tag* htpt_env = PRF_ENV_GET(HTPT, htpt);
 struct htpt_temp_send_req *req = KE_MSG_ALLOC(HTPT_TEMP_SEND_REQ,
                                        prf_src_task_get(&htpt_env->prf_env, conidx),
                                        TASK_APP,
                                        htpt_temp_send_req);

 req->stable_meas = false;

 //The central device will accept and display this value as a floating point number
 //when assigned to a float variable in the central device
 req->temp_meas.temp = convert_to_IEEE_format_from_float(temperature);

 ke_msg_send(req);

 //Set a timer for NOTIFICATION_DELAY ms
 timer_temperature_ntf = app_easy_timer(NOTIFICATION_DELAY/10, user_send_temperature_ntf);
}

HTPT needs to send temparature data as a notification to the peer device. The application will call user_send_temperature_ntf API. The float temperature = 10.1; is used as a constent for demonstration purposes. This can be easily replaced by the actual temparature sensor data read out operation API. The API convert_to_IEEE_format_from_float holds the real implementation of the single precision floating point data conversion.