1

I'm trying to get individual characters from a string array to write to an LCD display on Arduino. Only I'm not really getting any valid data back.

LCD Write code:

void LCD::drawString(uint16_t x, uint16_t y, uint16_t color, uint16_t background, uint8_t str_nr)
{
    for(uint16_t i=0; getChar(str_nr, i) != '\0'; i++)
    {
        drawChar(x + i * FONT_WIDTH, y, color, background, getChar(str_nr, i) );
    }
}

StringPool.h:

#ifndef STRINGPOOL_H_
#define STRINGPOOL_H_

#include <avr/pgmspace.h>

#define STR_SCREEN_CAL 0
#define STR_HIGHSCORE 1
#define STR_SETTINGS 2
#define STR_BACK 3
#define STR_MENU_MAIN_TITLE 4
#define STR_MENU_MAIN_PLAY 5
#define STR_MENU_SCORE_TEXT 6
#define STR_MENU_SETTINGS_RESETSCORE 7
#define STR_MENU_SETTINGS_SCORERESET 8
#define STR_MENU_SETTINGS_CAL 9
#define STR_MENU_SETTINGS_BRIGHTNESS 10
#define STR_GAME_SCORE_NEW 11
#define STR_GAME_SCORE_NONE 12
#define STR_GAME_TOUCH_SCREEN 13
#define STR_GAME_TO_CONTINUE 14

uint8_t getChar(uint8_t str_nr, uint8_t offset);

#endif /* STRINGPOOL_H_ */

StringPool.cpp:

#include "StringPool.h"

const PROGMEM char screen_cal[] = "Screen Calibration";
const PROGMEM char highscore[] = "Highscore";
const PROGMEM char settings[] = "Settings";
const PROGMEM char back[] = "Back";
const PROGMEM char menu_main_title[] = "Floppy Bird";
const PROGMEM char menu_main_play[] = "Play";
const PROGMEM char menu_score_text[] = "The current highscore is";
const PROGMEM char menu_settings_resetscore[] = "Reset Highscore";
const PROGMEM char menu_settings_scorereset[] = "Highscore Reset!";
const PROGMEM char menu_settings_cal[] = "Calibrate Touch";
const PROGMEM char menu_settings_brightness[] = "Set LCD Brightness";
const PROGMEM char game_score_new[] = "New Highscore!";
const PROGMEM char game_score_none[] = "No Highscore";
const PROGMEM char game_touch_screen[] = "Touch the screen";
const PROGMEM char game_to_continue[] = "to continue";

const char * StringPool[] =
{
    screen_cal,
    highscore,
    settings,
    back,
    menu_main_title,
    menu_main_play,
    menu_score_text,
    menu_settings_resetscore,
    menu_settings_scorereset,
    menu_settings_cal,
    menu_settings_brightness,
    game_score_new,
    game_score_none,
    game_touch_screen,
};

uint8_t getChar(uint8_t str_nr, uint8_t offset)
{
    char * ptr = (char *)pgm_read_word( &StringPool[str_nr] );
    return pgm_read_byte(ptr + offset);
    // return pgm_read_byte( &(StringPool[str_nr]) + offset); // doesn't work either
}

The function getChar doesn't seem to be returning the proper data.

How would I go about reading a single character from a string array in progmem?

1 Answer 1

1

Try

char * ptr = (char *)pgm_read_word( StringPool[str_nr] );

instead, leaving out the &, as this would yield the address of the pointer to your string. The address of the pointer is in RAM. StringPool[str_nr] is already the address in flash memory you want.

Sign up to request clarification or add additional context in comments.

1 Comment

return pgm_read_byte( StringPool[str_nr] + offset ); turns out to be the answer, so even though you weren't exactly right, you pointed me in the right direction

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.