rogerclarkmelbourne / Arduino_STM32

Arduino STM32. Hardware files to support STM32 boards, on Arduino IDE 1.8.x including LeafLabs Maple and other generic STM32F103 boards
Other
2.52k stars 1.26k forks source link

Fix for sprintf when compiling with nano.specs option set. #790

Closed danieltr3s closed 3 years ago

danieltr3s commented 4 years ago

Fix suggested by @HamidSaffari in issue https://github.com/rogerclarkmelbourne/Arduino_STM32/issues/620 .

kad commented 3 years ago

+1 for this fix

stevstrong commented 3 years ago

Was this tested by anyone?

kad commented 3 years ago

Was this tested by anyone?

I tried it with small hack in Marlin 2.0.7.2 firmware. builds fine, in places where it was broken, now seems to print floats ok.

https://github.com/kad/Marlin/commit/90878465bbc458ed516d325e9fa7629a056f3039#diff-f8926a81e11f2d9a94bb619e586b96c7d3cd4452a5e63d1b2c215efd0071a346

stevstrong commented 3 years ago

Is there a simple skecth wich shows the problem and the solution?

fpistm commented 3 years ago

If it can help, it is used since a while in STM32 core. Note that is is also used by String class: https://github.com/rogerclarkmelbourne/Arduino_STM32/blob/59227d1ecab7e931a390119c20655f1c4147a668/STM32F1/cores/maple/WString.cpp#L114

You can use this sketch to test:

void setup() {
  bool validate = true;
  char sout[256] = "";

  Serial.begin(9600);
  while (!Serial);

  Serial.println(dtostrf(1.23, 7, 2, sout));
  if (strcmp(sout, "   1.23") != 0) {
    validate = false;
  }
  Serial.println(dtostrf(1.899, 2, 2, sout));
  if (strcmp(sout, "1.90") != 0) {
    validate = false;
  }
  Serial.println(dtostrf(-123456.78910, 127, 6, sout));
  if (strcmp(sout,
             "                                                                                                                 -123456.789100"
            ) != 0) {
    validate = false;
  }
  Serial.println(dtostrf(-123456.78910, 128, 6, sout));
  if (strcmp(sout,
             "-123456.789100                                                                                                                  "
            ) != 0) {
    validate = false;
  }

  if (validate) {
    Serial.println("SUCCESS");
  } else {
    Serial.println("FAILED");
  }

}

void loop() {
}
stevstrong commented 3 years ago

I have tested it and it seems to work, so I will merge this.

stevstrong commented 3 years ago

this also solves #620.