/* everything specifical, the logic is in mainly and the timing is in [?] */

# include "Wire.h"
# define DS3231_I2C_ADDRESS 0x68

# include <Adafruit_NeoPixel.h>
# ifdef __AVR__
# include <avr/power.h>
# endif
 
# define PIN 6    // and 5

Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

//unsigned char abMap[] = {0, 0xff, 1, 0xff, 2, 0xff, 3}; // wide four
unsigned char abMap[] = {15, 14, 13, 12, 11, 10, 9, 8, }; // give it to me straight
//unsigned char abMap[] = {8, 0, 9, 1, 10, 2, 11, }; // 4/3 time
const byte mapSize = sizeof abMap / sizeof *abMap;

# define ABPIN 6

Adafruit_NeoPixel abbrevio = Adafruit_NeoPixel(mapSize, PIN - 1, NEO_GRB + NEO_KHZ800);

void mainx(void);
void doit(void);
void publishLamps(void);
extern unsigned int dLamp;

void setup() {
  Serial.begin(115200);
  Serial.println("moberly neo world!\n");

  strip.begin();
  strip.setPixelColor(1, 0xffffff);
  strip.show();

  abbrevio.begin();
  abbrevio.setPixelColor(1, 0xffffff);
  abbrevio.show();

  mainx();
}

unsigned long now;
# define LENGTH 10000
int countOff = LENGTH;

void loop() {
  static unsigned long lastTime;

  static unsigned long startTime;
  if (countOff == LENGTH) {
    startTime = micros();
  }

  now = micros();
//+++ if (now - lastTime < 4000) return;  // 250 Hz frame rate (244.14 in original)
  
  lastTime = now;

  doit();

  if (countOff) countOff--;
  else {
    Serial.print((micros() - startTime) / LENGTH); Serial.println(" per");
    countOff = LENGTH;
  }
}

void loop0() {
  unsigned long startTime = micros();
  for (int ii = 0; ii < 1000; ii++) {
    doit();
    delay(1);
  }

  Serial.print(micros() - startTime); Serial.print(" us, ");
  Serial.println(" 1000x");

  delay(777);
}

//unsigned char remap[16] = {0, 1, 2, 3, 7, 6, 5, 4, 8, 9, 10, 11, 15, 14, 13, 12,};

unsigned char remap[16] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,};

void publishLamps()
{
  int bit;
  unsigned char ik;

  for (ik = 0, bit = 1; ik < 16; bit <<= 1, ik++)
    strip.setPixelColor(remap[ik], (dLamp & bit) ? 0xff0000 : 0x004080);

  abbrevio.clear();
  for (byte ii = 0; ii < mapSize; ii++) {
    if (abMap[ii] != 0xff) abbrevio.setPixelColor(ii, (1 << abMap[ii]) & dLamp ? 0xff00ff : 0x008000);
  }

  strip.show();
  abbrevio.show();
}