On 1/29/08, [email protected] <[email protected]>
wrote:

> #define PxIN(x)    ( (x) == 1 ? (P1IN) : \
>                      (x) == 2 ? (P2IN) : \
>                      (x) == 3 ? (P3IN) : \
>                      (x) == 4 ? (P4IN) : \
>                      (x) == 5 ? (P5IN) : \
>                      (x) == 6 ? (P6IN) : 0 )
>
> Haven't figured out a general solution for the default part '0'
> esp. when it comes to PxOUT(x) stuff etc.
>
> Well, since x is defined at runtime, it ought to be a runtime error
message, right?
Since it is an embedded system, all of the following are appropriate
depending on
your assumptions, expectations and personal style:
 - reset and reinitialize the system if x is not 0,1,2,3,4,5,6
 - force the argument into range, by #define PxIN(x)    ( (x%7) == 1 ?
(P1IN) : \...
 - use an enumeration type for the variable x (but then we couldn't run
loops over x)
 - ignore x outside of range (that's what you did above)
 - return the highest port (P6IN in your case)

I personally like the wrapping method (x%7) because it is closest to what
we'd write if C was a strong-typing
language and we had a first-class enumeration type and use auto-wrapping
properties for loops involving that variable

Reply via email to