Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't define macros in macros, but you can do some other tricks.</p> <p>This is in the "fairly ugly macro" territory. </p> <pre><code>#define MY_CAT(x, y) x ## y #define MY_CAT2(x, y) MY_CAT(x, y) #define LED(x) (MY_CAT2(MY_CAT2(LAT, PORT_ ## x), bits) \ .MY_CAT2(LAT, MY_CAT2(PORT_ ## x, POS_ ## x))) #define PORT_RED E #define POS_RED 2 #define PORT_YELLOW B #define POS_YELLOW 3 #define PORT_GREEN A #define POS_GREEN 4 LED(RED) LED(YELLOW) LED(GREEN) </code></pre> <p>The expansion (check with <code>gcc -E</code>) is:</p> <pre><code>(LATEbits .LATE2) (LATBbits .LATB3) (LATAbits .LATA4) </code></pre> <p>Just because macros are your hammer doesn't mean this is a nail. Try generating the source code with Python or something, much nicer.</p> <pre><code>LEDS = [ ('RED', 'E2'), ('YELLOW', 'B3'), ('GREEN', 'A4'), ] for name, pos in LEDS: print '#define LED_%s (LAT%sbits.LAT%s)' % (name, pos[0], pos) print '#define LED_%s_TRIS (TRIS%sbits.TRIS%s)' % (name, pos[0], pos) </code></pre> <p>The output is:</p> <pre><code>#define LED_RED (LATEbits.LATE2) #define LED_RED_TRIS (TRISEbits.TRISE2) #define LED_YELLOW (LATBbits.LATB3) #define LED_YELLOW_TRIS (TRISBbits.TRISB3) #define LED_GREEN (LATAbits.LATA4) #define LED_GREEN_TRIS (TRISAbits.TRISA4) </code></pre> <p>Then you just go ahead and check both the script and its output into your revision control system.</p> <p><strong>Addendum:</strong> I'm not going to explain how the macro trick works because I don't want to learn the trick well enough to explain it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload