#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Question Problem understanding a certain set of preprocessor statements for an IC's code


    Hello there. As an electronics-guy I'm planning on using the VS1053 IC for a project. I'm not that good in C, so I'm having some problems understanding preprocessor directives found in their "playback & recording" application note zip file. In the C file player1053.C found in the zip, there are a set of lines early on:

    Code:
    #define SKIP_PLUGIN_VARNAME
    const u_int16 imaFix[] = {
    #include "imafix.plg"
    };
    #undef SKIP_PLUGIN_VARNAME
    I don't understand these lines. Why would one define a macro to only undefine it a few lines right after? What does this accomplish?

    Also, does #include "imafix.plg" cause the contents of imafix.plg to be thrown right into the imaFix array? If so, then this is what's contained in imafix.plg:-
    Code:
    /* User application code loading tables for VS10xx */
    
    #if 0
    void LoadUserCode(void) {
      int i = 0;
    
      while (i<sizeof(plugin)/sizeof(plugin[0])) {
        unsigned short addr, n, val;
        addr = plugin[i++];
        n = plugin[i++];
        if (n & 0x8000U) { /* RLE run, replicate n samples */
          n &= 0x7FFF;
          val = plugin[i++];
          while (n--) {
            WriteVS10xxRegister(addr, val);
          }
        } else {           /* Copy run, copy n samples */
          while (n--) {
            val = plugin[i++];
            WriteVS10xxRegister(addr, val);
          }
        }
      }
    }
    #endif
    
    #ifndef SKIP_PLUGIN_VARNAME
    #define PLUGIN_SIZE 40
    const unsigned short plugin[40] = { /* Compressed plugin */
    #endif
      0x0007, 0x0001, 0x8010, 0x0006, 0x001c, 0x3e12, 0xb817, 0x3e14, /*    0 */
      0xf812, 0x3e01, 0xb811, 0x0007, 0x9717, 0x0020, 0xffd2, 0x0030, /*    8 */
      0x11d1, 0x3111, 0x8024, 0x3704, 0xc024, 0x3b81, 0x8024, 0x3101, /*   10 */
      0x8024, 0x3b81, 0x8024, 0x3f04, 0xc024, 0x2808, 0x4800, 0x36f1, /*   18 */
      0x9811, 0x0007, 0x0001, 0x8028, 0x0006, 0x0002, 0x2a00, 0x040e,
    #ifndef SKIP_PLUGIN_VARNAME
    };
    #endif
    By the looks of it, it doesn't seem to be a comma-separated set of elements. In fact, there are preprocessor directives and compiler statements! So how do these just get thrown into the imaFix array without error?

    Lastly, see lines 62/3/4:
    Code:
    #if 1
    #define REPORT_ON_SCREEN
    #endif
    What's the purpose to this, now? What does #if 1 (which I presume will always return true) accomplish? Why not simply just utilize line 62 as is, deleting 62 & 64?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,960
    Rep Power
    481
    Yes, the #include directive causes insertion of the named file, which is then subject to preprocessing.

    You'll see that imafix.plg uses the SKIP_PLUGIN_VARNAME macro.
    Therefore defining it or not was important.

    The other issue:
    #if 1
    #define REPORT_ON_SCREEN
    #endif

    Let's do this your way and remove the
    #if 1
    and
    #endif
    Later, you decide you don't want that feature, so from the source you delete the line
    #define REPORT_ON_SCREEN
    You just removed the information about a compilation option. Bloody nice work.

    Couldn't you just comment the line,
    //#define REPORT_ON_SCREEN
    to turn off the feature? Sure, but now you've got a source with a preprocessor directive, a # sign, that's not in column 1.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo