Thread: Help with macro

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    6

    Help with macro

    I need to implement a macro for a byte x , which is represented by

    0 1 2 3 4 5 6 7
    blink bg red bg green bg blue intensity fg red fg green fg blue

    Each of the bits is having this attribute . I need to implement 3 macros.

    Code:
    #ifndef TEXTMODE_H
    #define TEXTMODE_H
    
    
    #define T_BLINK 0x80
    #define T_INTENSITY 0x08
    #define T_RED 0x04
    #define T_GREEN 0x02
    #define T_BLUE 0x01
    
    /* DESCRIPTION:
     * ------------
     * Modifies the attribute byte x by setting the attribute attr. If the attribute
     * is already set, the attribute byte remains unchanged.
     *
     * The attribute bytes are defined in the beginning of this header file.
     *
     * ARGUMENTS:
     * ------------
     * x: the attribute byte.
     * attr: the attribute bit to set as active (1).
     *
     */
    #define SETATTR(x, attr) ((x) |= (attr))
    
    
    
    
    
    /* DESCRIPTION:
     * ------------
     * Modifies the attribute byte x by setting the foreground and background color.
     *
     * This has no effect on the blink and intensity bits or the color bits already
     * set. For example, if the color is already red and SETCOLOR is used to set the
     * color to be green, then the attribute byte has both red and green set.
     *
     * See above for the definitions for different colors (T_RED, T_GREEN, T_BLUE).
     * The same definitions are used for both foreground and background colors.
     *
     * ARGUMENTS:
     * ------------
     * x: the attribute byte.
     * bg: the background color.
     * fg: the foreground color.
     *
     */
    #define SETCOLOR(x, bg, fg)((x) |= (bg | fg))
    
    
    
    
    /* DESCRIPTION:
     * ------------
     * Modifies the attribute byte by setting the color bits to 0 (black color).
     * This has no effect on the blink and intensity bits.
     *
     * ARGUMENTS:
     * ------------
     * x: the attribute byte.
     *
     */
    #define RESETCOLOR(x)((x) &= ~(T_GREEN | T_BLUE| T_RED))
    
    
    #endif

    My SETCOLOR macro implementation is giving wrong results. Please give a correct implementation. Also please see that others are implemented properly or not.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    can you give an example of (exactly) how you use SETCOLOR when it doesn't work properly?

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    6

    help with macro

    Quote Originally Posted by dmh2000 View Post
    can you give an example of (exactly) how you use SETCOLOR when it doesn't work properly?

    attr = 0x00


    SETATTR(attr, T_BLINK); // attr is blinking, black on black (0x80)
    SETCOLOR(attr, T_RED, T_GREEN); // attr is blinking, green on red (0xC2).
    SETCOLOR(attr, T_BLUE, T_RED); // attr is blinking, red+green on blue+red (0xD6).
    RESETCOLOR(attr); // attr is blinking, black on black (0x80)
    SETCOLOR(attr, 0, T_GREEN); // attr is blinking, green on black (0x82)
    SETATTR(attr, T_INTENSITY); // attr is blinking and has high intensity, green on black (0x8A).

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    6
    Quote Originally Posted by spanlength View Post
    attr = 0x00


    SETATTR(attr, T_BLINK); // attr is blinking, black on black (0x80)
    SETCOLOR(attr, T_RED, T_GREEN); // attr is blinking, green on red (0xC2).
    SETCOLOR(attr, T_BLUE, T_RED); // attr is blinking, red+green on blue+red (0xD6).
    RESETCOLOR(attr); // attr is blinking, black on black (0x80)
    SETCOLOR(attr, 0, T_GREEN); // attr is blinking, green on black (0x82)
    SETATTR(attr, T_INTENSITY); // attr is blinking and has high intensity, green on black (0x8A).
    For SETCOLOR i did the following
    #define SETCOLOR(x, bg, fg)((x) |= ((bg << 4) | (fg)))

    and for RESETCOLOR
    #define RESETCOLOR(x)((x) &= 0x88)

    Let me know if correct or not.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Consider the following:
    Code:
    #define T_BLINK      0x80
    #define T_INTENSITY  0x08
    
    #define T_RED        0x44
    #define T_GREEN      0x22
    #define T_BLUE       0x11
    
    #define T_ATTR_MASK  0x88
    #define T_COLOR_MASK 0x77
    #define T_FG_MASK    0x07
    #define T_BG_MASK    0x70
    The key point is that the generic color constant has the correct value to set both foreground and background at the same time. Given any color value,
    Code:
    foreground = (color & T_FG_MASK)
    background = (color & T_BG_MASK)
    foreandback = (color & T_COLOR_MASK) == (color & (T_FG_MASK | T_BG_MASK))
    attribute = (color & T_ATTR_MASK)
    To write the macros, first figure out which bits are kept from the old value; doing a bitwise AND with the correct mask keeps those bits and clears the rest.

    For the new values, you'll simply need a bitwise OR. Remember to use parentheses around each expression, because the bitwise operator precedence is dicey; better not assume. You'll want to first mask (AND) the correct parts from the macro variable, then OR it to the result. This is the technique I use to work with bit masks; it is universal, not limited to this particular task here.

    You should be able to write the macros using the above. I'd like to see your efforts; I'm very willing to help you understand, but I am not going to write the macros for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macro
    By cable in forum C Programming
    Replies: 6
    Last Post: 09-17-2011, 06:54 AM
  2. What does following macro mean?
    By sanddune008 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 06:27 PM
  3. Macro
    By rajashree in forum C Programming
    Replies: 5
    Last Post: 01-10-2008, 05:24 AM
  4. RGB macro
    By Magos in forum Game Programming
    Replies: 1
    Last Post: 04-10-2006, 03:57 PM
  5. need a macro that does nothing
    By Nyda in forum C Programming
    Replies: 5
    Last Post: 11-18-2004, 10:16 AM