divert(-1)

                          `Header([A][1|2], rows, wid, ht, 
                           boxspec, pinlen;pinsize)
                            arg1: A= type A; number of columns
                            arg2: pins per column
                            arg3,4: custom wid, ht
                            arg5: eg fill_(0.9)'
define(`Header',
`[ define(`m4tp',ifinstr(`$1',A,A))define(`m4Hq',patsubst(`$1',A))dnl
  define(`m4Hn',`ifelse(m4Hq,,1,m4Hq)')dnl
  define(`m4Hm',`ifelse(`$2',,2,`$2')')dnl
  define(`m4Hw',`ifelse(`$3',,`m4Hn*L_unit*3',`($3)')')dnl
  define(`m4Hh',`ifelse(`$4',,`m4Hm*L_unit*3',`($4)')')dnl
  ifinstr(`$6',;,
  `stacksplit_(`m4Jdr',`$6',;)dnl
   define(`m4Jps',ifelse(m4Jdr,,`L_unit',m4Jdr))popdef(`m4Jdr')dnl
   define(`m4Jpl',ifelse(m4Jdr,,`lg_plen*L_unit',m4Jdr))popdef(`m4Jdr') ',
  `define(`m4Jps',L_unit)dnl
   define(`m4Jpl',`ifelse(`$6',,lg_plen*L_unit,`$6')')')dnl
  Block: rotbox(m4Hw,m4Hh,`$5')
  ifelse(`m4tp',A,`pushdef(`L_unit',(linewid/6))')
  define(`m4Hct',1)dnl
  for_(1,m4Hm,1,
   `HeaderPin(Block.NW+vec_(m4Jps*3/2,-(m4x-1/2)*m4Hh/m4Hm),
     ifinstr(m4tp,A,1,eval(m4Hct-1)),
     P`'m4Hct, w, (m4Jpl+m4Jps);m4Jps)  define(`m4Hct',incr(m4Hct))
    ifelse(m4Hn, 2, `HeaderPin(Block.NE+vec_(-m4Jps*3/2,-(m4x-1/2)*m4Hh/m4Hm),
       1, P`'m4Hct, e, (m4Jpl+m4Jps);m4Hps) define(`m4Hct',incr(m4Hct))') ')
  ifelse(`m4Hq',A,`popdef(`L_unit')')
  `$7' ]')
                          `HeaderPin(location, type, Picname, n|e|s|w,
                            [length][;pinsize])
                           arg2: 0= square' 
define(`HeaderPin',`
  ifinstr(`$5',;,
  `stacksplit_(`m4Hdr',`$5',;)dnl
   define(`m4Hps',ifelse(m4Hdr,,`L_unit',m4Hdr))popdef(`m4Hdr')dnl
   define(`m4Hpl',ifelse(m4Hdr,,`lg_plen*L_unit',m4Hdr))popdef(`m4Hdr') ',
  `define(`m4Hps',L_unit)dnl
   define(`m4Hpl',`ifelse(`$5',,lg_plen*L_unit,`$5')')')dnl
  ifelse(`$1',,,`move to `$1';')
  ifelse(`$3',,,`$3':) line to ifelse(
   `$4',n,`rvec_(0,m4Hpl)', `$4',e,`rvec_(m4Hpl,0)',
   `$4',s,`rvec_(0,-m4Hpl)', `rvec_(-m4Hpl,0)')
  ifelse(`$3',,,Pin`$3':) ifelse(ifelse(`$2',,0,`$2'),0,
   `rotbox(m4Hps,m4Hps,fill_(1))',
   `circle diam m4Hps fill_(1)') at last line.start ')

define(`ATMEGA16U2',`[
  Chip: box wid 10*lg_pinsep ht 25*lg_pinsep
  foreach_(`x',
   `Arightpin(patsubst(x,;,`,'))',
   6;PD0, 7;PD1, 8;PD2, 9;PD3, 10;PD4, 11;PD5, 12;PD6, 13;PD7,
   131;nul,
   5;PC2, 26;PC4, 25;PC5, 23;PC6, 22;PC7,
   132;nul,
   133;nul,
   14;PB0, 15;PB1, 16;PB2, 17;PB3, 18;PB4, 19;PB5, 20;PB6, 21;PB7)
  foreach_(`x',
   `Aleftpin(patsubst(x,;,`,'))',
   33;PAD,
   100;nul,
   28;UGND, 29;D$+$, 30;D$-$, 31;UVCC, 27;UCAP,
   101;nul, 102;nul,
   3;GND, 4;VCC, 103;nul,
   104;nul,
   32;AVCC,
   105;nul, 106;nul,
   1;XTAL1,
   107;nul,
   2;XTAL2(PC0),
   108;nul, 109;nul)
   lg_pin(Pin109+(0,lg_pinsep),ifpostscript(,\tiny\sf) RESET,Pin24,wN,24,pinlen)
  ]')
define(`Aleftpin',`move to Chip.sw+(0,m4Lx*lg_pinsep)
 Pin`$1': ifelse(`$2',nul,Here,dnl
 `line left pinlen "`$1'" above
  "`$2'" ljust at last line.start')') 
define(`Arightpin',`move to Chip.se+(0,m4Lx*lg_pinsep)
 Pin`$1': ifelse(`$2',nul,Here,dnl
 `line right pinlen "`$1'" above
  "`$2'" rjust at last line.start')') 

define(`ATMEGA328P',`[
  Chip: box wid 10*lg_pinsep ht 23*lg_pinsep
  foreach_(`x',
   `Arightpin(patsubst(x,;,`,'))',
   2;PD0, 3;PD1, 4;PD2, 5;PD3, 6;PD4, 11;PD5, 12;PD6, 13;PD7,
   100;nul,
   23;PC0, 24;PC1, 25;PC2, 26;PC3, 27;PC4, 28;PC5,
   101;nul,
   14;PB0, 15;PB1, 16;PB2, 17;PB3, 18;PB4, 19;PB5)
  foreach_(`x',
   `Aleftpin(patsubst(x,;,`,'))',
   201;nul, 202;nul, 203;nul, 204;nul, 205;nul, 206;nul, 207;nul, 208;nul,
   209;nul,
   8;GND, 7;VCC,
   301;nul,
   22;AGND, 20;AVCC, 21;AREF,
   302;nul,
   9;XTAL1,
   303;nul,
   10;XTAL2,
   304;nul, 305;nul)
   lg_pin(Pin305+(0,lg_pinsep),ifpostscript(,\tiny\sf) RESET,Pin1,wN,1,pinlen)
  ]')

define(`rswitch',`dswitch(`$1',,WBoDCP)
  DS: last line.start; DC: last line.c; DE: last line.end
  DL: DC-vec_(dimen_/6,0); DR: DC+vec_(dimen_/6,0)
  {{"1" at DL above rjust}; {"3" at DR above ljust}
  { line from DL to DL-vec_(0,dimen_/6) chop dotrad_ chop 0
    {"2" below rjust}
    continue to DS-vec_(0,dimen_/6) then to DS; dot }
  { line from DR to DR+vec_(0,-dimen_/6) chop dotrad_ chop 0
    {"4" below ljust}
    continue to DE+vec_(0,-dimen_/6) then to DE; dot }
  line from DC+vec_(dimen_/10,-dimen_/6) to DC+vec_(-dimen_/10,-dimen_/6)
  move to last line.c; line to rvec_(0,-dimen_/3) \
    then to rvec_(-dimen_/3,-dimen_/3)
    {"5" below}} ')

divert(0)dnl
