Need advice with dtostrf alternative

So I’m taking some code that was written for the ESP32 chip that I am modifying to run on an Argon.

I am getting errors due to the “dtostrf” in the code so I need some help figuring out the best way to fix the error.

Instead of assuming I know the best way to fix this I figure I would ask people smarter than me for some help with a the most efficient alternative to eliminate the errors.

Is it sprintf() ?

If so can anybody throw me a bone about how I would make the change?

char vin[10], vout[10], vbat[10], iin[10], ibat[10], die_temp[10], bsr[10], thermistor_voltage[10], tcharge_timer[15], tcv_timer[15], temp[15];


LTC4162_read_register(&ltc4162, LTC4162_VBAT, &data);
dtostrf(LTC4162_VBAT_FORMAT_I2R(data) * cell_count, 5, 3, vbat);

LTC4162_read_register(&ltc4162, LTC4162_VIN, &data);
dtostrf(LTC4162_VIN_FORMAT_I2R(data), 5, 3, vin);

LTC4162_read_register(&ltc4162, LTC4162_VOUT, &data);
dtostrf(LTC4162_VOUT_FORMAT_I2R(data), 5, 3, vout);

LTC4162_read_register(&ltc4162, LTC4162_IBAT, &data);
dtostrf(LTC4162_IBAT_FORMAT_I2R(data), 5, 3, ibat);

LTC4162_read_register(&ltc4162, LTC4162_IIN, &data);
dtostrf(LTC4162_IIN_FORMAT_I2R(data), 5, 3, iin);

LTC4162_read_register(&ltc4162, LTC4162_DIE_TEMP, &data);
dtostrf(LTC4162_DIE_TEMP_FORMAT_I2R(data), 5, 3, die_temp);

LTC4162_read_register(&ltc4162, LTC4162_THERMISTOR_VOLTAGE, &data);
dtostrf(LTC4162_NTCS0402E3103FLT_I2R(data), 5, 3, thermistor_voltage);

Any help is appreciated :spark:

1 Like

Can you try this macro I just created?

#define dtostrf(DOUBLE, LENGTH, DECIMAL, BUFFER) snprintf(BUFFER, MIN(LENGTH + 1, sizeof(BUFFER)), "%." #DECIMAL "f", DOUBLE)

Example:

#include "Particle.h"

#define dtostrf(DOUBLE, LENGTH, DECIMAL, BUFFER) snprintf(BUFFER, MIN(LENGTH + 1, sizeof(BUFFER)), "%." #DECIMAL "f", DOUBLE)

void setup() {
    Serial.begin(115200);
}

void loop() {
    char buffer[10];
    double number = 123.6794;
    dtostrf(number, 7, 3, buffer);
    Serial.println(buffer);
    delay(1000);
}

I used the test case from:
https://www.hobbytronics.co.uk/arduino-float-vars

EDIT:

You might also find this useful:

2 Likes

Thanks, @nrobinson2000 :+1:

Let me make sure I’m doing what I should with the code you provided :grinning:

#1. I added the define to the top of my sketch

After I add that the sketch compiles without any errors.

But should your macro allow the code below to work without any additional changes or will I need to change anything with Buffer name so the data is saved to the correct variable?

Sorry, I just want to make sure I’m understanding your macro correctly :thinking:

char vin[10], vout[10], vbat[10], iin[10], ibat[10], die_temp[10], bsr[10], thermistor_voltage[10], tcharge_timer[15], tcv_timer[15], temp[15];


LTC4162_read_register(&ltc4162, LTC4162_VBAT, &data);
dtostrf(LTC4162_VBAT_FORMAT_I2R(data) * cell_count, 5, 3, vbat);

LTC4162_read_register(&ltc4162, LTC4162_VIN, &data);
dtostrf(LTC4162_VIN_FORMAT_I2R(data), 5, 3, vin);

LTC4162_read_register(&ltc4162, LTC4162_VOUT, &data);
dtostrf(LTC4162_VOUT_FORMAT_I2R(data), 5, 3, vout);

LTC4162_read_register(&ltc4162, LTC4162_IBAT, &data);
dtostrf(LTC4162_IBAT_FORMAT_I2R(data), 5, 3, ibat);

LTC4162_read_register(&ltc4162, LTC4162_IIN, &data);
dtostrf(LTC4162_IIN_FORMAT_I2R(data), 5, 3, iin);

LTC4162_read_register(&ltc4162, LTC4162_DIE_TEMP, &data);
dtostrf(LTC4162_DIE_TEMP_FORMAT_I2R(data), 5, 3, die_temp);

LTC4162_read_register(&ltc4162, LTC4162_THERMISTOR_VOLTAGE, &data);
dtostrf(LTC4162_NTCS0402E3103FLT_I2R(data), 5, 3, thermistor_voltage);
1 Like

The macro should allow the code to work without any additional changes, but the behavior/output might be slightly different than the ESP32 implementation in some cases. Let me know if you run into any issues. :+1:

Yep, worked like a charm!

Its always nice when a weekend worth of coding ends up working as expected when everything is hooked up :grinning:

Thanks for your help with this :spark:

1 Like