//
//
//      C         us=USB;sd=SD;pl=Play;dp=:ps=Pause;mp=MP3;fm=FM
//      -
//   B| _  |D
//      G
//   A| _ |E
//      F
uint16_t led_sg1 [36] = {
  0b1111011000000001,  //A
  0b1111110000000010,  //B
  0b1111110000000001,  //C
  0b1111101000000001,  //D
  0b1111011000001000,  //E
  0b1110111000010000,  //F
  0b1111101000000100,  //G  --ende 1. seg
  0b1110110100000010,  //A pos7
  0b1111100100000100,  //B
  0b1111100100000010,  //C
  0b1111010100000010,  //D
  0b1110110100010000,  //E
  0b1101110100000010,  //F
  0b1111010100001000,  //G  --ende 2. seg
  0b1101101100100000,  //Pos14
  0b1111001100001000,
  0b1110011100010000,
  0b1110101100000100,
  0b1110011100001000,
  0b1101111000100000,
  0b1110101100010000,   //ende 3. seg
  0b1101011100001000,  //Pos21
  0b1100111100100000,
  0b1001111101000000,
  0b1001111100100000,
  0b1100111100010000,
  0b1101011100100000,
  0b1010111100010000,  //ende 4. seg
  0b1111001100000100,  // 1-DOPPELPUNKT    1-DOPPELPUNKT,2-PLAY,4-PAUSE,8-USB, 16-SD, 32-FM, 64-MP3
  0b1101111000000001,  // 2-PLAY --ende sonder
  0b1101101100000100,  // 4-PAUSE
  0b1101110100100000,  // 8-USB
  0b1110111000000001,  //16-SD
  0b1011101101000000,  //32-FM Pos28  , --ende sonder
  0b1011101100000100,  //64-MP3
  0b1111111100000000   //Pos35 ALLES  AUS
};

//---Zahlen
uint8_t zahl[10] = {
  0b0111111,  //0
  0b0011000,  //1
  0b1101101,  //2
  0b1111100,  //3
  0b1011010,  //4
  0b1110110,  //5
  0b1110111,  //6
  0b0011100,  //7
  0b1111111,  //8
  0b1111110,  //9
};
//fuer Uhr
uint8_t time[4] = {0, 0, 0, 0};
uint8_t colon = 0;
uint8_t seconds = 00;
uint8_t minutes = 57;
uint8_t hours = 18;
long lastmillis_time = millis();
//Test
long lastmillis = millis();
bool t_blink = false;
long lastmillis1 = millis();
bool t_blink1 = false;
long lastmillis2 = millis();
bool t_blink2 = false;
long lastmillis3 = millis();
bool t_blink3 = false;
long lastmillis4 = millis();
bool t_blink4 = false;
void display_segment(int seg1, int seg2, int seg3, int seg4, int seg_sonder);
// Hardware
const uint8_t out_pins[] = {8, 7, 6, 5, 4, 3, 2};
const uint8_t mask_pins[] = {A2, A1, A0, 12, 11, 10, 9};
uint8_t out_byte = 0;
uint8_t input_mask;
uint8_t sonder = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("Bring up Monitor");
/*
  uint32_t start_mill = millis();
  for (uint16_t i = 0; i <10000; i++) 
    display_segment(8, 8, 8, 8, 127);
  uint32_t stop_mill = millis();
    Serial.print("start_mill>");
    Serial.print(start_mill);
    Serial.print("-stop_mill>");
    Serial.print(stop_mill);
    Serial.print("-Zeit fuer 10000>");
    Serial.print(stop_mill-start_mill);
    Serial.print("-Wie oft pro ms>");
    Serial.println(10000/(stop_mill-start_mill));
    Serial.print("-Wieviele pro Durchlauf ms>");
    Serial.println((stop_mill-start_mill)/10000);

   // Serial.print("-millis>");
   // Serial.print(millis());
   // Serial.print("-lastmillis_time>");
   // Serial.println(lastmillis_time, DEC);
*/

    Serial.print("uint8_t>");
    Serial.print(sizeof(uint8_t), DEC);
    Serial.print("-int>");
    Serial.print(sizeof(int), DEC);
    Serial.print("-long>");
    Serial.println(sizeof(long), DEC);
  //  Serial.print("-Wie oft pro ms>");
  //  Serial.println(10000/(stop_mill-start_mill));



  for (int i = 0; i < sizeof(out_pins); i++) {
    pinMode(out_pins[i], INPUT);
    //    digitalWrite(out_pins[i], LOW);
  }
  //  pinMode(13, OUTPUT);
  //  digitalWrite(13, LOW);
  //  for (int i = 0; i < 36; i++) {
  //    write_seg(led_sg1[i]);
  //    uint16_t out_int = input_mask << 8;
  //    out_int = out_int | out_byte;
  //    Serial.print("out_byte> ");
  //    Serial.println(out_int, BIN);
  //    delay(100);
  //  }
}  //END SETUP





void display_segment(int seg1, int seg2, int seg3, int seg4, int seg_sonder) {
  if ((seg1 > -1) && (seg1 < 10)) {
    for (int i = 0; i < 7; i++) {
      if (bitRead(zahl[seg1], i)) {
        write_seg(led_sg1[i]);
      }
    }
  }
  if ((seg1 > -1) && (seg1 < 10)) {
    for (int i = 0; i < 7; i++) {
      if (bitRead(zahl[seg2], i)) {
        write_seg(led_sg1[i + 7]);
      }
    }
  }
  if ((seg1 > -1) && (seg1 < 10)) {
    for (int i = 0; i < 7; i++) {
      if (bitRead(zahl[seg3], i)) {
        write_seg(led_sg1[i + 14]);
      }
    }
  }
  if ((seg1 > -1) && (seg1 < 10)) {
    for (int i = 0; i < 7; i++) {
      if (bitRead(zahl[seg4], i)) {
        write_seg(led_sg1[i + 21]);
      }
    }
  }  //  if(seg_sonder){
  if (seg_sonder > 0) {
    for (int i = 0; i < 7; i++) {
      if (bitRead(seg_sonder, i)) {
        write_seg(led_sg1[i + 28]);
      }
    }
  }
}
void updateClock()
{
  seconds = seconds + 1;
  if (seconds > 59) {
    seconds = 0;
    minutes = minutes + 1;
  }
  if (minutes > 59) {
    minutes = 0;
    hours = hours + 1;
  }
  if (hours > 23) {
    hours = 0;
    //  minutes=minutes+1;
  }

  time[0] = hours / 10;
  time[1] = hours % 10;
  time[2] = minutes / 10;
  time[3] = minutes % 10;
  if (colon)
    colon = 0;
  else
    colon = 0b1; //1. Sonderbit

  if (seconds == 0) {
    sonder = 0;
  }
  else if (seconds > 9)
    bitWrite(sonder, (seconds / 10), HIGH);
  //
  //  Serial.print("minutes>");
  //  Serial.print(minutes);
  //  Serial.print("-seconds>");
  //  Serial.print(seconds);
  //  Serial.print("-seconds/10>");
  //  Serial.print(seconds/10);
  //  Serial.print("-Sonder>");
  //  Serial.println(sonder, BIN);
  //  Serial.print("-millis>");
  //  Serial.print(millis());
  //  Serial.print("-lastmillis_time>");
  //  Serial.println(lastmillis_time, DEC);

}

void loop() {
  if (millis() > lastmillis_time + 998) {
    updateClock();
    lastmillis_time = millis();
  }
  //  if(millis()>lastmillis+9999){
  //    sec_10_count++;
  //    if(sec_10_count==6){
  //      sec_10_count=0;
  //      sonder=0;
  //    }
  //    else
  //      bitWrite(sonder,sec_10_count+1,HIGH);
  //    switch (sec_10_count) {  // 1-DOPPELPUNKT,2-PLAY,4-PAUSE,8-USB, 16-SD, 32-FM, 64-MP3
  //      case 1:
  //        bitWrite(sonder,2,HIGH);
  //        break;
  //      case 1:
  //        bitWrite(out_byte, i, HIGH);
  //        break;
  //      default:
  //        bitWrite(input_mask, i, HIGH);
  //    }
  //      sonder=0;
  //    }
  //    t_blink=!t_blink;
  //    bitWrite(sonder,0,t_blink);
  //    lastmillis = millis();
  //  }
  if (millis() > lastmillis1 + 200) {  // 1-DOPPELPUNKT,2-PLAY,4-PAUSE,8-USB, 16-SD, 32-FM, 64-MP3
    t_blink1 = !t_blink1;
    bitWrite(sonder, 6, t_blink1);
    lastmillis1 = millis();
  }
  //  if(millis()>lastmillis2+30000){
  //    t_blink2=!t_blink2;
  //    bitWrite(sonder,3,t_blink2);
  //    lastmillis2 = millis();
  //  }
  //  if(millis()>lastmillis3+40000){
  //    t_blink3=!t_blink3;
  //    bitWrite(sonder,4,t_blink3);
  //    lastmillis3 = millis();
  //  }
  //  if(millis()>lastmillis4+50000){
  //    t_blink4=!t_blink4;
  //    bitWrite(sonder,6,t_blink4);
  //    lastmillis4 = millis();
  //  }
  display_segment(time[0], time[1], time[2], time[3], sonder | colon);
}

void write_seg(uint16_t out_seg) {
  out_byte = out_seg;
  input_mask = out_seg >> 8;
  write_to_hw();

}

void write_to_hw() {
  for (int i = 0; i < 7; i++) {
    if (bitRead(input_mask, i))
      pinMode(out_pins[i], INPUT_PULLUP);
    else
      pinMode(out_pins[i], OUTPUT);
  }
  delayMicroseconds(5);
  //delay(1);
  for (int i = 0; i < 7; i++) {
    if (bitRead(input_mask, i) == false)
      digitalWrite(out_pins[i], bitRead(out_byte, i));
  }
  delayMicroseconds(20);
  //delay(1);
  for (int i = 0; i < 7; i++) {
    if (bitRead(input_mask, i) == false)
      digitalWrite(out_pins[i], HIGH);
  }
}

void simu_mask_hw() {
  for (int i = 0; i < 7; i++) {
    if (bitRead(input_mask, i))
      pinMode(mask_pins[i], OUTPUT);
    else
      pinMode(mask_pins[i], INPUT);
  }
  for (int i = 0; i < 7; i++)
    digitalWrite(mask_pins[i], bitRead(input_mask, i));
}


/*// Remainder test

  //  if(millis()>lastmillis+10000){
  //    sec_10_count++;
  //    if(sec_10_count==6){
  //      sec_10_count=0;
  //      sonder=0;
  //    }
  //    else
  //      bitWrite(sonder,sec_10_count+1,HIGH);
  //    switch (sec_10_count) {  // 1-DOPPELPUNKT,2-PLAY,4-PAUSE,8-USB, 16-SD, 32-FM, 64-MP3
  //      case 1:
  //        bitWrite(sonder,2,HIGH);
  //        break;
  //      case 1:
  //        bitWrite(out_byte, i, HIGH);
  //        break;
  //      default:
  //        bitWrite(input_mask, i, HIGH);
  //    }
  //      sonder=0;
  //    }
  //    t_blink=!t_blink;
  //    bitWrite(sonder,0,t_blink);
  //    lastmillis = millis();
  //  }


//3, 3, 0, 3, 3, 3, 1},  //x5
// val        1  2  3  4  5  6  7---
int led_seg1 [8][7] {
  {1, 3, 3, 0, 3, 3, 3}, //a1
  {0, 1, 3, 3, 3, 3, 3},  //b1
  {1, 0, 3, 3, 3, 3, 3},  //c1
  {1, 3, 0, 3, 3, 3, 3},  //d1
  {0, 3, 3, 1, 3, 3, 3},  //e1
  {0, 3, 3, 3, 1, 3, 3},  //f1
  {0, 3, 1, 3, 3, 3, 3},  //g1
  {3, 3, 3, 3, 3, 3, 3}  //aus
};
//----2
int led_seg2 [8][7] {
  {3, 1, 3, 3, 0, 3, 3},
  {3, 0, 1, 3, 3, 3, 3},
  {3, 1, 0, 3, 3, 3, 3},
  {3, 1, 3, 0, 3, 3, 3},
  {3, 0, 3, 3, 1, 3, 3},
  {3, 1, 3, 3, 3, 0, 3},
  {3, 0, 3, 1, 3, 3, 3},
  {3, 3, 3, 3, 3, 3, 3}  //aus
};
//---3
int led_seg3 [8][7] {
  {3, 3, 0, 3, 3, 1, 3},
  {3, 3, 0, 1, 3, 3, 3},
  {3, 3, 3, 0, 1, 3, 3},
  {3, 3, 1, 3, 0, 3, 3},
  {3, 3, 3, 1, 0, 3, 3},
  {0, 3, 3, 3, 3, 1, 3},
  {3, 3, 0, 3, 1, 3, 3},
  {3, 3, 3, 3, 3, 3, 3}  //aus
};
//---4
int led_seg4 [8][7] {
  {3, 3, 3, 1, 3, 0, 3},
  {3, 3, 3, 3, 0, 1, 3},
  {3, 3, 3, 3, 3, 0, 1},
  {3, 3, 3, 3, 3, 1, 0},
  {3, 3, 3, 3, 1, 0, 3},
  {3, 3, 3, 0, 3, 1, 3},
  {3, 3, 3, 3, 1, 3, 0},
  {3, 3, 3, 3, 3, 3, 3}  //aus
};
//---Sonder
int led_special [8][7] {
  {3, 0, 3, 3, 3, 3, 1},  //fm
  {3, 3, 1, 0, 3, 3, 3},  //dp
  {3, 0, 3, 3, 3, 1, 3},  //us
  {3, 3, 1, 3, 3, 0, 3},  //ps
  {3, 3, 1, 3, 3, 3, 0},  //mp
  {1, 3, 3, 3, 0, 3, 3},  //sd
  {1, 3, 3, 3, 3, 0, 3},  //pl
  {3, 3, 3, 3, 3, 3, 3}  //aus
};
//Leer
int led_empty [8][7] {
  {1, 3, 3, 3, 3, 3, 0},  //x1
  {3, 1, 3, 3, 3, 3, 0},  //x2
  {3, 3, 3, 1, 3, 3, 0},  //x3
  {0, 3, 3, 3, 3, 3, 1},  //X4
  {3, 3, 0, 3, 3, 3, 1},  //x5
  {3, 3, 3, 0, 3, 3, 1},  //x6
  {3, 3, 3, 3, 0, 3, 1},  //x7
  {3, 3, 3, 3, 3, 3, 3}  //aus
};


void write_bit(int led_byte[7] ) {
  out_byte = 0;
  input_mask = 0;
  //int led_byte
  for (int i = 0; i < (7); i++) {
    //    Serial.print("i:");
    //    Serial.print(i);
    //    Serial.print("--:led_byte");
    //    Serial.println(led_byte[i]);
    switch (led_byte[i]) {
      case 0:
        bitWrite(out_byte, i, LOW);
        break;
      case 1:
        bitWrite(out_byte, i, HIGH);
        break;
      default:
        bitWrite(input_mask, i, HIGH);
    }
  }
}


  string letters[2][4] = {
  { "A", "B", "C", "D" },
  { "E", "F", "G", "H" }
  };
  void myFunction(int myNumbers[5]) {
  for (int i = 0; i < 5; i++) {
    cout << myNumbers[i] << "\n";
  }
  }

  int main() {
  int myNumbers[5] = {10, 20, 30, 40, 50};
  myFunction(myNumbers);
  return 0;
  }

  //
  //      C         us=USB;sd=SD;pl=Play;dp=:ps=Pause;mp=MP3;fm=FM
  //      -
  //   B| _  |D
  //      G
  //   A| _ |E
  //      F
  // val        1  2  3  4  5  6  7---
  int c1 [7] = {1, 0, 3, 3, 3, 3, 3};
  int d1 [7] = {1, 3, 0, 3, 3, 3, 3};
  int a1 [7] = {1, 3, 3, 0, 3, 3, 3};
  int sd [7] = {1, 3, 3, 3, 0, 3, 3};
  int pl [7] = {1, 3, 3, 3, 3, 0, 3};
  int x1 [7] = {1, 3, 3, 3, 3, 3, 0};
  //----2
  int b1 [7] = {0, 1, 3, 3, 3, 3, 3};
  int c2 [7] = {3, 1, 0, 3, 3, 3, 3};
  int d2 [7] = {3, 1, 3, 0, 3, 3, 3};
  int a2 [7] = {3, 1, 3, 3, 0, 3, 3};
  int f2 [7] = {3, 1, 3, 3, 3, 0, 3};
  int x2 [7] = {3, 1, 3, 3, 3, 3, 0};
  //---3
  int g1 [7] = {0, 3, 1, 3, 3, 3, 3};
  int b2 [7] = {3, 0, 1, 3, 3, 3, 3};
  int dp [7] = {3, 3, 1, 0, 3, 3, 3};
  int d3 [7] = {3, 3, 1, 3, 0, 3, 3};
  int ps [7] = {3, 3, 1, 3, 3, 0, 3};
  int mp [7] = {3, 3, 1, 3, 3, 3, 0};
  //---4
  int e1 [7] = {0, 3, 3, 1, 3, 3, 3};
  int g2 [7] = {3, 0, 3, 1, 3, 3, 3};
  int b3 [7] = {3, 3, 0, 1, 3, 3, 3};
  int e3 [7] = {3, 3, 3, 1, 0, 3, 3};
  int a4 [7] = {3, 3, 3, 1, 3, 0, 3};
  int x3 [7] = {3, 3, 3, 1, 3, 3, 0};
  //---5
  int f1 [7] = {0, 3, 3, 3, 1, 3, 3};
  int e2 [7] = {3, 0, 3, 3, 1, 3, 3};
  int g3 [7] = {3, 3, 0, 3, 1, 3, 3};
  int c3 [7] = {3, 3, 3, 0, 1, 3, 3};
  int e4 [7] = {3, 3, 3, 3, 1, 0, 3};
  int g4 [7] = {3, 3, 3, 3, 1, 3, 0};
  //---6
  int f3 [7] = {0, 3, 3, 3, 3, 1, 3};
  int us [7] = {3, 0, 3, 3, 3, 1, 3};
  int a3 [7] = {3, 3, 0, 3, 3, 1, 3};
  int f4 [7] = {3, 3, 3, 0, 3, 1, 3};
  int b4 [7] = {3, 3, 3, 3, 0, 1, 3};
  int d4 [7] = {3, 3, 3, 3, 3, 1, 0};
  //---7
  int x4 [7] = {0, 3, 3, 3, 3, 3, 1};
  int fm [7] = {3, 0, 3, 3, 3, 3, 1};
  int x5 [7] = {3, 3, 0, 3, 3, 3, 1};
  int x6 [7] = {3, 3, 3, 0, 3, 3, 1};
  int x7 [7] = {3, 3, 3, 3, 0, 3, 1};
  int c4 [7] = {3, 3, 3, 3, 3, 0, 1};

*/