Big 2-dimensional char lookup table in progmem

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all!

I am facing a problem. From an accelerometer I have two uchar values, acceleration in x and y (the z is not important to my application). What I need is the accelerometers angle to earth, and I don't want to use math.h.

Therefore I have conclude to create a lookup table in Freemat (Matlab alternative), store it in progmem as a 2-dimensional lookup table. It will be 128x128 elements big (only one quadrant needed, if x and/or y is negative, I will just add and subtract stuff) and contain chars (char is good enough resolution for my application). How can I declare this massive matrix (will take up 50% of my flash)?

Is the following syntax correct (shown with smaller dimensions 3x4 and random content)?

#include

const prog_uchar lookupTable[3][4] PROGMEM =
{
{7, 2, 5, 25},
{2, 7, 3, 2},
{1, 1, 23, 2}
}

Would then the following return 25?

pgm_read_byte(&lookupTable[2][3]);

Any comment is highly appreciated.

Thanks /Johan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Johan, [2][3] would return 3, think [2] down, [3] over.

To get to 25 you would need to use [1][4].

HTH
Bruce.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
const prog_uchar lookupTable[3][4] PROGMEM =
{
   {7, 2, 5, 25},    // x[0][0..3]
   {2, 7, 3, 2},     // x[1][0..3]
   {1, 1, 23, 2}     // x[2][0..3]
}
...
pgm_read_byte(&lookupTable[2][3]);
...
// should return 2.
// x[0][3] would be 25

Totally untested. Why not just try it and see? I am sure that I could double-check in K&R .... but I am lazy.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use codepad.org when I'm lazy. Like this:

http://codepad.org/m4h0BgY3

The Dark Boxes are coming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remember, indices start at 0, not 1.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

svofski wrote:
I use codepad.org when I'm lazy. Like this:

http://codepad.org/m4h0BgY3

:D Way cool! Thanks for the pointer!

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bruceg wrote:
Hi Johan, [2][3] would return 3, think [2] down, [3] over.

To get to 25 you would need to use [1][4].

HTH
Bruce.

I have got to stop drinking at lunch :lol:

Indices do start at 0, so lets try again:

Hi Johan, [2][3] would return 2, think [2+1] down, [3+1] over. (remember indices start at 0!)

To get to 25 you would need to use [0][3].

Or use the really cool codepad link above!