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

In reply to Re^3: How to make a variable in hard call. by cavac
in thread How to make a variable in hard call. by Konan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.