
#define CLK      2          //Connected to TPIC pin 13: SRCLK (aka Clock)
#define LATCH    3          //Connected to TPIC pin 12: RCLK (aka Latch/load/CS/SS...)
#define DOUT     4          //Connected to TPIC pin 3: SER (aka MOSI)
#define SEG_0   A0
#define SEG_1   A1
#define SEG_2   A2
#define SEG_3   A3
#define SEG_4   A4
#define SEG_5   A5
#define SEG_6   11
#define SEG_7   12

#define FET_COUNT     8
#define BYTE_PER_FET  6

volatile uint8_t data[FET_COUNT][BYTE_PER_FET];
long dataMillis = 0;
uint8_t dataDemoStep = 0;

void setup() {
  Serial.begin(115200);

  pinMode(CLK,OUTPUT);
  pinMode(LATCH,OUTPUT);
  pinMode(DOUT, OUTPUT);
  pinMode(SEG_0, OUTPUT);
  pinMode(SEG_1, OUTPUT);
  pinMode(SEG_2, OUTPUT);
  pinMode(SEG_3, OUTPUT);
  pinMode(SEG_4, OUTPUT);
  pinMode(SEG_5, OUTPUT);
  pinMode(SEG_6, OUTPUT);
  pinMode(SEG_7, OUTPUT);

  initializeSRData();          //Prepares SR and clears data on serial line

}

void loop() {

  if(millis() - dataMillis > 10) {
    static uint8_t counterFet = 0;
    static uint8_t counterDigit = 0;
    static uint8_t counterBit = 0;
    
    for(int fet=0; fet<FET_COUNT; fet++) {
      
      for(int digit=0; digit<BYTE_PER_FET; digit++) {
          
        if(fet == counterFet && digit == counterDigit) {
          data[fet][digit] = _BV(counterBit);
        } else {
          data[fet][digit] = 0;
        } 
      }
    }

    counterBit++;
    if(counterBit > 7) {
      counterBit = 0;
      counterDigit++;

      if(counterDigit > 5) {
        counterFet++;
        counterDigit = 0;
        counterFet %= FET_COUNT;
      }
    }

    

    dataMillis = millis();
  }

  
  refreshDisplay();            //Cycles through all displays and digits
}

void initializeSRData() {
  
  digitalWrite(LATCH,HIGH);      //Tells all SRs that uController is sending data

  for(int digit = 0; digit < BYTE_PER_FET; digit++) {    
    shiftOut(DOUT, CLK, LSBFIRST,0);
  }
  
  digitalWrite(LATCH,LOW);      //Tells all SRs that uController is done sending data
}




void clearDisplay(int dispID) {
  initializeSRData();
  refreshDisplay();
}

void refreshDisplay() {
  
  for(int fet=0; fet<FET_COUNT; fet++) {
    
    // switch fets
    digitalWrite(SEG_0, LOW);
    digitalWrite(SEG_1, LOW);
    digitalWrite(SEG_2, LOW);
    digitalWrite(SEG_3, LOW);
    digitalWrite(SEG_4, LOW);
    digitalWrite(SEG_5, LOW);
    digitalWrite(SEG_6, LOW);
    digitalWrite(SEG_7, LOW);
    
    digitalWrite(LATCH, HIGH);
    
    for(int digit = 0; digit < BYTE_PER_FET; digit++) {
      shiftOut(DOUT, CLK, LSBFIRST, data[fet][digit]);
    }
    
    digitalWrite(LATCH, LOW);

    // switch fets
    digitalWrite(SEG_0, (fet == 0) ? HIGH : LOW);
    digitalWrite(SEG_1, (fet == 1) ? HIGH : LOW);
    digitalWrite(SEG_2, (fet == 2) ? HIGH : LOW);
    digitalWrite(SEG_3, (fet == 3) ? HIGH : LOW);
    digitalWrite(SEG_4, (fet == 4) ? HIGH : LOW);
    digitalWrite(SEG_5, (fet == 5) ? HIGH : LOW);
    digitalWrite(SEG_6, (fet == 6) ? HIGH : LOW);
    digitalWrite(SEG_7, (fet == 7) ? HIGH : LOW);
    
    delayMicroseconds(500);
    //delay(1);
  }
}


