|
|
View previous topic :: View next topic |
Author |
Message |
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
I need some help on this Datalogger function... |
Posted: Sun Dec 08, 2013 3:24 am |
|
|
I need some help on this Datalogger function...
How to do this on a smart way? No problem to do it with a lot of "for loop". I think maybe use pointer or mem-move?
I look forward to some clever suggestion:-)
Code: | signed int8 datalog[24]; |
Code: | /*
The datalog is a array of signes int8, temperature logging, the newest element will be most left, and the oldest will be most right.
Initial array will be filled with 'x', or just unused element will.
Every new element will be pushed in at the the most left, all element move 1 to the right.
The most right element will be lost, this is as design!
When printing it will look like: "Temp:-3,-3,-2,-1,-0,4,5,8,0,-2,x,x,x,x,x,x,x,x,x,x,x,x,x,x" Maximum 24 element!
datalog=Newest......Oldest
*/
|
This is the function i need some clever help for.
Code: |
//Insert the new data most left and push all the other element to the right. Most right will be lost!
void UpdateDataLogger(signed int8 data){}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Sun Dec 08, 2013 5:32 am |
|
|
The most efficient way, is not to move the data at all.
Instead implement a circular buffer. Just update addresses, and leave the data stationary.
If you do instead want to physically move the data, a pointer is no more efficient than a simple array index.
Mem_move will move the data, but you might as well just move the array using for..next. Mem_move will do the move by copying the entire array into a temporary buffer, then copying this back. Each move will be more efficient (it will use a successive table copy), but doing the double move will probably be less efficient. It does it this way, to handle the possibility that the array could be moving in either direction, while you 'know' which way the data is going, so can simply start copying at the end.
Best Wishes |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Sun Dec 08, 2013 10:49 am |
|
|
Ttelmah wrote: | The most efficient way, is not to move the data at all.
Instead implement a circular buffer. Just update addresses, and leave the data stationary.
|
Absolutely.
Code: | #define NUM_TEMP_ELEMENTS 24
signed int8 datalog[NUM_TEMP_ELEMENTS];
unsigned int8 datalog_wr_index = 0, datalog_rd_index, i;
// insert new temperature into your array
datalog[datalog_wr_index++] = new_temperature;
// you have to guard against the write index overflowing your array
if (datalog_wr_index >= NUM_TEMP_ELEMENTS) {
datalog_wr_index = 0;
}
// it's actually easier to do this by choosing an array size that's a power of
// 2 and then just masking your index every time you use it to step into
// the array
// to read out the data, newest first:
datalog_rd_index = datalog_wr_index - 1;
if (datalog_rd_index > NUM_TEMP_ELEMENTS) {
datalog_rd_index = NUM_TEMP_ELEMENTS - 1; // have to ensure you
// don't run off the end of the array
}
for (i = 0; i < NUM_TEMP_ELEMENTS; i++) {
printf(datalog[datalog_rd_index--]);
if (datalog_rd_index > NUM_TEMP_ELEMENTS) {
datalog_rd_index = NUM_TEMP_ELEMENTS - 1;
}
} |
|
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Sun Dec 08, 2013 10:56 am |
|
|
Thanks for hints, code, and support, nice |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|