Go Down

Topic: array of pointers (pointer memory reqs?) (Read 1 time)previous topic - next topic

roypardi

Dec 17, 2010, 01:54 pmLast Edit: Dec 17, 2010, 02:04 pm by roypardi Reason: 1
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: [Select]

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;
}

PaulS

#1
Dec 17, 2010, 02:50 pm
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.

robtillaart

#2
Dec 17, 2010, 03:05 pmLast Edit: Dec 17, 2010, 03:06 pm by robtillaart Reason: 1
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: [Select]
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;
Rob Tillaart

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

roypardi

#3
Dec 17, 2010, 03:33 pmLast Edit: Dec 17, 2010, 03:45 pm by roypardi Reason: 1
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.

--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: [Select]

// pseudo code

void loop() {
int**  currentWord;

currentWord = cat;

draw(currentWord);

currentWord = elephant;

draw(currentWord);

//etc.
}

Go Up

Please enter a valid email to subscribe