array assignment?

Understanding the language, error messages, etc.

array assignment?

Postby yodasvideoarcade » Wed Jul 30, 2014 4:30 pm

If i have a byte-array in progmem (that holds the level), is it not possible to assign it to another array, like

Code: Select all
byte screen[896];
const byte PROGMEM playfield[]={1, 2, 3, 4, 5, 6 (etc. etc. 896 values) };

and in the loop:

screen=playfield;


??? It returns an "incompatible types in assignment" error. But both should be byte-arrays, right?
User avatar
yodasvideoarcade
 
Posts: 102
Joined: Sat Apr 19, 2014 10:48 am
Location: Frankfurt/Germany

Re: array assignment?

Postby Matrix828 » Wed Jul 30, 2014 4:40 pm

Arrays are groups of logically linked memory addresses. You can not assign one group of memory addresses to another group of memory addresses.

You can copy what's in one group of logically linked memory addresses (one array) to another in several ways. All depend on knowing how much memory is to be copied, to where.

So, you'd need to do this:
Code: Select all
for(int i=0; i<896; i++)
{
   screen[i] = playfield[i];
}
Matrix828
 
Posts: 43
Joined: Tue Jul 22, 2014 7:44 pm

Re: array assignment?

Postby rodot » Wed Jul 30, 2014 4:47 pm

With arduino you can't assign an array to another because there is no dynamic memory allocation for array. What you can do is to assign the pointer of the array instead of the array itself.

PROGMEM arrays are not regular array as they are store in the flash memory. You can't directly access their values using playfield[] or you'll get random values, you have to use the macro pgm_readbyte or pgm_readword. So their type and pointers are different from regular arrays. You'll find more explanations on the Arduino PROGMEM page.

Can you provide an SSCCE of what you want to do?

If you only want to transfer the values from a PROGMEM array to a RAM array here is how you could do:
Code: Select all
for(int i=0; i<896; i++)
{
   screen[i] = pgm_read_byte(playfield + i);
}


But 896 if far to large to fit in the RAM considering there 2KB of RAM of which .5KB are taken by the screen buffer.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: array assignment?

Postby yodasvideoarcade » Wed Jul 30, 2014 4:57 pm

What I want to do is have the maze in PROGMEM since it's fixed.
Then during runtime, copy it into a non-static array so I can read from it and manipulate it.
It contains 896 bytes (the maze is made of tiles of 3 x 3 pixels size, equals 896 tiles. Only a part of the maze will be visible on screen at a time, but I need the whole manipulated maze in memory to access it.
User avatar
yodasvideoarcade
 
Posts: 102
Joined: Sat Apr 19, 2014 10:48 am
Location: Frankfurt/Germany

Re: array assignment?

Postby rodot » Wed Jul 30, 2014 5:00 pm

When you say that you want to "manipulate" the maze, what do you mean? Read the value of each tile to display them on screen, or do you want to be able to modify the maze after compilation?
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: array assignment?

Postby yodasvideoarcade » Wed Jul 30, 2014 10:01 pm

As the player wanders through the maze, it changes (because he picks up something, or destroys a wall), so the copied version of the maze has to be modify-able by changing the values in the array. But after he finishes a level, the maze is restored to original.
User avatar
yodasvideoarcade
 
Posts: 102
Joined: Sat Apr 19, 2014 10:48 am
Location: Frankfurt/Germany

Re: array assignment?

Postby rodot » Wed Jul 30, 2014 10:08 pm

Then I think you should store the maze in a PROGMEM array, and the modifiable items in a regular array.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: array assignment?

Postby yodasvideoarcade » Wed Jul 30, 2014 10:51 pm

Yes, that's what I wanted to do, but you said 896 bytes would be too much, since there's only 1.5kb of RAM.

Is there another data-type I can use? The values are only 0-15, so I could use nibbles instead. Does that data-type exist? A nibble-array?
Or is a bit-array possible, so I can just store a "modified" bit for each position?
User avatar
yodasvideoarcade
 
Posts: 102
Joined: Sat Apr 19, 2014 10:48 am
Location: Frankfurt/Germany

Re: array assignment?

Postby Tiash » Wed Jul 30, 2014 11:10 pm

you can use like a "mask" of the same size of playfield, where you can trace where the labyrinth is being destroyed, so you can print the maze using logical operators (and or not)
so it should be easier to initialize the game and you must not change the maze directly :D
User avatar
Tiash
 
Posts: 18
Joined: Sat Jul 12, 2014 7:28 am
Location: Italy

Re: array assignment?

Postby rodot » Wed Jul 30, 2014 11:39 pm

Your maze is too large to be stored all at once it the RAM (2KB), so store it in the PROGMEM (32KB) like a bitmap (you can even use the bitmap encoder for that):
Code: Select all
const byte maze[896] PROGMEM = { .... large amount of data ....}

Then, you'll have just a few items which you can pick up. For example 3 apples. You don't have to store a huge array of 896 bytes just for 3 apples. Instead store only the apple coordinates :
Code: Select all
//declare the "item" structure:
typedef struct {
  byte x; //horizontal coordinate
  byte y; //vertical coordinate
  byte i; //ID of the item. for example 0 for apples, 1 for keys, 3 for traps, etc.
  boolean exists; //set to false once picked up
} Item;

//create an array to store all the items:
Item items[3];

//set the first item as an apple at the location 10;20 :
items[0].x = 10;
items[0].y = 20;
items[0].i = 0;
items[0].exists = true;
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 5 guests

cron