#if preprocessor directive

I recently downloaded this library for talking to a PN532 device from Elechouse.

The board comes with 3 different communication protocols to choose from.

  • HSU
  • SPI
  • I2C

There are a couple of slide switches on the board to determine which one to use.

The example code that comes with it, includes the following preprocessor statements.

#if 0
  #include <SPI.h>
  #include <PN532_SPI.h>
  #include "PN532.h"

  PN532_SPI pn532spi(SPI, 10);
  PN532 nfc(pn532spi);
#elif 1
  #include <PN532_HSU.h>
  #include <PN532.h>
      
  PN532_HSU pn532hsu(Serial1);
  PN532 nfc(pn532hsu);
#else 
  #include <Wire.h>
  #include <PN532_I2C.h>
  #include <PN532.h>
  PN532_I2C pn532i2c(Wire);
  PN532 nfc(pn532i2c);	
#endif

So the setup code required depending of what communication protocol is required.

What I don't understand is where do the values: 0 or 1 or neither, come from ? From what I read I thought anything non zero would be considered true, so the above code would resolve to the following every time.

  #include <PN532_HSU.h>
  #include <PN532.h>
      
  PN532_HSU pn532hsu(Serial1);
  PN532 nfc(pn532hsu);

Am I missing something?

You are right in your understanding

It’s probably just poorly written, a quick way to show you what to include depending on your switch position.

They would have been better off having multiple define for the modes and test against one

Just get rid of the conditional compiling and keep the part that makes sense for your setup

Cool... thanks.

Just to make it a little more confusing the numbering doesn't actually line up with the switch on the board. :slight_smile:

image

That’s a way to boost the skills of their customers, a few hurdles to get your neurons ready :slight_smile:

1 Like

the dip switch doesn't suggests using c preprocessor directives. instead code is needed to read the switches at run-time to determine the board configuration.

doesn't seem that the code is written properly

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.