Pages: [1]   Go Down
Author Topic: Pointers to Flash objects?  (Read 395 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm stuck on a problem using Mikal Hart's Flash library.

I have a bunch of flash tables, storing data associated with multiple possible objects:
Code:
FLASH_TABLE(byte, dataforobject1, <lots of data>);
FLASH_TABLE(byte, dataforobject2, <lots of data>);

etc.

what I would like to do is have a class which contains a pointer to a flash table with the appropriate data:
Code:
class foo {
public:
 <pointer to a flash table>
};

I was hoping to dynamically create an instance of foo, then set the pointer to the appropriate flash table.  Then be able to access the data in fooinstance's flash table through that pointer.

Is that possible?  I can't include a flash table in the class itself of course, but I can't figure out how to describe a pointer that will let me get at the flash table's data.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 199
Posts: 12767
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Give this a try...

Code:
#include <Flash.h>

FLASH_TABLE(byte, dataforobject1, 1, 2, 3, 4 );
FLASH_TABLE(byte, dataforobject2, 5, 6, 7, 8, 9, 10 );

class foo
{
public:

  void setData( _FLASH_TABLE<byte> * data )
  {
    pdfo = data;
  }

  byte first( void )
  {
    return( (*pdfo)[0][0] );
  }
 
private:

  _FLASH_TABLE<byte> * pdfo;
};

foo bar;

void setup( void )
{
  Serial.begin( 115200 );

  bar.setData( &dataforobject1 );
  Serial.println( bar.first() );

  bar.setData( &dataforobject2 );
  Serial.println( bar.first() );
}

void loop( void )
{
}
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks Coding Badly - that worked!  What I was really missing was the parentheses around the pointer name.

[Though for anyone else looking here, I think the code as written is really for a one-dimensional array, not a table.  And you would need DEC formatters in the prints to get the results you expect.]
Logged

Pages: [1]   Go Up
Jump to: