I have a couple of questions about Arduino pin-to-port mapping, and preprocessor capabilities.
I am developing a hardware-related library and would like to allow the user (sketch-writer) to specify from 2 to 5 (or maybe 2 to 6) consecutive pin numbers for the hardware connection (by specifying the minimum pin number, and the maximum pin number), but they must be in the same parallel I/O port register (for the speed afforded by parallel port manipulations instead of “bit-at-a-time” manipulations, and also because reading inputs from the device must be atomic, which means that the “bit-at-a-time” reads may not work if changes occur while reading.)
For example, say the user/sketch-writer specifies the following:
#define HWDEVICE_MIN_PIN 8 #define HWDEVICE_MAX_PIN 10
(where HWDEVICE is the name of the hardware library - which, BTW, is NOT “HWDevice” ;).
This would mean that the user/sketch-writer would connect a 3-pin device to pins 8, 9 and 10.
I want to check. at compile-time, that the consecutive pin numbers defined by the user/sketch-writer are in the same port register (to be able to use the PORTx, DDRx and PINx registers), and issue a preprocessor #error directive if the consecutive pin numbers are not all in the same parallel I/O port.
(It would be nice to be able to specify the minimum-pin and maximum-pin as parameters in the class library’s constructor, but I strongly believe that would prevent the ability to check, at compile-time, that the consecutive pins defined would be in the same port register.)
Is/are there any built-in Arduino macro(s) that map an Arduino pin number to its corresponding parallel port registers, and bit position (or bit mask) within that port register (depending, of course, on the Arduino board declared for the sketch build)? If such macros are available, where might I find documentation describing them?
I am looking for something like (inside my library’s header file):
#define HWDEVICE_PORT PIN_TO_PORTREG_MAP(HWDEVICE_MIN_PIN) #define HWDEVICE_DDR PIN_TO_DDRREG_MAP(HWDEVICE_MIN_PIN) #define HWDEVICE_PIN PIN_TO_PINREG_MAP(HWDEVICE_MIN_PIN)
Does the preprocessor provide for any means of forward referencing of macro definitions? For example, suppose the sketch has the following, for the UNO board:
#include <HwDeviceLib.h> #define HWDEVICE_MIN_PIN 6 #define HWDEVICE_MAX_PIN 9
Is there any possible way for the HwDevice.h header to issue a compile-time error directive (because pins 6 and 7 are in different port registers than pins 8 and 9, for the UNO)?
If not, then the library will require the user to place the “constant” macros before the #include directive:
#define HWDEVICE_MIN_PIN 6 #define HWDEVICE_NUM_PIN 4 #include <HwDeviceLib.h>
which may “look funny” to a beginner sketch-writer.
So, in any case, the error checking would (I hope) look something like:
#if (HWDEVICE_PORT != PIN_TO_PORTREG_MAP(HWDEVICE_MAX_PIN)) #error "The device pins must all be in the same AVR parallel I/O port register." #endif
So is any of the above possible?
Thanks and best regards,