Forum: Mikrocontroller und Digitale Elektronik flasstinghelper und vsnprintf _va_args_


von A. D. (anmalo)


Lesenswert?

Hi
Ich habe ein Problem mit PROFMEM char* mit vsnprintf.
Die Ausgabe ist ok mit F(string) und normalen Parametern.
Sobal aber ein F() Parameter angegeben ist funktioniert das ganze nicht 
mehr.

Wie muss ich die arglist Konvertieren und wie sehe ich ob es ein F( 
string ist?

1
#define   fstr_Test1    F("Test %s")
2
#define   fstr_TestOn   F("on")
3
#define   str_TestOff   "off"
4
5
void out(const __FlashStringHelper* pStr, ...);
6
7
void setup() {
8
  Serial.begin(115200);
9
  out(fstr_Test1, fstr_TestOn);   // do not works
10
  out(fstr_Test1, str_TestOff);   // ok works
11
}
12
13
void loop() {
14
}
15
16
void out(const __FlashStringHelper* pStr, ...) {
17
  char* sTemp;
18
  sTemp = (char*)malloc(100);
19
  va_list argList;
20
  va_start(argList, pStr);
21
#ifdef __AVR__
22
  vsnprintf_P(sTemp, 100, (const char*)pStr, argList); // progmem for AVR
23
#else
24
  vsnprintf(sTemp, 100, (const char*)pStr, argList); // for the rest of the world
25
#endif  
26
  va_end(argList);
27
  Serial.println(sTemp);
28
  free(sTemp);
29
}

von foobar (Gast)


Lesenswert?

Du musst zwischen den Format-String und den Format-Parametern 
unterscheiden.  die _P-Funktionen benötigen einen PROGMEM-Format-String 
- die zu formatierenden Parameter bleiben gleich.  Um einen 
PROGMEM-Parameter zu formatieren, mußt du "%S" (groß-S) statt "%s" 
benutzen (AVR-Erweiterung).

von A. D. (anmalo)


Lesenswert?

foobar schrieb:
> Du musst zwischen den Format-String und den Format-Parametern
> unterscheiden.  die _P-Funktionen benötigen einen PROGMEM-Format-String
> - die zu formatierenden Parameter bleiben gleich.  Um einen
> PROGMEM-Parameter zu formatieren, mußt du "%S" (groß-S) statt "%s"
> benutzen (AVR-Erweiterung).

Herzlichen Dank!
Funktioniert.

Kann Du mir noch verraten wo das geschrieben steht.
Konnte das im Netz nirgends finden.

Grüsse.

von ArglisTiger (Gast)


Lesenswert?

A. D. schrieb:

> Wie muss ich die arglist Konvertieren und wie sehe ich ob es ein F(
> string ist?

Die Arglist muß in eine Täuschung konvertiert werden. Danach kann man 
sie in der Rubrik "Markt" anbieten.

:)

von foobar (Gast)


Lesenswert?

> Kann Du mir noch verraten wo das geschrieben steht.

In der Doku der avr-libc.  Entweder "man printf_P" oder:

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html
> - S Similar to the s format, except the pointer is expected to
> point to a program-memory (ROM) string instead of a RAM string.

von A. D. (anmalo)


Lesenswert?

foobar schrieb:
>> Kann Du mir noch verraten wo das geschrieben steht.
>
> In der Doku der avr-libc.  Entweder "man printf_P" oder:
>
> https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html
>> - S Similar to the s format, except the pointer is expected to
>> point to a program-memory (ROM) string instead of a RAM string.

Danke und Gruss

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.