简体   繁体   中英

How do I use enumerate type structs in C?

I am trying to set a Nokia LCD screen to turn all pixels off, all pixels on, inverse mode on and normal mode on, depending on which button the user pushes. I have all the code complete apart for setting the mode of the LCD screen. This is because they are displayed as enumerated type structs and I am not familiar with either concepts. The struct is:

typedef enum lcd_display_mode_t {
    lcd_display_all_off = 0b000,
    lcd_display_all_on  = 0b001,
    lcd_display_normal  = 0b100,
    lcd_display_inverse = 0b101,
} lcd_display_mode_t;

My best guess is that, being a enumerate type, I would simply have to type:

if SWITCH X IS ON{
    lcd_display_mode_t = 0;
}

Which would set the display mode to lcd_display_all_off. Is this the correct use of structs in this context? If not, what would I type to set the display modes?

An enum is not a struct. Using enums for storing binary data is a bad idea. One gets all kinds of weird side effects such as the type used being a signed int - which in turn is entirely unsuitable for the kind of hardware-related programming it will be used for. In addition, binary literals are not even standard C.

Note that the typedef makes lcd_display_mode_t a type, not a variable. Whoever wrote the code was a bit confused, it would be sufficient to just write typedef enum { ... } lcd_display_mode_t; .

They intended you to use the code like this:

lcd_display_mode_t mode;
...
mode = lcd_display_all_off;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM