Jump to content

switch on bitfield?


ilmenator
 Share

Recommended Posts

Hi all,

I have created a struct that holds status information which is 2 bit wide, each:

// status of menu
typedef union{
	struct {
		unsigned MENU:16;
	};
	struct{
		unsigned MENU_MODE:2;								
		unsigned MENU_LEVEL:2;							
		unsigned SOURCE_BANK_TYPE:2;				
		unsigned MENU_ACTION:2;							
		unsigned DEST_BANK_TYPE:2;					
	};
} volatile menu_flags_t;
Now, I need to analyse these states, and I would like to use a switch command to increase readability:
// status of menu (see declaration in main.h)
menu_flags_t menu_flags;
// call functions that depend on menu level

switch(menu_flags.MENU_LEVEL){
	// menu level 1:
        case 1:		
		switch(pin){
			case DIN_INC:
				....
			case DIN_DEC:
				....
			case DIN_ENTER:
				....
		}
		break;


	// menu level 2:
	case 2:		// menu_level == 2
		switch(pin){
			case DIN_INC:
                                ....
			case DIN_DEC:
				....

	        }			
	        break;
Unfortunately, the compiler complains:
main.c:316: warning 94: comparison is always true resp. false due to limited range of data type
main.c:316: warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG

Line 316 is the one with the first switch command. So how to do it correctly? Or is switch a No-No with bitfields?

Thanks, ilmenator

Link to comment
Share on other sites

Ahhh good old evelyn.

Alas you can't pass an aggregate as an argument in SDCC, it'll be passing the address, and seeing as the address will never change, the optimiser has a fit.

Try

foo = menu_flags.MENU_LEVEL;

switch (foo)

Or something...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...