unsigned char countBits(int n)
{
  unsigned char count = 0;
  while (n) {
    // printf(" %d\n", n);
    n &= (n - 1);
    count++;
  }
  return count;
}

/*
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring 5 is next",
      "top": -195.48,
      "left": -153.5,
      "attrs": { "pixleate": "1", "rows": "1", "cols": "8" }
    },
*/

/* Program to test function countSetBits
  int main()
  {
    char i = 17;
    printf("%d\n\n", countBits(i));


    for (unsigned char ii = 120; ii < 150; ii++) {
        printf("%3x: %3d\n", ii, countSetBits(ii));
    }

    return 0;
  }
*/

/*
  8E
  95
  96
  A6
  AF.
  B1
  B2
  B4.
  B8
  C3
  C6.
  D4.
  E1
  E7.
  F3
  FA
*/

unsigned char lsfr8(unsigned char x) {
  x |= x == 0;   // if x == 0, set x = 1 instead
  x ^= (x & 0x0F) << 4;  // feedback from positions 0, 1, 2, 3 to 4th position
  x ^= x >> 1;  // shift right by 1 bit and XOR
  return x;
}

// ... try to get this right again
unsigned char lsfr8X(unsigned char xx) {
  /*
    static char was = 0xff;
    const unsigned char kCycle = 0xa3;

    if (xx == 0)
      return was;
  */

  char parity = countBits(xx & 0xa6) & 0x1;

  xx <<= 1;
  xx |= parity;
  /*
    if (was == kCycle) {
      was = xx;
      return 0;
    }
  */

  return xx;
}


unsigned char next(unsigned char xOK)
{

}

unsigned char binaryToGray(unsigned char num)
{
  return num ^ (num >> 1);
}

unsigned char grayToBinary(unsigned char num)
{
  unsigned char mask;

  for (mask = num >> 1; mask != 0; mask = mask >> 1)
    num = num ^ mask;

  return num;
}

const unsigned char balancedGray[256] = {
  0x00, 0x01, 0x03, 0x02, 0x06, 0x0e, 0x0a, 0x0b, 0x09, 0x0d, 0x0f, 0x07, 0x05, 0x04, 0x0c, 0x08,
  0x18, 0x1c, 0x14, 0x15, 0x17, 0x1f, 0x3f, 0x37, 0x35, 0x34, 0x3c, 0x38, 0x28, 0x2c, 0x24, 0x25,
  0x27, 0x2f, 0x2d, 0x29, 0x39, 0x3d, 0x1d, 0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, 0xc7, 0x47, 0x4f,
  0xcf, 0x8f, 0x8d, 0xcd, 0x4d, 0x49, 0xc9, 0x89, 0x8b, 0xcb, 0x4b, 0x4a, 0xca, 0x8a, 0x8e, 0xce,
  0x4e, 0x46, 0xc6, 0x86, 0x82, 0xc2, 0x42, 0x43, 0xc3, 0x83, 0x81, 0xc1, 0x41, 0x40, 0xc0, 0x80,
};


const unsigned char hBitCombos[256] = { 0,
  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x03, 0x05, 0x09, 0x11, 0x21, 0x41, 0x81, 0x06,
  0x0a, 0x12, 0x22, 0x42, 0x82, 0x0c, 0x14, 0x24, 0x44, 0x84, 0x18, 0x28, 0x48, 0x88, 0x30, 0x50,
  0x90, 0x60, 0xa0, 0xc0, 0x07, 0x0b, 0x13, 0x23, 0x43, 0x83, 0x0d, 0x15, 0x25, 0x45, 0x85, 0x19,
  0x29, 0x49, 0x89, 0x31, 0x51, 0x91, 0x61, 0xa1, 0xc1, 0x0e, 0x16, 0x26, 0x46, 0x86, 0x1a, 0x2a,
  0x4a, 0x8a, 0x32, 0x52, 0x92, 0x62, 0xa2, 0xc2, 0x1c, 0x2c, 0x4c, 0x8c, 0x34, 0x54, 0x94, 0x64,
  0xa4, 0xc4, 0x38, 0x58, 0x98, 0x68, 0xa8, 0xc8, 0x70, 0xb0, 0xd0, 0xe0, 0x0f, 0x17, 0x27, 0x47,
  0x87, 0x1b, 0x2b, 0x4b, 0x8b, 0x33, 0x53, 0x93, 0x63, 0xa3, 0xc3, 0x1d, 0x2d, 0x4d, 0x8d, 0x35,
  0x55, 0x95, 0x65, 0xa5, 0xc5, 0x39, 0x59, 0x99, 0x69, 0xa9, 0xc9, 0x71, 0xb1, 0xd1, 0xe1, 0x1e,
  0x2e, 0x4e, 0x8e, 0x36, 0x56, 0x96, 0x66, 0xa6, 0xc6, 0x3a, 0x5a, 0x9a, 0x6a, 0xaa, 0xca, 0x72,
  0xb2, 0xd2, 0xe2, 0x3c, 0x5c, 0x9c, 0x6c, 0xac, 0xcc, 0x74, 0xb4, 0xd4, 0xe4, 0x78, 0xb8, 0xd8,
  0xe8, 0xf0, 0x1f, 0x2f, 0x4f, 0x8f, 0x37, 0x57, 0x97, 0x67, 0xa7, 0xc7, 0x3b, 0x5b, 0x9b, 0x6b,
  0xab, 0xcb, 0x73, 0xb3, 0xd3, 0xe3, 0x3d, 0x5d, 0x9d, 0x6d, 0xad, 0xcd, 0x75, 0xb5, 0xd5, 0xe5,
  0x79, 0xb9, 0xd9, 0xe9, 0xf1, 0x3e, 0x5e, 0x9e, 0x6e, 0xae, 0xce, 0x76, 0xb6, 0xd6, 0xe6, 0x7a,
  0xba, 0xda, 0xea, 0xf2, 0x7c, 0xbc, 0xdc, 0xec, 0xf4, 0xf8, 0x3f, 0x5f, 0x9f, 0x6f, 0xaf, 0xcf,
  0x77, 0xb7, 0xd7, 0xe7, 0x7b, 0xbb, 0xdb, 0xeb, 0xf3, 0x7d, 0xbd, 0xdd, 0xed, 0xf5, 0xf9, 0x7e,
  0xbe, 0xde, 0xee, 0xf6, 0xfa, 0xfc, 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe, 0xff,
};



unsigned char sequence[256];

void shuffle() {
  for (int ii = 256 - 1; ii > 0; ii--) {
    int jj = random(ii + 1);
    int temp = sequence[ii];
    sequence[ii] = sequence[jj];
    sequence[jj] = temp;
  }
}

# define WIDTH  64    // should be sum of sizes... or at least number of 8s.
# define PIN 6

# include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel track(WIDTH, PIN, NEO_GRB + NEO_KHZ800);
bool trackDirt;

void setup() {
  Serial.begin(115200);
  Serial.println("blinking lights");

  pinMode(7, INPUT_PULLUP);

  for (int ii = 0; ii < 256; ii++) sequence[ii] = ii;
  shuffle();

  //  for (int ii = 0; ii < 256; ii++) Serial.println(sequence[ii]);


  track.begin();
  track.setPixelColor(0, 0xff0000);
  track.setPixelColor(1, 0x00ff00);
  track.setPixelColor(2, 0x0000ff);
  track.setPixelColor(7, 0x404040);
  track.setPixelColor(15, 0x404040);
  track.setPixelColor(23, 0x404040);
  track.setPixelColor(31, 0x404040);
  track.setPixelColor(39, 0x404040);
  track.setPixelColor(47, 0x404040);
  track.setPixelColor(55, 0x404040);
  track.setPixelColor(63, 0xffffff);

  track.show(); delay(777); track.clear();
}

unsigned char myRegister = 1;
int count;

# define COUNT 8
# define LFSR8 16
# define GRAY 0
# define NCK 24
# define KCAB 32
# define DECK 40
# define HILO 48

# define INV  56   // redundant  ~(count & 0xff)

void loop() {
  if (digitalRead(7) == HIGH) delay(377);
  else delay(17);

  if (0) {
    Serial.println(flip(0xaa), BIN);

    if (0) for (int ii = 0; ii < 50; ii++) {
        Serial.print(ii, BIN); Serial.print(" ");
        Serial.println(flip(ii), BIN);
      }
  }

  //   for (; ; );


  //  show8At(myRegister, LFSR8);
  myRegister = lsfr8X(myRegister);

  //  if (!(count & 0xff)) shuffle();
  //  show8At(sequence[count & 0xff], DECK);

    show8At(count & 0xff, COUNT);

  // show8At(255 - (count & 0xff), KCAB);

    show8At(binaryToGray(count & 0xff), KCAB);

  //  show8At(hBitCombos[count & 0xff], NCK);

  //  show8At(flip(count & 0xff), HILO);

  // show8At(~(count & 0xff), INV);
  
  show8At(balancedGray[count & 0xff], INV);

  if (myRegister == 0x1) Serial.println(count);

  if (trackDirt) {
    track.show();
    trackDirt = false;
  }

  count++;

  //  for (; ; );
}

void show(unsigned char x) {
  unsigned char mask = 0x80;

  for (byte ii = 0; ii < 8; ii++) {
    track.setPixelColor(ii, (mask & x) ? 0xff0000 : 0);
    mask >>= 1;
  }

  track.show();
}

/*
unsigned char flip_yes(unsigned char n)
{
  unsigned char rev = 0;

  // traversing bits of 'n' from the right
  while (n > 0) {
    // bitwise left shift 'rev' by 1
    rev <<= 1;

    // if current bit is '1'
    if (n & 1 == 1)
      rev ^= 1;

    // bitwise right shift 'n' by 1
    n >>= 1;
  }
  // required number
  return rev;
}
*/

unsigned char flip(unsigned char xx)
{
  unsigned char result = 0;
  unsigned char mask = 0x1;
  byte ii;

  for (ii = 0; ii < 8; ii++, mask <<= 1) {
    result <<= 1;
    if (xx & mask) result |= 0x1;
  }

  return result;
}

void show8At(unsigned char xx, int address) {
  unsigned char mask = 0x80;

  for (byte ii = 0; ii < 8; ii++) {
    track.setPixelColor(ii + address, (mask & xx) ? 0xff0000 : 0);
    mask >>= 1;
  }

  trackDirt = true;
}

/*
  for (unsigned char ii = 120; ii < 150; ii++) {
    Serial.print(ii, 2);
    Serial.print("   ");
    Serial.print(countBits(ii));

    Serial.println("");
  }
*/

/*
  void loop0() {
  for (unsigned char ii = 1; ii; ii++) {
    Serial.print(ii, 2);
  //    Serial.print("   ");
  //    Serial.print(lsfr8(ii));

    Serial.print("\t");
    Serial.print(lfsr8(ii));

    show(); delay(256);

    Serial.println("");
  }

  //  for (; ; );
  }
*/

/*
               ,
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring9",
      "top": -196.68,
      "left": -153.5,
      "attrs": { "pixleate": "1", "rows": "8", "cols": "8" }
    }
*/

/*

  if (0) {
    Serial.print(ii); Serial.print(" ");
    Serial.print(xx, BIN); Serial.print(" ");
  //    Serial.print(mask, BIN); Serial.print(" ");
    Serial.print(result, BIN);

    Serial.println("");
  }
*/
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
ring1:GND
ring1:VCC
ring1:DIN
ring1:DOUT
ring4:GND
ring4:VCC
ring4:DIN
ring4:DOUT
ring2:GND
ring2:VCC
ring2:DIN
ring2:DOUT
ring3:GND
ring3:VCC
ring3:DIN
ring3:DOUT
ring5:GND
ring5:VCC
ring5:DIN
ring5:DOUT
ring6:GND
ring6:VCC
ring6:DIN
ring6:DOUT
ring7:GND
ring7:VCC
ring7:DIN
ring7:DOUT
ring8:GND
ring8:VCC
ring8:DIN
ring8:DOUT
ring9:GND
ring9:VCC
ring9:DIN
ring9:DOUT
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r