Port manipulation made easy

@HazardsMind

Came across this thread again today and dived into it. Came up with the following code.
As I lent my logic analyser to a friend so I could not check it yet,

so warning, highly experimental code, use at own risk

Goal: this code is to merge the masks of the bits to set simultaneously.

to be included in wiring_digital.c

void multiDigitalWrite(uint8_t *pins, uint8_t size, uint8_t val)
{
  // pins_arduino.h/c
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  const int numberOfPorts = 12;
  uint8_t bits[numberOfPorts] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  
#else // 328 - 168
  const int numberOfPorts = 5;  
  uint8_t bits[numberOfPorts] = { 0, 0, 0, 0, 0 };
  
#endif
  
  for (uint8_t i = 0; i < size; i++) 
  {
    uint8_t p = digitalPinToPort(pins[i]);
    if (p == NOT_A_PIN) continue;

    bits[p] | = digitalPinToBitMask(pins[i]);
	
    uint8_t timer = digitalPinToTimer(pins[i]);
    if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  }

  volatile uint8_t *out;
  for (int p = 0; p < numberOfPorts; p++)
  {
    if (bits[p] == 0) continue;  // no bits => skip port

    out = portOutputRegister(p);

    uint8_t oldSREG = SREG;
    cli();    
    if (val == LOW) 
    {
      *out &= ~bits[p];
    } else {
      *out |= bits[p];
    }
    SREG = oldSREG;
   }
}