Pages: [1]   Go Down
Author Topic: array of pointers (pointer memory reqs?)  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Got Karma?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Starting on a plotter type project and trying to assess if I can make it stand alone rather than tethered (to a PC).

The situation is I want to store the plotting instructions for a very simplified stick font. The req. data is along the lines of: 41; 18; 1,0 9,21 17,0; 4,7 14,7 - that's for the letter 'A'. I haven't figured out a data format yet - likely a simple array of ints.

What I am wondering is if once I have defined my char plot arrays, can I then define 'words' through pointers to those arrays? My goal/hope is that once the byte heavy font plot data is defined (and stored in PROGMEM), I will still have enough memory left to define lots of "words" (arrays of pointers to the plot data).

Does this make any sense? ;-)

Simple pseudo code (plot data is fake) but this compiles:

Code:
int A[] = {41,18,1,0, 9,21, 17,0, 4,7, 14,7};
int B[] = {23, 55, 44, 21, 77, 88, 41,18,1,0, 9,21, 17,0, 4,7, 14,7};
int C[] = {41,18,1,0, 9,21, 17,0, 4,7,33, 22, 14,7};
int T[] = {41,18,1,0, 9,21, 4,7, 14,7};

int* cat[3] = {C, A, T};
int* bat[3] = {B, A, T};


void setup() {
 
}

void loop() {
 int**  currentWord = cat;
}
« Last Edit: December 17, 2010, 08:04:12 am by roypardi » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 614
Posts: 49385
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks reasonable to me. The hardest part will be to correctly dereference currentword, given that you have no idea the size of the arrays involved, from the pointer. If there were some obvious end-of-data marker in each array, you could detect that.

For instance, add a NULL to the end of cat. If the values in A[] will all be positive, a negative value could indicate the end  of data. Alternatively, the first value in A[] could be the number of values to follow.
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13738
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course this makes sense as a plotter works somehow similar although it often has bigger memory than an Arduino. But OK.

I should add a length to the array's. e.g the length of the array T is 5
int T[] = {[glow]5[/glow], 41,18,1,0, 9,21, 4,7, 14,7};

To spell words I would use normal strings and write a small interpreter. Something like below

Code:
void setup()
{
  Serial.begin(19200);
}

void loop()
{
  String s = "cat";
  interpret(s);
  while(1);
}

int A[] = {41,18,1,0, 9,21, 17,0, 4,7, 14,7};
int B[] = {23, 55, 44, 21, 77, 88, 41,18,1,0, 9,21, 17,0, 4,7, 14,7};
int C[] = {41,18,1,0, 9,21, 17,0, 4,7,33, 22, 14,7};
int T[] = {41,18,1,0, 9,21, 4,7, 14,7};

void interpret(String s)
{
  for (byte i=0; i< s.length(); i++)
  {
    switch (s[i])
    {
      case 'a' : Draw(A); break;
      case 'b' : Draw(B); break;
      case 'c' : Draw(C); break;
      case 't' : Draw(T); break;
      default: break;
    }
  }
}

void Draw(int * p)
{
  int len = p[0];
  for (byte i=0; i< len; i++)
  {
    // draw the vectors.
  }
}

As you have 26 letters you use quite some RAM for the vectors. This can be circumvented by dividing the letters in elementary vectors. That means the code above would change like this (pseudocode)

case 'a' : Draw(/); Draw(-); Draw(\); break;
« Last Edit: December 17, 2010, 09:06:30 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Got Karma?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the replies! I agree, it is completely plotter-esque in terms of reinventing that wheel - except for the memory limits on the Arduino. I've been digging through what I can find on the web @ plotter char sets, etc.

Since I'd like this to be stand alone, I'm trying to get a rough sense of what the limits are (in terms of memory and my coding ability... ;-) I have a mega I can use so I have some space.

The reason for the array of 'word' pointers is so that I can define a sort of vocabulary and construct sentences through various code-based rules. I figured staying away from the String class would be best in terms of memory.

Coming up with 'elementary' vectors is an interesting idea.

A follow up question:

--dereferencing; I get the idea of it but have never done it. What should I look up @ that (C method, etc.)? I can easily add a length indicator to each array in my data structure.

EDIT: just looked at this page: http://www.arduino.cc/playground/Code/Pointer - and maybe I don't understand dereferencing.

Will I run into problems with 'word' arrays of different lengths?:

Code:
// pseudo code

void loop() {
 int**  currentWord;


 currentWord = cat;

 draw(currentWord);

 currentWord = elephant;

 draw(currentWord);

//etc.
}



« Last Edit: December 17, 2010, 09:45:56 am by roypardi » Logged

Pages: [1]   Go Up
Jump to: