CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

How to access string in struct?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
_olaf_



Joined: 24 Feb 2005
Posts: 32

View user's profile Send private message

How to access string in struct?
PostPosted: Sat Oct 01, 2016 2:55 pm     Reply with quote

Hi,

I want to set up a LCD menu for my new project. I read a lot of examples but at the end I couldn't implement something that worked.
I found an example which looked like it would fulfill my needs, but I only get nonsense on my LCD. The LCD output itself works fine.

I found following Code

Code:


#define Menu_Entries     10
enum ids{_BEGIN,_,_MAIN1,_SUBMENU_1A,_SUBMENU_1B,_MAIN2,_SUBMENU_2A,_SUBMENU_2B,_MAIN3,_SUBMENU_3A,_SUBMENU_3B};

const struct LCD_Menu
{
     int8 MenuID;
     int8 SubMenu;
     char MenuName[16];//17
};

const struct LCD_Menu Menu[Menu_Entries] =
{
   {_,_BEGIN,""},
   {_MAIN1,_BEGIN,"Mainmenu 1"},
   {_SUBMENU_1A,_MAIN1,"Sub_Menu 1a"},
   {_SUBMENU_1B,_MAIN1,"Sub_Menu 1b"},
   {_MAIN2,_BEGIN,"Mainmenu 2"},
   {_SUBMENU_2A,_MAIN2,"Sub_Menu 2a"},
   {_SUBMENU_2B,_MAIN2,"Sub_Menu 2b"},
   {_MAIN3,_BEGIN,"Mainmenu 2"},
   {_SUBMENU_3A,_MAIN3,"Sub_Menu 3a"},
   {_SUBMENU_3B,_MAIN3,"Sub_Menu 3b"}
};struct LCD_Menu MenuPointer[3];

I don't know if I have understood the code, but I don't think so.
In the following line
Code:
const struct LCD_Menu ...
the types of the entries in the following
Code:
const struct LCD_Menu Menu[Menu_Entries] =
{

are declared. Is this right?

In my main I wanted to write the MenuName to the LCD.

Code:
printf(LCD_char,"%s",MenuPointer[1].MenuName);

But all I get on the LCD are cryptic characters. The printf(...) does work. I have some other parts in the program where I can write to the LCD without any problem.

How can I write the string from the const struct LCD_Menu to my LCD? I doesn't make a difference if I use

printf(LCD_string,"%s",...) or printf(LCD_char,"%c",...)

BR

Edit: Changed subject
PCM programmer



Joined: 06 Sep 2003
Posts: 20421

View user's profile Send private message

PostPosted: Sat Oct 01, 2016 4:35 pm     Reply with quote

In the following line, you are creating empty RAM-based structures.
They have to be initialized with data.
Quote:
struct LCD_Menu MenuPointer[3];


To initialize the RAM-based structures in the MenuPointer array, you
can copy the const data into it. Add the code shown below:
Quote:

void main()
{
int8 i;

for(i = 0; i < 3; i++)
{
MenuPointer[i] = Menu[i];
}


printf(LCDchar, "%s", MenuPointer[1].MenuName);

while(TRUE);
}


If it still doesn't work, it could be a compiler version issue as I point out
near the end of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=55193
Always post your compiler version.
_olaf_



Joined: 24 Feb 2005
Posts: 32

View user's profile Send private message

PostPosted: Sun Oct 02, 2016 1:54 am     Reply with quote

Sorry I forgot. Compiler Version is 5.064

Thank you very much PCM_Programmer.
So I have to copy the data I want to use into the MenuPointer.
e.g. if I want to get the 6 values of the struct array I have to copy
Code:

MenuPointer[5] = Menu[5];


I tried this from your link

Code:

for(i = 0; i < ALL_MENU; i++)
   {
    MenuPointer[k] =  MenuTest[i];
    printf("> %s \r", MenuPointer[0].MenuName); 
   }


This doesn't work propper, but I think it is because I copied the data into the MenuPointer in the beginning of main and then used it later in the program.
Code:

void main()
{
//do some initializaton
...
...
for(i = 0; i < ALL_MENU; i++)
   {
    MenuPointer[k] =  MenuTest[i];
   }

...
...
//do somthing else
...
...
    printf("> %s \r", MenuPointer[0].MenuName); 



Copying the data into the MenuPointer just in front of the printf(...) does work.

Best Regards
PCM programmer



Joined: 06 Sep 2003
Posts: 20421

View user's profile Send private message

PostPosted: Sun Oct 02, 2016 2:14 am     Reply with quote

Post a complete, compilable test program. Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=55193&start=10
It can be pasted into MPLAB and it will compile with no errors.
Ttelmah



Joined: 11 Mar 2010
Posts: 13766

View user's profile Send private message

PostPosted: Sun Oct 02, 2016 7:27 am     Reply with quote

As a comment, think about it. If it works putting the data in 'close' to where you use it, but not when you fill it earlier, it implies _something_ you are doing between these points, is changing the data......
_olaf_



Joined: 24 Feb 2005
Posts: 32

View user's profile Send private message

PostPosted: Mon Oct 03, 2016 6:21 am     Reply with quote

Now it works. I had a little code snippet in front of the code.
Thanks for your help.
avatarengineer



Joined: 13 May 2013
Posts: 30

View user's profile Send private message

constant strings for LCD
PostPosted: Mon Oct 03, 2016 11:19 am     Reply with quote

I found the most robust means to create menus, templates for LCDs
was to list strings as constants such as in a #define then load a selected page into an array using such as:
Code:

  strcpy(page.line[0].field,Line91);
  strcpy(page.line[1].field,Line92);
  strcpy(page.line[2].field,Line93);
  strcpy(page.line[3].field,Line94);

A slicker approach:
I now load the strings directly as programmed into Flash
at fixed addresses then just read what I need.
Much less code. Simple read_program_eeprom
using memory locations as pages of lines to read into data structure.

ok? Wink
avatarengineer



Joined: 13 May 2013
Posts: 30

View user's profile Send private message

Menus in ROM
PostPosted: Mon Oct 03, 2016 11:29 am     Reply with quote

Forgot to add this:
Code:
#DEFINE EndofMemory       0x4000    // Outside End of ROM address                         
#DEFINE MemoryBlock       0x0040    // allocated space per log 64 byte limit

#DEFINE LCD_Line1 "blah blah ..."
#DEFINE LCD_Line2 "more blah blah ..."

#DEFINE COMMENT1MEM       EndofMemory-(1*MemoryBlock) // LOCATE STRING INFO
#DEFINE COMMENT2MEM       EndofMemory-(2*MemoryBlock) // LOCATE STRING INFO

#ROM COMMENT1MEM={LCD_Line1}
#ROM COMMENT2MEM={LCD_Line2}

The compiler does all the work,...
then just read out as needed.


Enjoy

Idea
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group