Multidimensional Array In PROGMEM

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

Hi Folks,

I have a multidimensional array of chars stored in progmem. I want to read out of it and just wanted confirmation that I'll get what I want (can't really test it until a bunch of other code is overhauled so I want to get it right this time around).

static const char PROGMEM BOX_reference[7][4][4] = {

//bunch of data here...
};

void BOX_load_reference(unsigned char piece, unsigned char rotation)
{
	  BOX_piece[0] = pgm_read_byte((char *)&BOX_reference[piece][rotation][0]);
	  BOX_piece[1] = pgm_read_byte((char *)&BOX_reference[piece][rotation][1]);
	  BOX_piece[2] = pgm_read_byte((char *)&BOX_reference[piece][rotation][2]);
	  BOX_piece[3] = pgm_read_byte((char *)&BOX_reference[piece][rotation][3]);
}

Questions:

1. Am I passing the correct pointer to pgm_read_byte?
2. Do I need to typecast this as a (Char *)??

While I have you here, one more rather unrelated questions. Since I'm loading four sequential bytes, would there be an advantage to coding it in this fashion instead:??

void BOX_load_reference(unsigned char piece, unsigned char rotation)
{
  for (unsigned char i=0; i<4; i++)
  {
    BOX_piece[i] = pgm_read_byte((char *)&BOX_reference[piece][rotation][i]);
  }
}

Thank you!

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

Quote:
2. Do I need to typecast this as a (Char *)??
The pgm_read_byte macro already typecasts it to (uint16_t), so no.
Quote:
(can't really test it until a bunch of other code is overhauled so I want to get it right this time around).
But you can test it. Just make a simple program, run it in the simulator-
#include 
#include 

static const char PROGMEM BOX_reference[2][2][4] = {
    {{1,2,3,4},{5,6,7,8}},
    {{9,10,11,12},{13,14,15,16}}
};
uint8_t BOX_piece[4];

void BOX_load_reference(unsigned char piece, unsigned char rotation){
  for (unsigned char i=0; i<4; i++){
    BOX_piece[i] = pgm_read_byte(&BOX_reference[piece][rotation][i]);
  } 
} 

int main(){
    BOX_load_reference(0,0);
    BOX_load_reference(0,1);
    BOX_load_reference(1,0);
    BOX_load_reference(1,1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the help.

curtvm wrote:
But you can test it. Just make a simple program, run it in the simulator

I run linux so I don't use AVRstudio. I'm currently using eclipse and as far as I know simulavr doesn't support the m168.

Thanks though.

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

Quote:

I run linux so I don't use AVRstudio.

As I understand it things has gotten a bit better with this in the last year or so. Look in the latter part of this thread: https://www.avrfreaks.net/index.p...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

I think I would attempt to install VirtualBox just so I could use the AVR Studio simulator. I have played around a little with VirtualBox on a both Windows and Linux hosts, and it seems to work pretty good (I have not tried AVR Studio inside a Windows guest os, though).

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

I'm a BIG fan of Vmware server (completely free). I actually use it the other way round to run Linux machines within a Windows host but you can just a easily run a Windows virtual machine in a Linux host if you want. (you will need a set of Windows install disk to do this unless you want to join Captain Jack and crew of the good ship bittorrent)

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

Thanks for the support. I do not own a copy of Windows. I'm using Linux because:

1. I like it
2. I'm not stealing software by using

(don't worry, I'm not a militant linux user)

Since working with electronics is my hobby I don't mind not having the absolute best tools. I end up doing ok with what I have.

I do have an AVR dragon so I should be able to use the Debug wire with the mega168 as an alternative to simulation. I just have had some trouble getting everything to work together (avarice, gdb, eclipse). Some day.

ps- I'm acually looking for a mentor that can look at some of my code and tell me where the gaps in my knowledge might be (ie: you're doing it the hard way in this function, etc.). I have SVN setup, if anyone would like to check out the code and tear it to pieces I'd appreciate it! (please pm me).