in reply to Re^2: How to make a variable in hard call.
in thread How to make a variable in hard call.

Just a quick note: A hash with named pins might be easier to understand and adapt in the long run. E.g. name pins according to their function in code instead of their pin number. This is especially true if, say, the hardware changes. Pin "40" doesn't say anything, but "RTC_IRQ" tells me it's the IRQ pin for the real time clock.

Additionally, named pins are much easier to find&replace in code. If you search for "1" you could find a lot of stuff not relevant to the problem at hand, "SPI_CLK" on the other hand would be pretty unique to that pin functionality:

my %rpi_v2_gpio_p1 = ( SPI_CLK => Device::BCM2835::RPI_V2_GPIO_P1_03, SPI_CS => Device::BCM2835::RPI_V2_GPIO_P1_05, SPI_MOSI => Device::BCM2835::RPI_V2_GPIO_P1_07, SPI_MISO => Device::BCM2835::RPI_V2_GPIO_P1_07, ..., );

That's the same stuff i do in C for my Arduino projects:

... globals.h: #define PIN_RTC_IRQ 2 ...Firmware.ino: #if RADIODUINO_BOARD_REVISION < 2 // RTC PIN uses internal pullup resistor pinMode(PIN_RTC_IRQ, INPUT_PULLUP); #else // REV B and up already have an external pullup pinMode(PIN_RTC_IRQ, INPUT); #endif ... alarms.cpp: attachInterrupt(digitalPinToInterrupt(PIN_RTC_IRQ), RTCIRQ, FALLING);

If, say, the next hardware revision moves the IRQ pin to PIN 3, all i would need to change is globals.h:

#if RADIODUINO_BOARD_REVISION < 4 #define PIN_RTC_IRQ 2 #else #define PIN_RTC_IRQ 3 #endif

And with named pin mappings, it's also way easier to find all parts of the code that touch that specific pin:

$ fgrep PIN_RTC_IRQ * alarms.cpp: attachInterrupt(digitalPinToInterrupt(PIN_RTC_IRQ), RTC +IRQ, FALLING); Firmware.ino: pinMode(PIN_RTC_IRQ, INPUT_PULLUP); Firmware.ino: pinMode(PIN_RTC_IRQ, INPUT); globals.h:#define PIN_RTC_IRQ 2

So, in my opinion, don't use "magic numbers", use proper names for everything.

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP