*char and gb.popup() ?

Understanding the language, error messages, etc.

*char and gb.popup() ?

Postby JWinslow23 » Sat Mar 07, 2015 1:08 am

I'm trying to make an array of strings and reference them with gb.popup() for use in a popup. Around the beginning of the code, I have:

Code: Select all
const char* newTileStrings[] PROGMEM = {"Good. You merged an 8 tile.", "Great. You merged a 16 tile.", "Awesome! You merged a 32 tile.",
"Sweet! You merged a 64 tile.", "Getting bigger... You merged a 128 tile.", "Even bigger... You merged a 256 tile.",
"Nearly there... You merged a 512 tile.", "Just one more... You merged a 1024 tile.", "You are a winner! You merged a 2048 tile.",
"Next step up! You merged a 4096 tile.", "You're getting good! You merged a 8192 tile.", "You gonna keep playing? You merged a 16384 tile.",
"This is getting serious... You merged a 32768 tile.", "You are a 2048 god! You merged a 65536 tile.", "INSANE!!! You merged a 131072 tile."
};


which are all of my messages.

And this is the function where I try to use gb.popup with one of those strings:

Code: Select all
void PopupMessage() {
  int MaxTile = 0;
  for( int x; x < 16; x++ ) {
    MaxTile = max(Board2048[x],MaxTile);
  }
  for( int x; x < 16; x++ ) {
    if (Board2048Old[x] == MaxTile) MaxTile = 0;
  }
  if (MaxTile >= 3) {
    gb.popup(newTileStrings[MaxTile-3],100);
  }
}


And I'm getting these error messages:
Code: Select all
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28:0,
                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI/SPI.h:17,
                 from Gamebuino2048.ino:1:
Gamebuino2048.ino:18:30: error: variable 'newTileStrings' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
Gamebuino2048.ino: In function 'void PopupMessage()':
Gamebuino2048.ino:626:43: error: no matching function for call to 'Gamebuino::popup(const char*&, int)'
Gamebuino2048.ino:626:43: note: candidate is:
In file included from Gamebuino2048.ino:2:0:
C:\Users\Owner\Documents\Arduino\libraries\Gamebuino/Gamebuino.h:64:10: note: void Gamebuino::popup(const __FlashStringHelper*, uint8_t)
     void popup(const __FlashStringHelper* text, uint8_t duration);
          ^
C:\Users\Owner\Documents\Arduino\libraries\Gamebuino/Gamebuino.h:64:10: note:   no known conversion for argument 1 from 'const char*' to 'const __FlashStringHelper*'
Error compiling.


I think there are two problems: I'm not putting the array of strings in PROGMEM correctly, and I'm not referencing it correctly in gb.popup(). What should I do?
User avatar
JWinslow23
 
Posts: 36
Joined: Sun Feb 22, 2015 8:16 pm

Re: *char and gb.popup() ?

Postby Myndale » Sat Mar 07, 2015 4:48 am

There are actually two kinds of arrays here, the string themselves (array of char) and the table that points to them. You're declaring the table in PROGMEM but it's still ambiguous to the compiler where you want the strings themselves. Try this instead:

Code: Select all
const char msg1[] PROGMEM = "Good. You merged an 8 tile.";
const char msg2[] PROGMEM = "Great. You merged a 16 tile.";
const char msg3[] PROGMEM = "Awesome! You merged a 32 tile.";
// etc

const char* const newTileStrings[] PROGMEM = { msg1, msg2, msg3 /* etc */ };

// then this:
gb.popup((const __FlashStringHelper*)newTileStrings[msgNum],100);
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: *char and gb.popup() ?

Postby JWinslow23 » Sat Mar 07, 2015 6:34 pm

Myndale wrote:There are actually two kinds of arrays here, the string themselves (array of char) and the table that points to them. You're declaring the table in PROGMEM but it's still ambiguous to the compiler where you want the strings themselves. Try this instead:

Code: Select all
const char msg1[] PROGMEM = "Good. You merged an 8 tile.";
const char msg2[] PROGMEM = "Great. You merged a 16 tile.";
const char msg3[] PROGMEM = "Awesome! You merged a 32 tile.";
// etc

const char* const newTileStrings[] PROGMEM = { msg1, msg2, msg3 /* etc */ };

// then this:
gb.popup((const __FlashStringHelper*)newTileStrings[msgNum],100);

That works fine if msgNum is simply a number, but for variables, it seems not to work. It just displays garbage. Any fix for that?
User avatar
JWinslow23
 
Posts: 36
Joined: Sun Feb 22, 2015 8:16 pm

Re: *char and gb.popup() ?

Postby Myndale » Sat Mar 07, 2015 9:18 pm

JWinslow23 wrote:That works fine if msgNum is simply a number, but for variables, it seems not to work. It just displays garbage. Any fix for that?


Sorry, my bad. I was tired when I wrote that and didn't realize the compiler would be optimizing the table-lookup out because of the constant.

As I indicated above the table is in PROGMEM, so you need to use pgm_read_word to read the address of the string from that table. Try this:

Code: Select all
gb.popup((const __FlashStringHelper*)pgm_read_word(&newTileStrings[msgNum]),100);


(Note the ampsersand in front of the newTileStrings identifier).
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: *char and gb.popup() ?

Postby JWinslow23 » Sat Mar 07, 2015 10:58 pm

Ah OK. Problem solved. Thank you ;)
User avatar
JWinslow23
 
Posts: 36
Joined: Sun Feb 22, 2015 8:16 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 28 guests

cron