system
September 15, 2009, 5:43pm
1
Hi - feel kind of lame posting this but I'm a bit over my head and getting giddy from the lack of oxygen...
Trying to convert some code to work in Arduino and I am stumped on what this line does:
sscanf_P(scratchpad, PSTR("%f"), &fx);
scratchpad is an array of type char
fx is a float - so &fx is a reference to fx
Digging around I have found this:
int sscanf_P (const char * __buf, const char * __fmt, ...)
Variant of sscanf() using a fmt string in program memory.
And this:
#define PSTR ( s ) ((const PROGMEM char *)(s))
Used to declare a static pointer to a string in program space.
but I still can't figure out what is being done here.
-what is "%f"?
-what is fmt? format?
tx,
-Roy
system
September 15, 2009, 6:22pm
2
Hi,
this line of code scans the buffer scratchpad for float variable and stores it in the variable fx.
The first parameter is a pointer to a buffer (array) of char. The second parameter is the format of the data to look for in the buffer, the next parameters are pointers to the expected data. For example:
scratchpad = "value 12.3 Test";
char s[10];
float a;
int n = sscanf_P(scratchpad,PSTR("value %f %s"),&a,s);
n holds 2 (two variables scanned
f holds 12.3
s holds Test
Mike
system
September 16, 2009, 6:14pm
3
thanks - that's very helpful.
Beyond the scope of my question - but it's odd that this code snippet is scanning for floats-
sscanf_P(scratchpad, PSTR("%f"), &fx);
scratchpad is declared as an array of type char:
char scratchpad[64];
and is used to store chars read from serial input. I am assuming the code works - but what I get is '0.00' - since there are no floats in scratchpad.