Loading...
Pages: [1]   Go Down
Author Topic: Copying a float into a uint8_t array efficiently  (Read 404 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am putting together a uint8_t array payload for my xbees and I am reading data from a sensor in a uint8_t array.  This array is then copied into the payload with a for loop like this.

Code:
ds2438 hum1(&oneWire, TempSerial);                    //Call sensor
dtostrf(hum1.readHum(),5, 1, dtostrfbuffer);         //read humidity(float) into array buffer
humidity_string = dtostrfbuffer;                             // coppy into a string
for (  int j = 0; j < 6; j++)
        payload[9+j] = humidity_string.charAt(j);

The location is always the same and the length of the data is always the same.  Is there some way to avoid the for loop here or even the float the string conversion?
« Last Edit: May 01, 2012, 10:04:58 am by fnsnoop » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
humidity_string = dtostrfbuffer;                             // coppy into a string
This does not copy to a string. It (wastefully) copies to a String. Big difference, and completely unnecessary.

What are you doing with the string representation of the humidity on the other end?

It might be better to send the humidity value in binary, and reconstruct the float on the other end.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I 100% agree, there is no reason to send it as a string.  I would like to send it as binary which would make it much more compact.  Could you recommend some code for assembling this?  I could handle the receiving end after that.
Logged

Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Walking on Nails
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For binary copying you could use memcpy (http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html#ga5f60008005ea7557430149926cf583d7). Another idea for building your payload is a struct and union combination.
Logged

loved the 68000 assembler back then and now I have to deal with THIS 8 bit thingy

Pages: [1]   Go Up
Print
 
Jump to: